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

Commit

Permalink
[proxy]https在对方访问的URL是IP时才会从SNI中获取域名,否则使用URL中的域名
Browse files Browse the repository at this point in the history
  • Loading branch information
iwind committed Jun 25, 2020
1 parent 347aa0e commit be3a24d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
35 changes: 26 additions & 9 deletions teaproxy/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,15 +407,17 @@ func (this *Listener) handleHTTP(writer http.ResponseWriter, rawRequest *http.Re
reqHost := rawRequest.Host

// TLS域名
if rawRequest.TLS != nil {
serverName := rawRequest.TLS.ServerName
if len(serverName) > 0 {
// 端口
index := strings.LastIndex(reqHost, ":")
if index >= 0 {
reqHost = serverName + reqHost[index:]
} else {
reqHost = serverName
if this.isIP(reqHost) {
if rawRequest.TLS != nil {
serverName := rawRequest.TLS.ServerName
if len(serverName) > 0 {
// 端口
index := strings.LastIndex(reqHost, ":")
if index >= 0 {
reqHost = serverName + reqHost[index:]
} else {
reqHost = serverName
}
}
}
}
Expand Down Expand Up @@ -748,3 +750,18 @@ func (this *Listener) connectTCPBackend(clientConn net.Conn, serverName string)
delete(this.connectingTCPMap, clientConn)
this.connectingTCPLocker.Unlock()
}

func (this *Listener) isIP(host string) bool {
// IPv6
if strings.Index(host, "[") > -1 {
return true
}

for _, b := range host {
if b >= 'a' && b <= 'z' {
return false
}
}

return true
}
39 changes: 39 additions & 0 deletions teaproxy/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"github.com/TeaWeb/code/teaconfigs"
"github.com/TeaWeb/code/teaconfigs/shared"
"github.com/TeaWeb/code/teatesting"
"github.com/iwind/TeaGo/assert"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"runtime"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -331,3 +334,39 @@ func printListener(listener *Listener, t *testing.T) {
}, t)
}
}

func TestDetectIPOrDomain(t *testing.T) {
a := assert.NewAssertion(t)
a.IsTrue(testIsIP("192.168.1.101"))
a.IsTrue(testIsIP("192.168.1.102:1000"))
a.IsTrue(testIsIP("[1:2:3:4]:1000"))
a.IsTrue(!testIsIP("192.168.1.com"))
a.IsTrue(!testIsIP("192.168.1.com:12345"))
a.IsTrue(!testIsIP("local345:12345"))
}

func BenchmarkIPOrDomain(b *testing.B) {
runtime.GOMAXPROCS(1)

for i := 0; i < b.N; i++ {
_ = testIsIP("192.168.1.101")
_ = testIsIP("192.168.1.101:1000")
_ = testIsIP("www.example.com")
_ = testIsIP("www.example.com:12345")
}
}

func testIsIP(host string) bool {
// IPv6
if strings.Index(host, "[") > -1 {
return true
}

for _, b := range host {
if b >= 'a' && b <= 'z' {
return false
}
}

return true
}

0 comments on commit be3a24d

Please sign in to comment.