Categorygithub.com/innovative247/sipgo
modulepackage
0.16.2
Repository: https://github.com/innovative247/sipgo.git
Documentation: pkg.go.dev

# README

SIPGO

Go Report Card License GitHub go.mod Go version

SIPGO is library for writing fast SIP services in GO language.
It comes with SIP stack (RFC 3261|RFC3581) optimized for fast parsing.

For experimental features checkout also
github.com/emiago/sipgox It adds media, call, dialog creation on top of sipgo more easily.

Fetch lib with:

go get github.com/emiago/sipgo

NOTE: LIB MAY HAVE API CHANGES UNTIL STABLE VERSION.

To keep project development you can support it

Also, if you are using lib in any way, we would like to share it here.

Supported protocols

  • UDP
  • TCP
  • TLS
  • WS
  • WSS

Examples

Tools developed:

  • CLI softphone for easy testing gophone
  • Simple proxy where NAT is problem psip
  • ... your tool can be here

Performance

As example you can find example/proxysip as simple version of statefull proxy. It is used for stress testing with sipp. To find out more about performance check the latest results:
example/proxysip

Used By

babelforce

Usage

Lib allows you to write easily sip servers, clients, stateful proxies, registrars or any sip routing. Writing in GO we are not limited to handle SIP requests/responses in many ways, or to integrate and scale with any external services (databases, caches...).

UAS/UAC build

Using server or client handle for UA you can build incoming or outgoing requests.

ua, _ := sipgo.NewUA() // Build user agent
srv, _ := sipgo.NewServer(ua) // Creating server handle
client, _ := sipgo.NewClient(ua) // Creating client handle
srv.OnInvite(inviteHandler)
srv.OnAck(ackHandler)
srv.OnCancel(cancelHandler)
srv.OnBye(byeHandler)

// For registrars
// srv.OnRegister(registerHandler)
ctx, _ := signal.NotifyContext(ctx, os.Interrupt)
go srv.ListenAndServe(ctx, "udp", "127.0.0.1:5060")
go srv.ListenAndServe(ctx, "tcp", "127.0.0.1:5061")
go srv.ListenAndServe(ctx, "ws", "127.0.0.1:5080")
<-ctx.Done()

TLS transports

// TLS
conf :=  sipgo.GenerateTLSConfig(certFile, keyFile, rootPems)
srv.ListenAndServeTLS(ctx, "tcp", "127.0.0.1:5061", conf)
srv.ListenAndServeTLS(ctx, "ws", "127.0.0.1:5081", conf)

Server Transaction

Server transaction is passed on handler

// Incoming request
srv.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
    res := sip.NewResponseFromRequest(req, code, reason, body)
    // Send response
    tx.Respond(res)

    select {
        case m := <-tx.Acks(): // Handle ACK . ACKs on 2xx are send as different request
        case m := <-tx.Cancels(): // Handle Cancel 
        case <-tx.Done():
            // Signal transaction is done. 
            // Check any errors with tx.Err() to have more info why terminated
            return
    }

    // terminating handler terminates Server transaction automaticaly
})

Server stateless response

srv := sipgo.NewServer()
...
func ackHandler(req *sip.Request, tx sip.ServerTransaction) {
    res := sip.NewResponseFromRequest(req, code, reason, body)
    srv.WriteResponse(res)
}
srv.OnACK(ackHandler)

Client Transaction

Using client handle allows easy creating and sending request. Unless you customize transaction request with opts by default client.TransactionRequest will build all other headers needed to pass correct sip request.

Here is full example:

ctx := context.Background()
client, _ := sipgo.NewClient(ua) // Creating client handle

// Request is either from server request handler or created
req.SetDestination("10.1.2.3") // Change sip.Request destination
tx, err := client.TransactionRequest(ctx, req) // Send request and get client transaction handle

defer tx.Terminate() // Client Transaction must be terminated for cleanup
...

select {
    case res := <-tx.Responses():
    // Handle responses
    case <-tx.Done():
    // Wait for termination
    return
}

Client stateless request

client, _ := sipgo.NewClient(ua) // Creating client handle
req := sip.NewRequest(method, &recipment)
// Send request and forget
client.WriteRequest(req)

Dialog handling (NEW)

DialogClient and DialogServer allow easier managing multiple dialog (Calls) sessions

UAC:

contactHDR := sip.ContactHeader{
    Address: sip.Uri{User: "test", Host: "127.0.0.200", Port: 5088},
}
dialogCli := sipgo.NewDialogClient(cli, contactHDR)

// Attach Bye handling for dialog
srv.OnBye(func(req *sip.Request, tx sip.ServerTransaction) {
    err := dialogCli.ReadBye(req, tx)
    //handle error
})

// Create dialog session
dialog, err := dialogCli.Invite(ctx, recipientURI, nil)
// Wait for answer
err = dialog.WaitAnswer(ctx, AnswerOptions{})
// Check dialog response dialog.InviteResponse (SDP) and return ACK
err = dialog.Ack(ctx)
// Send BYE to terminate call
err = dialog.Bye(ctx)

