Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit be3a24d

Browse files
committed
[proxy]https在对方访问的URL是IP时才会从SNI中获取域名,否则使用URL中的域名
1 parent 347aa0e commit be3a24d

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

teaproxy/listener.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,17 @@ func (this *Listener) handleHTTP(writer http.ResponseWriter, rawRequest *http.Re
407407
reqHost := rawRequest.Host
408408

409409
// TLS域名
410-
if rawRequest.TLS != nil {
411-
serverName := rawRequest.TLS.ServerName
412-
if len(serverName) > 0 {
413-
// 端口
414-
index := strings.LastIndex(reqHost, ":")
415-
if index >= 0 {
416-
reqHost = serverName + reqHost[index:]
417-
} else {
418-
reqHost = serverName
410+
if this.isIP(reqHost) {
411+
if rawRequest.TLS != nil {
412+
serverName := rawRequest.TLS.ServerName
413+
if len(serverName) > 0 {
414+
// 端口
415+
index := strings.LastIndex(reqHost, ":")
416+
if index >= 0 {
417+
reqHost = serverName + reqHost[index:]
418+
} else {
419+
reqHost = serverName
420+
}
419421
}
420422
}
421423
}
@@ -748,3 +750,18 @@ func (this *Listener) connectTCPBackend(clientConn net.Conn, serverName string)
748750
delete(this.connectingTCPMap, clientConn)
749751
this.connectingTCPLocker.Unlock()
750752
}
753+
754+
func (this *Listener) isIP(host string) bool {
755+
// IPv6
756+
if strings.Index(host, "[") > -1 {
757+
return true
758+
}
759+
760+
for _, b := range host {
761+
if b >= 'a' && b <= 'z' {
762+
return false
763+
}
764+
}
765+
766+
return true
767+
}

teaproxy/listener_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55
"github.com/TeaWeb/code/teaconfigs"
66
"github.com/TeaWeb/code/teaconfigs/shared"
77
"github.com/TeaWeb/code/teatesting"
8+
"github.com/iwind/TeaGo/assert"
89
"github.com/iwind/TeaGo/logs"
910
"github.com/iwind/TeaGo/maps"
11+
"runtime"
12+
"strings"
1013
"testing"
1114
"time"
1215
)
@@ -331,3 +334,39 @@ func printListener(listener *Listener, t *testing.T) {
331334
}, t)
332335
}
333336
}
337+
338+
func TestDetectIPOrDomain(t *testing.T) {
339+
a := assert.NewAssertion(t)
340+
a.IsTrue(testIsIP("192.168.1.101"))
341+
a.IsTrue(testIsIP("192.168.1.102:1000"))
342+
a.IsTrue(testIsIP("[1:2:3:4]:1000"))
343+
a.IsTrue(!testIsIP("192.168.1.com"))
344+
a.IsTrue(!testIsIP("192.168.1.com:12345"))
345+
a.IsTrue(!testIsIP("local345:12345"))
346+
}
347+
348+
func BenchmarkIPOrDomain(b *testing.B) {
349+
runtime.GOMAXPROCS(1)
350+
351+
for i := 0; i < b.N; i++ {
352+
_ = testIsIP("192.168.1.101")
353+
_ = testIsIP("192.168.1.101:1000")
354+
_ = testIsIP("www.example.com")
355+
_ = testIsIP("www.example.com:12345")
356+
}
357+
}
358+
359+
func testIsIP(host string) bool {
360+
// IPv6
361+
if strings.Index(host, "[") > -1 {
362+
return true
363+
}
364+
365+
for _, b := range host {
366+
if b >= 'a' && b <= 'z' {
367+
return false
368+
}
369+
}
370+
371+
return true
372+
}

0 commit comments

Comments
 (0)