-
Notifications
You must be signed in to change notification settings - Fork 34
/
websocket.go
176 lines (148 loc) · 4.17 KB
/
websocket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package teaconfigs
import (
"github.com/TeaWeb/code/teaconfigs/shared"
"github.com/TeaWeb/code/teautils"
"github.com/iwind/TeaGo/maps"
"net/http"
"time"
)
// websocket设置
type WebsocketConfig struct {
// 后端服务器列表
BackendList `yaml:",inline"`
On bool `yaml:"on" json:"on"` // 是否开启
// 握手超时时间
HandshakeTimeout string `yaml:"handshakeTimeout" json:"handshakeTimeout"`
// 允许的域名,支持 www.example.com, example.com, .example.com, *.example.com
AllowAllOrigins bool `yaml:"allowAllOrigins" json:"allowAllOrigins"`
Origins []string `yaml:"origins" json:"origins"`
// 转发方式
ForwardMode WebsocketForwardMode `yaml:"forwardMode" json:"forwardMode"`
// 请求分组(从server复制而来)
requestGroups []*RequestGroup
defaultRequestGroup *RequestGroup
hasRequestGroupFilters bool
handshakeTimeoutDuration time.Duration
}
// 获取新对象
func NewWebsocketConfig() *WebsocketConfig {
return &WebsocketConfig{
On: true,
}
}
// 校验
func (this *WebsocketConfig) Validate() error {
// backends
err := this.ValidateBackends()
if err != nil {
return err
}
// duration
this.handshakeTimeoutDuration, _ = time.ParseDuration(this.HandshakeTimeout)
// request groups
for _, group := range this.requestGroups {
if group.IsDefault {
this.defaultRequestGroup = group
}
for _, backend := range this.Backends {
if len(backend.RequestGroupIds) == 0 && group.Id == "default" {
group.AddBackend(backend)
} else if backend.HasRequestGroupId(group.Id) {
group.AddBackend(backend)
}
}
err := group.Validate()
if err != nil {
return err
}
if group.HasFilters() {
this.hasRequestGroupFilters = true
}
}
return nil
}
// 获取握手超时时间
func (this *WebsocketConfig) HandshakeTimeoutDuration() time.Duration {
return this.handshakeTimeoutDuration
}
// 转发模式名称
func (this *WebsocketConfig) ForwardModeSummary() maps.Map {
for _, mode := range AllWebsocketForwardModes() {
if mode["mode"] == this.ForwardMode {
return mode
}
}
return nil
}
// 匹配域名
func (this *WebsocketConfig) MatchOrigin(origin string) bool {
if this.AllowAllOrigins {
return true
}
return teautils.MatchDomains(this.Origins, origin)
}
// 添加请求分组
func (this *WebsocketConfig) AddRequestGroup(group *RequestGroup) {
this.requestGroups = append(this.requestGroups, group)
}
// 使用请求匹配分组
func (this *WebsocketConfig) MatchRequestGroup(formatter func(source string) string) *RequestGroup {
if !this.hasRequestGroupFilters {
return nil
}
for _, group := range this.requestGroups {
if group.HasFilters() && group.Match(formatter) {
return group
}
}
return nil
}
// 取得下一个可用的后端服务
func (this *WebsocketConfig) NextBackend(call *shared.RequestCall) *BackendConfig {
if this.hasRequestGroupFilters {
group := this.MatchRequestGroup(call.Formatter)
if group != nil {
// request
if group.HasRequestHeaders() {
for _, h := range group.RequestHeaders {
call.Request.Header.Set(h.Name, call.Formatter(h.Value))
}
}
// response
if group.HasResponseHeaders() {
call.AddResponseCall(func(resp http.ResponseWriter) {
for _, h := range group.ResponseHeaders {
resp.Header().Set(h.Name, call.Formatter(h.Value))
}
})
}
return group.BackendList.NextBackend(call)
}
}
// 默认分组
if this.defaultRequestGroup != nil {
// request
if this.defaultRequestGroup.HasRequestHeaders() {
for _, h := range this.defaultRequestGroup.RequestHeaders {
call.Request.Header.Set(h.Name, call.Formatter(h.Value))
}
}
// response
if this.defaultRequestGroup.HasResponseHeaders() {
call.AddResponseCall(func(resp http.ResponseWriter) {
for _, h := range this.defaultRequestGroup.ResponseHeaders {
resp.Header().Set(h.Name, call.Formatter(h.Value))
}
})
}
return this.defaultRequestGroup.NextBackend(call)
}
return this.BackendList.NextBackend(call)
}
// 设置调度算法
func (this *WebsocketConfig) SetupScheduling(isBackup bool) {
for _, group := range this.requestGroups {
group.SetupScheduling(isBackup)
}
this.BackendList.SetupScheduling(isBackup)
}