修复集群启用“允许使用节点IP访问”时无法访问IPv6的问题

This commit is contained in:
GoEdgeLab
2023-09-07 14:48:27 +08:00
parent ca95ea5c3d
commit a362eb0f4c
4 changed files with 66 additions and 36 deletions

View File

@@ -7,9 +7,9 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/TeaOSLab/EdgeNode/internal/ttlcache" "github.com/TeaOSLab/EdgeNode/internal/ttlcache"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/waf" "github.com/TeaOSLab/EdgeNode/internal/waf"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"net"
"net/http" "net/http"
"time" "time"
) )
@@ -48,7 +48,7 @@ func (this *HTTPRequest) doMismatch() {
} }
// 是否正在访问IP // 是否正在访问IP
if globalServerConfig.HTTPAll.NodeIPShowPage && net.ParseIP(this.ReqHost) != nil { if globalServerConfig.HTTPAll.NodeIPShowPage && utils.IsWildIP(this.ReqHost) {
this.writer.statusCode = statusCode this.writer.statusCode = statusCode
var contentHTML = this.Format(globalServerConfig.HTTPAll.NodeIPPageHTML) var contentHTML = this.Format(globalServerConfig.HTTPAll.NodeIPPageHTML)
this.writer.Header().Set("Content-Type", "text/html; charset=utf-8") this.writer.Header().Set("Content-Type", "text/html; charset=utf-8")

View File

@@ -7,6 +7,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"net" "net"
) )
@@ -179,10 +180,10 @@ func (this *BaseListener) findNamedServer(name string) (serverConfig *serverconf
if globalServerConfig != nil && if globalServerConfig != nil &&
len(globalServerConfig.HTTPAll.DefaultDomain) > 0 && len(globalServerConfig.HTTPAll.DefaultDomain) > 0 &&
(!matchDomainStrictly || configutils.MatchDomains(globalServerConfig.HTTPAll.AllowMismatchDomains, name) || (globalServerConfig.HTTPAll.AllowNodeIP && net.ParseIP(name) != nil)) { (!matchDomainStrictly || configutils.MatchDomains(globalServerConfig.HTTPAll.AllowMismatchDomains, name) || (globalServerConfig.HTTPAll.AllowNodeIP && utils.IsWildIP(name))) {
if globalServerConfig.HTTPAll.AllowNodeIP && if globalServerConfig.HTTPAll.AllowNodeIP &&
globalServerConfig.HTTPAll.NodeIPShowPage && globalServerConfig.HTTPAll.NodeIPShowPage &&
net.ParseIP(name) != nil { utils.IsWildIP(name) {
return return
} else { } else {
var defaultDomain = globalServerConfig.HTTPAll.DefaultDomain var defaultDomain = globalServerConfig.HTTPAll.DefaultDomain
@@ -193,7 +194,7 @@ func (this *BaseListener) findNamedServer(name string) (serverConfig *serverconf
} }
} }
if matchDomainStrictly && !configutils.MatchDomains(globalServerConfig.HTTPAll.AllowMismatchDomains, name) && (!globalServerConfig.HTTPAll.AllowNodeIP || net.ParseIP(name) == nil) { if matchDomainStrictly && !configutils.MatchDomains(globalServerConfig.HTTPAll.AllowMismatchDomains, name) && (!globalServerConfig.HTTPAll.AllowNodeIP || !utils.IsWildIP(name)) {
return return
} }

View File

@@ -72,3 +72,18 @@ func IsIPv6(ip string) bool {
} }
return !IsIPv4(ip) return !IsIPv4(ip)
} }
// IsWildIP 宽泛地判断一个数据是否为IP
func IsWildIP(v string) bool {
var l = len(v)
if l == 0 {
return false
}
// for [IPv6]
if v[0] == '[' && v[l-1] == ']' {
return IsWildIP(v[1 : l-1])
}
return net.ParseIP(v) != nil
}

View File

