2020-09-13 19:27:47 +08:00
|
|
|
|
package serverconfigs
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
2020-09-27 15:25:52 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
2020-09-13 19:27:47 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-08-01 21:56:15 +08:00
|
|
|
|
// OriginConfig 源站服务配置
|
2020-09-21 20:21:20 +08:00
|
|
|
|
type OriginConfig struct {
|
2020-09-13 19:27:47 +08:00
|
|
|
|
Id int64 `yaml:"id" json:"id"` // ID
|
2021-03-26 22:10:57 +08:00
|
|
|
|
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
2020-09-13 19:27:47 +08:00
|
|
|
|
Version int `yaml:"version" json:"version"` // 版本
|
2021-03-26 22:10:57 +08:00
|
|
|
|
Name string `yaml:"name" json:"name"` // 名称
|
2020-09-13 19:27:47 +08:00
|
|
|
|
Addr *NetworkAddressConfig `yaml:"addr" json:"addr"` // 地址
|
|
|
|
|
|
Description string `yaml:"description" json:"description"` // 描述 TODO
|
|
|
|
|
|
Code string `yaml:"code" json:"code"` // 代号 TODO
|
|
|
|
|
|
|
2021-03-26 22:10:57 +08:00
|
|
|
|
Weight uint `yaml:"weight" json:"weight"` // 权重
|
|
|
|
|
|
ConnTimeout *shared.TimeDuration `yaml:"connTimeout" json:"connTimeout"` // 连接失败超时
|
|
|
|
|
|
ReadTimeout *shared.TimeDuration `yaml:"readTimeout" json:"readTimeout"` // 读取超时时间
|
|
|
|
|
|
IdleTimeout *shared.TimeDuration `yaml:"idleTimeout" json:"idleTimeout"` // 空闲连接超时时间
|
2020-09-16 09:09:31 +08:00
|
|
|
|
MaxFails int `yaml:"maxFails" json:"maxFails"` // 最多失败次数 TODO
|
2021-03-26 22:10:57 +08:00
|
|
|
|
MaxConns int `yaml:"maxConns" json:"maxConns"` // 最大并发连接数
|
|
|
|
|
|
MaxIdleConns int `yaml:"idleConns" json:"idleConns"` // 最大空闲连接数
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
2021-09-20 11:54:21 +08:00
|
|
|
|
Domains []string `yaml:"domains" json:"domains"` // 所属域名
|
|
|
|
|
|
|
2020-09-27 15:25:52 +08:00
|
|
|
|
StripPrefix string `yaml:"stripPrefix" json:"stripPrefix"` // 去除URL前缀
|
|
|
|
|
|
RequestURI string `yaml:"requestURI" json:"requestURI"` // 转发后的请求URI TODO
|
2022-03-17 15:48:16 +08:00
|
|
|
|
RequestHost string `yaml:"requestHost" json:"requestHost"` // 自定义主机名
|
2022-06-29 21:55:37 +08:00
|
|
|
|
FollowPort bool `yaml:"followPort" json:"followPort"` // 端口跟随
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
2020-09-26 08:07:24 +08:00
|
|
|
|
RequestHeaderPolicyRef *shared.HTTPHeaderPolicyRef `yaml:"requestHeaderPolicyRef" json:"requestHeaderPolicyRef"` // 请求Header
|
|
|
|
|
|
RequestHeaderPolicy *shared.HTTPHeaderPolicy `yaml:"requestHeaderPolicy" json:"requestHeaderPolicy"` // 请求Header策略
|
|
|
|
|
|
ResponseHeaderPolicyRef *shared.HTTPHeaderPolicyRef `yaml:"responseHeaderPolicyRef" json:"responseHeaderPolicyRef"` // 响应Header`
|
|
|
|
|
|
ResponseHeaderPolicy *shared.HTTPHeaderPolicy `yaml:"responseHeaderPolicy" json:"responseHeaderPolicy"` // 响应Header策略
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
// 健康检查URL,目前支持:
|
|
|
|
|
|
// - http|https 返回2xx-3xx认为成功
|
2021-03-26 22:10:57 +08:00
|
|
|
|
HealthCheck *HealthCheckConfig `yaml:"healthCheck" json:"healthCheck"` // TODO
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
2022-01-16 19:51:30 +08:00
|
|
|
|
CertRef *sslconfigs.SSLCertRef `yaml:"certRef" json:"certRef"` // 证书的引用
|
|
|
|
|
|
Cert *sslconfigs.SSLCertConfig `yaml:"cert" json:"cert"` // 请求源服务器用的证书
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
// ftp
|
2021-03-26 22:10:57 +08:00
|
|
|
|
FTPServerRef *FTPServerRef `yaml:"ftpServerRef" json:"ftpServerRef"` // TODO
|
|
|
|
|
|
FTPServer *FTPServerConfig `yaml:"ftpServer" json:"ftpServer"` // TODO
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
2022-06-29 21:55:37 +08:00
|
|
|
|
IsOk bool `yaml:"isOk" json:"isOk"` // 是否可以正常访问,仅仅用于运行时检查
|
2021-08-01 21:56:15 +08:00
|
|
|
|
|
2020-09-13 19:27:47 +08:00
|
|
|
|
connTimeoutDuration time.Duration
|
|
|
|
|
|
readTimeoutDuration time.Duration
|
|
|
|
|
|
idleTimeoutDuration time.Duration
|
|
|
|
|
|
|
|
|
|
|
|
hasRequestURI bool
|
|
|
|
|
|
requestPath string
|
|
|
|
|
|
requestArgs string
|
|
|
|
|
|
|
|
|
|
|
|
hasRequestHeaders bool
|
|
|
|
|
|
hasResponseHeaders bool
|
|
|
|
|
|
|
|
|
|
|
|
uniqueKey string
|
|
|
|
|
|
|
2020-09-27 15:25:52 +08:00
|
|
|
|
requestHostHasVariables bool
|
|
|
|
|
|
requestURIHasVariables bool
|
2020-09-13 19:27:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-01 21:56:15 +08:00
|
|
|
|
// Init 校验
|
2020-09-21 20:21:20 +08:00
|
|
|
|
func (this *OriginConfig) Init() error {
|
2021-08-01 21:56:15 +08:00
|
|
|
|
this.IsOk = true
|
|
|
|
|
|
|
2020-09-27 15:25:52 +08:00
|
|
|
|
// URL
|
|
|
|
|
|
this.requestHostHasVariables = configutils.HasVariables(this.RequestHost)
|
|
|
|
|
|
this.requestURIHasVariables = configutils.HasVariables(this.RequestURI)
|
|
|
|
|
|
|
|
|
|
|
|
// unique key
|
|
|
|
|
|
this.uniqueKey = strconv.FormatInt(this.Id, 10) + "@" + strconv.Itoa(this.Version) + "@" + fmt.Sprintf("%p", this)
|
|
|
|
|
|
|
|
|
|
|
|
// addr
|
|
|
|
|
|
if this.Addr != nil {
|
|
|
|
|
|
err := this.Addr.Init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-13 19:27:47 +08:00
|
|
|
|
// 证书
|
|
|
|
|
|
if this.Cert != nil {
|
|
|
|
|
|
err := this.Cert.Init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-26 22:10:57 +08:00
|
|
|
|
// connTimeout
|
2020-09-26 08:07:24 +08:00
|
|
|
|
if this.ConnTimeout != nil {
|
|
|
|
|
|
this.connTimeoutDuration = this.ConnTimeout.Duration()
|
|
|
|
|
|
}
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
// readTimeout
|
2020-09-26 08:07:24 +08:00
|
|
|
|
if this.ReadTimeout != nil {
|
|
|
|
|
|
this.readTimeoutDuration = this.ReadTimeout.Duration()
|
|
|
|
|
|
}
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
// idleTimeout
|
2020-09-26 08:07:24 +08:00
|
|
|
|
if this.IdleTimeout != nil {
|
|
|
|
|
|
this.idleTimeoutDuration = this.IdleTimeout.Duration()
|
|
|
|
|
|
}
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
// Headers
|
2020-09-26 19:54:20 +08:00
|
|
|
|
if this.RequestHeaderPolicyRef != nil {
|
|
|
|
|
|
err := this.RequestHeaderPolicyRef.Init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-09-26 08:07:24 +08:00
|
|
|
|
if this.RequestHeaderPolicy != nil {
|
|
|
|
|
|
err := this.RequestHeaderPolicy.Init()
|
2020-09-13 19:27:47 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-09-26 19:54:20 +08:00
|
|
|
|
if this.ResponseHeaderPolicyRef != nil {
|
|
|
|
|
|
err := this.ResponseHeaderPolicyRef.Init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-09-26 08:07:24 +08:00
|
|
|
|
if this.ResponseHeaderPolicy != nil {
|
|
|
|
|
|
err := this.ResponseHeaderPolicy.Init()
|
2020-09-13 19:27:47 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// request uri
|
|
|
|
|
|
if len(this.RequestURI) == 0 || this.RequestURI == "${requestURI}" {
|
|
|
|
|
|
this.hasRequestURI = false
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.hasRequestURI = true
|
|
|
|
|
|
|
|
|
|
|
|
if strings.Contains(this.RequestURI, "?") {
|
|
|
|
|
|
pieces := strings.SplitN(this.RequestURI, "?", -1)
|
|
|
|
|
|
this.requestPath = pieces[0]
|
|
|
|
|
|
this.requestArgs = pieces[1]
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.requestPath = this.RequestURI
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-27 15:25:52 +08:00
|
|
|
|
// health check
|
|
|
|
|
|
if this.HealthCheck != nil {
|
|
|
|
|
|
err := this.HealthCheck.Init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-01 21:56:15 +08:00
|
|
|
|
// CandidateCodes 候选对象代号
|
2020-09-21 20:21:20 +08:00
|
|
|
|
func (this *OriginConfig) CandidateCodes() []string {
|
2020-09-13 19:27:47 +08:00
|
|
|
|
codes := []string{strconv.FormatInt(this.Id, 10)}
|
|
|
|
|
|
if len(this.Code) > 0 {
|
|
|
|
|
|
codes = append(codes, this.Code)
|
|
|
|
|
|
}
|
|
|
|
|
|
return codes
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-01 21:56:15 +08:00
|
|
|
|
// CandidateWeight 候选对象权重
|
2020-09-21 20:21:20 +08:00
|
|
|
|
func (this *OriginConfig) CandidateWeight() uint {
|
2020-09-13 19:27:47 +08:00
|
|
|
|
return this.Weight
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-01 21:56:15 +08:00
|
|
|
|
// ConnTimeoutDuration 连接超时时间
|
2020-09-27 15:25:52 +08:00
|
|
|
|
func (this *OriginConfig) ConnTimeoutDuration() time.Duration {
|
|
|
|
|
|
return this.connTimeoutDuration
|
2020-09-26 08:07:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-01 21:56:15 +08:00
|
|
|
|
// ReadTimeoutDuration 读取超时时间
|
2020-09-27 15:25:52 +08:00
|
|
|
|
func (this *OriginConfig) ReadTimeoutDuration() time.Duration {
|
|
|
|
|
|
return this.readTimeoutDuration
|
2020-09-26 08:07:24 +08:00
|
|
|
|
}
|
2020-09-26 19:54:20 +08:00
|
|
|
|
|
2021-08-01 21:56:15 +08:00
|
|
|
|
// IdleTimeoutDuration 休眠超时时间
|
2020-09-27 15:25:52 +08:00
|
|
|
|
func (this *OriginConfig) IdleTimeoutDuration() time.Duration {
|
|
|
|
|
|
return this.idleTimeoutDuration
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-01 21:56:15 +08:00
|
|
|
|
// RequestHostHasVariables 判断RequestHost是否有变量
|
2020-09-27 15:25:52 +08:00
|
|
|
|
func (this *OriginConfig) RequestHostHasVariables() bool {
|
|
|
|
|
|
return this.requestHostHasVariables
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-01 21:56:15 +08:00
|
|
|
|
// RequestURIHasVariables 判断RequestURI是否有变量
|
2020-09-27 15:25:52 +08:00
|
|
|
|
func (this *OriginConfig) RequestURIHasVariables() bool {
|
|
|
|
|
|
return this.requestURIHasVariables
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-01 21:56:15 +08:00
|
|
|
|
// UniqueKey 唯一Key
|
2020-09-27 15:25:52 +08:00
|
|
|
|
func (this *OriginConfig) UniqueKey() string {
|
|
|
|
|
|
return this.uniqueKey
|
2020-09-26 19:54:20 +08:00
|
|
|
|
}
|