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