From afe338f3d85e23e88276068b16e8a77d2d2787ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Thu, 7 Nov 2024 23:26:17 +0800 Subject: [PATCH] Replace xml lib --- ge_test.go | 28 ++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 4 ++++ onvif/Device.go | 3 ++- onvif/gosoap/soap-builder.go | 31 ++++++++++++++++--------------- onvif/gosoap/ws-action.go | 10 +++++----- onvif/gosoap/ws-security.go | 17 +++++++++++------ onvif/sdk/doc.go | 3 ++- onvif/xsd/onvif/onvif.go | 2 +- search/search_test.go | 3 ++- 10 files changed, 72 insertions(+), 30 deletions(-) create mode 100644 ge_test.go diff --git a/ge_test.go b/ge_test.go new file mode 100644 index 0000000..9f6a310 --- /dev/null +++ b/ge_test.go @@ -0,0 +1,28 @@ +package goonvif + +import ( + "os" + "testing" + + "git.pyer.club/kingecg/goonvif/onvif/media" + "git.pyer.club/kingecg/goxml" +) + +func TestDecodeGetProfile(t *testing.T) { + type Envelope struct { + Header struct{} + Body struct { + GetProfilesResponse media.GetProfilesResponse + } + } + data, err := os.ReadFile("result.xml") + if err != nil { + t.Fatal(err) + } + e := Envelope{} + de := goxml.Unmarshal(data, &e) + if de != nil { + t.Fatal(de) + } + t.Log(e.Body.GetProfilesResponse) +} diff --git a/go.mod b/go.mod index 4a30330..0c19b68 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( ) require ( + git.pyer.club/kingecg/goxml v1.0.1 // indirect github.com/bytedance/sonic v1.11.6 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect diff --git a/go.sum b/go.sum index 6d49a0c..9a659ba 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +git.pyer.club/kingecg/goxml v1.0.0 h1:csJ+gcWjTvMTy2pR4XpYLoevTro8hT7l1kpoUt4Xmpw= +git.pyer.club/kingecg/goxml v1.0.0/go.mod h1:w/5zeL8crK/Be3k+RxEn4NNzQ/ybqZuU3uplw86mTt4= +git.pyer.club/kingecg/goxml v1.0.1 h1:V2PbUSDkoq4gQUqkFO4zud+41GtSKq28A24lDeLkXaM= +git.pyer.club/kingecg/goxml v1.0.1/go.mod h1:w/5zeL8crK/Be3k+RxEn4NNzQ/ybqZuU3uplw86mTt4= github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs= github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= diff --git a/onvif/Device.go b/onvif/Device.go index 6838fa5..e1b7ec8 100644 --- a/onvif/Device.go +++ b/onvif/Device.go @@ -1,7 +1,6 @@ package onvif import ( - "encoding/xml" "errors" "io/ioutil" "net/http" @@ -10,6 +9,8 @@ import ( "strconv" "strings" + xml "git.pyer.club/kingecg/goxml" + "git.pyer.club/kingecg/goonvif/onvif/device" "git.pyer.club/kingecg/goonvif/onvif/gosoap" "git.pyer.club/kingecg/goonvif/onvif/networking" diff --git a/onvif/gosoap/soap-builder.go b/onvif/gosoap/soap-builder.go index d3ac554..f179849 100644 --- a/onvif/gosoap/soap-builder.go +++ b/onvif/gosoap/soap-builder.go @@ -1,13 +1,14 @@ package gosoap import ( - "encoding/xml" "log" + xml "git.pyer.club/kingecg/goxml" + "github.com/beevik/etree" ) -//SoapMessage type from string +// SoapMessage type from string type SoapMessage string // NewEmptySOAP return new SoapMessage @@ -20,7 +21,7 @@ func NewEmptySOAP() SoapMessage { return SoapMessage(res) } -//NewSOAP Get a new soap message +// NewSOAP Get a new soap message func NewSOAP(headContent []*etree.Element, bodyContent []*etree.Element, namespaces map[string]string) SoapMessage { doc := buildSoapRoot() //doc.IndentTabs() @@ -34,7 +35,7 @@ func (msg SoapMessage) String() string { return string(msg) } -//StringIndent handle indent +// StringIndent handle indent func (msg SoapMessage) StringIndent() string { doc := etree.NewDocument() @@ -48,7 +49,7 @@ func (msg SoapMessage) StringIndent() string { return res } -//Body return body from Envelope +// Body return body from Envelope func (msg SoapMessage) Body() string { doc := etree.NewDocument() @@ -65,7 +66,7 @@ func (msg SoapMessage) Body() string { return res } -//AddStringBodyContent for Envelope +// AddStringBodyContent for Envelope func (msg *SoapMessage) AddStringBodyContent(data string) { doc := etree.NewDocument() @@ -89,7 +90,7 @@ func (msg *SoapMessage) AddStringBodyContent(data string) { *msg = SoapMessage(res) } -//AddBodyContent for Envelope +// AddBodyContent for Envelope func (msg *SoapMessage) AddBodyContent(element *etree.Element) { doc := etree.NewDocument() if err := doc.ReadFromString(msg.String()); err != nil { @@ -105,7 +106,7 @@ func (msg *SoapMessage) AddBodyContent(element *etree.Element) { *msg = SoapMessage(res) } -//AddBodyContents for Envelope body +// AddBodyContents for Envelope body func (msg *SoapMessage) AddBodyContents(elements []*etree.Element) { doc := etree.NewDocument() if err := doc.ReadFromString(msg.String()); err != nil { @@ -126,7 +127,7 @@ func (msg *SoapMessage) AddBodyContents(elements []*etree.Element) { *msg = SoapMessage(res) } -//AddStringHeaderContent for Envelope body +// AddStringHeaderContent for Envelope body func (msg *SoapMessage) AddStringHeaderContent(data string) error { doc := etree.NewDocument() @@ -154,7 +155,7 @@ func (msg *SoapMessage) AddStringHeaderContent(data string) error { return nil } -//AddHeaderContent for Envelope body +// AddHeaderContent for Envelope body func (msg *SoapMessage) AddHeaderContent(element *etree.Element) { doc := etree.NewDocument() if err := doc.ReadFromString(msg.String()); err != nil { @@ -170,7 +171,7 @@ func (msg *SoapMessage) AddHeaderContent(element *etree.Element) { *msg = SoapMessage(res) } -//AddHeaderContents for Envelope body +// AddHeaderContents for Envelope body func (msg *SoapMessage) AddHeaderContents(elements []*etree.Element) { doc := etree.NewDocument() if err := doc.ReadFromString(msg.String()); err != nil { @@ -191,7 +192,7 @@ func (msg *SoapMessage) AddHeaderContents(elements []*etree.Element) { *msg = SoapMessage(res) } -//AddRootNamespace for Envelope body +// AddRootNamespace for Envelope body func (msg *SoapMessage) AddRootNamespace(key, value string) { doc := etree.NewDocument() if err := doc.ReadFromString(msg.String()); err != nil { @@ -204,7 +205,7 @@ func (msg *SoapMessage) AddRootNamespace(key, value string) { *msg = SoapMessage(res) } -//AddRootNamespaces for Envelope body +// AddRootNamespaces for Envelope body func (msg *SoapMessage) AddRootNamespaces(namespaces map[string]string) { for key, value := range namespaces { msg.AddRootNamespace(key, value) @@ -242,7 +243,7 @@ func buildSoapRoot() *etree.Document { return doc } -//AddWSSecurity Header for soapMessage +// AddWSSecurity Header for soapMessage func (msg *SoapMessage) AddWSSecurity(username, password string) { //doc := etree.NewDocument() //if err := doc.ReadFromString(msg.String()); err != nil { @@ -276,7 +277,7 @@ func (msg *SoapMessage) AddWSSecurity(username, password string) { //*msg = SoapMessage(res) } -//AddAction Header handling for soapMessage +// AddAction Header handling for soapMessage func (msg *SoapMessage) AddAction() { doc := etree.NewDocument() diff --git a/onvif/gosoap/ws-action.go b/onvif/gosoap/ws-action.go index f5a9e3d..9c3b534 100644 --- a/onvif/gosoap/ws-action.go +++ b/onvif/gosoap/ws-action.go @@ -1,10 +1,10 @@ package gosoap import ( - "encoding/xml" + xml "git.pyer.club/kingecg/goxml" ) -//Xlmns XML Scheam +// Xlmns XML Scheam var actionHeaders = map[string]string{ "wsnt:Subscribe": "http://docs.oasis-open.org/wsn/bw-2/NotificationProducer/SubscribeRequest", "ResumeSubscription": "http://docs.oasis-open.org/wsn/bw-2/PausableSubscriptionManager/ResumeSubscriptionRequest", @@ -17,7 +17,7 @@ var actionHeaders = map[string]string{ Action Type in Header *************************/ -//Action type +// Action type type Action struct { //XMLName xml.Name `xml:"wsse:Security"` XMLName xml.Name `xml:"wsa:Action"` @@ -30,13 +30,13 @@ type Action struct { */ -//NewAction get a new Action Section +// NewAction get a new Action Section func NewAction(key, value string) Action { /** Generating Nonce sequence **/ auth := Action{ - // Created: time.Now().UTC().Format(time.RFC3339Nano), + // Created: time.Now().UTC().Format(time.RFC3339Nano), } return auth diff --git a/onvif/gosoap/ws-security.go b/onvif/gosoap/ws-security.go index 3821402..547b05d 100644 --- a/onvif/gosoap/ws-security.go +++ b/onvif/gosoap/ws-security.go @@ -3,21 +3,26 @@ package gosoap import ( "crypto/sha1" "encoding/base64" - "encoding/xml" "time" + xml "git.pyer.club/kingecg/goxml" + "github.com/elgs/gostrgen" ) -/************************* +/* +************************ + WS-Security types -*************************/ + +************************ +*/ const ( passwordType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest" encodingType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ) -//Security type :XMLName xml.Name `xml:"http://purl.org/rss/1.0/modules/content/ encoded"` +// Security type :XMLName xml.Name `xml:"http://purl.org/rss/1.0/modules/content/ encoded"` type Security struct { //XMLName xml.Name `xml:"wsse:Security"` XMLName xml.Name `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd Security"` @@ -55,7 +60,7 @@ type wsAuth struct { */ -//NewSecurity get a new security +// NewSecurity get a new security func NewSecurity(username, passwd string) Security { /** Generating Nonce sequence **/ charsToGenerate := 32 @@ -81,7 +86,7 @@ func NewSecurity(username, passwd string) Security { return auth } -//Digest = B64ENCODE( SHA1( B64DECODE( Nonce ) + Date + Password ) ) +// Digest = B64ENCODE( SHA1( B64DECODE( Nonce ) + Date + Password ) ) func generateToken(Username string, Nonce string, Created string, Password string) string { sDec, _ := base64.StdEncoding.DecodeString(Nonce) diff --git a/onvif/sdk/doc.go b/onvif/sdk/doc.go index fe59b54..d98e0f7 100644 --- a/onvif/sdk/doc.go +++ b/onvif/sdk/doc.go @@ -2,12 +2,13 @@ package sdk import ( "context" - "encoding/xml" "io/ioutil" "net/http" "os" "time" + xml "git.pyer.club/kingecg/goxml" + "github.com/beevik/etree" "github.com/juju/errors" "github.com/rs/zerolog" diff --git a/onvif/xsd/onvif/onvif.go b/onvif/xsd/onvif/onvif.go index 5245387..1f62599 100644 --- a/onvif/xsd/onvif/onvif.go +++ b/onvif/xsd/onvif/onvif.go @@ -358,7 +358,7 @@ type Profile struct { type VideoSourceConfiguration struct { ConfigurationEntity ViewMode string `xml:"ViewMode,attr"` - SourceToken ReferenceToken `xml:"onvif:SourceToken"` + SourceToken ReferenceToken `xml:"tt:SourceToken"` Bounds IntRectangle `xml:"onvif:Bounds"` Extension *VideoSourceConfigurationExtension `xml:"onvif:Extension"` } diff --git a/search/search_test.go b/search/search_test.go index e52bbbd..75f46bf 100644 --- a/search/search_test.go +++ b/search/search_test.go @@ -1,9 +1,10 @@ package search import ( - "encoding/xml" "testing" + xml "git.pyer.club/kingecg/goxml" + xsd "git.pyer.club/kingecg/goonvif/onvif/xsd" )