阶段性提交

This commit is contained in:
GoEdgeLab
2020-08-21 12:32:16 +08:00
parent e6266b7dc3
commit 8f22f2fda1
181 changed files with 4897 additions and 218 deletions

View File

@@ -0,0 +1,4 @@
package serverconfigs
type ComponentConfig struct {
}

View File

@@ -0,0 +1,4 @@
package serverconfigs
type FilterConfig struct {
}

View File

@@ -0,0 +1,8 @@
package serverconfigs
type IPVersion = string
const (
IPv4 IPVersion = "4"
IPv6 IPVersion = "6"
)

View File

@@ -0,0 +1,4 @@
package serverconfigs
type LocationConfig struct {
}

View File

@@ -0,0 +1,5 @@
package serverconfigs
import "sync"
var sharedLocker = &sync.RWMutex{}

View File

@@ -0,0 +1,70 @@
package serverconfigs
import (
"github.com/iwind/TeaGo/types"
"regexp"
"strconv"
"strings"
)
var regexpSinglePort = regexp.MustCompile(`^\d+$`)
// 网络地址配置
type NetworkAddressConfig struct {
Protocol string `yaml:"protocol" json:"protocol"` // 协议http、tcp、tcp4、tcp6、unix、udp等
Host string `yaml:"host" json:"host"` // 主机地址或主机名
PortRange string `yaml:"portRange" json:"portRange"` // 端口范围,支持 8080、8080-8090、8080:8090
minPort int
maxPort int
}
func (this *NetworkAddressConfig) Init() error {
// 8080
if regexpSinglePort.MatchString(this.PortRange) {
this.minPort = types.Int(this.PortRange)
this.maxPort = this.minPort
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
}
this.minPort = minPort
this.maxPort = maxPort
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
}
this.minPort = minPort
this.maxPort = maxPort
return nil
}
return nil
}
func (this *NetworkAddressConfig) FullAddresses() []string {
if this.Protocol == ProtocolUnix {
return []string{this.Protocol + ":" + this.Host}
}
result := []string{}
for i := this.minPort; i <= this.maxPort; i++ {
host := this.Host
result = append(result, this.Protocol+"://"+host+":"+strconv.Itoa(i))
}
return result
}

View File

@@ -0,0 +1,57 @@
package serverconfigs
import "testing"
func TestNetworkAddressConfig_FullAddresses(t *testing.T) {
{
addr := &NetworkAddressConfig{
Protocol: "http",
Host: "127.0.0.1",
PortRange: "8080",
}
err := addr.Init()
if err != nil {
t.Fatal(err)
}
t.Log(addr.FullAddresses())
}
{
addr := &NetworkAddressConfig{
Protocol: "http",
Host: "127.0.0.1",
PortRange: "8080:8090",
}
err := addr.Init()
if err != nil {
t.Fatal(err)
}
t.Log(addr.FullAddresses())
}
{
addr := &NetworkAddressConfig{
Protocol: "http",
Host: "127.0.0.1",
PortRange: "8080-8090",
}
err := addr.Init()
if err != nil {
t.Fatal(err)
}
t.Log(addr.FullAddresses())
}
{
addr := &NetworkAddressConfig{
Protocol: "http",
Host: "127.0.0.1",
PortRange: "8080-8070",
}
err := addr.Init()
if err != nil {
t.Fatal(err)
}
t.Log(addr.FullAddresses())
}
}

View File

