From 876b2d2588dcca788694983f2fc06f9ad080a8ef Mon Sep 17 00:00:00 2001 From: okawao <166350905+okawao@users.noreply.github.com> Date: Sat, 14 Mar 2026 16:44:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E8=A7=A3=E6=9E=90=E5=88=86=E6=AE=B5client=20hello?= =?UTF-8?q?=EF=BC=9B=E6=96=B0=E5=A2=9E=E5=85=BC=E5=AE=B9ip=E8=AF=81?= =?UTF-8?q?=E4=B9=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/proxy/https.go | 65 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/server/proxy/https.go b/server/proxy/https.go index 054e89a..bdbc3d0 100644 --- a/server/proxy/https.go +++ b/server/proxy/https.go @@ -1,6 +1,7 @@ package proxy import ( + "io" "net" "net/http" "net/url" @@ -38,6 +39,9 @@ func (https *HttpsServer) Start() error { conn.Accept(https.listener, func(c net.Conn) { serverName, rb := GetServerNameFromClientHello(c) + if serverName == "" { + serverName = getFallbackServerName() + } r := buildHttpsRequest(serverName) if host, err := file.GetDb().GetInfoByHost(serverName, r); err != nil { c.Close() @@ -140,6 +144,35 @@ func (https *HttpsServer) Start() error { return nil } +func getFallbackServerName() string { + global := file.GetDb().GetGlobal() + if global == nil { + return "" + } + serverName := strings.TrimSpace(global.ServerUrl) + if serverName == "" { + return "" + } + if strings.Contains(serverName, "://") { + if u, err := url.Parse(serverName); err == nil && u.Host != "" { + serverName = u.Host + } + } + if i := strings.Index(serverName, "/"); i >= 0 { + serverName = serverName[:i] + } + serverName = common.GetIpByAddr(serverName) + serverName = strings.TrimSpace(serverName) + if serverName == "" { + return "" + } + ip := net.ParseIP(serverName) + if ip != nil && ip.IsUnspecified() { + return "" + } + return serverName +} + func (https *HttpsServer) cert(host *file.Host, c net.Conn, rb []byte, certFileUrl string, keyFileUrl string) { var l *HttpsListener i := 0 @@ -300,21 +333,33 @@ func (httpsListener *HttpsListener) Addr() net.Addr { return httpsListener.parentListener.Addr() } -// get server name from connection by read client hello bytes +// get server name from connection by read full client hello bytes func GetServerNameFromClientHello(c net.Conn) (string, []byte) { - buf := make([]byte, 4096) - data := make([]byte, 4096) - n, err := c.Read(buf) + header := make([]byte, 5) + headerN, err := io.ReadFull(c, header) if err != nil { - return "", nil + return "", header[:headerN] } - if n < 42 { - return "", nil + if header[0] != 0x16 { + return "", header } - copy(data, buf[:n]) + recordLen := int(header[3])<<8 | int(header[4]) + if recordLen <= 0 { + return "", header + } + + body := make([]byte, recordLen) + bodyN, err := io.ReadFull(c, body) + if err != nil { + return "", append(header, body[:bodyN]...) + } + + rawBytes := append(header, body...) clientHello := new(crypt.ClientHelloMsg) - clientHello.Unmarshal(data[5:n]) - return clientHello.GetServerName(), buf[:n] + if !clientHello.Unmarshal(body) { + return "", rawBytes + } + return clientHello.GetServerName(), rawBytes } // build https request