Files
EdgeCommon/pkg/serverconfigs/server_config.go

456 lines
12 KiB
Go
Raw Normal View History

2020-09-13 19:27:47 +08:00
package serverconfigs
import (
"context"
2020-09-13 19:27:47 +08:00
"encoding/json"
2020-09-15 14:44:38 +08:00
"errors"
2020-11-17 16:53:50 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
2021-08-01 14:52:10 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
2020-09-13 19:27:47 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
"golang.org/x/net/idna"
"regexp"
2020-09-13 19:27:47 +08:00
)
var normalServerNameReg = regexp.MustCompile(`^[a-zA-Z0-9.-]+$`)
2020-09-13 19:27:47 +08:00
type ServerConfig struct {
2020-11-17 16:53:50 +08:00
Id int64 `yaml:"id" json:"id"` // ID
2021-08-01 11:14:13 +08:00
ClusterId int64 `yaml:"clusterId" json:"clusterId"` // 集群ID
UserId int64 `yaml:"userId" json:"userId"` // 用户ID
2020-11-17 16:53:50 +08:00
Type string `yaml:"type" json:"type"` // 类型
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
Name string `yaml:"name" json:"name"` // 名称
Description string `yaml:"description" json:"description"` // 描述
AliasServerNames []string `yaml:"aliasServerNames" json:"aliasServerNames"` // 关联的域名比如CNAME之类的
ServerNames []*ServerNameConfig `yaml:"serverNames" json:"serverNames"` // 域名
2022-07-18 09:12:58 +08:00
CNameDomain string `yaml:"cnameDomain" json:"cnameDomain"` // CNAME末尾不带点.
CNameAsDomain bool `yaml:"cnameAsDomain" json:"cnameAsDomain"` // 启用CNAME域名访问
2021-10-16 12:03:44 +08:00
SupportCNAME bool `yaml:"supportCNAME" json:"supportCNAME"` // 是否支持CNAME
2020-09-13 19:27:47 +08:00
// 前端协议
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配置
UDP *UDPProtocolConfig `yaml:"udp" json:"udp"` // UDP配置
// Web配置
2020-09-16 09:09:31 +08:00
Web *HTTPWebConfig `yaml:"web" json:"web"`
2020-09-13 19:27:47 +08:00
// 反向代理配置
2020-09-21 11:37:09 +08:00
ReverseProxyRef *ReverseProxyRef `yaml:"reverseProxyRef" json:"reverseProxyRef"`
ReverseProxy *ReverseProxyConfig `yaml:"reverseProxy" json:"reverseProxy"`
2021-08-01 14:52:10 +08:00
// WAF策略
HTTPFirewallPolicyId int64 `yaml:"httpFirewallPolicyId" json:"httpFirewallPolicyId"`
HTTPFirewallPolicy *firewallconfigs.HTTPFirewallPolicy `yaml:"httpFirewallPolicy" json:"httpFirewallPolicy"` // 通过 HTTPFirewallPolicyId 获取
// 缓存策略
HTTPCachePolicyId int64 `yaml:"httpCachePolicyId" json:"httpCachePolicyId"`
HTTPCachePolicy *HTTPCachePolicy `yaml:"httpCachePolicy" json:"httpCachePolicy"` // 通过 HTTPCachePolicyId 获取
2021-11-09 17:36:34 +08:00
// 流量限制
TrafficLimit *TrafficLimitConfig `yaml:"trafficLimit" json:"trafficLimit"`
TrafficLimitStatus *TrafficLimitStatus `yaml:"trafficLimitStatus" json:"trafficLimitStatus"`
2021-11-09 15:36:31 +08:00
// 套餐
UserPlan *UserPlanConfig `yaml:"userPlan" json:"userPlan"`
2021-09-22 19:39:55 +08:00
// 分组
Group *ServerGroupConfig `yaml:"group" json:"group"`
2022-03-29 21:24:36 +08:00
// UAM
UAM *UAMConfig `yaml:"uam" json:"uam"`
2022-09-22 11:09:11 +08:00
isInitialized bool
isOk bool
2021-11-11 08:31:32 +08:00
planId int64
2020-09-13 19:27:47 +08:00
}
2021-07-20 10:55:52 +08:00
// NewServerConfigFromJSON 从JSON中解析Server配置
2020-09-13 19:27:47 +08:00
func NewServerConfigFromJSON(data []byte) (*ServerConfig, error) {
config := &ServerConfig{}
err := json.Unmarshal(data, config)
return config, err
}
func NewServerConfig() *ServerConfig {
return &ServerConfig{}
}
func (this *ServerConfig) Init(ctx context.Context) (results []error) {
2022-09-22 11:09:11 +08:00
if this.isInitialized {
return
}
this.isInitialized = true
2021-09-22 19:39:55 +08:00
// 分解Group
if this.Group != nil && this.Group.IsOn {
// reverse proxy
if this.IsHTTPFamily() && this.Group.HTTPReverseProxyRef != nil && this.Group.HTTPReverseProxyRef.IsPrior {
this.ReverseProxyRef = this.Group.HTTPReverseProxyRef
this.ReverseProxy = this.Group.HTTPReverseProxy
}
if this.IsTCPFamily() && this.Group.TCPReverseProxyRef != nil && this.Group.TCPReverseProxyRef.IsPrior {
this.ReverseProxyRef = this.Group.TCPReverseProxyRef
this.ReverseProxy = this.Group.TCPReverseProxy
}
if this.IsUDPFamily() && this.Group.UDPReverseProxyRef != nil && this.Group.UDPReverseProxyRef.IsPrior {
this.ReverseProxyRef = this.Group.UDPReverseProxyRef
this.ReverseProxy = this.Group.UDPReverseProxy
}
// web
if this.Group.Web != nil {
if this.Web == nil {
this.Web = this.Group.Web
} else {
var groupWeb = this.Group.Web
// root
if groupWeb.Root != nil && groupWeb.Root.IsPrior {
this.Web.Root = groupWeb.Root
}
// waf
if groupWeb.FirewallRef != nil && groupWeb.FirewallRef.IsPrior {
this.Web.FirewallRef = groupWeb.FirewallRef
this.Web.FirewallPolicy = groupWeb.FirewallPolicy
}
// cache
if groupWeb.Cache != nil && groupWeb.Cache.IsPrior {
this.Web.Cache = groupWeb.Cache
}
// charset
if groupWeb.Charset != nil && groupWeb.Charset.IsPrior {
this.Web.Charset = groupWeb.Charset
}
// accessLog
if groupWeb.AccessLogRef != nil && groupWeb.AccessLogRef.IsPrior {
this.Web.AccessLogRef = groupWeb.AccessLogRef
}
// stat
if groupWeb.StatRef != nil && groupWeb.StatRef.IsPrior {
this.Web.StatRef = groupWeb.StatRef
}
// compression
if groupWeb.Compression != nil && groupWeb.Compression.IsPrior {
this.Web.Compression = groupWeb.Compression
}
// headers
if groupWeb.RequestHeaderPolicyRef != nil && groupWeb.RequestHeaderPolicyRef.IsPrior {
this.Web.RequestHeaderPolicyRef = groupWeb.RequestHeaderPolicyRef
this.Web.RequestHeaderPolicy = groupWeb.RequestHeaderPolicy
}
if groupWeb.ResponseHeaderPolicyRef != nil && groupWeb.ResponseHeaderPolicyRef.IsPrior {
this.Web.ResponseHeaderPolicyRef = groupWeb.ResponseHeaderPolicyRef
this.Web.ResponseHeaderPolicy = groupWeb.ResponseHeaderPolicy
}
// websocket
if groupWeb.WebsocketRef != nil && groupWeb.WebsocketRef.IsPrior {
this.Web.WebsocketRef = groupWeb.WebsocketRef
this.Web.Websocket = groupWeb.Websocket
}
// webp
if groupWeb.WebP != nil && groupWeb.WebP.IsPrior {
this.Web.WebP = groupWeb.WebP
}
// remote addr
if groupWeb.RemoteAddr != nil && groupWeb.RemoteAddr.IsPrior {
this.Web.RemoteAddr = groupWeb.RemoteAddr
}
2021-10-10 10:53:09 +08:00
// pages
if len(groupWeb.Pages) > 0 || (groupWeb.Shutdown != nil && groupWeb.Shutdown.IsOn) {
this.Web.Pages = groupWeb.Pages
this.Web.Shutdown = groupWeb.Shutdown
}
2021-12-12 11:44:58 +08:00
// request limit
if groupWeb.RequestLimit != nil && groupWeb.RequestLimit.IsPrior {
this.Web.RequestLimit = groupWeb.RequestLimit
}
}
}
2021-09-22 19:39:55 +08:00
}
2020-09-13 19:27:47 +08:00
if this.HTTP != nil {
err := this.HTTP.Init()
if err != nil {
results = append(results, err)
2020-09-13 19:27:47 +08:00
}
}
if this.HTTPS != nil {
err := this.HTTPS.Init(ctx)
2020-09-13 19:27:47 +08:00
if err != nil {
results = append(results, err)
2020-09-13 19:27:47 +08:00
}
}
if this.TCP != nil {
err := this.TCP.Init()
if err != nil {
results = append(results, err)
2020-09-13 19:27:47 +08:00
}
}
if this.TLS != nil {
err := this.TLS.Init(ctx)
2020-09-13 19:27:47 +08:00
if err != nil {
results = append(results, err)
2020-09-13 19:27:47 +08:00
}
}
if this.UDP != nil {
err := this.UDP.Init()
if err != nil {
results = append(results, err)
2020-09-13 19:27:47 +08:00
}
}
2020-09-26 19:54:20 +08:00
if this.ReverseProxyRef != nil {
err := this.ReverseProxyRef.Init()
if err != nil {
results = append(results, err)
2020-09-26 19:54:20 +08:00
}
}
2020-09-13 19:27:47 +08:00
if this.ReverseProxy != nil {
err := this.ReverseProxy.Init(ctx)
2020-09-13 19:27:47 +08:00
if err != nil {
results = append(results, err)
2020-09-13 19:27:47 +08:00
}
}
2020-09-26 08:07:24 +08:00
if this.Web != nil {
err := this.Web.Init(ctx)
2020-09-26 08:07:24 +08:00
if err != nil {
results = append(results, err)
2020-09-26 08:07:24 +08:00
}
}
2021-11-09 15:36:31 +08:00
// 套餐
if this.UserPlan != nil {
err := this.UserPlan.Init()
if err != nil {
results = append(results, err)
2021-11-09 15:36:31 +08:00
}
2021-11-11 08:31:32 +08:00
2024-01-11 15:22:37 +08:00
this.planId = this.UserPlan.PlanId
2021-11-09 15:36:31 +08:00
}
2022-03-29 21:24:36 +08:00
// UAM
if this.UAM != nil {
err := this.UAM.Init()
if err != nil {
results = append(results, err)
}
}
this.isOk = true
2023-08-08 15:12:28 +08:00
return
2020-09-13 19:27:47 +08:00
}
2022-09-22 11:09:11 +08:00
func (this *ServerConfig) IsInitialized() bool {
return this.isInitialized
}
2021-07-20 10:55:52 +08:00
// IsOk 配置是否正确
func (this *ServerConfig) IsOk() bool {
return this.isOk
}
2020-09-13 19:27:47 +08:00
func (this *ServerConfig) FullAddresses() []string {
2020-09-15 14:44:38 +08:00
result := []string{}
2020-09-13 19:27:47 +08:00
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.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.UDP != nil {
result = append(result, this.UDP.Listen...)
}
return result
}
func (this *ServerConfig) AsJSON() ([]byte, error) {
return json.Marshal(this)
}
func (this *ServerConfig) IsHTTPFamily() bool {
2020-09-13 19:27:47 +08:00
return this.HTTP != nil || this.HTTPS != nil
}
func (this *ServerConfig) IsTCPFamily() bool {
2020-09-13 19:27:47 +08:00
return this.TCP != nil || this.TLS != nil
}
func (this *ServerConfig) IsUDPFamily() bool {
2020-09-13 19:27:47 +08:00
return this.UDP != nil
}
// AllStrictNames 所有严格域名
func (this *ServerConfig) AllStrictNames() []string {
var result = []string{}
for _, name := range this.AliasServerNames {
if len(name) > 0 {
if !configutils.IsFuzzyDomain(name) {
result = append(result, name)
// unicode domain
if !normalServerNameReg.MatchString(name) {
asciiName, err := idna.ToASCII(name)
if err == nil && len(asciiName) > 0 {
result = append(result, asciiName)
}
}
}
}
2020-11-17 16:53:50 +08:00
}
2020-09-13 19:27:47 +08:00
for _, serverName := range this.ServerNames {
var name = serverName.Name
if len(name) > 0 {
if !configutils.IsFuzzyDomain(name) {
result = append(result, name)
// unicode domain
if !normalServerNameReg.MatchString(name) {
asciiName, err := idna.ToASCII(name)
if err == nil && len(asciiName) > 0 {
result = append(result, asciiName)
}
}
}
}
for _, name := range serverName.SubNames {
if len(name) > 0 {
if !configutils.IsFuzzyDomain(name) {
result = append(result, name)
// unicode domain
if !normalServerNameReg.MatchString(name) {
asciiName, err := idna.ToASCII(name)
if err == nil && len(asciiName) > 0 {
result = append(result, asciiName)
}
}
}
}
2020-09-13 19:27:47 +08:00
}
}
return result
2020-09-13 19:27:47 +08:00
}
// AllFuzzyNames 所有模糊域名
func (this *ServerConfig) AllFuzzyNames() []string {
var result = []string{}
for _, name := range this.AliasServerNames {
if len(name) > 0 {
if configutils.IsFuzzyDomain(name) {
result = append(result, name)
}
}
}
2020-09-13 19:27:47 +08:00
for _, serverName := range this.ServerNames {
var name = serverName.Name
if len(name) > 0 {
if configutils.IsFuzzyDomain(name) {
result = append(result, name)
}
}
for _, name := range serverName.SubNames {
if len(name) > 0 {
if configutils.IsFuzzyDomain(name) {
result = append(result, name)
}
}
2020-09-13 19:27:47 +08:00
}
}
return result
2020-09-13 19:27:47 +08:00
}
2021-07-20 10:55:52 +08:00
// SSLPolicy SSL信息
2020-09-30 17:46:33 +08:00
func (this *ServerConfig) SSLPolicy() *sslconfigs.SSLPolicy {
2020-09-13 19:27:47 +08:00
if this.HTTPS != nil {
2020-09-30 17:46:33 +08:00
return this.HTTPS.SSLPolicy
2020-09-13 19:27:47 +08:00
}
if this.TLS != nil {
2020-09-30 17:46:33 +08:00
return this.TLS.SSLPolicy
2020-09-13 19:27:47 +08:00
}
return nil
}
2020-09-15 14:44:38 +08:00
2021-07-20 10:55:52 +08:00
// FindAndCheckReverseProxy 根据条件查找ReverseProxy
2020-09-15 14:44:38 +08:00
func (this *ServerConfig) FindAndCheckReverseProxy(dataType string) (*ReverseProxyConfig, error) {
switch dataType {
case "server":
if this.ReverseProxy == nil {
return nil, errors.New("reverse proxy not been configured")
}
return this.ReverseProxy, nil
default:
return nil, errors.New("invalid data type:'" + dataType + "'")
}
}
2021-11-10 14:39:13 +08:00
// ShouldCheckTrafficLimit 检查是否需要检查流量限制
func (this *ServerConfig) ShouldCheckTrafficLimit() bool {
return this.TrafficLimit != nil && !this.TrafficLimit.IsEmpty()
}
2021-11-11 08:31:32 +08:00
// PlanId 套餐ID
func (this *ServerConfig) PlanId() int64 {
return this.planId
}
2023-06-01 17:48:01 +08:00
// SupportsHTTP3 是否支持HTTP/3
func (this *ServerConfig) SupportsHTTP3() bool {
return this.isOk &&
this.HTTPS != nil &&
this.HTTPS.IsOn &&
this.HTTPS.SSLPolicy != nil &&
this.HTTPS.SSLPolicy.IsOn &&
this.HTTPS.SSLPolicy.HTTP3Enabled
}