forked from emiago/sipgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
405 lines (336 loc) · 10.6 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
package sipgo
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"log/slog"
"net"
"strings"
"github.com/livekit/sipgo/sip"
"github.com/livekit/sipgo/transport"
)
// RequestHandler is a callback that will be called on the incoming request
type RequestHandler func(req *sip.Request, tx sip.ServerTransaction)
// Server is a SIP server
type Server struct {
*UserAgent
// requestHandlers map of all registered request handlers
requestHandlers map[sip.RequestMethod]RequestHandler
noRouteHandler RequestHandler
log *slog.Logger
requestMiddlewares []func(r *sip.Request)
responseMiddlewares []func(r *sip.Response)
}
type ServerOption func(s *Server) error
// WithServerLogger allows customizing server logger
func WithServerLogger(logger *slog.Logger) ServerOption {
return func(s *Server) error {
s.log = logger
return nil
}
}
// NewServer creates new instance of SIP server handle.
// Allows creating server transaction handlers
// It uses User Agent transport and transaction layer
func NewServer(ua *UserAgent, options ...ServerOption) (*Server, error) {
s, err := newBaseServer(ua, options...)
if err != nil {
return nil, err
}
// Handle our transaction layer requests
s.tx.OnRequest(s.onRequest)
return s, nil
}
func newBaseServer(ua *UserAgent, options ...ServerOption) (*Server, error) {
s := &Server{
UserAgent: ua,
// userAgent: "SIPGO",
// dnsResolver: net.DefaultResolver,
requestMiddlewares: make([]func(r *sip.Request), 0),
responseMiddlewares: make([]func(r *sip.Response), 0),
requestHandlers: make(map[sip.RequestMethod]RequestHandler),
log: slog.With("caller", "Server"),
}
for _, o := range options {
if err := o(s); err != nil {
return nil, err
}
}
// TODO have this exported as option
s.noRouteHandler = s.defaultUnhandledHandler
return s, nil
}
var (
// Used only for testing, better way is to pass listener with Serve{Transport}
ctxTestListenAndServeReady = "ctxTestListenAndServeReady"
)
// Serve will fire all listeners
// Network supported: udp, tcp, ws
func (srv *Server) ListenAndServe(ctx context.Context, network string, addr string) error {
network = strings.ToLower(network)
var connCloser io.Closer
// TODO consider different design to avoid this additional go routines
go func() {
select {
case <-ctx.Done():
if connCloser == nil {
return
}
if err := connCloser.Close(); err != nil {
srv.log.Error("Failed to close listener", "err", err)
}
}
}()
switch network {
case "udp":
// resolve local UDP endpoint
laddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return fmt.Errorf("fail to resolve address. err=%w", err)
}
udpConn, err := net.ListenUDP("udp", laddr)
if err != nil {
return fmt.Errorf("listen udp error. err=%w", err)
}
connCloser = udpConn
if v := ctx.Value(ctxTestListenAndServeReady); v != nil {
close(v.(chan any))
}
return srv.tp.ServeUDP(udpConn)
case "ws", "tcp":
laddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return fmt.Errorf("fail to resolve address. err=%w", err)
}
conn, err := net.ListenTCP("tcp", laddr)
if err != nil {
return fmt.Errorf("listen tcp error. err=%w", err)
}
connCloser = conn
if v := ctx.Value(ctxTestListenAndServeReady); v != nil {
close(v.(chan any))
}
// and uses listener to buffer
if network == "ws" {
return srv.tp.ServeWS(conn)
}
return srv.tp.ServeTCP(conn)
}
return transport.ErrNetworkNotSuported
}
// Serve will fire all listeners that are secured.
// Network supported: tls, wss
func (srv *Server) ListenAndServeTLS(ctx context.Context, network string, addr string, conf *tls.Config) error {
network = strings.ToLower(network)
var connCloser io.Closer
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// TODO consider different design to avoid this additional go routines
go func() {
select {
case <-ctx.Done():
if connCloser == nil {
return
}
if err := connCloser.Close(); err != nil {
srv.log.Error("Failed to close listener", "err", err)
}
}
}()
// Do some filtering
switch network {
case "tls", "tcp", "ws", "wss":
laddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return fmt.Errorf("fail to resolve address. err=%w", err)
}
listener, err := tls.Listen("tcp", laddr.String(), conf)
if err != nil {
return fmt.Errorf("listen tls error. err=%w", err)
}
connCloser = listener
if v := ctx.Value(ctxTestListenAndServeReady); v != nil {
close(v.(chan any))
}
if network == "ws" || network == "wss" {
return srv.tp.ServeWSS(listener)
}
return srv.tp.ServeTLS(listener)
}
return transport.ErrNetworkNotSuported
}
// ServeUDP starts serving request on UDP type listener.
func (srv *Server) ServeUDP(l net.PacketConn) error {
return srv.tp.ServeUDP(l)
}
// ServeTCP starts serving request on TCP type listener.
func (srv *Server) ServeTCP(l net.Listener) error {
return srv.tp.ServeTCP(l)
}
// ServeTLS starts serving request on TLS type listener.
func (srv *Server) ServeTLS(l net.Listener) error {
return srv.tp.ServeTLS(l)
}
// ServeWS starts serving request on WS type listener.
func (srv *Server) ServeWS(l net.Listener) error {
return srv.tp.ServeWS(l)
}
// ServeWS starts serving request on WS type listener.
func (srv *Server) ServeWSS(l net.Listener) error {
return srv.tp.ServeWSS(l)
}
// onRequest gets request from Transaction layer
func (srv *Server) onRequest(req *sip.Request, tx sip.ServerTransaction) {
go srv.handleRequest(req, tx)
}
// handleRequest must be run in seperate goroutine
func (srv *Server) handleRequest(req *sip.Request, tx sip.ServerTransaction) {
for _, mid := range srv.requestMiddlewares {
mid(req)
}
handler := srv.getHandler(req.Method)
handler(req, tx)
if tx != nil {
// Must be called to prevent any transaction leaks
tx.Terminate()
}
}
// WriteResponse will proxy message to transport layer. Use it in stateless mode
func (srv *Server) WriteResponse(r *sip.Response) error {
return srv.tp.WriteMsg(r)
}
// Close server handle. UserAgent must be closed for full transaction and transport layer closing.
func (srv *Server) Close() error {
return nil
}
// OnRequest registers new request callback. Can be used as generic way to add handler
func (srv *Server) OnRequest(method sip.RequestMethod, handler RequestHandler) {
srv.requestHandlers[method] = handler
}
// OnInvite registers Invite request handler
func (srv *Server) OnInvite(handler RequestHandler) {
srv.requestHandlers[sip.INVITE] = handler
}
// OnAck registers Ack request handler
func (srv *Server) OnAck(handler RequestHandler) {
srv.requestHandlers[sip.ACK] = handler
}
// OnCancel registers Cancel request handler
func (srv *Server) OnCancel(handler RequestHandler) {
srv.requestHandlers[sip.CANCEL] = handler
}
// OnBye registers Bye request handler
func (srv *Server) OnBye(handler RequestHandler) {
srv.requestHandlers[sip.BYE] = handler
}
// OnRegister registers Register request handler
func (srv *Server) OnRegister(handler RequestHandler) {
srv.requestHandlers[sip.REGISTER] = handler
}
// OnOptions registers Options request handler
func (srv *Server) OnOptions(handler RequestHandler) {
srv.requestHandlers[sip.OPTIONS] = handler
}
// OnSubscribe registers Subscribe request handler
func (srv *Server) OnSubscribe(handler RequestHandler) {
srv.requestHandlers[sip.SUBSCRIBE] = handler
}
// OnNotify registers Notify request handler
func (srv *Server) OnNotify(handler RequestHandler) {
srv.requestHandlers[sip.NOTIFY] = handler
}
// OnRefer registers Refer request handler
func (srv *Server) OnRefer(handler RequestHandler) {
srv.requestHandlers[sip.REFER] = handler
}
// OnInfo registers Info request handler
func (srv *Server) OnInfo(handler RequestHandler) {
srv.requestHandlers[sip.INFO] = handler
}
// OnMessage registers Message request handler
func (srv *Server) OnMessage(handler RequestHandler) {
srv.requestHandlers[sip.MESSAGE] = handler
}
// OnPrack registers Prack request handler
func (srv *Server) OnPrack(handler RequestHandler) {
srv.requestHandlers[sip.PRACK] = handler
}
// OnUpdate registers Update request handler
func (srv *Server) OnUpdate(handler RequestHandler) {
srv.requestHandlers[sip.UPDATE] = handler
}
// OnPublish registers Publish request handler
func (srv *Server) OnPublish(handler RequestHandler) {
srv.requestHandlers[sip.PUBLISH] = handler
}
// OnNoRoute registers no route handler
// default is handling is responding 405 Method Not allowed
// This allows customizing your response for any non handled message
func (srv *Server) OnNoRoute(handler RequestHandler) {
srv.noRouteHandler = handler
}
// RegisteredMethods returns list of registered handlers.
// Can be used for constructing Allow header
func (srv *Server) RegisteredMethods() []string {
r := make([]string, len(srv.requestHandlers))
for k, _ := range srv.requestHandlers {
r = append(r, k.String())
}
return r
}
func (srv *Server) getHandler(method sip.RequestMethod) (handler RequestHandler) {
handler, ok := srv.requestHandlers[method]
if !ok {
return srv.noRouteHandler
}
return handler
}
func (srv *Server) defaultUnhandledHandler(req *sip.Request, tx sip.ServerTransaction) {
srv.log.Warn("SIP request handler not found")
res := sip.NewResponseFromRequest(req, 405, "Method Not Allowed", nil)
// Send response directly and let transaction terminate
if err := srv.WriteResponse(res); err != nil {
srv.log.Error("respond '405 Method Not Allowed' failed", "err", err)
}
}
// ServeRequest can be used as middleware for preprocessing message
func (srv *Server) ServeRequest(f func(r *sip.Request)) {
srv.requestMiddlewares = append(srv.requestMiddlewares, f)
}
func (srv *Server) onTransportMessage(m sip.Message) {
//Register transport middleware
// this avoids allocations and it forces devs to avoid sip.Message usage
switch r := m.(type) {
case *sip.Response:
for _, mid := range srv.responseMiddlewares {
mid(r)
}
}
}
// Transport is function to get transport layer of server
// Can be used for modifying
func (srv *Server) TransportLayer() *transport.Layer {
return srv.tp
}
// GenerateTLSConfig creates basic tls.Config that you can pass for ServerTLS
// It needs rootPems for client side
func GenerateTLSConfig(certFile string, keyFile string, rootPems []byte) (*tls.Config, error) {
roots := x509.NewCertPool()
if rootPems != nil {
ok := roots.AppendCertsFromPEM(rootPems)
if !ok {
return nil, fmt.Errorf("failed to parse root certificate")
}
}
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, fmt.Errorf("fail to load cert. err=%w", err)
}
conf := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: roots,
}
return conf, nil
}