forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
309 lines (273 loc) · 7.63 KB
/
router.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
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package router
import (
"errors"
"sync"
"github.com/ortuman/jackal/host"
"github.com/ortuman/jackal/log"
"github.com/ortuman/jackal/storage"
"github.com/ortuman/jackal/stream"
"github.com/ortuman/jackal/xml"
"github.com/ortuman/jackal/xml/jid"
)
var (
// ErrNotExistingAccount will be returned by Route method
// if destination user does not exist.
ErrNotExistingAccount = errors.New("router: account does not exist")
// ErrResourceNotFound will be returned by Route method
// if destination resource does not match any of user's available resources.
ErrResourceNotFound = errors.New("router: resource not found")
// ErrNotAuthenticated will be returned by Route method if
// destination user is not available at this moment.
ErrNotAuthenticated = errors.New("router: user not authenticated")
// ErrBlockedJID will be returned by Route method if
// destination JID matches any of the user's blocked JID.
ErrBlockedJID = errors.New("router: destination jid is blocked")
// ErrFailedRemoteConnect will be returned by Route method if
// couldn't establish a connection to the remote server.
ErrFailedRemoteConnect = errors.New("router: failed remote connection")
)
// Config represents router configuration.
type Config struct {
// GetS2SOut if set, acts as an s2s outgoing stream provider.
GetS2SOut func(localDomain, remoteDomain string) (stream.S2SOut, error)
}
type router struct {
cfg *Config
mu sync.RWMutex
localStreams map[string][]stream.C2S
blockListsMu sync.RWMutex
blockLists map[string][]*jid.JID
}
// singleton interface
var (
instMu sync.RWMutex
inst *router
initialized bool
)
// Initialize initializes the router manager.
func Initialize(cfg *Config) {
instMu.Lock()
defer instMu.Unlock()
if initialized {
return
}
inst = &router{
cfg: cfg,
blockLists: make(map[string][]*jid.JID),
localStreams: make(map[string][]stream.C2S),
}
initialized = true
}
// Shutdown shuts down router manager system.
// This method should be used only for testing purposes.
func Shutdown() {
instMu.Lock()
defer instMu.Unlock()
if !initialized {
return
}
inst = nil
initialized = false
}
// Bind marks a c2s stream as binded.
// An error will be returned in case no assigned resource is found.
func Bind(stm stream.C2S) {
instance().bind(stm)
}
// Unbind unbinds a previously binded c2s.
// An error will be returned in case no assigned resource is found.
func Unbind(stm stream.C2S) {
instance().unbind(stm)
}
// UserStreams returns all streams associated to a user.
func UserStreams(username string) []stream.C2S {
return instance().userStreams(username)
}
// IsBlockedJID returns whether or not the passed jid matches any
// of a user's blocking list JID.
func IsBlockedJID(jid *jid.JID, username string) bool {
return instance().isBlockedJID(jid, username)
}
// ReloadBlockList reloads in memory block list for a given user and starts
// applying it for future stanza routing.
func ReloadBlockList(username string) {
instance().reloadBlockList(username)
}
// Route routes a stanza applying server rules for handling XML stanzas.
// (https://xmpp.org/rfcs/rfc3921.html#rules)
func Route(elem xml.Stanza) error {
return instance().route(elem, false)
}
// MustRoute routes a stanza applying server rules for handling XML stanzas
// ignoring blocking lists.
func MustRoute(elem xml.Stanza) error {
return instance().route(elem, true)
}
func instance() *router {
instMu.RLock()
defer instMu.RUnlock()
if inst == nil {
log.Fatalf("router manager not initialized")
}
return inst
}
func (r *router) bind(stm stream.C2S) {
if len(stm.Resource()) == 0 {
return
}
r.mu.Lock()
defer r.mu.Unlock()
if authenticated := r.localStreams[stm.Username()]; authenticated != nil {
r.localStreams[stm.Username()] = append(authenticated, stm)
} else {
r.localStreams[stm.Username()] = []stream.C2S{stm}
}
log.Infof("binded c2s stream... (%s/%s)", stm.Username(), stm.Resource())
return
}
func (r *router) unbind(stm stream.C2S) {
if len(stm.Resource()) == 0 {
return
}
r.mu.Lock()
defer r.mu.Unlock()
if resources := r.localStreams[stm.Username()]; resources != nil {
res := stm.Resource()
for i := 0; i < len(resources); i++ {
if res == resources[i].Resource() {
resources = append(resources[:i], resources[i+1:]...)
break
}
}
if len(resources) > 0 {
r.localStreams[stm.Username()] = resources
} else {
delete(r.localStreams, stm.Username())
}
}
log.Infof("unbinded c2s stream... (%s/%s)", stm.Username(), stm.Resource())
}
func (r *router) userStreams(username string) []stream.C2S {
r.mu.Lock()
defer r.mu.Unlock()
return r.localStreams[username]
}
func (r *router) isBlockedJID(jid *jid.JID, username string) bool {
bl := r.getBlockList(username)
for _, blkJID := range bl {
if r.jidMatchesBlockedJID(jid, blkJID) {
return true
}
}
return false
}
func (r *router) jidMatchesBlockedJID(j, blockedJID *jid.JID) bool {
if blockedJID.IsFullWithUser() {
return j.Matches(blockedJID, jid.MatchesNode|jid.MatchesDomain|jid.MatchesResource)
} else if blockedJID.IsFullWithServer() {
return j.Matches(blockedJID, jid.MatchesDomain|jid.MatchesResource)
} else if blockedJID.IsBare() {
return j.Matches(blockedJID, jid.MatchesNode|jid.MatchesDomain)
}
return j.Matches(blockedJID, jid.MatchesDomain)
}
func (r *router) reloadBlockList(username string) {
r.blockListsMu.Lock()
defer r.blockListsMu.Unlock()
delete(r.blockLists, username)
log.Infof("block list reloaded... (username: %s)", username)
}
func (r *router) getBlockList(username string) []*jid.JID {
r.blockListsMu.RLock()
bl := r.blockLists[username]
r.blockListsMu.RUnlock()
if bl != nil {
return bl
}
blItms, err := storage.Instance().FetchBlockListItems(username)
if err != nil {
log.Error(err)
return nil
}
bl = []*jid.JID{}
for _, blItm := range blItms {
j, _ := jid.NewWithString(blItm.JID, true)
bl = append(bl, j)
}
r.blockListsMu.Lock()
r.blockLists[username] = bl
r.blockListsMu.Unlock()
return bl
}
func (r *router) route(stanza xml.Stanza, ignoreBlocking bool) error {
toJID := stanza.ToJID()
if !ignoreBlocking && !toJID.IsServer() {
if r.isBlockedJID(stanza.FromJID(), toJID.Node()) {
return ErrBlockedJID
}
}
if !host.IsLocalHost(toJID.Domain()) {
return r.remoteRoute(stanza)
}
rcps := r.userStreams(toJID.Node())
if len(rcps) == 0 {
exists, err := storage.Instance().UserExists(toJID.Node())
if err != nil {
return err
}
if exists {
return ErrNotAuthenticated
}
return ErrNotExistingAccount
}
if toJID.IsFullWithUser() {
for _, stm := range rcps {
if stm.Resource() == toJID.Resource() {
stm.SendElement(stanza)
return nil
}
}
return ErrResourceNotFound
}
switch stanza.(type) {
case *xml.Message:
// send to highest priority stream
stm := rcps[0]
var highestPriority int8
if p := stm.Presence(); p != nil {
highestPriority = p.Priority()
}
for i := 1; i < len(rcps); i++ {
rcp := rcps[i]
if p := rcp.Presence(); p != nil && p.Priority() > highestPriority {
stm = rcp
highestPriority = p.Priority()
}
}
stm.SendElement(stanza)
default:
// broadcast toJID all streams
for _, stm := range rcps {
stm.SendElement(stanza)
}
}
return nil
}
func (r *router) remoteRoute(stanza xml.Stanza) error {
if r.cfg.GetS2SOut == nil {
return ErrFailedRemoteConnect
}
localDomain := stanza.FromJID().Domain()
remoteDomain := stanza.ToJID().Domain()
out, err := r.cfg.GetS2SOut(localDomain, remoteDomain)
if err != nil {
log.Error(err)
return ErrFailedRemoteConnect
}
out.SendElement(stanza)
return nil
}