@@ -0,0 +1,10 @@
package serverconfigs
// 源站服务配置
type OriginServerConfig struct {
Id string `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Name string `yaml:"name" json:"name"` // 名称 TODO
Addr *NetworkAddressConfig `yaml:"addr" json:"addr"` // 地址
Description string `yaml:"description" json:"description"` // 描述 TODO
}

View File

@@ -0,0 +1,6 @@
package serverconfigs
// TODO 需要实现
type OriginServerGroupConfig struct {
Origins []*OriginServerConfig `yaml:"origins" json:"origins"` // 源站列表
}

View File

@@ -0,0 +1,29 @@
package serverconfigs
type Protocol = string
const (
ProtocolHTTP Protocol = "http"
ProtocolHTTPS Protocol = "https"
ProtocolTCP Protocol = "tcp"
ProtocolTLS Protocol = "tls"
ProtocolUnix Protocol = "unix"
ProtocolUDP Protocol = "udp"
// 子协议
ProtocolHTTP4 Protocol = "http4"
ProtocolHTTP6 Protocol = "http6"
ProtocolHTTPS4 Protocol = "https4"
ProtocolHTTPS6 Protocol = "https6"
ProtocolTCP4 Protocol = "tcp4"
ProtocolTCP6 Protocol = "tcp6"
ProtocolTLS4 Protocol = "tls4"
ProtocolTLS6 Protocol = "tls6"
)
func AllProtocols() []Protocol {
return []Protocol{ProtocolHTTP, ProtocolHTTPS, ProtocolTCP, ProtocolTLS, ProtocolUnix, ProtocolUDP, ProtocolHTTP4, ProtocolHTTP6, ProtocolHTTPS4, ProtocolHTTPS6, ProtocolTCP4, ProtocolTCP6, ProtocolTLS4, ProtocolTLS6}
}

View File

@@ -0,0 +1,32 @@
package serverconfigs
// 协议基础数据结构
type BaseProtocol struct {
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
Listen []*NetworkAddressConfig `yaml:"listen" json:"listen"` // 绑定的网络地址
}
// 初始化
func (this *BaseProtocol) InitBase() error {
for _, addr := range this.Listen {
err := addr.Init()
if err != nil {
return err
}
}
return nil
}
// 获取完整的地址列表
func (this *BaseProtocol) FullAddresses() []string {
result := []string{}
for _, addr := range this.Listen {
result = append(result, addr.FullAddresses()...)
}
return result
}
// 添加地址
func (this *BaseProtocol) AddListen(addr ...*NetworkAddressConfig) {
this.Listen = append(this.Listen, addr...)
}

View File

@@ -0,0 +1,14 @@
package serverconfigs
type HTTPProtocolConfig struct {
BaseProtocol `yaml:",inline"`
}
func (this *HTTPProtocolConfig) Init() error {
err := this.InitBase()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,14 @@
package serverconfigs
type HTTPSProtocolConfig struct {
BaseProtocol `yaml:",inline"`
}
func (this *HTTPSProtocolConfig) Init() error {
err := this.InitBase()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,14 @@
package serverconfigs
type TCPProtocolConfig struct {
BaseProtocol `yaml:",inline"`
}
func (this *TCPProtocolConfig) Init() error {
err := this.InitBase()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,14 @@
package serverconfigs
type TLSProtocolConfig struct {
BaseProtocol `yaml:",inline"`
}
func (this *TLSProtocolConfig) Init() error {
err := this.InitBase()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,14 @@
package serverconfigs
type UDPProtocolConfig struct {
BaseProtocol `yaml:",inline"`
}
func (this *UDPProtocolConfig) Init() error {
err := this.InitBase()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,14 @@
package serverconfigs
type UnixProtocolConfig struct {
BaseProtocol `yaml:",inline"`
}
func (this *UnixProtocolConfig) Init() error {
err := this.InitBase()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,6 @@
package serverconfigs
type ReverseProxyConfig struct {
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Origins []*OriginServerConfig `yaml:"origins" json:"origins"` // 源站列表
}

View File

@@ -0,0 +1,144 @@
package serverconfigs
import "encoding/json"
type ServerConfig struct {
Id string `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
Components []*ComponentConfig `yaml:"components" json:"components"` // 组件
Filters []*FilterConfig `yaml:"filters" json:"filters"` // 过滤器
Name string `yaml:"name" json:"name"` // 名称
Description string `yaml:"description" json:"description"` // 描述
ServerNames []*ServerNameConfig `yaml:"serverNames" json:"serverNames"` // 域名
// 前端协议
HTTP *HTTPProtocolConfig `yaml:"http" json:"http"` // HTTP配置
HTTPS *HTTPSProtocolConfig `yaml:"https" json:"https"` // HTTPS配置
TCP *TCPProtocolConfig `yaml:"tcp" json:"tcp"` // TCP配置
TLS *TLSProtocolConfig `yaml:"tls" json:"tls"` // TLS配置
Unix *UnixProtocolConfig `yaml:"unix" json:"unix"` // Unix配置
UDP *UDPProtocolConfig `yaml:"udp" json:"udp"` // UDP配置
// Web配置
Web *WebConfig `yaml:"web" json:"web"`
// 反向代理配置
ReverseProxy *ReverseProxyConfig `yaml:"reverseProxy" json:"reverseProxy"`
}
func NewServerConfig() *ServerConfig {
return &ServerConfig{}
}
func (this *ServerConfig) Init() error {
if this.HTTP != nil {
err := this.HTTP.Init()
if err != nil {
return err
}
}
if this.HTTPS != nil {
err := this.HTTPS.Init()
if err != nil {
return err
}
}
if this.TCP != nil {
err := this.TCP.Init()
if err != nil {
return err
}
}
if this.TLS != nil {
err := this.TLS.Init()
if err != nil {
return err
}
}
if this.Unix != nil {
err := this.Unix.Init()
if err != nil {
return err
}
}
if this.UDP != nil {
err := this.UDP.Init()
if err != nil {
return err
}
}
return nil
}
func (this *ServerConfig) FullAddresses() []string {
result := []Protocol{}
if this.HTTP != nil && this.HTTP.IsOn {
result = append(result, this.HTTP.FullAddresses()...)
}
if this.HTTPS != nil && this.HTTPS.IsOn {
result = append(result, this.HTTPS.FullAddresses()...)
}
if this.TCP != nil && this.TCP.IsOn {
result = append(result, this.TCP.FullAddresses()...)
}
if this.TLS != nil && this.TLS.IsOn {
result = append(result, this.TLS.FullAddresses()...)
}
if this.Unix != nil && this.Unix.IsOn {
result = append(result, this.Unix.FullAddresses()...)
}
if this.UDP != nil && this.UDP.IsOn {
result = append(result, this.UDP.FullAddresses()...)
}
return result
}
func (this *ServerConfig) Listen() []*NetworkAddressConfig {
result := []*NetworkAddressConfig{}
if this.HTTP != nil {
result = append(result, this.HTTP.Listen...)
}
if this.HTTPS != nil {
result = append(result, this.HTTPS.Listen...)
}
if this.TCP != nil {
result = append(result, this.TCP.Listen...)
}
if this.TLS != nil {
result = append(result, this.TLS.Listen...)
}
if this.Unix != nil {
result = append(result, this.Unix.Listen...)
}
if this.UDP != nil {
result = append(result, this.UDP.Listen...)
}
return result
}
func (this *ServerConfig) AsJSON() ([]byte, error) {
return json.Marshal(this)
}
func (this *ServerConfig) IsHTTP() bool {
return this.HTTP != nil || this.HTTPS != nil
}
func (this *ServerConfig) IsTCP() bool {
return this.TCP != nil || this.TLS != nil
}
func (this *ServerConfig) IsUnix() bool {
return this.Unix != nil
}
func (this *ServerConfig) IsUDP() bool {
return this.UDP != nil
}

