2020-09-13 19:27:47 +08:00
|
|
|
|
package serverconfigs
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2020-09-27 15:25:52 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
|
|
|
|
|
"github.com/iwind/TeaGo/rands"
|
2020-09-13 19:27:47 +08:00
|
|
|
|
"github.com/iwind/TeaGo/types"
|
|
|
|
|
|
"regexp"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var regexpSinglePort = regexp.MustCompile(`^\d+$`)
|
|
|
|
|
|
|
2021-07-20 10:55:52 +08:00
|
|
|
|
// NetworkAddressConfig 网络地址配置
|
2020-09-13 19:27:47 +08:00
|
|
|
|
type NetworkAddressConfig struct {
|
2020-09-15 14:44:38 +08:00
|
|
|
|
Protocol Protocol `yaml:"protocol" json:"protocol"` // 协议,http、tcp、tcp4、tcp6、unix、udp等
|
2020-09-27 15:25:52 +08:00
|
|
|
|
Host string `yaml:"host" json:"host"` // 主机地址或主机名,支持变量
|
2020-09-15 14:44:38 +08:00
|
|
|
|
PortRange string `yaml:"portRange" json:"portRange"` // 端口范围,支持 8080、8080-8090、8080:8090
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
2021-10-10 16:30:09 +08:00
|
|
|
|
MinPort int `yaml:"minPort" json:"minPort"` // minPort和maxPort只是用来记录PortRange分解后的结果,不需要用户输入
|
|
|
|
|
|
MaxPort int `yaml:"maxPort" json:"maxPort"`
|
2020-09-27 15:25:52 +08:00
|
|
|
|
|
|
|
|
|
|
hostHasVariables bool
|
2020-09-13 19:27:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-20 10:55:52 +08:00
|
|
|
|
// Init 初始化
|
2020-09-13 19:27:47 +08:00
|
|
|
|
func (this *NetworkAddressConfig) Init() error {
|
2020-09-27 15:25:52 +08:00
|
|
|
|
this.hostHasVariables = configutils.HasVariables(this.Host)
|
|
|
|
|
|
|
2023-06-16 08:47:47 +08:00
|
|
|
|
// 特殊端口自动修复,防止有些小白用户不了解HTTP和HTTPS的区别而选择了错误的协议
|
|
|
|
|
|
if this.PortRange == "80" && this.Protocol == ProtocolHTTPS {
|
|
|
|
|
|
this.Protocol = ProtocolHTTP
|
|
|
|
|
|
}
|
|
|
|
|
|
if this.PortRange == "443" && this.Protocol == ProtocolHTTP {
|
|
|
|
|
|
this.Protocol = ProtocolHTTPS
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-13 19:27:47 +08:00
|
|
|
|
// 8080
|
|
|
|
|
|
if regexpSinglePort.MatchString(this.PortRange) {
|
2021-10-10 16:30:09 +08:00
|
|
|
|
this.MinPort = types.Int(this.PortRange)
|
|
|
|
|
|
this.MaxPort = this.MinPort
|
2020-09-13 19:27:47 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 8080:8090
|
|
|
|
|
|
if strings.Contains(this.PortRange, ":") {
|
|
|
|
|
|
pieces := strings.SplitN(this.PortRange, ":", 2)
|
|
|
|
|
|
minPort := types.Int(pieces[0])
|
|
|
|
|
|
maxPort := types.Int(pieces[1])
|
|
|
|
|
|
if minPort > maxPort {
|
|
|
|
|
|
minPort, maxPort = maxPort, minPort
|
|
|
|
|
|
}
|
2021-10-10 16:30:09 +08:00
|
|
|
|
this.MinPort = minPort
|
|
|
|
|
|
this.MaxPort = maxPort
|
2020-09-13 19:27:47 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 8080-8090
|
|
|
|
|
|
if strings.Contains(this.PortRange, "-") {
|
|
|
|
|
|
pieces := strings.SplitN(this.PortRange, "-", 2)
|
|
|
|
|
|
minPort := types.Int(pieces[0])
|
|
|
|
|
|
maxPort := types.Int(pieces[1])
|
|
|
|
|
|
if minPort > maxPort {
|
|
|
|
|
|
minPort, maxPort = maxPort, minPort
|
|
|
|
|
|
}
|
2021-10-10 16:30:09 +08:00
|
|
|
|
this.MinPort = minPort
|
|
|
|
|
|
this.MaxPort = maxPort
|
2020-09-13 19:27:47 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-20 10:55:52 +08:00
|
|
|
|
// Addresses 所有的地址列表,不包括scheme
|
2020-10-04 16:10:19 +08:00
|
|
|
|
func (this *NetworkAddressConfig) Addresses() []string {
|
|
|
|
|
|
result := []string{}
|
2021-10-10 16:30:09 +08:00
|
|
|
|
for i := this.MinPort; i <= this.MaxPort; i++ {
|
2020-10-04 16:10:19 +08:00
|
|
|
|
host := this.Host
|
2021-07-20 10:55:52 +08:00
|
|
|
|
result = append(result, configutils.QuoteIP(host)+":"+strconv.Itoa(i))
|
2020-10-04 16:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-20 10:55:52 +08:00
|
|
|
|
// FullAddresses 所有的地址列表,包含scheme
|
2020-09-13 19:27:47 +08:00
|
|
|
|
func (this *NetworkAddressConfig) FullAddresses() []string {
|
|
|
|
|
|
result := []string{}
|
2021-10-10 16:30:09 +08:00
|
|
|
|
for i := this.MinPort; i <= this.MaxPort; i++ {
|
2020-09-13 19:27:47 +08:00
|
|
|
|
host := this.Host
|
2021-07-20 10:55:52 +08:00
|
|
|
|
result = append(result, this.Protocol.String()+"://"+configutils.QuoteIP(host)+":"+strconv.Itoa(i))
|
2020-09-13 19:27:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|
2020-09-27 15:25:52 +08:00
|
|
|
|
|
2021-07-20 10:55:52 +08:00
|
|
|
|
// PickAddress 选择其中一个地址
|
2020-09-27 15:25:52 +08:00
|
|
|
|
func (this *NetworkAddressConfig) PickAddress() string {
|
2021-10-10 16:30:09 +08:00
|
|
|
|
if this.MaxPort > this.MinPort {
|
|
|
|
|
|
return configutils.QuoteIP(this.Host) + ":" + strconv.Itoa(rands.Int(this.MinPort, this.MaxPort))
|
2020-09-27 15:25:52 +08:00
|
|
|
|
}
|
2021-10-10 16:30:09 +08:00
|
|
|
|
return configutils.QuoteIP(this.Host) + ":" + strconv.Itoa(this.MinPort)
|
2020-09-27 15:25:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-20 10:55:52 +08:00
|
|
|
|
// HostHasVariables 判断Host是否包含变量
|
2020-09-27 15:25:52 +08:00
|
|
|
|
func (this *NetworkAddressConfig) HostHasVariables() bool {
|
|
|
|
|
|
return this.hostHasVariables
|
|
|
|
|
|
}
|