forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
124 lines (112 loc) · 3.16 KB
/
config.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
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package s2s
import (
"crypto/tls"
"time"
"github.com/ortuman/jackal/module"
"github.com/ortuman/jackal/stream"
"github.com/ortuman/jackal/transport"
"github.com/ortuman/jackal/xmpp"
"github.com/pkg/errors"
)
const (
defaultTransportPort = 5269
defaultTransportKeepAlive = time.Duration(10) * time.Minute
defaultDialTimeout = time.Duration(15) * time.Second
defaultConnectTimeout = time.Duration(5) * time.Second
defaultMaxStanzaSize = 131072
)
// TransportConfig represents s2s transport configuration.
type TransportConfig struct {
BindAddress string
Port int
KeepAlive time.Duration
}
type transportConfigProxy struct {
BindAddress string `yaml:"bind_addr"`
Port int `yaml:"port"`
KeepAlive int `yaml:"keep_alive"`
}
// UnmarshalYAML satisfies Unmarshaler interface.
func (c *TransportConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
p := transportConfigProxy{}
if err := unmarshal(&p); err != nil {
return err
}
c.BindAddress = p.BindAddress
c.Port = p.Port
if c.Port == 0 {
c.Port = defaultTransportPort
}
if p.KeepAlive > 0 {
c.KeepAlive = time.Duration(p.KeepAlive) * time.Second
} else {
c.KeepAlive = defaultTransportKeepAlive
}
return nil
}
// TLSConfig represents a server TLS configuration.
type TLSConfig struct {
CertFile string `yaml:"cert_path"`
PrivKeyFile string `yaml:"privkey_path"`
}
// Config represents an s2s configuration.
type Config struct {
ID string
DialTimeout time.Duration
ConnectTimeout time.Duration
DialbackSecret string
MaxStanzaSize int
Transport TransportConfig
}
type configProxy struct {
ID string `yaml:"id"`
DialTimeout int `yaml:"dial_timeout"`
ConnectTimeout int `yaml:"connect_timeout"`
DialbackSecret string `yaml:"dialback_secret"`
MaxStanzaSize int `yaml:"max_stanza_size"`
Transport TransportConfig `yaml:"transport"`
}
// UnmarshalYAML satisfies Unmarshaler interface.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
p := configProxy{}
if err := unmarshal(&p); err != nil {
return err
}
c.ID = p.ID
c.DialbackSecret = p.DialbackSecret
if len(c.DialbackSecret) == 0 {
return errors.New("s2s.Config: must specify a dialback secret")
}
c.DialTimeout = time.Duration(p.DialTimeout) * time.Second
if c.DialTimeout == 0 {
c.DialTimeout = defaultDialTimeout
}
c.ConnectTimeout = time.Duration(p.ConnectTimeout) * time.Second
if c.ConnectTimeout == 0 {
c.ConnectTimeout = defaultConnectTimeout
}
c.Transport = p.Transport
c.MaxStanzaSize = p.MaxStanzaSize
if c.MaxStanzaSize == 0 {
c.MaxStanzaSize = defaultMaxStanzaSize
}
return nil
}
type streamConfig struct {
modConfig *module.Config
keyGen *keyGen
localDomain string
remoteDomain string
connectTimeout time.Duration
tls *tls.Config
transport transport.Transport
maxStanzaSize int
dbVerify xmpp.XElement
dialer *dialer
onInDisconnect func(s stream.S2SIn)
onOutDisconnect func(s stream.S2SOut)
}