View File

@@ -0,0 +1,74 @@
package serverconfigs
import "testing"
func TestServerConfig_Protocols(t *testing.T) {
{
server := NewServerConfig()
t.Log(server.FullAddresses())
}
{
server := NewServerConfig()
server.HTTP = &HTTPProtocolConfig{BaseProtocol: BaseProtocol{
IsOn: true,
Listen: []*NetworkAddressConfig{
{
Protocol: ProtocolHTTP,
PortRange: "1234",
},
},
}}
server.HTTPS = &HTTPSProtocolConfig{BaseProtocol: BaseProtocol{
IsOn: true,
Listen: []*NetworkAddressConfig{
{
Protocol: ProtocolUnix,
Host: "/hello.sock",
PortRange: "1235",
},
},
}}
server.TCP = &TCPProtocolConfig{BaseProtocol: BaseProtocol{
IsOn: true,
Listen: []*NetworkAddressConfig{
{
Protocol: ProtocolHTTPS,
PortRange: "1236",
},
},
}}
server.TLS = &TLSProtocolConfig{BaseProtocol: BaseProtocol{
IsOn: true,
Listen: []*NetworkAddressConfig{
{
Protocol: ProtocolTCP,
PortRange: "1234",
},
},
}}
server.Unix = &UnixProtocolConfig{BaseProtocol: BaseProtocol{
IsOn: true,
Listen: []*NetworkAddressConfig{
{
Protocol: ProtocolTLS,
PortRange: "1234",
},
},
}}
server.UDP = &UDPProtocolConfig{BaseProtocol: BaseProtocol{
IsOn: true,
Listen: []*NetworkAddressConfig{
{
Protocol: ProtocolUDP,
PortRange: "1234",
},
},
}}
err := server.Init()
if err != nil {
t.Fatal(err)
}
t.Log(server.FullAddresses())
}
}

