forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
322 lines (281 loc) · 8.09 KB
/
server.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package config
import (
"errors"
"fmt"
"strings"
)
const defaultTransportPort = 5222
const defaultTransportBufferSize = 4096
const defaultTransportConnectTimeout = 5
const defaultTransportKeepAlive = 120
// ServerType represents a server type (c2s, s2s).
type ServerType int
const (
// C2SServerType represents a client to client server type.
C2SServerType ServerType = iota
// S2SServerType represents a server-to-client server type.
S2SServerType
)
// String returns ServerType string representation.
func (st ServerType) String() string {
switch st {
case C2SServerType:
return "c2s"
case S2SServerType:
return "s2s"
}
return ""
}
// ChannelBindingMechanism represents a scram channel binding mechanism.
type ChannelBindingMechanism int
const (
// TLSUnique represents 'tls-unique' channel binding mechanism.
TLSUnique ChannelBindingMechanism = iota
)
// TransportType represents a stream transport type (socket).
type TransportType int
// ResourceConflictPolicy represents a resource conflict policy.
type ResourceConflictPolicy int
const (
// Override represents 'override' resource conflict policy.
Override ResourceConflictPolicy = iota
// Reject represents 'reject' resource conflict policy.
Reject
// Replace represents 'replace' resource conflict policy.
Replace
)
const (
// SocketTransportType represents a socket transport type.
SocketTransportType TransportType = iota + 1
// WebSocketTransportType represents a websocket transport type.
WebSocketTransportType
)
// String returns TransportType string representation.
func (tt TransportType) String() string {
switch tt {
case SocketTransportType:
return "socket"
case WebSocketTransportType:
return "websocket"
}
return ""
}
// CompressionLevel represents a stream compression level.
type CompressionLevel int
const (
// NoCompression represents no stream compression.
NoCompression CompressionLevel = iota
// DefaultCompression represents 'default' stream compression level.
DefaultCompression
// BestCompression represents 'best for size' stream compression level.
BestCompression
// SpeedCompression represents 'best for speed' stream compression level.
SpeedCompression
)
// String returns CompressionLevel string representation.
func (cl CompressionLevel) String() string {
switch cl {
case DefaultCompression:
return "default"
case BestCompression:
return "best"
case SpeedCompression:
return "speed"
}
return ""
}
// Server represents an XMPP server configuration.
type Server struct {
ID string
Type ServerType
ResourceConflict ResourceConflictPolicy
Transport Transport
SASL []string
TLS TLS
Modules map[string]struct{}
Compression Compression
ModOffline ModOffline
ModRegistration ModRegistration
ModVersion ModVersion
ModPing ModPing
}
type serverProxyType struct {
ID string `yaml:"id"`
Type string `yaml:"type"`
ResourceConflict string `yaml:"resource_conflict"`
Transport Transport `yaml:"transport"`
SASL []string `yaml:"sasl"`
TLS TLS `yaml:"tls"`
Modules []string `yaml:"modules"`
Compression Compression `yaml:"compression"`
ModOffline ModOffline `yaml:"mod_offline"`
ModRegistration ModRegistration `yaml:"mod_registration"`
ModVersion ModVersion `yaml:"mod_version"`
ModPing ModPing `yaml:"mod_ping"`
}
// UnmarshalYAML satisfies Unmarshaler interface.
func (s *Server) UnmarshalYAML(unmarshal func(interface{}) error) error {
p := serverProxyType{}
if err := unmarshal(&p); err != nil {
return err
}
// validate server type
switch strings.ToLower(p.Type) {
case "c2s":
s.Type = C2SServerType
case "s2s":
return errors.New("config.Server: s2s server type not yet supported")
default:
return fmt.Errorf("config.Server: unrecognized server type: %s", p.Type)
}
// validate resource conflict policy type
rc := strings.ToLower(p.ResourceConflict)
switch rc {
case "override":
s.ResourceConflict = Override
case "reject":
s.ResourceConflict = Reject
case "", "replace":
s.ResourceConflict = Replace
default:
return fmt.Errorf("invalid resource_conflict option: %s", rc)
}
// validate SASL mechanisms
for _, sasl := range p.SASL {
switch sasl {
case "plain", "digest_md5", "scram_sha_1", "scram_sha_256":
continue
default:
return fmt.Errorf("config.Server: unrecognized SASL mechanism: %s", sasl)
}
}
// validate modules
s.Modules = map[string]struct{}{}
for _, module := range p.Modules {
switch module {
case "roster", "private", "vcard", "registration", "version", "ping", "offline":
break
default:
return fmt.Errorf("config.Server: unrecognized module: %s", module)
}
s.Modules[module] = struct{}{}
}
s.ID = p.ID
s.Transport = p.Transport
s.SASL = p.SASL
s.TLS = p.TLS
s.Compression = p.Compression
s.ModOffline = p.ModOffline
s.ModRegistration = p.ModRegistration
s.ModVersion = p.ModVersion
s.ModPing = p.ModPing
return nil
}
// Transport represents an XMPP stream transport configuration.
type Transport struct {
Type TransportType
BindAddress string
Port int
ConnectTimeout int
KeepAlive int
BufferSize int
}
type transportProxyType struct {
Type string `yaml:"type"`
BindAddress string `yaml:"bind_addr"`
Port int `yaml:"port"`
ConnectTimeout int `yaml:"connect_timeout"`
KeepAlive int `yaml:"keep_alive"`
MaxStanzaSize int `yaml:"max_stanza_size"`
BufferSize int `yaml:"buf_size"`
}
// UnmarshalYAML satisfies Unmarshaler interface.
func (t *Transport) UnmarshalYAML(unmarshal func(interface{}) error) error {
p := transportProxyType{}
if err := unmarshal(&p); err != nil {
return err
}
// validate transport type
switch p.Type {
case "", "socket":
t.Type = SocketTransportType
case "websocket":
t.Type = WebSocketTransportType
default:
return fmt.Errorf("config.Transport: unrecognized transport type: %s", p.Type)
}
t.BindAddress = p.BindAddress
t.Port = p.Port
// assign transport's defaults
if t.Port == 0 {
t.Port = defaultTransportPort
}
t.ConnectTimeout = p.ConnectTimeout
if t.ConnectTimeout == 0 {
t.ConnectTimeout = defaultTransportConnectTimeout
}
t.KeepAlive = p.KeepAlive
if t.KeepAlive == 0 {
t.KeepAlive = defaultTransportKeepAlive
}
t.BufferSize = p.BufferSize
if t.BufferSize == 0 {
t.BufferSize = defaultTransportBufferSize
}
return nil
}
// TLS represents a server TLS configuration.
type TLS struct {
CertFile string `yaml:"cert_path"`
PrivKeyFile string `yaml:"privkey_path"`
}
// Compression represents a server stream compression configuration.
type Compression struct {
Level CompressionLevel
}
type compressionProxyType struct {
Level string `yaml:"level"`
}
// UnmarshalYAML satisfies Unmarshaler interface.
func (c *Compression) UnmarshalYAML(unmarshal func(interface{}) error) error {
p := compressionProxyType{}
if err := unmarshal(&p); err != nil {
return err
}
switch p.Level {
case "":
c.Level = NoCompression
case "best":
c.Level = BestCompression
case "speed":
c.Level = SpeedCompression
case "default":
c.Level = DefaultCompression
default:
return fmt.Errorf("config.Compress: unrecognized compression level: %s", p.Level)
}
return nil
}
// ModOffline represents Offline Storage module configuration.
type ModOffline struct {
QueueSize int `yaml:"queue_size"`
}
// ModRegistration represents XMPP In-Band Registration module (XEP-0077) configuration.
type ModRegistration struct {
AllowRegistration bool `yaml:"allow_registration"`
AllowChange bool `yaml:"allow_change"`
AllowCancel bool `yaml:"allow_cancel"`
}
// ModVersion represents XMPP Software Version module (XEP-0092) configuration.
type ModVersion struct {
ShowOS bool `yaml:"show_os"`
}
// ModPing represents XMPP Ping module (XEP-0199) configuration.
type ModPing struct {
Send bool `yaml:"send"`
SendInterval int `yaml:"send_interval"`
}