/
xmpp.go
402 lines (355 loc) · 10.9 KB
/
xmpp.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package xmpp
import (
"crypto/tls"
"encoding/base64"
"encoding/xml"
"errors"
"io"
"log"
"math/rand"
"net"
"strings"
)
// var dbg *bool = flag.Bool("debug", false, "enable debug logging")
const dbg = false
func random_string(str_size int) string {
alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, str_size)
for i, _ := range bytes {
bytes[i] = alphanum[rand.Intn(len(alphanum))]
}
return string(bytes)
}
func debug(format string, args ...interface{}) {
if dbg {
log.Println("DEBUG: "+format, args)
}
}
func errorf(format string, args ...interface{}) {
log.Println("ERROR: "+format, args)
}
type xmppConn struct {
net.Conn
username string
extension string
}
func (conn xmppConn) Read(buf []byte) (total int, err error) {
n, err := conn.Conn.Read(buf)
if err != nil {
return n, err
}
total = n
for buf[total-1] != '>' {
n, err = conn.Conn.Read(buf[total:])
if err != nil {
return total, err
}
total += n
}
debug("Read : ", string(buf[:total]))
return total, err
}
func (conn xmppConn) Write(buf []byte) (n int, err error) {
n, err = conn.Conn.Write(buf)
debug("Write : ", string(buf[:n]))
return n, err
}
type Server struct {
addr string // Used to know where to listen to.
domain string // Domain is the name of the server
tlsConfig tls.Config
MessageChannel chan Message
authenticate func(username, password string) bool
handleMessage func(msg XmppMessage)
messageChannels map[string][]chan Message
handlers map[string]func(io.Writer, Request)
}
// StreamError is a generic error related to a stream.
type StreamError struct {
Condition string // one of the conditions from §4.9.3 of RFC 6120
Message string
}
type Message struct {
To string
Message string
From string
}
type XmppMessage struct {
XMLName xml.Name `xml:"message"`
Type string `xml:"type,attr,omitempty`
Id string `xml:"id,attr,omitempty"`
To string `xml:"to,attr,omitempty"`
Actives []Active `xml:"active"`
Bodies []Body `xml:"body"`
}
type Active struct {
Xmlns string `xml:"xmlns,attr"`
}
type Body struct {
Body string `xml:",innerxml"`
}
// Stream represents an XMPP stream.
type Stream struct {
XMLName xml.Name `xml:"http://etherx.jabber.org/streams stream"`
To string `xml:"to,attr,omitempty"`
From string `xml:"from,attr,omitempty"`
Language string `xml:"lang,attr,omitempty"`
Id string `xml:"id,attr,omitempty"`
Stream string `xml:"http://etherx.jabber.org/streams stream,attr,omitempty"`
Version string `xml:"version,attr,omitempty"`
}
type Bind struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
Body string `xml:",innerxml"`
}
type Query struct {
Xmlns string `xml:"xmlns,attr"`
Body string `xml:",innerxml"`
}
type Ping struct {
Xmlns string `xml:"xmlns,attr"`
Body string `xml:",innerxml"`
}
type IQ struct {
XMLName xml.Name `xml:"iq"`
Type string `xml:"type,attr"`
Id string `xml:"id,attr"`
Binds []Bind
Queries []Query `xml:"query"`
Pings []Ping `xml:"ping"`
}
type Request struct{
Id string
User string
}
func NewServer(domain string) (srv Server) {
srv.domain = domain
srv.MessageChannel = make(chan Message)
srv.messageChannels = make(map[string][]chan Message)
srv.handlers = make(map[string]func(io.Writer, Request))
go srv.distributeMessages()
return srv
}
func (srv *Server) HandleQuery(query string, handler func(io.Writer, Request)) {
srv.handlers[query] = handler
}
func (srv *Server) distributeMessages() {
for msg := range srv.MessageChannel {
debug("Msg Received", msg)
if user_chans, ok := srv.messageChannels[msg.To]; ok {
debug("User Found", user_chans)
for _, user_chan := range user_chans {
debug("Channel Found", user_chan)
go func(user_chan chan Message) {
user_chan <- msg
}(user_chan)
}
} else {
debug("Unknown user message received")
}
}
}
func (srv *Server) SetKeyPair(certfile, keyfile string) error {
cert, err := tls.LoadX509KeyPair(certfile, keyfile)
if err != nil {
return errors.New(err.Error() + "\nYou can create keypairs with the following command\n" +
"`openssl genrsa 2048 > " + keyfile + "`\n" +
"`chmod 400 " + keyfile + "`\n" +
"`openssl req -new -x509 -nodes -sha1 -days 3650 -key " + keyfile + " > " + certfile)
}
config := tls.Config{
Certificates: []tls.Certificate{
cert,
},
}
srv.tlsConfig = config
return nil
}
func (srv *Server) SetAuthFunc(authenticate func(username, password string) bool) {
srv.authenticate = authenticate
}
func (srv *Server) SetHandleIncomingMessage(handle func(msg XmppMessage)) {
srv.handleMessage = handle
}
func (srv Server) ListenAndServe(protocol, addr string) error {
l, err := net.Listen(protocol, addr)
if err != nil {
return err
}
debug("Starting server")
for {
conn, err := l.Accept()
xmppconn := xmppConn{conn, "", ""}
if err != nil {
return err
}
go srv.handle(xmppconn, false)
}
}
func (srv *Server) handle(conn xmppConn, is_tls bool) {
buf := make([]byte, 1032)
// XML Header
if !is_tls {
_, err := conn.Read(buf)
if err != nil {
log.Println("Cannot read from client")
}
}
m, err := conn.Read(buf)
if err != nil {
log.Println("Cannot read from client")
}
stream := Stream{}
err = xml.Unmarshal([]byte(string(buf[:m])), &stream)
if err == io.EOF {
err = nil
} else if err != nil {
log.Println(err)
}
conn.Write([]byte(`<?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="` + srv.domain + `" id="7c1d95c6" xml:lang="en" version="1.0">`))
if is_tls {
conn.Write([]byte(`<stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms><auth xmlns="http://jabber.org/features/iq-auth"/><register xmlns="http://jabber.org/features/iq-register"/></stream:features>`))
} else {
conn.Write([]byte(`<stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms><auth xmlns="http://jabber.org/features/iq-auth"/><register xmlns="http://jabber.org/features/iq-register"/></stream:features>`))
n, _ := conn.Read(buf)
if string(buf[:n]) == "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>" {
srv.switchToTLS(conn)
return
}
}
n, err := conn.Read(buf)
received := "AG5pY29sYXMAY281bXV4eTI="
data, err := base64.StdEncoding.DecodeString(received)
checkErr(conn, err, "Cannot decode base64")
tokens := strings.Split(string(data), "\000")
username := tokens[1]
password := tokens[2]
if !srv.authenticate(username, password) {
return
}
conn.username = username
// User is authentified, now let's bind him
conn.extension = random_string(8)
conn.Write([]byte("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>"))
conn.Read(buf)
conn.Write([]byte("<stream:stream from='" + srv.domain + "' id='1' version='1.0' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en'><stream:features><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></stream:features>"))
n, err = conn.Read(buf)
iq := IQ{}
err = xml.Unmarshal([]byte(string(buf[:n+m])), &iq)
checkErr(conn, err, "Error Unmarshaling binding IQ")
result := IQ{
Type: "result",
Id: iq.Id,
Binds: []Bind{Bind{
Body: "<jid>" + conn.username + "@" + srv.domain + "/" + conn.extension + "</jid>",
}},
}
output, err := xml.MarshalIndent(result, "", " ")
checkErr(conn, err, "Error Marshaling binding IQ")
conn.Write(output)
n, err = conn.Read(buf)
iq = IQ{}
err = xml.Unmarshal([]byte(string(buf[:n])), &iq)
checkErr(conn, err, "Error Unmarshaling binding IQ")
result = IQ{
Type: "result",
Id: iq.Id,
}
output, err = xml.MarshalIndent(result, "", " ")
checkErr(conn, err, "Error Marshaling binding IQ")
conn.Write(output)
user := conn.username
debug("New user %s correctly bound", user)
//Allocate channel to send messages
if _, ok := srv.messageChannels[user]; !ok {
srv.messageChannels[user] = make([]chan Message, 0)
}
user_chans := srv.messageChannels[user]
user_chan := make(chan Message)
user_chans = append(user_chans, user_chan)
srv.messageChannels[user] = user_chans
go srv.handleReads(conn, user_chan)
// go func(){
// srv.MessageChannel <- Message{To:user, From:"+33672317534", Message: "Bienvenue"}
// }()
for msg := range user_chan {
debug("Writing to connection", conn)
debug("From channel", user_chan)
conn.Write([]byte(`<message from='` + msg.From + `@` + srv.domain + `'
to='` + msg.To + `@` + srv.domain + `'
xml:lang='en'><body>` + msg.Message + `</body></message>`))
}
}
func (srv *Server) handleReads(conn xmppConn, user_chan chan Message) {
buf := make([]byte, 4096)
user := conn.username
// Whenever closing signal is received remove channel from user channels
defer func(user_chan chan Message) {
user_chans := srv.messageChannels[user]
debug("Removing user channel %s", len(user_chans))
user_chan_index := -1
for i, _chan := range user_chans {
if _chan == user_chan {
user_chan_index = i
}
}
user_chans[user_chan_index] = user_chans[len(user_chans)-1]
user_chans = user_chans[:len(user_chans)-1]
srv.messageChannels[user] = user_chans
debug("Removed user channel %s", len(user_chans))
}(user_chan)
for {
n, err := conn.Read(buf)
if err != nil {
// Close this goroutine on error
return
}
if string(buf[:n]) == `</stream:stream>` {
//Close the stream
return
}
iq := IQ{}
err = xml.Unmarshal([]byte(string(buf[:n])), &iq)
if err == nil {
srv.handleIQ(conn, iq)
}
msg := XmppMessage{}
err = xml.Unmarshal([]byte(string(buf[:n])), &msg)
if err == nil {
srv.handleMessage(msg)
}
}
}
// func (srv *Server) defaultHandleMessage(conn xmppConn, msg XmppMessage){
// debug("Received Message", msg)
// for _, body := range(msg.Bodies){
// debug("Sending to appropriate recipient")
// srv.MessageChannel <- Message{To: msg.To, From:user, Message: body.Body}
// }
// }
func (srv *Server) handleIQ(conn xmppConn, iq IQ) {
for _, query := range iq.Queries {
if handler, ok := srv.handlers[query.Xmlns]; ok {
request := Request{iq.Id, conn.username}
handler(conn, request)
}
}
for _, ping := range iq.Pings {
if ping.Xmlns == "urn:xmpp:ping" {
conn.Write([]byte(`<iq type='result' to=' ` + conn.username + `@` + srv.domain + `/` + conn.extension + `' id='` + iq.Id + `'/>`))
}
}
}
func (srv *Server) switchToTLS(conn xmppConn) {
conn.Write([]byte(`<proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>`))
var tls_conn *tls.Conn
tls_conn = tls.Server(conn.Conn, &srv.tlsConfig)
tls_conn.Handshake()
new_conn := xmppConn{tls_conn, "", ""}
srv.handle(new_conn, true)
}
func checkErr(conn io.Writer, err error, msg string) {
if err != nil {
log.Println(err, msg)
}
}