123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- // The MIT License (MIT)
- //
- // # Copyright (c) 2022-2024 Winlin
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy of
- // this software and associated documentation files (the "Software"), to deal in
- // the Software without restriction, including without limitation the rights to
- // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- // the Software, and to permit persons to whom the Software is furnished to do so,
- // subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in all
- // copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- package gb28181
- import (
- "context"
- "fmt"
- "github.com/ghettovoice/gosip/log"
- "github.com/ghettovoice/gosip/sip"
- "github.com/ghettovoice/gosip/transport"
- "github.com/ossrs/go-oryx-lib/errors"
- "github.com/ossrs/go-oryx-lib/logger"
- "math/rand"
- "net/url"
- "strings"
- "sync"
- "time"
- )
- type SIPConfig struct {
- // The server address, for example: tcp://127.0.0.1:5060k
- addr string
- // The SIP domain, for example: ossrs.io or 3402000000
- domain string
- // The SIP device ID, for example: camera or 34020000001320000001
- user string
- // The N number of random device ID, for example, 10 means 1320000001
- random int
- // The SIP server ID, for example: srs or 34020000002000000001
- server string
- // The cached device id.
- deviceID string
- }
- // The global cache to avoid conflict of deviceID.
- // Note that it's not coroutine safe, but it should be OK for utest.
- var deviceIDCache map[string]bool
- func init() {
- deviceIDCache = make(map[string]bool)
- }
- func (v *SIPConfig) DeviceID() string {
- for v.deviceID == "" {
- // Generate a random ID.
- var rid string
- for len(rid) < v.random {
- rid += fmt.Sprintf("%v", rand.Uint64())
- }
- deviceID := fmt.Sprintf("%v%v", v.user, rid[:v.random])
- // Ignore if exists.
- if _, ok := deviceIDCache[deviceID]; !ok {
- v.deviceID = deviceID
- deviceIDCache[deviceID] = true
- }
- }
- return v.deviceID
- }
- func (v *SIPConfig) String() string {
- sb := []string{}
- if v.addr != "" {
- sb = append(sb, fmt.Sprintf("addr=%v", v.addr))
- }
- if v.domain != "" {
- sb = append(sb, fmt.Sprintf("domain=%v", v.domain))
- }
- if v.user != "" {
- sb = append(sb, fmt.Sprintf("user=%v", v.user))
- sb = append(sb, fmt.Sprintf("deviceID=%v", v.DeviceID()))
- }
- if v.random > 0 {
- sb = append(sb, fmt.Sprintf("random=%v", v.random))
- }
- if v.server != "" {
- sb = append(sb, fmt.Sprintf("server=%v", v.server))
- }
- return strings.Join(sb, ",")
- }
- type SIPSession struct {
- conf *SIPConfig
- rb *sip.RequestBuilder
- requests chan sip.Request
- responses chan sip.Response
- wg sync.WaitGroup
- ctx context.Context
- cancel context.CancelFunc
- client *SIPClient
- seq uint
- }
- func NewSIPSession(c *SIPConfig) *SIPSession {
- return &SIPSession{
- conf: c, client: NewSIPClient(), rb: sip.NewRequestBuilder(),
- requests: make(chan sip.Request, 1024), responses: make(chan sip.Response, 1024),
- seq: 100,
- }
- }
- func (v *SIPSession) Close() error {
- if v.cancel != nil {
- v.cancel()
- }
- v.client.Close()
- v.wg.Wait()
- return nil
- }
- func (v *SIPSession) Connect(ctx context.Context) error {
- if ctx.Err() != nil {
- return ctx.Err()
- }
- ctx, cancel := context.WithCancel(ctx)
- v.ctx, v.cancel = ctx, cancel
- if err := v.client.Connect(ctx, v.conf.addr); err != nil {
- return errors.Wrapf(err, "connect with sipConfig %v", v.conf.String())
- }
- // Dispatch requests and responses.
- go func() {
- v.wg.Add(1)
- defer v.wg.Done()
- for {
- select {
- case <-v.ctx.Done():
- return
- case msg := <-v.client.incoming:
- if req, ok := msg.(sip.Request); ok {
- select {
- case v.requests <- req:
- case <-v.ctx.Done():
- return
- }
- } else if res, ok := msg.(sip.Response); ok {
- select {
- case v.responses <- res:
- case <-v.ctx.Done():
- return
- }
- } else {
- logger.Wf(ctx, "Drop message %v", msg.String())
- }
- }
- }
- }()
- return nil
- }
- func (v *SIPSession) Register(ctx context.Context) (sip.Message, sip.Message, error) {
- return v.doRegister(ctx, 3600)
- }
- func (v *SIPSession) UnRegister(ctx context.Context) (sip.Message, sip.Message, error) {
- return v.doRegister(ctx, 0)
- }
- func (v *SIPSession) doRegister(ctx context.Context, expires int) (sip.Message, sip.Message, error) {
- if ctx.Err() != nil {
- return nil, nil, ctx.Err()
- }
- sipPort := sip.Port(5060)
- sipCallID := sip.CallID(fmt.Sprintf("%v", rand.Uint64()))
- sipBranch := fmt.Sprintf("z9hG4bK_%v", rand.Uint32())
- sipTag := fmt.Sprintf("%v", rand.Uint32())
- sipMaxForwards := sip.MaxForwards(70)
- sipExpires := sip.Expires(uint32(expires))
- sipPIP := "192.168.3.99"
- v.seq++
- rb := v.rb
- rb.SetTransport("TCP")
- rb.SetMethod(sip.REGISTER)
- rb.AddVia(&sip.ViaHop{
- ProtocolName: "SIP", ProtocolVersion: "2.0", Transport: "TCP", Host: sipPIP, Port: &sipPort,
- Params: sip.NewParams().Add("branch", sip.String{Str: sipBranch}),
- })
- rb.SetFrom(&sip.Address{
- Uri: &sip.SipUri{FUser: sip.String{v.conf.DeviceID()}, FHost: v.conf.domain},
- Params: sip.NewParams().Add("tag", sip.String{Str: sipTag}),
- })
- rb.SetTo(&sip.Address{
- Uri: &sip.SipUri{FUser: sip.String{v.conf.DeviceID()}, FHost: v.conf.domain},
- })
- rb.SetCallID(&sipCallID)
- rb.SetSeqNo(v.seq)
- rb.SetRecipient(&sip.SipUri{FUser: sip.String{v.conf.server}, FHost: v.conf.domain})
- rb.SetContact(&sip.Address{
- Uri: &sip.SipUri{FUser: sip.String{v.conf.DeviceID()}, FHost: sipPIP, FPort: &sipPort},
- })
- rb.SetMaxForwards(&sipMaxForwards)
- rb.SetExpires(&sipExpires)
- req, err := rb.Build()
- if err != nil {
- return req, nil, errors.Wrap(err, "build request")
- }
- if err = v.client.Send(req); err != nil {
- return req, nil, errors.Wrapf(err, "send request %v", req.String())
- }
- callID := sipGetCallID(req)
- if callID == "" {
- return req, nil, errors.Errorf("Invalid SIP Call-ID register %v", req.String())
- }
- logger.Tf(ctx, "Send REGISTER request, Call-ID=%v, Expires=%v", callID, expires)
- for {
- select {
- case <-ctx.Done():
- return nil, nil, ctx.Err()
- case <-v.ctx.Done():
- return nil, nil, v.ctx.Err()
- case msg := <-v.responses:
- if tv := sipGetCallID(msg); tv == callID {
- return req, msg, nil
- } else {
- logger.Wf(v.ctx, "Not callID=%v, msg=%v, drop message %v", callID, tv, msg.String())
- }
- }
- }
- }
- func (v *SIPSession) Trying(ctx context.Context, invite sip.Message) error {
- if ctx.Err() != nil {
- return ctx.Err()
- }
- req, ok := invite.(sip.Request)
- if !ok {
- return errors.Errorf("Invalid SIP request invite %v", invite.String())
- }
- res := sip.NewResponseFromRequest("", req, sip.StatusCode(100), "Trying", "")
- if err := v.client.Send(res); err != nil {
- return errors.Wrapf(err, "send response %v", res.String())
- }
- return nil
- }
- func (v *SIPSession) InviteResponse(ctx context.Context, invite sip.Message) (sip.Message, error) {
- if ctx.Err() != nil {
- return nil, ctx.Err()
- }
- req, ok := invite.(sip.Request)
- if !ok {
- return nil, errors.Errorf("Invalid SIP request invite %v", invite.String())
- }
- callID := sipGetCallID(invite)
- if callID == "" {
- return nil, errors.Errorf("Invalid SIP Call-ID invite %v", invite.String())
- }
- res := sip.NewResponseFromRequest("", req, sip.StatusCode(200), "OK", "")
- if err := v.client.Send(res); err != nil {
- return nil, errors.Wrapf(err, "send response %v", res.String())
- }
- logger.Tf(ctx, "Send INVITE response, Call-ID=%v", callID)
- for {
- select {
- case <-ctx.Done():
- return nil, ctx.Err()
- case <-v.ctx.Done():
- return nil, v.ctx.Err()
- case msg := <-v.requests:
- // Must be an ACK message.
- if !msg.IsAck() {
- return msg, errors.Errorf("invalid ACK message %v", msg.String())
- }
- // Check CALL-ID of ACK, should be equal to 200 OK.
- if tv := sipGetCallID(msg); tv == callID {
- return msg, nil
- } else {
- logger.Wf(v.ctx, "Not callID=%v, msg=%v, drop message %v", callID, tv, msg.String())
- }
- }
- }
- }
- func (v *SIPSession) Message(ctx context.Context) (sip.Message, sip.Message, error) {
- if ctx.Err() != nil {
- return nil, nil, ctx.Err()
- }
- sipPort := sip.Port(5060)
- sipCallID := sip.CallID(fmt.Sprintf("%v", rand.Uint64()))
- sipBranch := fmt.Sprintf("z9hG4bK_%v", rand.Uint32())
- sipTag := fmt.Sprintf("%v", rand.Uint32())
- sipMaxForwards := sip.MaxForwards(70)
- sipExpires := sip.Expires(3600)
- sipPIP := "192.168.3.99"
- v.seq++
- rb := v.rb
- rb.SetTransport("TCP")
- rb.SetMethod(sip.MESSAGE)
- rb.AddVia(&sip.ViaHop{
- ProtocolName: "SIP", ProtocolVersion: "2.0", Transport: "TCP", Host: sipPIP, Port: &sipPort,
- Params: sip.NewParams().Add("branch", sip.String{Str: sipBranch}),
- })
- rb.SetFrom(&sip.Address{
- Uri: &sip.SipUri{FUser: sip.String{v.conf.DeviceID()}, FHost: v.conf.domain},
- Params: sip.NewParams().Add("tag", sip.String{Str: sipTag}),
- })
- rb.SetTo(&sip.Address{
- Uri: &sip.SipUri{FUser: sip.String{v.conf.server}, FHost: v.conf.domain},
- })
- rb.SetCallID(&sipCallID)
- rb.SetSeqNo(v.seq)
- rb.SetRecipient(&sip.SipUri{FUser: sip.String{v.conf.server}, FHost: v.conf.domain})
- rb.SetContact(&sip.Address{
- Uri: &sip.SipUri{FUser: sip.String{v.conf.DeviceID()}, FHost: sipPIP, FPort: &sipPort},
- })
- rb.SetMaxForwards(&sipMaxForwards)
- rb.SetExpires(&sipExpires)
- v.seq++
- rb.SetBody(strings.Join([]string{
- `<?xml version="1.0" encoding="GB2312"?>`,
- "<Notify>",
- "<CmdType>Keepalive</CmdType>",
- fmt.Sprintf("<SN>%v</SN>", v.seq),
- fmt.Sprintf("<DeviceID>%v</DeviceID>", v.conf.DeviceID()),
- "<Status>OK</Status>",
- "</Notify>\n",
- }, "\n"))
- req, err := rb.Build()
- if err != nil {
- return req, nil, errors.Wrap(err, "build request")
- }
- if err = v.client.Send(req); err != nil {
- return req, nil, errors.Wrapf(err, "send request %v", req.String())
- }
- callID := sipGetCallID(req)
- if callID == "" {
- return req, nil, errors.Errorf("Invalid SIP Call-ID message %v", req.String())
- }
- logger.Tf(ctx, "Send MESSAGE request, Call-ID=%v", callID)
- for {
- select {
- case <-ctx.Done():
- return nil, nil, ctx.Err()
- case <-v.ctx.Done():
- return nil, nil, v.ctx.Err()
- case msg := <-v.responses:
- if tv := sipGetCallID(msg); tv == callID {
- return req, msg, nil
- } else {
- logger.Wf(v.ctx, "Not callID=%v, msg=%v, drop message %v", callID, tv, msg.String())
- }
- }
- }
- }
- func (v *SIPSession) Bye(ctx context.Context) (sip.Message, sip.Message, error) {
- if ctx.Err() != nil {
- return nil, nil, ctx.Err()
- }
- sipPort := sip.Port(5060)
- sipCallID := sip.CallID(fmt.Sprintf("%v", rand.Uint64()))
- sipBranch := fmt.Sprintf("z9hG4bK_%v", rand.Uint32())
- sipTag := fmt.Sprintf("%v", rand.Uint32())
- sipMaxForwards := sip.MaxForwards(70)
- sipExpires := sip.Expires(3600)
- sipPIP := "192.168.3.99"
- v.seq++
- rb := v.rb
- rb.SetTransport("TCP")
- rb.SetMethod(sip.BYE)
- rb.AddVia(&sip.ViaHop{
- ProtocolName: "SIP", ProtocolVersion: "2.0", Transport: "TCP", Host: sipPIP, Port: &sipPort,
- Params: sip.NewParams().Add("branch", sip.String{Str: sipBranch}),
- })
- rb.SetFrom(&sip.Address{
- Uri: &sip.SipUri{FUser: sip.String{v.conf.DeviceID()}, FHost: v.conf.domain},
- Params: sip.NewParams().Add("tag", sip.String{Str: sipTag}),
- })
- rb.SetTo(&sip.Address{
- Uri: &sip.SipUri{FUser: sip.String{v.conf.server}, FHost: v.conf.domain},
- })
- rb.SetCallID(&sipCallID)
- rb.SetSeqNo(v.seq)
- rb.SetRecipient(&sip.SipUri{FUser: sip.String{v.conf.server}, FHost: v.conf.domain})
- rb.SetContact(&sip.Address{
- Uri: &sip.SipUri{FUser: sip.String{v.conf.DeviceID()}, FHost: sipPIP, FPort: &sipPort},
- })
- rb.SetMaxForwards(&sipMaxForwards)
- rb.SetExpires(&sipExpires)
- req, err := rb.Build()
- if err != nil {
- return req, nil, errors.Wrap(err, "build request")
- }
- if err = v.client.Send(req); err != nil {
- return req, nil, errors.Wrapf(err, "send request %v", req.String())
- }
- callID := sipGetCallID(req)
- if callID == "" {
- return req, nil, errors.Errorf("Invalid SIP Call-ID bye %v", req.String())
- }
- logger.Tf(ctx, "Send BYE request, Call-ID=%v", callID)
- for {
- select {
- case <-ctx.Done():
- return nil, nil, ctx.Err()
- case <-v.ctx.Done():
- return nil, nil, v.ctx.Err()
- case msg := <-v.responses:
- if tv := sipGetCallID(msg); tv == callID {
- return req, msg, nil
- } else {
- logger.Wf(v.ctx, "Not callID=%v, msg=%v, drop message %v", callID, tv, msg.String())
- }
- }
- }
- }
- func (v *SIPSession) Wait(ctx context.Context, method sip.RequestMethod) (sip.Message, error) {
- if ctx.Err() != nil {
- return nil, ctx.Err()
- }
- for {
- select {
- case <-ctx.Done():
- return nil, ctx.Err()
- case <-v.ctx.Done():
- return nil, v.ctx.Err()
- case msg := <-v.requests:
- if r, ok := msg.(sip.Request); ok && r.Method() == method {
- return msg, nil
- } else {
- logger.Wf(v.ctx, "Not method=%v, drop message %v", method, msg.String())
- }
- }
- }
- }
- type SIPClient struct {
- ctx context.Context
- cancel context.CancelFunc
- incoming chan sip.Message
- target *transport.Target
- protocol transport.Protocol
- cleanupTimeout time.Duration
- }
- func NewSIPClient() *SIPClient {
- return &SIPClient{
- cleanupTimeout: 5 * time.Second,
- }
- }
- func (v *SIPClient) Close() error {
- if v.cancel != nil {
- v.cancel()
- }
- // Wait for protocol stack to cleanup.
- if v.protocol != nil {
- select {
- case <-time.After(v.cleanupTimeout):
- logger.E(v.ctx, "Wait for protocol cleanup timeout")
- case <-v.protocol.Done():
- logger.T(v.ctx, "SIP protocol stack done")
- }
- }
- return nil
- }
- func (v *SIPClient) Connect(ctx context.Context, addr string) error {
- prURL, err := url.Parse(addr)
- if err != nil {
- return errors.Wrapf(err, "parse addr=%v", addr)
- }
- if prURL.Scheme != "tcp" && prURL.Scheme != "tcp4" {
- return errors.Errorf("invalid scheme=%v of addr=%v", prURL.Scheme, addr)
- }
- target, err := transport.NewTargetFromAddr(prURL.Host)
- if err != nil {
- return errors.Wrapf(err, "create target to %v", prURL.Host)
- }
- v.target = target
- incoming := make(chan sip.Message, 1024)
- errs := make(chan error, 1)
- cancels := make(chan struct{}, 1)
- protocol := transport.NewTcpProtocol(incoming, errs, cancels, nil, log.NewDefaultLogrusLogger())
- v.protocol = protocol
- v.incoming = incoming
- // Convert protocol stack errs to context signal.
- ctx, cancel := context.WithCancel(ctx)
- v.cancel = cancel
- v.ctx = ctx
- go func() {
- select {
- case <-ctx.Done():
- return
- case r0 := <-errs:
- logger.Ef(ctx, "SIP stack err %+v", r0)
- cancel()
- }
- }()
- // Covert context signal to cancels for protocol stack.
- go func() {
- <-ctx.Done()
- close(cancels)
- logger.Tf(ctx, "Notify SIP stack to cancel")
- }()
- return nil
- }
- func (v *SIPClient) Send(msg sip.Message) error {
- logger.Tf(v.ctx, "Send msg %v", msg.String())
- return v.protocol.Send(v.target, msg)
- }
|