UAS:

uasContact := sip.ContactHeader{
    Address: sip.Uri{User: "test", Host: "127.0.0.200", Port: 5099},
}
dialogSrv := sipgo.NewDialogServer(cli, uasContact)

srv.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
    dlg, err := dialogSrv.ReadInvite(req, tx)
    // handle error
    dlg.Respond(sip.StatusTrying, "Trying", nil)
    dlg.Respond(sip.StatusOK, "OK", nil)
    
    // Instead Done also dlg.State() can be used
    <-dlg.Done()
})

srv.OnAck(func(req *sip.Request, tx sip.ServerTransaction) {
    dialogSrv.ReadAck(req, tx)
})

srv.OnBye(func(req *sip.Request, tx sip.ServerTransaction) {
    dialogSrv.ReadBye(req, tx)
})

Stateful Proxy build

Proxy is combination client and server handle that creates server/client transaction. They need to share same ua same like uac/uas build. Forwarding request is done via client handle:


srv.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
    ctx := context.Background()
    req.SetDestination("10.1.2.3") // Change sip.Request destination
    // Start client transaction and relay our request. Add Via and Record-Route header
    clTx, err := client.TransactionRequest(ctx, req, sipgo.ClientRequestAddVia, sipgo.ClientRequestAddRecordRoute)
    // Send back response
    res := <-cltx.Responses()
    tx.Respond(res)
})

SIP Debug

You can have full SIP messages dumped from transport into Debug level message.

Example:

sip.SIPDebug = true
Feb 24 23:32:26.493191 DBG UDP read 10.10.0.10:5060 <- 10.10.0.100:5060:
SIP/2.0 100 Trying
Via: SIP/2.0/UDP 10.10.0.10:5060;rport=5060;received=10.10.0.10;branch=z9hG4bK.G3nCwpXAKJQ0T2oZUII70wuQx9NeXc61;alias
Via: SIP/2.0/UDP 10.10.1.1:5060;branch=z9hG4bK-1-1-0
Record-Route: <sip:10.10.0.10;transport=udp;lr>
Call-ID: [email protected]
From: "sipp" <sip:[email protected]>;tag=1SIPpTag001
To: "uac" <sip:[email protected]>
CSeq: 1 INVITE
Server: Asterisk PBX 18.16.0
Content-Length:  0

Documentation

More on documentation you can find on Go doc

E2E/integration testing

If you are interested using lib for your testing services then checkout article on how easy you can make calls and other

Tests

Coverage: 36.7%

Library will be covered with more tests. Focus is more on benchmarking currently.

go test ./...  

Credits

This project was influenced by gosip, project by @ghetovoice, but started as new project to achieve best/better performance and to improve API. This unfortunately required many design changes, therefore this libraries are not compatible.

Support

If you find this project interesting for bigger support or contributing, you can contact me on mail

For bugs features pls create issue.

# Packages

No description provided by the author
Originally forked from https://github.com/ghettovoice/gosip by @ghetovoice.
No description provided by the author

# Functions

ClientRequestAddRecordRoute is option for adding record route header Based on proxy setup https://www.rfc-editor.org/rfc/rfc3261#section-16.
ClientRequestAddVia is option for adding via header Based on proxy setup https://www.rfc-editor.org/rfc/rfc3261.html#section-16.6.
ClientRequestBuild will build missing fields in request This is by default but can be used to combine with other ClientRequestOptions.
Based on proxy setup https://www.rfc-editor.org/rfc/rfc3261#section-16 ClientRequestDecreaseMaxForward should be used when forwarding request.
GenerateTLSConfig creates basic tls.Config that you can pass for ServerTLS It needs rootPems for client side.
No description provided by the author
NewClient creates client handle for user agent.
NewDialogClient provides handle for managing UAC dialog Contact hdr must be provided for correct invite In case handling different transports you should have multiple instances per transport.
NewDialogServer provides handle for managing UAS dialog Contact hdr must be provided for responses Client is needed for termination dialog session In case handling different transports you should have multiple instances per transport.
NewServer creates new instance of SIP server handle.
NewUA creates User Agent User Agent will create transport and transaction layer Check options for customizing user agent.
WithClientAddr is merge of WithClientHostname and WithClientPort addr is format <host>:<port>.
WithClientHost allows setting default route host default it will be used user agent IP.
WithClientLogger allows customizing client logger.
WithClientNAT makes client aware that is behind NAT.
WithClientPort allows setting default route port.
WithServerLogger allows customizing server logger.
WithUserAgent changes user agent name Default: sipgo.
WithUserAgentDNSResolver allows customizing default DNS resolver for transport layer.
WithUserAgentIP sets local IP that will be used in building request If not used IP will be resolved Deprecated: Use on client WithClientHostname WithClientPort.
WithUserAgenTLSConfig allows customizing default tls config.
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Used only for testing, better way is to pass listener with Serve{Transport}.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Server is a SIP server.
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
RequestHandler is a callback that will be called on the incoming request.
No description provided by the author
No description provided by the author