View File

@@ -0,0 +1,41 @@
package serverconfigs
import "strings"
type ServerGroup struct {
fullAddr string
Servers []*ServerConfig
}
func NewServerGroup(fullAddr string) *ServerGroup {
return &ServerGroup{fullAddr: fullAddr}
}
// 添加服务
func (this *ServerGroup) Add(server *ServerConfig) {
this.Servers = append(this.Servers, server)
}
// 获取完整的地址
func (this *ServerGroup) FullAddr() string {
return this.fullAddr
}
// 获取当前分组的协议
func (this *ServerGroup) Protocol() Protocol {
for _, p := range AllProtocols() {
if strings.HasPrefix(this.fullAddr, p+":") {
return p
}
}
return ProtocolHTTP
}
// 获取当前分组的地址
func (this *ServerGroup) Addr() string {
protocol := this.Protocol()
if protocol == ProtocolUnix {
return strings.TrimPrefix(this.fullAddr, protocol+":")
}
return strings.TrimPrefix(this.fullAddr, protocol+"://")
}

View File

@@ -0,0 +1,34 @@
package serverconfigs
import (
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestServerGroup_Protocol(t *testing.T) {
a := assert.NewAssertion(t)
{
group := NewServerGroup("tcp://127.0.0.1:1234")
a.IsTrue(group.Protocol() == ProtocolTCP)
a.IsTrue(group.Addr() == "127.0.0.1:1234")
}
{
group := NewServerGroup("http4://127.0.0.1:1234")
a.IsTrue(group.Protocol() == ProtocolHTTP4)
a.IsTrue(group.Addr() == "127.0.0.1:1234")
}
{
group := NewServerGroup("127.0.0.1:1234")
a.IsTrue(group.Protocol() == ProtocolHTTP)
a.IsTrue(group.Addr() == "127.0.0.1:1234")
}
{
group := NewServerGroup("unix:/tmp/my.sock")
a.IsTrue(group.Protocol() == ProtocolUnix)
a.IsTrue(group.Addr() == "/tmp/my.sock")
}
}

View File

@@ -0,0 +1,15 @@
package serverconfigs
type ServerNameType = string
const (
ServerNameTypeFull = "full" // 完整的域名,包含通配符等
ServerNameTypePrefix = "prefix" // 前缀
ServerNameTypeSuffix = "suffix" // 后缀
ServerNameTypeMatch = "match" // 正则匹配
)
type ServerNameConfig struct {
Name string `yaml:"name" json:"name"` // 名称
Type string `yaml:"type" json:"type"` // 类型
}

View File

@@ -0,0 +1,10 @@
package serverconfigs
type WebConfig struct {
IsOn bool `yaml:"isOn" json:"isOn"`
Locations []*LocationConfig `yaml:"locations" json:"locations"` // 路径规则 TODO
// 本地静态资源配置
Root string `yaml:"root" json:"root"` // 资源根目录 TODO
}