-
Notifications
You must be signed in to change notification settings - Fork 92
/
server.go
611 lines (515 loc) · 13.5 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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
package gosip
import (
"context"
"errors"
"fmt"
"io"
"net"
"sync"
"github.com/ghettovoice/gosip/log"
"github.com/ghettovoice/gosip/sip"
"github.com/ghettovoice/gosip/transaction"
"github.com/ghettovoice/gosip/transport"
"github.com/ghettovoice/gosip/util"
"github.com/tevino/abool"
)
// RequestHandler is a callback that will be called on the incoming request
// of the certain method
// tx argument can be nil for 2xx ACK request
type RequestHandler func(req sip.Request, tx sip.ServerTransaction)
type Server interface {
Shutdown()
Listen(network, addr string, options ...transport.ListenOption) error
Send(msg sip.Message) error
Request(req sip.Request) (sip.ClientTransaction, error)
RequestWithContext(
ctx context.Context,
request sip.Request,
options ...RequestWithContextOption,
) (sip.Response, error)
OnRequest(method sip.RequestMethod, handler RequestHandler) error
Respond(res sip.Response) (sip.ServerTransaction, error)
RespondOnRequest(
request sip.Request,
status sip.StatusCode,
reason, body string,
headers []sip.Header,
) (sip.ServerTransaction, error)
}
type TransportLayerFactory func(
ip net.IP,
dnsResolver *net.Resolver,
msgMapper sip.MessageMapper,
logger log.Logger,
) transport.Layer
type TransactionLayerFactory func(tpl sip.Transport, logger log.Logger) transaction.Layer
// ServerConfig describes available options
type ServerConfig struct {
// Public IP address or domain name, if empty auto resolved IP will be used.
Host string
// Dns is an address of the public DNS server to use in SRV lookup.
Dns string
Extensions []string
MsgMapper sip.MessageMapper
UserAgent string
}
// Server is a SIP server
type server struct {
running abool.AtomicBool
tp transport.Layer
tx transaction.Layer
host string
ip net.IP
hwg *sync.WaitGroup
hmu *sync.RWMutex
requestHandlers map[sip.RequestMethod]RequestHandler
extensions []string
userAgent string
log log.Logger
}
// NewServer creates new instance of SIP server.
func NewServer(
config ServerConfig,
tpFactory TransportLayerFactory,
txFactory TransactionLayerFactory,
logger log.Logger,
) Server {
if tpFactory == nil {
tpFactory = transport.NewLayer
}
if txFactory == nil {
txFactory = transaction.NewLayer
}
logger = logger.WithPrefix("gosip.Server")
var host string
var ip net.IP
if config.Host != "" {
host = config.Host
if addr, err := net.ResolveIPAddr("ip", host); err == nil {
ip = addr.IP
} else {
logger.Panicf("resolve host IP failed: %s", err)
}
} else {
if v, err := util.ResolveSelfIP(); err == nil {
ip = v
host = v.String()
} else {
logger.Panicf("resolve host IP failed: %s", err)
}
}
var dnsResolver *net.Resolver
if config.Dns != "" {
dnsResolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, "udp", config.Dns)
},
}
} else {
dnsResolver = net.DefaultResolver
}
var extensions []string
if config.Extensions != nil {
extensions = config.Extensions
}
userAgent := config.UserAgent
if userAgent == "" {
userAgent = "GoSIP"
}
srv := &server{
host: host,
ip: ip,
hwg: new(sync.WaitGroup),
hmu: new(sync.RWMutex),
requestHandlers: make(map[sip.RequestMethod]RequestHandler),
extensions: extensions,
userAgent: userAgent,
}
srv.log = logger.WithFields(log.Fields{
"sip_server_ptr": fmt.Sprintf("%p", srv),
})
srv.tp = tpFactory(ip, dnsResolver, config.MsgMapper, srv.Log())
sipTp := &sipTransport{
tpl: srv.tp,
srv: srv,
}
srv.tx = txFactory(sipTp, log.AddFieldsFrom(srv.Log(), srv.tp))
srv.running.Set()
go srv.serve()
return srv
}
func (srv *server) Log() log.Logger {
return srv.log
}
// ListenAndServe starts serving listeners on the provided address
func (srv *server) Listen(network string, listenAddr string, options ...transport.ListenOption) error {
return srv.tp.Listen(network, listenAddr, options...)
}
func (srv *server) serve() {
defer srv.Shutdown()
for {
select {
case tx, ok := <-srv.tx.Requests():
if !ok {
return
}
srv.hwg.Add(1)
go srv.handleRequest(tx.Origin(), tx)
case ack, ok := <-srv.tx.Acks():
if !ok {
return
}
srv.hwg.Add(1)
go srv.handleRequest(ack, nil)
case response, ok := <-srv.tx.Responses():
if !ok {
return
}
logger := srv.Log().WithFields(response.Fields())
logger.Warn("received not matched response")
// FIXME do something with this?
case err, ok := <-srv.tx.Errors():
if !ok {
return
}
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) {
srv.Log().Debugf("received SIP transaction error: %s", err)
} else {
srv.Log().Warnf("received SIP transaction error: %s", err)
}
case err, ok := <-srv.tp.Errors():
if !ok {
return
}
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) {
srv.Log().Debugf("received SIP transport error: %s", err)
} else {
srv.Log().Warnf("received SIP transport error: %s", err)
}
}
}
}
func (srv *server) handleRequest(req sip.Request, tx sip.ServerTransaction) {
defer srv.hwg.Done()
logger := srv.Log().WithFields(req.Fields())
logger.Debug("routing incoming SIP request...")
srv.hmu.RLock()
handler, ok := srv.requestHandlers[req.Method()]
srv.hmu.RUnlock()
if !ok {
logger.Warn("SIP request handler not found")
// ACK request doesn't have any transaction, so just skip this step
if tx != nil {
go func(tx sip.ServerTransaction, logger log.Logger) {
for {
select {
case <-srv.tx.Done():
return
case err, ok := <-tx.Errors():
if !ok {
return
}
logger.Warnf("error from SIP server transaction %s: %s", tx, err)
}
}
}(tx, logger)
}
// ACK request doesn't require any response, so just skip this step
if !req.IsAck() {
res := sip.NewResponseFromRequest("", req, 405, "Method Not Allowed", "")
if _, err := srv.Respond(res); err != nil {
logger.Errorf("respond '405 Method Not Allowed' failed: %s", err)
}
}
return
}
handler(req, tx)
}
// Send SIP message
func (srv *server) Request(req sip.Request) (sip.ClientTransaction, error) {
if !srv.running.IsSet() {
return nil, fmt.Errorf("can not send through stopped server")
}
return srv.tx.Request(srv.prepareRequest(req))
}
func (srv *server) RequestWithContext(
ctx context.Context,
request sip.Request,
options ...RequestWithContextOption,
) (sip.Response, error) {
return srv.requestWithContext(ctx, request, 1, options...)
}
func (srv *server) requestWithContext(
ctx context.Context,
request sip.Request,
attempt int,
options ...RequestWithContextOption,
) (sip.Response, error) {
optionsHash := &RequestWithContextOptions{}
for _, opt := range options {
opt.ApplyRequestWithContext(optionsHash)
}
tx, err := srv.Request(request)
if err != nil {
return nil, err
}
if optionsHash.OnAck != nil {
tx.OnAck(optionsHash.OnAck)
}
if optionsHash.OnCancel != nil {
tx.OnCancel(optionsHash.OnCancel)
}
txResponses := tx.Responses()
txErrs := tx.Errors()
responses := make(chan sip.Response, 1)
errs := make(chan error, 1)
done := make(chan struct{})
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
select {
case <-done:
case <-ctx.Done():
if err := tx.Cancel(); err != nil {
srv.Log().Error("cancel transaction failed", log.Fields{
"transaction_key": tx.Key(),
})
}
}
}()
go func() {
defer func() {
close(done)
wg.Done()
}()
var lastResponse sip.Response
previousMessages := make([]sip.Response, 0)
previousResponsesStatuses := make(map[string]bool)
getKey := func(res sip.Response) string {
return fmt.Sprintf("%d %s", res.StatusCode(), res.Reason())
}
for {
select {
case err, ok := <-txErrs:
if !ok {
txErrs = nil
// errors chan was closed
// we continue to pull responses until close
continue
}
errs <- err
return
case response, ok := <-txResponses:
if !ok {
if lastResponse != nil {
lastResponse.SetPrevious(previousMessages)
}
errs <- sip.NewRequestError(487, "Request Terminated", request, lastResponse)
return
}
response = sip.CopyResponse(response)
lastResponse = response
if optionsHash.ResponseHandler != nil {
optionsHash.ResponseHandler(response, request)
}
if response.IsProvisional() {
if _, ok := previousResponsesStatuses[getKey(response)]; !ok {
previousMessages = append(previousMessages, response)
previousResponsesStatuses[getKey(response)] = true
}
continue
}
// success
if response.IsSuccess() {
response.SetPrevious(previousMessages)
responses <- response
go func() {
for response := range tx.Responses() {
if optionsHash.ResponseHandler != nil {
optionsHash.ResponseHandler(response, request)
}
}
}()
return
}
// unauth request
needAuth := (response.StatusCode() == 401 || response.StatusCode() == 407) && attempt < 2
if needAuth && optionsHash.Authorizer != nil {
if err := optionsHash.Authorizer.AuthorizeRequest(request, response); err != nil {
errs <- err
return
}
if response, err := srv.requestWithContext(ctx, request, attempt+1, options...); err == nil {
responses <- response
} else {
errs <- err
}
return
}
// failed request
response.SetPrevious(previousMessages)
errs <- sip.NewRequestError(uint(response.StatusCode()), response.Reason(), request, response)
return
}
}
}()
var res sip.Response
select {
case err = <-errs:
case res = <-responses:
}
wg.Wait()
return res, err
}
func (srv *server) prepareRequest(req sip.Request) sip.Request {
srv.appendAutoHeaders(req)
return req
}
func (srv *server) Respond(res sip.Response) (sip.ServerTransaction, error) {
if !srv.running.IsSet() {
return nil, fmt.Errorf("can not send through stopped server")
}
return srv.tx.Respond(srv.prepareResponse(res))
}
func (srv *server) RespondOnRequest(
request sip.Request,
status sip.StatusCode,
reason, body string,
headers []sip.Header,
) (sip.ServerTransaction, error) {
response := sip.NewResponseFromRequest("", request, status, reason, body)
for _, header := range headers {
response.AppendHeader(header)
}
tx, err := srv.Respond(response)
if err != nil {
return nil, fmt.Errorf("respond '%d %s' failed: %w", response.StatusCode(), response.Reason(), err)
}
return tx, nil
}
func (srv *server) Send(msg sip.Message) error {
if !srv.running.IsSet() {
return fmt.Errorf("can not send through stopped server")
}
switch m := msg.(type) {
case sip.Request:
msg = srv.prepareRequest(m)
case sip.Response:
msg = srv.prepareResponse(m)
}
return srv.tp.Send(msg)
}
func (srv *server) prepareResponse(res sip.Response) sip.Response {
srv.appendAutoHeaders(res)
return res
}
// Shutdown gracefully shutdowns SIP server
func (srv *server) Shutdown() {
if !srv.running.IsSet() {
return
}
srv.running.UnSet()
// stop transaction layer
srv.tx.Cancel()
<-srv.tx.Done()
// stop transport layer
srv.tp.Cancel()
<-srv.tp.Done()
// wait for handlers
srv.hwg.Wait()
}
// OnRequest registers new request callback
func (srv *server) OnRequest(method sip.RequestMethod, handler RequestHandler) error {
srv.hmu.Lock()
srv.requestHandlers[method] = handler
srv.hmu.Unlock()
return nil
}
func (srv *server) appendAutoHeaders(msg sip.Message) {
autoAppendMethods := map[sip.RequestMethod]bool{
sip.INVITE: true,
sip.REGISTER: true,
sip.OPTIONS: true,
sip.REFER: true,
sip.NOTIFY: true,
}
var msgMethod sip.RequestMethod
switch m := msg.(type) {
case sip.Request:
msgMethod = m.Method()
if hdrs := msg.GetHeaders("User-Agent"); len(hdrs) == 0 {
hdr := sip.UserAgentHeader(srv.userAgent)
msg.AppendHeader(&hdr)
}
case sip.Response:
if cseq, ok := m.CSeq(); ok && !m.IsProvisional() {
msgMethod = cseq.MethodName
}
if hdrs := msg.GetHeaders("Server"); len(hdrs) == 0 {
hdr := sip.ServerHeader(srv.userAgent)
msg.AppendHeader(&hdr)
}
}
if len(msgMethod) > 0 {
if _, ok := autoAppendMethods[msgMethod]; ok {
hdrs := msg.GetHeaders("Allow")
if len(hdrs) == 0 {
allow := make(sip.AllowHeader, 0)
for _, method := range srv.getAllowedMethods() {
allow = append(allow, method)
}
if len(allow) > 0 {
msg.AppendHeader(allow)
}
}
hdrs = msg.GetHeaders("Supported")
if len(hdrs) == 0 && len(srv.extensions) > 0 {
msg.AppendHeader(&sip.SupportedHeader{
Options: srv.extensions,
})
}
}
}
if hdrs := msg.GetHeaders("Content-Length"); len(hdrs) == 0 {
msg.SetBody(msg.Body(), true)
}
}
func (srv *server) getAllowedMethods() []sip.RequestMethod {
methods := []sip.RequestMethod{
sip.INVITE,
sip.ACK,
sip.CANCEL,
}
added := map[sip.RequestMethod]bool{
sip.INVITE: true,
sip.ACK: true,
sip.CANCEL: true,
}
srv.hmu.RLock()
for method := range srv.requestHandlers {
if _, ok := added[method]; !ok {
methods = append(methods, method)
}
}
srv.hmu.RUnlock()
return methods
}
type sipTransport struct {
tpl transport.Layer
srv *server
}
func (tp *sipTransport) Messages() <-chan sip.Message {
return tp.tpl.Messages()
}
func (tp *sipTransport) Send(msg sip.Message) error {
return tp.srv.Send(msg)
}
func (tp *sipTransport) IsReliable(network string) bool {
return tp.tpl.IsReliable(network)
}
func (tp *sipTransport) IsStreamed(network string) bool {
return tp.tpl.IsStreamed(network)
}