dev: encode sip msg gb2312

This commit is contained in:
langhuihui
2024-07-31 14:21:29 +08:00
parent 17241550bb
commit b8c1fa5ac6
7 changed files with 169 additions and 76 deletions

12
go.mod
View File

@@ -8,9 +8,11 @@ require (
github.com/Eyevinn/mp4ff v0.45.1
github.com/asavie/xdp v0.3.3
github.com/cilium/ebpf v0.15.0
github.com/cloudwego/goref v0.0.0-20240724113447-685d2a9523c8
github.com/deepch/vdk v0.0.27
github.com/emiago/sipgo v0.22.0
github.com/glebarez/sqlite v1.11.0
github.com/go-delve/delve v1.23.0
github.com/google/gopacket v1.1.19
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
github.com/husanpao/ip v0.0.0-20220711082147-73160bb611a8
@@ -23,8 +25,10 @@ require (
github.com/polarsignals/frostdb v0.0.0-20240613134636-1d823f7d7299
github.com/quic-go/quic-go v0.43.1
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.0
github.com/vishvananda/netlink v1.1.0
github.com/yapingcat/gomedia v0.0.0-20240601043430-920523f8e5c7
golang.org/x/text v0.16.0
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.33.0
@@ -62,6 +66,8 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hamba/avro/v2 v2.20.1 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
@@ -110,6 +116,8 @@ require (
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b // indirect
github.com/segmentio/encoding v0.3.6 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/thanos-io/objstore v0.0.0-20240512204237-71ef2d0cf7c4 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
@@ -120,8 +128,8 @@ require (
go.etcd.io/bbolt v1.3.6 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
golang.org/x/arch v0.6.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
modernc.org/libc v1.41.0 // indirect
@@ -144,7 +152,7 @@ require (
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/net v0.26.0
golang.org/x/sys v0.21.0
golang.org/x/tools v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1

21
go.sum
View File

@@ -31,6 +31,8 @@ github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moA
github.com/cilium/ebpf v0.4.0/go.mod h1:4tRaxcgiL706VnOzHOdBlY8IEAIdxINsQBcU4xJJXRs=
github.com/cilium/ebpf v0.15.0 h1:7NxJhNiBT3NG8pZJ3c+yfrVdHY8ScgKD27sScgjLMMk=
github.com/cilium/ebpf v0.15.0/go.mod h1:DHp1WyrLeiBh19Cf/tfiSMhqheEiK8fXFZ4No0P1Hso=
github.com/cloudwego/goref v0.0.0-20240724113447-685d2a9523c8 h1:K7L7KFg5siEysLit42Bf7n4qNRkGxniPeBtmpsxsfdQ=
github.com/cloudwego/goref v0.0.0-20240724113447-685d2a9523c8/go.mod h1:IMGV1p8Mw3uyZYClI5bA8uqk8LGr/MYFv92V0m88XUk=
github.com/coreos/etcd v3.3.27+incompatible h1:QIudLb9KeBsE5zyYxd1mjzRSkzLg9Wf9QlRwFgd6oTA=
github.com/coreos/etcd v3.3.27+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU=
@@ -38,6 +40,9 @@ github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/pkg v0.0.0-20220810130054-c7d1c02cb6cf h1:GOPo6vn/vTN+3IwZBvXX0y5doJfSC7My0cdzelyOCsQ=
github.com/coreos/pkg v0.0.0-20220810130054-c7d1c02cb6cf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.20 h1:VIPb/a2s17qNeQgDnkfZC35RScx+blkKF8GV68n80J4=
github.com/creack/pty v1.1.20/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -58,6 +63,8 @@ github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9g
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
github.com/go-delve/delve v1.23.0 h1:jYgZISZ14KAO3ys8kD07kjrowrygE9F9SIwnpz9xXys=
github.com/go-delve/delve v1.23.0/go.mod h1:S3SLuEE2mn7wipKilTvk1p9HdTMnXXElcEpiZ+VcuqU=
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
@@ -122,6 +129,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0Q
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM=
github.com/hamba/avro/v2 v2.20.1 h1:3WByQiVn7wT7d27WQq6pvBRC00FVOrniP6u67FLA/2E=
github.com/hamba/avro/v2 v2.20.1/go.mod h1:xHiKXbISpb3Ovc809XdzWow+XGTn+Oyf/F9aZbTLAig=
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
@@ -129,6 +138,8 @@ github.com/husanpao/ip v0.0.0-20220711082147-73160bb611a8 h1:4Jk58quTZmzJcTrLlbB
github.com/husanpao/ip v0.0.0-20220711082147-73160bb611a8/go.mod h1:medl9/CfYoQlqAXtAARmMW5dAX2UOdwwkhaszYPk0AM=
github.com/icholy/digest v0.1.22 h1:dRIwCjtAcXch57ei+F0HSb5hmprL873+q7PoVojdMzM=
github.com/icholy/digest v0.1.22/go.mod h1:uLAeDdWKIWNFMH0wqbwchbTQOmJWhzSnL7zmqSPqEEc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
@@ -291,6 +302,7 @@ github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/samber/slog-formatter v1.0.0 h1:ULxHV+jNqi6aFP8xtzGHl2ejFRMl2+jI2UhCpgoXTDA=
@@ -309,7 +321,13 @@ github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFt
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -358,6 +376,8 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc=
golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
@@ -430,6 +450,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@@ -14,6 +14,21 @@ var LocalCert []byte
//go:embed local.monibuca.com.key
var LocalKey []byte
func GetTLSConfig(certFile, keyFile string) (tslConfig *tls.Config, err error) {
var keyPair tls.Certificate
if certFile != "" || keyFile != "" {
keyPair, err = tls.LoadX509KeyPair(certFile, keyFile)
} else {
keyPair, err = tls.X509KeyPair(LocalCert, LocalKey)
}
if err == nil {
tslConfig = &tls.Config{
Certificates: []tls.Certificate{keyPair},
}
}
return
}
type TCP struct {
ListenAddr string `desc:"监听地址格式为ip:portip 可省略默认监听所有网卡"`
ListenAddrTLS string `desc:"监听地址格式为ip:portip 可省略默认监听所有网卡"`
@@ -51,14 +66,8 @@ func (tcp *TCP) Listen(handler func(*net.TCPConn)) (err error) {
}
func (tcp *TCP) ListenTLS(handler func(*net.TCPConn)) (err error) {
keyPair, _ := tls.X509KeyPair(LocalCert, LocalKey)
if tcp.CertFile != "" || tcp.KeyFile != "" {
keyPair, err = tls.LoadX509KeyPair(tcp.CertFile, tcp.KeyFile)
}
if err == nil {
tcp.listenerTls, err = tls.Listen("tcp", tcp.ListenAddrTLS, &tls.Config{
Certificates: []tls.Certificate{keyPair},
})
if tlsConfig, err := GetTLSConfig(tcp.CertFile, tcp.KeyFile); err == nil {
tcp.listenerTls, err = tls.Listen("tcp", tcp.ListenAddrTLS, tlsConfig)
if err == nil {
count := tcp.ListenNum
if count == 0 {
@@ -68,6 +77,8 @@ func (tcp *TCP) ListenTLS(handler func(*net.TCPConn)) (err error) {
go tcp.listen(tcp.listenerTls, handler)
}
}
} else {
return err
}
return
}

View File

@@ -1,20 +1,26 @@
package plugin_debug
import (
"github.com/go-delve/delve/pkg/config"
"github.com/go-delve/delve/service/debugger"
"io"
"net/http"
"net/http/pprof"
"os"
"strings"
"time"
myproc "github.com/cloudwego/goref/pkg/proc"
"m7s.live/m7s/v5"
)
var _ = m7s.InstallPlugin[DebugPlugin]()
var conf, _ = config.LoadConfig()
type DebugPlugin struct {
m7s.Plugin
ChartPeriod time.Duration `default:"1s" desc:"图表更新周期"`
Grfout string `default:"grf.out" desc:"grf输出文件"`
}
type WriteToFile struct {
@@ -66,3 +72,25 @@ func (p *DebugPlugin) Charts_data(w http.ResponseWriter, r *http.Request) {
func (p *DebugPlugin) Charts_datafeed(w http.ResponseWriter, r *http.Request) {
s.dataFeedHandler(w, r)
}
func (p *DebugPlugin) Grf(w http.ResponseWriter, r *http.Request) {
dConf := debugger.Config{
AttachPid: os.Getpid(),
Backend: "default",
CoreFile: "",
DebugInfoDirectories: conf.DebugInfoDirectories,
AttachWaitFor: "",
AttachWaitForInterval: 1,
AttachWaitForDuration: 0,
}
dbg, err := debugger.New(&dConf, nil)
defer dbg.Detach(false)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = myproc.ObjectReference(dbg.Target(), p.Grfout); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Write([]byte("ok"))
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
"log/slog"
"m7s.live/m7s/v5"
"m7s.live/m7s/v5/pkg/util"
gb28181 "m7s.live/m7s/v5/plugin/gb28181/pkg"
"net/http"
@@ -40,6 +41,8 @@ type Device struct {
*slog.Logger
eventChan chan any
dialogClient *sipgo.DialogClient
contactHDR sip.ContactHeader
fromHDR sip.FromHeader
}
func (d *Device) GetKey() string {
@@ -84,6 +87,7 @@ func (d *Device) onMessage(req *sip.Request, tx sip.ServerTransaction, msg *gb28
func (d *Device) eventLoop(gb *GB28181Plugin) {
send := func(req *sip.Request) (*sip.Response, error) {
d.Debug("send", "req", req.String())
return gb.client.Do(gb, req)
}
defer func() {
@@ -96,13 +100,13 @@ func (d *Device) eventLoop(gb *GB28181Plugin) {
if err != nil {
d.Error("catalog", "err", err)
} else {
d.Debug("catalog", "response", response.Short())
d.Debug("catalog", "response", response.String())
}
response, err = d.queryDeviceInfo(send)
if err != nil {
d.Error("deviceInfo", "err", err)
} else {
d.Debug("deviceInfo", "response", response.Short())
d.Debug("deviceInfo", "response", response.String())
}
subTick := time.NewTicker(time.Second * 3600)
defer subTick.Stop()
@@ -115,13 +119,13 @@ func (d *Device) eventLoop(gb *GB28181Plugin) {
if err != nil {
d.Error("subCatalog", "err", err)
} else {
d.Debug("subCatalog", "response", response.Short())
d.Debug("subCatalog", "response", response.String())
}
response, err = d.subscribePosition(int(gb.Position.Interval/time.Second), send)
if err != nil {
d.Error("subPosition", "err", err)
} else {
d.Debug("subPosition", "response", response.Short())
d.Debug("subPosition", "response", response.String())
}
case <-catalogTick.C:
if time.Since(d.LastKeepaliveAt) > time.Second*3600 {
@@ -132,7 +136,7 @@ func (d *Device) eventLoop(gb *GB28181Plugin) {
if err != nil {
d.Error("catalog", "err", err)
} else {
d.Debug("catalog", "response", response.Short())
d.Debug("catalog", "response", response.String())
}
case event, ok := <-d.eventChan:
if !ok {
@@ -166,36 +170,43 @@ func (d *Device) eventLoop(gb *GB28181Plugin) {
func (d *Device) createRequest(Method sip.RequestMethod) (req *sip.Request) {
req = sip.NewRequest(Method, d.Recipient)
req.AppendHeader(&d.fromHDR)
contentType := sip.ContentTypeHeader("Application/MANSCDP+xml")
req.AppendHeader(sip.NewHeader("User-Agent", "M7S/"+m7s.Version))
req.AppendHeader(&contentType)
req.AppendHeader(&d.contactHDR)
return
}
func (d *Device) catalog(send func(*sip.Request) (*sip.Response, error)) (*sip.Response, error) {
d.SN++
request := d.createRequest(sip.MESSAGE)
//d.subscriber.Timeout = time.Now().Add(time.Second * time.Duration(expires))
request.AppendHeader(sip.NewHeader("Expires", "3600"))
request.SetBody([]byte(gb28181.BuildCatalogXML(d.SN, d.ID)))
request.SetBody(gb28181.BuildCatalogXML(d.SN, d.ID))
return send(request)
}
func (d *Device) subscribeCatalog(send func(*sip.Request) (*sip.Response, error)) (*sip.Response, error) {
d.SN++
request := d.createRequest(sip.SUBSCRIBE)
request.AppendHeader(sip.NewHeader("Expires", "3600"))
request.SetBody([]byte(gb28181.BuildCatalogXML(d.SN, d.ID)))
request.SetBody(gb28181.BuildCatalogXML(d.SN, d.ID))
return send(request)
}
func (d *Device) queryDeviceInfo(send func(*sip.Request) (*sip.Response, error)) (*sip.Response, error) {
d.SN++
request := d.createRequest(sip.MESSAGE)
request.SetBody([]byte(gb28181.BuildDeviceInfoXML(d.SN, d.ID)))
request.SetBody(gb28181.BuildDeviceInfoXML(d.SN, d.ID))
return send(request)
}
func (d *Device) subscribePosition(interval int, send func(*sip.Request) (*sip.Response, error)) (*sip.Response, error) {
d.SN++
request := d.createRequest(sip.SUBSCRIBE)
request.AppendHeader(sip.NewHeader("Expires", "3600"))
request.SetBody([]byte(gb28181.BuildDevicePositionXML(d.SN, d.ID, interval)))
request.SetBody(gb28181.BuildDevicePositionXML(d.SN, d.ID, interval))
return send(request)
}

View File

@@ -1,7 +1,6 @@
package plugin_gb28181
import (
"crypto/tls"
"fmt"
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
@@ -61,9 +60,9 @@ func init() {
}
func (gb *GB28181Plugin) OnInit() (err error) {
logger := zerolog.New(os.Stdout)
gb.ua, err = sipgo.NewUA(sipgo.WithUserAgent("monibuca" + m7s.Version)) // Build user agent
gb.client, _ = sipgo.NewClient(gb.ua, sipgo.WithClientLogger(logger)) // Creating client handle for ua
gb.server, _ = sipgo.NewServer(gb.ua, sipgo.WithServerLogger(logger)) // Creating server handle for ua
gb.ua, err = sipgo.NewUA(sipgo.WithUserAgent("M7S/" + m7s.Version)) // Build user agent
gb.client, _ = sipgo.NewClient(gb.ua, sipgo.WithClientLogger(logger)) // Creating client handle for ua
gb.server, _ = sipgo.NewServer(gb.ua, sipgo.WithServerLogger(logger)) // Creating server handle for ua
gb.server.OnRegister(gb.OnRegister)
gb.server.OnMessage(gb.OnMessage)
gb.server.OnBye(gb.OnBye)
@@ -82,17 +81,14 @@ func (gb *GB28181Plugin) OnInit() (err error) {
netWork, addr, _ := strings.Cut(addr, ":")
go gb.server.ListenAndServe(gb, netWork, addr)
}
keyPair, _ := tls.X509KeyPair(config.LocalCert, config.LocalKey)
if gb.Sip.CertFile != "" || gb.Sip.KeyFile != "" {
keyPair, err = tls.LoadX509KeyPair(gb.Sip.CertFile, gb.Sip.KeyFile)
}
if err == nil {
tslConfig := &tls.Config{
Certificates: []tls.Certificate{keyPair},
}
for _, addr := range gb.Sip.ListenTLSAddr {
netWork, addr, _ := strings.Cut(addr, ":")
go gb.server.ListenAndServeTLS(gb, netWork, addr, tslConfig)
if len(gb.Sip.ListenTLSAddr) > 0 {
if tslConfig, err := config.GetTLSConfig(gb.Sip.CertFile, gb.Sip.KeyFile); err == nil {
for _, addr := range gb.Sip.ListenTLSAddr {
netWork, addr, _ := strings.Cut(addr, ":")
go gb.server.ListenAndServeTLS(gb, netWork, addr, tslConfig)
}
} else {
return err
}
}
return
@@ -202,13 +198,10 @@ func (gb *GB28181Plugin) OnMessage(req *sip.Request, tx sip.ServerTransaction) {
if d, ok := gb.devices.Get(id); ok {
d.UpdateTime = time.Now()
temp := &gb28181.Message{}
err := gb28181.DecodeGB2312(temp, req.Body())
err := gb28181.DecodeXML(temp, req.Body())
if err != nil {
err = gb28181.DecodeGbk(temp, req.Body())
if err != nil {
gb.Error("OnMessage", "error", err.Error())
return
}
gb.Error("OnMessage", "error", err.Error())
return
}
err = d.onMessage(req, tx, temp)
}
@@ -232,8 +225,10 @@ func (gb *GB28181Plugin) RecoverDevice(d *Device, req *sip.Request) {
func (gb *GB28181Plugin) StoreDevice(id string, req *sip.Request) (d *Device) {
from := req.From()
source := req.Source()
servIp := req.Recipient.Host
mediaIP := gb.GetPublicIP(servIp)
desc := req.Destination()
serverHostName := strings.Split(desc, ":")
servIp := serverHostName[0]
publicIP := gb.GetPublicIP(servIp)
//如果相等,则服务器是内网通道.海康摄像头不支持...自动获取
//if strings.LastIndex(deviceIp, ".") != -1 && strings.LastIndex(servIp, ".") != -1 {
// if servIp[0:strings.LastIndex(servIp, ".")] == deviceIp[0:strings.LastIndex(deviceIp, ".")] || mediaIP == "" {
@@ -242,6 +237,7 @@ func (gb *GB28181Plugin) StoreDevice(id string, req *sip.Request) (d *Device) {
//}
hostname := strings.Split(source, ":")
port, _ := strconv.Atoi(hostname[1])
serverPort, _ := strconv.Atoi(serverHostName[1])
d = &Device{
ID: id,
RegisterTime: time.Now(),
@@ -254,15 +250,27 @@ func (gb *GB28181Plugin) StoreDevice(id string, req *sip.Request) (d *Device) {
},
Transport: req.Transport(),
Logger: gb.Logger.With("id", id),
mediaIp: mediaIP,
mediaIp: publicIP,
eventChan: make(chan any, 10),
contactHDR: sip.ContactHeader{
Address: sip.Uri{
User: gb.Serial,
Host: publicIP,
Port: serverPort,
},
},
fromHDR: sip.FromHeader{
Address: sip.Uri{
User: gb.Serial,
Host: gb.Realm,
},
Params: sip.NewParams(),
},
}
contactHDR := sip.ContactHeader{
Address: req.Recipient,
}
d.dialogClient = sipgo.NewDialogClient(gb.client, contactHDR)
d.fromHDR.Params.Add("tag", sip.GenerateTagN(16))
d.dialogClient = sipgo.NewDialogClient(gb.client, d.contactHDR)
d.channels.L = new(sync.RWMutex)
d.Info("StoreDevice", "source", source, "servIp", servIp, "mediaIP", mediaIP, "recipient", req.Recipient)
d.Info("StoreDevice", "source", source, "desc", desc, "servIp", servIp, "publicIP", publicIP, "recipient", req.Recipient)
gb.devices.Add(d)
if gb.DB != nil {
//TODO

View File

@@ -7,7 +7,9 @@ import (
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"strconv"
"strings"
"time"
)
@@ -20,8 +22,7 @@ const (
</Query>
`
// RecordInfoXML 获取录像文件列表xml样式
RecordInfoXML = `<?xml version="1.0"?>
<Query>
RecordInfoXML = `<?xml version="1.0"?><Query>
<CmdType>RecordInfo</CmdType>
<SN>%d</SN>
<DeviceID>%s</DeviceID>
@@ -32,27 +33,26 @@ const (
</Query>
`
// DeviceInfoXML 查询设备详情xml样式
DeviceInfoXML = `<?xml version="1.0"?>
<Query>
DeviceInfoXML = `<?xml version="1.0"?><Query>
<CmdType>DeviceInfo</CmdType>
<SN>%d</SN>
<DeviceID>%s</DeviceID>
</Query>
`
// DevicePositionXML 订阅设备位置
DevicePositionXML = `<?xml version="1.0"?>
<Query>
DevicePositionXML = `<?xml version="1.0"?><Query>
<CmdType>MobilePosition</CmdType>
<SN>%d</SN>
<DeviceID>%s</DeviceID>
<Interval>%d</Interval>
</Query>`
AlarmResponseXML = `<?xml version="1.0"?>
<Response>
</Query>
`
AlarmResponseXML = `<?xml version="1.0"?><Response>
<CmdType>Alarm</CmdType>
<SN>17430</SN>
<DeviceID>%s</DeviceID>
</Response>`
</Response>
`
ChannelOnStatus ChannelStatus = "ON"
ChannelOffStatus ChannelStatus = "OFF"
)
@@ -68,28 +68,34 @@ func intTotime(t int64) time.Time {
return time.Now()
}
func toGB2312(s string) []byte {
reader := transform.NewReader(strings.NewReader(s), simplifiedchinese.GBK.NewEncoder())
d, _ := io.ReadAll(reader)
return d
}
// BuildDeviceInfoXML 获取设备详情指令
func BuildDeviceInfoXML(sn int, id string) string {
return fmt.Sprintf(DeviceInfoXML, sn, id)
func BuildDeviceInfoXML(sn int, id string) []byte {
return toGB2312(fmt.Sprintf(DeviceInfoXML, sn, id))
}
// BuildCatalogXML 获取NVR下设备列表指令
func BuildCatalogXML(sn int, id string) string {
return fmt.Sprintf(CatalogXML, sn, id)
func BuildCatalogXML(sn int, id string) []byte {
return toGB2312(fmt.Sprintf(CatalogXML, sn, id))
}
// BuildRecordInfoXML 获取录像文件列表指令
func BuildRecordInfoXML(sn int, id string, start, end int64) string {
return fmt.Sprintf(RecordInfoXML, sn, id, intTotime(start).Format("2006-01-02T15:04:05"), intTotime(end).Format("2006-01-02T15:04:05"))
func BuildRecordInfoXML(sn int, id string, start, end int64) []byte {
return toGB2312(fmt.Sprintf(RecordInfoXML, sn, id, intTotime(start).Format("2006-01-02T15:04:05"), intTotime(end).Format("2006-01-02T15:04:05")))
}
// BuildDevicePositionXML 订阅设备位置
func BuildDevicePositionXML(sn int, id string, interval int) string {
return fmt.Sprintf(DevicePositionXML, sn, id, interval)
func BuildDevicePositionXML(sn int, id string, interval int) []byte {
return toGB2312(fmt.Sprintf(DevicePositionXML, sn, id, interval))
}
func BuildAlarmResponseXML(id string) string {
return fmt.Sprintf(AlarmResponseXML, id)
func BuildAlarmResponseXML(id string) []byte {
return toGB2312(fmt.Sprintf(AlarmResponseXML, id))
}
type (
@@ -135,14 +141,14 @@ type (
}
)
func DecodeGB2312(v any, body []byte) error {
func DecodeXML(v any, body []byte) error {
decoder := xml.NewDecoder(bytes.NewReader(body))
decoder.CharsetReader = charset.NewReaderLabel
return decoder.Decode(v)
}
func DecodeGbk(v any, body []byte) error {
decoder := xml.NewDecoder(transform.NewReader(bytes.NewReader(body), simplifiedchinese.GBK.NewDecoder()))
decoder.CharsetReader = charset.NewReaderLabel
return decoder.Decode(v)
err := decoder.Decode(v)
if err != nil {
decoder = xml.NewDecoder(transform.NewReader(bytes.NewReader(body), simplifiedchinese.GBK.NewDecoder()))
decoder.CharsetReader = charset.NewReaderLabel
return decoder.Decode(v)
}
return nil
}