Files
EdgeCommon/pkg/serverconfigs/http_websocket_config.go

60 lines
1.7 KiB
Go
Raw Permalink Normal View History

2020-09-21 11:37:09 +08:00
package serverconfigs
import (
2024-07-27 13:29:26 +08:00
"time"
2020-09-26 08:07:24 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
2020-09-21 11:37:09 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
)
// HTTPWebsocketConfig websocket设置
2020-09-21 11:37:09 +08:00
type HTTPWebsocketConfig struct {
2020-09-23 18:43:50 +08:00
Id int64 `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启
2020-09-21 11:37:09 +08:00
// 握手超时时间
HandshakeTimeout *shared.TimeDuration `yaml:"handshakeTimeout" json:"handshakeTimeout"`
2020-09-26 19:54:20 +08:00
// 允许的来源域名,支持 www.example.com, example.com, .example.com, *.example.com
2020-09-21 11:37:09 +08:00
AllowAllOrigins bool `yaml:"allowAllOrigins" json:"allowAllOrigins"`
2020-09-26 19:54:20 +08:00
AllowedOrigins []string `yaml:"allowedOrigins" json:"allowedOrigins"`
2020-09-21 11:37:09 +08:00
2020-09-26 19:54:20 +08:00
// 向后传递的来源
RequestSameOrigin bool `yaml:"requestSameOrigin" json:"requestSameOrigin"` // 和请求一致
RequestOrigin string `yaml:"requestOrigin" json:"requestOrigin"` // 自行指定Origin支持变量
2020-09-21 11:37:09 +08:00
2020-09-26 19:54:20 +08:00
handshakeTimeoutDuration time.Duration
requestOriginHasVariables bool
2020-09-21 11:37:09 +08:00
}
// 校验
func (this *HTTPWebsocketConfig) Init() error {
// duration
if this.HandshakeTimeout != nil {
this.handshakeTimeoutDuration = this.HandshakeTimeout.Duration()
}
2020-09-26 19:54:20 +08:00
// requestOrigin
this.requestOriginHasVariables = configutils.HasVariables(this.RequestOrigin)
2020-09-21 11:37:09 +08:00
return nil
}
// 获取握手超时时间
func (this *HTTPWebsocketConfig) HandshakeTimeoutDuration() time.Duration {
return this.handshakeTimeoutDuration
}
// 匹配域名
func (this *HTTPWebsocketConfig) MatchOrigin(origin string) bool {
if this.AllowAllOrigins {
return true
}
2020-09-26 19:54:20 +08:00
return configutils.MatchDomains(this.AllowedOrigins, origin)
}
// 判断请求Origin是否有变量
func (this *HTTPWebsocketConfig) RequestOriginHasVariables() bool {
return this.requestOriginHasVariables
2020-09-21 11:37:09 +08:00
}