mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-25 04:10:55 +08:00
在各个地方支持IPv6
This commit is contained in:
@@ -24,3 +24,17 @@ func IP2Long(ip string) uint64 {
|
||||
}
|
||||
return uint64(binary.BigEndian.Uint32(s.To4()))
|
||||
}
|
||||
|
||||
// QuoteIP 为IPv6加上括号
|
||||
func QuoteIP(ip string) string {
|
||||
if len(ip) == 0 {
|
||||
return ip
|
||||
}
|
||||
if !strings.Contains(ip, ":") {
|
||||
return ip
|
||||
}
|
||||
if ip[0] != '[' {
|
||||
return "[" + ip + "]"
|
||||
}
|
||||
return ip
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
var regexpSinglePort = regexp.MustCompile(`^\d+$`)
|
||||
|
||||
// 网络地址配置
|
||||
// NetworkAddressConfig 网络地址配置
|
||||
type NetworkAddressConfig struct {
|
||||
Protocol Protocol `yaml:"protocol" json:"protocol"` // 协议,http、tcp、tcp4、tcp6、unix、udp等
|
||||
Host string `yaml:"host" json:"host"` // 主机地址或主机名,支持变量
|
||||
@@ -23,7 +23,7 @@ type NetworkAddressConfig struct {
|
||||
hostHasVariables bool
|
||||
}
|
||||
|
||||
// 初始化
|
||||
// Init 初始化
|
||||
func (this *NetworkAddressConfig) Init() error {
|
||||
this.hostHasVariables = configutils.HasVariables(this.Host)
|
||||
|
||||
@@ -63,7 +63,7 @@ func (this *NetworkAddressConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 所有的地址列表,不包括scheme
|
||||
// Addresses 所有的地址列表,不包括scheme
|
||||
func (this *NetworkAddressConfig) Addresses() []string {
|
||||
if this.Protocol == ProtocolUnix {
|
||||
return []string{this.Host}
|
||||
@@ -72,12 +72,12 @@ func (this *NetworkAddressConfig) Addresses() []string {
|
||||
result := []string{}
|
||||
for i := this.minPort; i <= this.maxPort; i++ {
|
||||
host := this.Host
|
||||
result = append(result, host+":"+strconv.Itoa(i))
|
||||
result = append(result, configutils.QuoteIP(host)+":"+strconv.Itoa(i))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 所有的地址列表,包含scheme
|
||||
// FullAddresses 所有的地址列表,包含scheme
|
||||
func (this *NetworkAddressConfig) FullAddresses() []string {
|
||||
if this.Protocol == ProtocolUnix {
|
||||
return []string{this.Protocol.String() + ":" + this.Host}
|
||||
@@ -86,20 +86,20 @@ func (this *NetworkAddressConfig) FullAddresses() []string {
|
||||
result := []string{}
|
||||
for i := this.minPort; i <= this.maxPort; i++ {
|
||||
host := this.Host
|
||||
result = append(result, this.Protocol.String()+"://"+host+":"+strconv.Itoa(i))
|
||||
result = append(result, this.Protocol.String()+"://"+configutils.QuoteIP(host)+":"+strconv.Itoa(i))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 选择其中一个地址
|
||||
// PickAddress 选择其中一个地址
|
||||
func (this *NetworkAddressConfig) PickAddress() string {
|
||||
if this.maxPort > this.minPort {
|
||||
return this.Host + ":" + strconv.Itoa(rands.Int(this.minPort, this.maxPort))
|
||||
return configutils.QuoteIP(this.Host) + ":" + strconv.Itoa(rands.Int(this.minPort, this.maxPort))
|
||||
}
|
||||
return this.Host + ":" + strconv.Itoa(this.minPort)
|
||||
return configutils.QuoteIP(this.Host) + ":" + strconv.Itoa(this.minPort)
|
||||
}
|
||||
|
||||
// 判断Host是否包含变量
|
||||
// HostHasVariables 判断Host是否包含变量
|
||||
func (this *NetworkAddressConfig) HostHasVariables() bool {
|
||||
return this.hostHasVariables
|
||||
}
|
||||
|
||||
@@ -54,6 +54,20 @@ func TestNetworkAddressConfig_FullAddresses(t *testing.T) {
|
||||
}
|
||||
t.Log(addr.FullAddresses())
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
addr := &NetworkAddressConfig{
|
||||
Protocol: "http",
|
||||
Host: "::1",
|
||||
PortRange: "8080-8070",
|
||||
}
|
||||
err := addr.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(addr.FullAddresses())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNetworkAddressConfig_PickAddress(t *testing.T) {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package serverconfigs
|
||||
|
||||
// 协议基础数据结构
|
||||
// BaseProtocol 协议基础数据结构
|
||||
type BaseProtocol struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
|
||||
Listen []*NetworkAddressConfig `yaml:"listen" json:"listen"` // 绑定的网络地址
|
||||
}
|
||||
|
||||
// 初始化
|
||||
// InitBase 初始化
|
||||
func (this *BaseProtocol) InitBase() error {
|
||||
for _, addr := range this.Listen {
|
||||
err := addr.Init()
|
||||
@@ -17,7 +17,7 @@ func (this *BaseProtocol) InitBase() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取完整的地址列表
|
||||
// FullAddresses 获取完整的地址列表
|
||||
func (this *BaseProtocol) FullAddresses() []string {
|
||||
result := []string{}
|
||||
for _, addr := range this.Listen {
|
||||
@@ -26,7 +26,7 @@ func (this *BaseProtocol) FullAddresses() []string {
|
||||
return result
|
||||
}
|
||||
|
||||
// 添加地址
|
||||
// AddListen 添加地址
|
||||
func (this *BaseProtocol) AddListen(addr ...*NetworkAddressConfig) {
|
||||
this.Listen = append(this.Listen, addr...)
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ type ServerConfig struct {
|
||||
isOk bool
|
||||
}
|
||||
|
||||
// 从JSON中解析Server配置
|
||||
// NewServerConfigFromJSON 从JSON中解析Server配置
|
||||
func NewServerConfigFromJSON(data []byte) (*ServerConfig, error) {
|
||||
config := &ServerConfig{}
|
||||
err := json.Unmarshal(data, config)
|
||||
@@ -114,7 +114,7 @@ func (this *ServerConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 配置是否正确
|
||||
// IsOk 配置是否正确
|
||||
func (this *ServerConfig) IsOk() bool {
|
||||
return this.isOk
|
||||
}
|
||||
@@ -186,7 +186,7 @@ func (this *ServerConfig) IsUDPFamily() bool {
|
||||
return this.UDP != nil
|
||||
}
|
||||
|
||||
// 判断是否和域名匹配
|
||||
// MatchName 判断是否和域名匹配
|
||||
func (this *ServerConfig) MatchName(name string) bool {
|
||||
if len(name) == 0 {
|
||||
return false
|
||||
@@ -202,7 +202,7 @@ func (this *ServerConfig) MatchName(name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// 判断是否严格匹配
|
||||
// MatchNameStrictly 判断是否严格匹配
|
||||
func (this *ServerConfig) MatchNameStrictly(name string) bool {
|
||||
for _, serverName := range this.AliasServerNames {
|
||||
if serverName == name {
|
||||
@@ -217,7 +217,7 @@ func (this *ServerConfig) MatchNameStrictly(name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SSL信息
|
||||
// SSLPolicy SSL信息
|
||||
func (this *ServerConfig) SSLPolicy() *sslconfigs.SSLPolicy {
|
||||
if this.HTTPS != nil {
|
||||
return this.HTTPS.SSLPolicy
|
||||
@@ -228,7 +228,7 @@ func (this *ServerConfig) SSLPolicy() *sslconfigs.SSLPolicy {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 根据条件查找ReverseProxy
|
||||
// FindAndCheckReverseProxy 根据条件查找ReverseProxy
|
||||
func (this *ServerConfig) FindAndCheckReverseProxy(dataType string) (*ReverseProxyConfig, error) {
|
||||
switch dataType {
|
||||
case "server":
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IP Range类型
|
||||
// IPRangeType IP Range类型
|
||||
type IPRangeType = int
|
||||
|
||||
const (
|
||||
@@ -19,7 +19,7 @@ const (
|
||||
IPRangeTypeWildcard IPRangeType = 4 // 通配符,可以使用*
|
||||
)
|
||||
|
||||
// IP Range
|
||||
// IPRangeConfig IP Range
|
||||
type IPRangeConfig struct {
|
||||
Id string `yaml:"id" json:"id"`
|
||||
|
||||
@@ -36,14 +36,14 @@ type IPRangeConfig struct {
|
||||
reg *regexp.Regexp
|
||||
}
|
||||
|
||||
// 获取新对象
|
||||
// NewIPRangeConfig 获取新对象
|
||||
func NewIPRangeConfig() *IPRangeConfig {
|
||||
return &IPRangeConfig{
|
||||
Id: stringutil.Rand(16),
|
||||
}
|
||||
}
|
||||
|
||||
// 从字符串中分析
|
||||
// ParseIPRange 从字符串中分析
|
||||
func ParseIPRange(s string) (*IPRangeConfig, error) {
|
||||
if len(s) == 0 {
|
||||
return nil, errors.New("invalid ip range")
|
||||
@@ -79,15 +79,15 @@ func ParseIPRange(s string) (*IPRangeConfig, error) {
|
||||
ipRange.IPTo = s
|
||||
}
|
||||
|
||||
err := ipRange.Validate()
|
||||
err := ipRange.Init()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ipRange, nil
|
||||
}
|
||||
|
||||
// 校验
|
||||
func (this *IPRangeConfig) Validate() error {
|
||||
// Init 初始化校验
|
||||
func (this *IPRangeConfig) Init() error {
|
||||
if this.Type == IPRangeTypeCIDR {
|
||||
if len(this.CIDR) == 0 {
|
||||
return errors.New("cidr should not be empty")
|
||||
@@ -116,10 +116,10 @@ func (this *IPRangeConfig) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 是否包含某个IP
|
||||
// Contains 是否包含某个IP
|
||||
func (this *IPRangeConfig) Contains(ipString string) bool {
|
||||
ip := net.ParseIP(ipString)
|
||||
if ip.To4() == nil {
|
||||
if ip == nil {
|
||||
return false
|
||||
}
|
||||
if this.Type == IPRangeTypeCIDR {
|
||||
|
||||
@@ -9,32 +9,52 @@ func TestGeoConfig_Contains(t *testing.T) {
|
||||
a := assert.NewAssertion(t)
|
||||
|
||||
{
|
||||
geo := NewIPRangeConfig()
|
||||
geo.Type = IPRangeTypeRange
|
||||
geo.IPFrom = "192.168.1.100"
|
||||
geo.IPTo = "192.168.1.110"
|
||||
a.IsNil(geo.Validate())
|
||||
a.IsTrue(geo.Contains("192.168.1.100"))
|
||||
a.IsTrue(geo.Contains("192.168.1.101"))
|
||||
a.IsTrue(geo.Contains("192.168.1.110"))
|
||||
a.IsFalse(geo.Contains("192.168.1.111"))
|
||||
r := NewIPRangeConfig()
|
||||
r.Type = IPRangeTypeRange
|
||||
r.IPFrom = "192.168.1.100"
|
||||
r.IPTo = "192.168.1.110"
|
||||
a.IsNil(r.Init())
|
||||
a.IsTrue(r.Contains("192.168.1.100"))
|
||||
a.IsTrue(r.Contains("192.168.1.101"))
|
||||
a.IsTrue(r.Contains("192.168.1.110"))
|
||||
a.IsFalse(r.Contains("192.168.1.111"))
|
||||
}
|
||||
|
||||
{
|
||||
geo := NewIPRangeConfig()
|
||||
geo.Type = IPRangeTypeCIDR
|
||||
geo.CIDR = "192.168.1.1/24"
|
||||
a.IsNil(geo.Validate())
|
||||
a.IsTrue(geo.Contains("192.168.1.100"))
|
||||
a.IsFalse(geo.Contains("192.168.2.100"))
|
||||
r := NewIPRangeConfig()
|
||||
r.Type = IPRangeTypeCIDR
|
||||
r.CIDR = "192.168.1.1/24"
|
||||
a.IsNil(r.Init())
|
||||
a.IsTrue(r.Contains("192.168.1.100"))
|
||||
a.IsFalse(r.Contains("192.168.2.100"))
|
||||
}
|
||||
|
||||
{
|
||||
geo := NewIPRangeConfig()
|
||||
geo.Type = IPRangeTypeCIDR
|
||||
geo.CIDR = "192.168.1.1/16"
|
||||
a.IsNil(geo.Validate())
|
||||
a.IsTrue(geo.Contains("192.168.2.100"))
|
||||
r := NewIPRangeConfig()
|
||||
r.Type = IPRangeTypeCIDR
|
||||
r.CIDR = "192.168.1.1/16"
|
||||
a.IsNil(r.Init())
|
||||
a.IsTrue(r.Contains("192.168.2.100"))
|
||||
}
|
||||
|
||||
{
|
||||
r := NewIPRangeConfig()
|
||||
r.Type = IPRangeTypeRange
|
||||
r.IPFrom = "::1"
|
||||
r.IPTo = "::1"
|
||||
a.IsNil(r.Init())
|
||||
a.IsTrue(r.Contains("::1"))
|
||||
}
|
||||
|
||||
{
|
||||
r := NewIPRangeConfig()
|
||||
r.Type = IPRangeTypeRange
|
||||
r.IPFrom = "::1"
|
||||
r.IPTo = "::100"
|
||||
a.IsNil(r.Init())
|
||||
a.IsTrue(r.Contains("::1"))
|
||||
a.IsTrue(r.Contains("::99"))
|
||||
a.IsFalse(r.Contains("::101"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user