From 1a29ab0046e5e153c1231c79dd9dc207a60a348a Mon Sep 17 00:00:00 2001 From: okawao <166350905+okawao@users.noreply.github.com> Date: Sat, 14 Mar 2026 20:05:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E5=BB=BA=E7=AB=8Btls=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=97=B6=E4=B8=8D=E5=BA=94=E6=AF=94=E8=BE=83location?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/file/db.go | 7 +++++++ server/proxy/https.go | 9 ++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/file/db.go b/lib/file/db.go index a68fefc..1a37b3b 100644 --- a/lib/file/db.go +++ b/lib/file/db.go @@ -377,6 +377,13 @@ func (s *DbUtils) GetInfoByHost(host string, r *http.Request) (h *Host, err erro if v.Location == "" { v.Location = "/" } + // "*" means SNI-based HTTPS lookup where actual URI is unknown, skip location filter + if r.RequestURI == "*" { + if h == nil || (len(v.Location) > len(h.Location)) { + h = v + } + continue + } if strings.Index(r.RequestURI, v.Location) == 0 { if h == nil || (len(v.Location) > len(h.Location)) { h = v diff --git a/server/proxy/https.go b/server/proxy/https.go index bdbc3d0..bb60dd6 100644 --- a/server/proxy/https.go +++ b/server/proxy/https.go @@ -41,11 +41,12 @@ func (https *HttpsServer) Start() error { serverName, rb := GetServerNameFromClientHello(c) if serverName == "" { serverName = getFallbackServerName() + logs.Debug("https fallback server name result, remote addr %s, server name %q", c.RemoteAddr().String(), serverName) } r := buildHttpsRequest(serverName) if host, err := file.GetDb().GetInfoByHost(serverName, r); err != nil { c.Close() - logs.Debug("the url %s can't be parsed!,remote addr %s", serverName, c.RemoteAddr().String()) + logs.Debug("https host lookup failed, server name %q, remote addr %s, error %v", serverName, c.RemoteAddr().String(), err) return } else { if host.CertFilePath == "" || host.KeyFilePath == "" { @@ -362,10 +363,12 @@ func GetServerNameFromClientHello(c net.Conn) (string, []byte) { return clientHello.GetServerName(), rawBytes } -// build https request +// build https request for SNI-based host lookup. +// RequestURI is set to "*" to skip Location filtering in GetInfoByHost, +// because at TLS handshake stage the actual HTTP request path is unknown. func buildHttpsRequest(hostName string) *http.Request { r := new(http.Request) - r.RequestURI = "/" + r.RequestURI = "*" r.URL = new(url.URL) r.URL.Scheme = "https" r.Host = hostName