@@ -1,51 +1,65 @@
package utils package utils_test
import ( import (
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/assert" "github.com/iwind/TeaGo/assert"
"testing" "testing"
) )
func TestIP2Long(t *testing.T) { func TestIP2Long(t *testing.T) {
t.Log(IP2Long("0.0.0.0")) t.Log(utils.IP2Long("0.0.0.0"))
t.Log(IP2Long("1.0.0.0")) t.Log(utils.IP2Long("1.0.0.0"))
t.Log(IP2Long("0.0.0.0.0")) t.Log(utils.IP2Long("0.0.0.0.0"))
t.Log(IP2Long("2001:db8:0:1::101")) t.Log(utils.IP2Long("2001:db8:0:1::101"))
t.Log(IP2Long("2001:db8:0:1::102")) t.Log(utils.IP2Long("2001:db8:0:1::102"))
t.Log(IP2Long("::1")) t.Log(utils.IP2Long("::1"))
} }
func TestIsLocalIP(t *testing.T) { func TestIsLocalIP(t *testing.T) {
var a = assert.NewAssertion(t) var a = assert.NewAssertion(t)
a.IsFalse(IsLocalIP("a")) a.IsFalse(utils.IsLocalIP("a"))
a.IsFalse(IsLocalIP("1.2.3")) a.IsFalse(utils.IsLocalIP("1.2.3"))
a.IsTrue(IsLocalIP("127.0.0.1")) a.IsTrue(utils.IsLocalIP("127.0.0.1"))
a.IsTrue(IsLocalIP("192.168.0.1")) a.IsTrue(utils.IsLocalIP("192.168.0.1"))
a.IsTrue(IsLocalIP("10.0.0.1")) a.IsTrue(utils.IsLocalIP("10.0.0.1"))
a.IsTrue(IsLocalIP("172.16.0.1")) a.IsTrue(utils.IsLocalIP("172.16.0.1"))
a.IsTrue(IsLocalIP("::1")) a.IsTrue(utils.IsLocalIP("::1"))
a.IsFalse(IsLocalIP("::1:2:3")) a.IsFalse(utils.IsLocalIP("::1:2:3"))
a.IsFalse(IsLocalIP("8.8.8.8")) a.IsFalse(utils.IsLocalIP("8.8.8.8"))
} }
func TestIsIPv4(t *testing.T) { func TestIsIPv4(t *testing.T) {
var a = assert.NewAssertion(t) var a = assert.NewAssertion(t)
a.IsTrue(IsIPv4("192.168.1.1")) a.IsTrue(utils.IsIPv4("192.168.1.1"))
a.IsTrue(IsIPv4("0.0.0.0")) a.IsTrue(utils.IsIPv4("0.0.0.0"))
a.IsFalse(IsIPv4("192.168.1.256")) a.IsFalse(utils.IsIPv4("192.168.1.256"))
a.IsFalse(IsIPv4("192.168.1")) a.IsFalse(utils.IsIPv4("192.168.1"))
a.IsFalse(IsIPv4("::1")) a.IsFalse(utils.IsIPv4("::1"))
a.IsFalse(IsIPv4("2001:0db8:85a3:0000:0000:8a2e:0370:7334")) a.IsFalse(utils.IsIPv4("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
a.IsFalse(IsIPv4("::ffff:192.168.0.1")) a.IsFalse(utils.IsIPv4("::ffff:192.168.0.1"))
} }
func TestIsIPv6(t *testing.T) { func TestIsIPv6(t *testing.T) {
var a = assert.NewAssertion(t) var a = assert.NewAssertion(t)
a.IsFalse(IsIPv6("192.168.1.1")) a.IsFalse(utils.IsIPv6("192.168.1.1"))
a.IsFloat32(IsIPv6("0.0.0.0")) a.IsFloat32(utils.IsIPv6("0.0.0.0"))
a.IsFalse(IsIPv6("192.168.1.256")) a.IsFalse(utils.IsIPv6("192.168.1.256"))
a.IsFalse(IsIPv6("192.168.1")) a.IsFalse(utils.IsIPv6("192.168.1"))
a.IsTrue(IsIPv6("::1")) a.IsTrue(utils.IsIPv6("::1"))
a.IsTrue(IsIPv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334")) a.IsTrue(utils.IsIPv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
a.IsTrue(IsIPv4("::ffff:192.168.0.1")) a.IsTrue(utils.IsIPv4("::ffff:192.168.0.1"))
a.IsTrue(IsIPv6("::ffff:192.168.0.1")) a.IsTrue(utils.IsIPv6("::ffff:192.168.0.1"))
}
func TestIsWildIP(t *testing.T) {
var a = assert.NewAssertion(t)
a.IsTrue(utils.IsWildIP("192.168.1.100"))
a.IsTrue(utils.IsWildIP("::1"))
a.IsTrue(utils.IsWildIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
a.IsTrue(utils.IsWildIP("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"))
a.IsFalse(utils.IsWildIP(""))
a.IsFalse(utils.IsWildIP("[]"))
a.IsFalse(utils.IsWildIP("[1]"))
a.IsFalse(utils.IsWildIP("192.168.2.256"))
a.IsFalse(utils.IsWildIP("192.168.2"))
} }