https://github.com/emiago/sipgo Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Resources + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} emiago / sipgo Public * * Notifications * Fork 11 * Star 117 SIP library for writing fast SIP servers in GO License BSD-2-Clause license 117 stars 11 forks Activity Star Notifications * Code * Issues 10 * Pull requests 0 * Discussions * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Discussions * Actions * Projects * Wiki * Security * Insights emiago/sipgo This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 6 branches 24 tags Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/e] Use Git or checkout with SVN using the web URL. [gh repo clone emiago] Work fast with our official CLI. Learn more about the CLI. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @emiago emiago feat: handle multiline parsing ... 5d153a7 Nov 9, 2023 feat: handle multiline parsing This also reduces one allocation 32 -> 31 5d153a7 Git stats * 165 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github example fakes sip siptest testdata transaction transport .gitignore Dockerfile LICENSE README.md client.go client_test.go dialog.go dialog_client.go dialog_integration_test.go dialog_server.go go.mod go.sum icon.png server.go server_integration_test.go server_test.go ua.go View code [ ] Supported protocols Examples Tools developed: E2E/integration testing Performance Usage UAS/UAC build TLS transports Server Transaction Server stateless response Client Transaction Client stateless request Dialog handling (NEW) Stateful Proxy build SIP Debug Documentation Tests Credits Support README.md 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) 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. Supported protocols * [*] UDP * [*] TCP * [*] TLS * [*] WS * [*] WSS Examples * Stateful proxy example/proxysip * Register with authentication example/register * RTP echo with sipgox example/dialog Tools developed: * CLI softphone for easy testing gophone * Simple proxy where NAT is problem psip * ... your tool can be here If you use this lib in some way, open issue for more sharing. 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 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 (Contributions are welcome, I would place your results here) 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() * Server handle creates listeners and reacts on incoming requests. More on server transactions * Client handle allows creating transaction requests More on client transactions 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 := 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) // Check dialog response (SDP) and return ACK err = dialog.Ack(ctx) // Send BYE to terminate call err = sess.Bye(ctx) UAS: uasContact := sip.ContactHeader{ Address: sip.Uri{User: "test", Host: "127.0.0.200", Port: 5099}, } dialogSrv := 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) <-tx.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: transport.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: Call-ID: 1-1@10.10.1.1 From: "sipp" ;tag=1SIPpTag001 To: "uac" CSeq: 1 INVITE Server: Asterisk PBX 18.16.0 Content-Length: 0 Documentation More on documentation you can find on Go doc Tests Coverage: 36.7% Library will be covered with more tests. Focus is more on benchmarking currently. go test ./... Credits This project was based on gosip by 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. About SIP library for writing fast SIP servers in GO Resources Readme License BSD-2-Clause license Activity Stars 117 stars Watchers 5 watching Forks 11 forks Report repository Releases 20 v0.15.0 Latest Nov 9, 2023 + 19 releases Sponsor this project * ko_fi ko-fi.com/emiasupport Packages 0 No packages published Contributors 4 * @emiago emiago Emia * @oystedal oystedal * @a12n a12n Anton Yabchinskiy * @Maratk1n Maratk1n Marat Languages * Go 98.5% * Other 1.5% Footer (c) 2023 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time.