mirror of
https://github.com/kerberos-io/onvif.git
synced 2026-08-25 15:28:33 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
48daf5acb5 | ||
|
|
d85a0742b9 | ||
|
|
24a85afc84 | ||
|
|
c8ac58ebd7 | ||
|
|
4ec21b20e0 | ||
|
|
4e696ec65a | ||
|
|
e1345d5e6b | ||
|
|
918ce541d2 | ||
|
|
15e268c1b7 | ||
|
|
dce0d0faad | ||
|
|
be84cab1fe | ||
|
|
c10b8ca105 | ||
|
|
e6593ad04f | ||
|
|
702fded39b | ||
|
|
298c2dabb9 | ||
|
|
bedd8886e4 | ||
|
|
ddb7588318 |
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Yakovlev Dmitry, Zhorzh Palanjyan,Crazybber
|
||||
|
||||
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.
|
||||
@@ -1,4 +1,4 @@
|
||||
# onvif[golang]
|
||||
# onvif protocol
|
||||
|
||||
Simple management of onvif IP-devices cameras. onvif is an implementation of ONVIF protocol for managing onvif IP devices. The purpose of this library is convenient and easy management of IP cameras and other devices that support ONVIF standard.
|
||||
|
||||
@@ -13,13 +13,16 @@ go get github.com/use-go/onvif
|
||||
|
||||
## Supported services
|
||||
|
||||
The following services are fully implemented:
|
||||
The following services are implemented:
|
||||
|
||||
- Device
|
||||
- Media
|
||||
- PTZ
|
||||
- Imaging
|
||||
- Event
|
||||
- Discovery
|
||||
- Auth(More Options)
|
||||
- Soap
|
||||
|
||||
## Using
|
||||
|
||||
@@ -98,4 +101,4 @@ resp, err := dev.CallMethod(createUsers)
|
||||
|
||||
## Great Thanks
|
||||
|
||||
Modified from: [goonvif](https://github.com/yakovlevdmv/goonvif)
|
||||
Enhanced and Improved from: [goonvif](https://github.com/yakovlevdmv/goonvif)
|
||||
|
||||
84
examples/discovery_test.go
Normal file
84
examples/discovery_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/beevik/etree"
|
||||
"github.com/use-go/onvif"
|
||||
"github.com/use-go/onvif/device"
|
||||
discover "github.com/use-go/onvif/ws-discovery"
|
||||
)
|
||||
|
||||
func TestGetAvailableDevicesAtSpecificEthernetInterface(t *testing.T) {
|
||||
|
||||
// client()
|
||||
// runDiscovery("en0")
|
||||
s := onvif.GetAvailableDevicesAtSpecificEthernetInterface("en0")
|
||||
|
||||
log.Printf("%s", s)
|
||||
}
|
||||
|
||||
func client() {
|
||||
dev, err := onvif.NewDevice("192.168.3.10")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
dev.Authenticate("admin", "zsyy12345")
|
||||
|
||||
log.Printf("output %+v", dev.GetServices())
|
||||
|
||||
res, err := dev.CallMethod(device.GetUsers{})
|
||||
bs, _ := ioutil.ReadAll(res.Body)
|
||||
log.Printf("output %+v %s", res.StatusCode, bs)
|
||||
}
|
||||
|
||||
// Host host
|
||||
type Host struct {
|
||||
URL string `json:"url"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func runDiscovery(interfaceName string) {
|
||||
var hosts []*Host
|
||||
devices := discover.SendProbe(interfaceName, nil, []string{"dn:NetworkVideoTransmitter"}, map[string]string{"dn": "http://www.onvif.org/ver10/network/wsdl"})
|
||||
for _, j := range devices {
|
||||
doc := etree.NewDocument()
|
||||
if err := doc.ReadFromString(j); err != nil {
|
||||
log.Printf("error %s", err)
|
||||
} else {
|
||||
|
||||
endpoints := doc.Root().FindElements("./Body/ProbeMatches/ProbeMatch/XAddrs")
|
||||
scopes := doc.Root().FindElements("./Body/ProbeMatches/ProbeMatch/Scopes")
|
||||
|
||||
flag := false
|
||||
|
||||
host := &Host{}
|
||||
|
||||
for _, xaddr := range endpoints {
|
||||
xaddr := strings.Split(strings.Split(xaddr.Text(), " ")[0], "/")[2]
|
||||
host.URL = xaddr
|
||||
}
|
||||
if flag {
|
||||
break
|
||||
}
|
||||
for _, scope := range scopes {
|
||||
re := regexp.MustCompile(`onvif:\/\/www\.onvif\.org\/name\/[A-Za-z0-9-]+`)
|
||||
match := re.FindStringSubmatch(scope.Text())
|
||||
host.Name = path.Base(match[0])
|
||||
}
|
||||
|
||||
hosts = append(hosts, host)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bys, _ := json.Marshal(hosts)
|
||||
log.Printf("done %s", bys)
|
||||
}
|
||||
3
go.mod
3
go.mod
@@ -1,12 +1,11 @@
|
||||
module github.com/use-go/onvif
|
||||
|
||||
go 1.14
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/beevik/etree v1.1.0
|
||||
github.com/elgs/gostrgen v0.0.0-20161222160715-9d61ae07eeae
|
||||
github.com/gin-gonic/gin v1.6.2
|
||||
github.com/gofrs/uuid v3.2.0+incompatible
|
||||
github.com/yakovlevdmv/Golang-iso8601-duration v0.0.0-20180403125811-e5db0413b903
|
||||
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0
|
||||
)
|
||||
|
||||
2
go.sum
2
go.sum
@@ -42,8 +42,6 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
|
||||
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
|
||||
github.com/yakovlevdmv/Golang-iso8601-duration v0.0.0-20180403125811-e5db0413b903 h1:W1Y09qPuFslSd+3famnSJpy4c3My7Gp1cgk5x4ZlbfU=
|
||||
github.com/yakovlevdmv/Golang-iso8601-duration v0.0.0-20180403125811-e5db0413b903/go.mod h1:9o96byDMk+osDZqiIS2a7E7y0cWmg4rRTjQRWVHpFWE=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
|
||||
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
|
||||
@@ -62,18 +62,19 @@ func NewSecurity(username, passwd string) Security {
|
||||
charSet := gostrgen.Lower | gostrgen.Digit
|
||||
|
||||
nonceSeq, _ := gostrgen.RandGen(charsToGenerate, charSet, "", "")
|
||||
created := time.Now().UTC().Format(time.RFC3339Nano)
|
||||
auth := Security{
|
||||
Auth: wsAuth{
|
||||
Username: username,
|
||||
Password: password{
|
||||
Type: passwordType,
|
||||
Password: generateToken(username, nonceSeq, time.Now().UTC(), passwd),
|
||||
Password: generateToken(username, nonceSeq, created, passwd),
|
||||
},
|
||||
Nonce: nonce{
|
||||
Type: encodingType,
|
||||
Nonce: nonceSeq,
|
||||
},
|
||||
Created: time.Now().UTC().Format(time.RFC3339Nano),
|
||||
Created: created,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -81,13 +82,11 @@ func NewSecurity(username, passwd string) Security {
|
||||
}
|
||||
|
||||
//Digest = B64ENCODE( SHA1( B64DECODE( Nonce ) + Date + Password ) )
|
||||
func generateToken(Username string, Nonce string, Created time.Time, Password string) string {
|
||||
|
||||
func generateToken(Username string, Nonce string, Created string, Password string) string {
|
||||
sDec, _ := base64.StdEncoding.DecodeString(Nonce)
|
||||
|
||||
hasher := sha1.New()
|
||||
//hasher.Write([]byte((base64.StdEncoding.EncodeToString([]byte(Nonce)) + Created.Format(time.RFC3339) + Password)))
|
||||
hasher.Write([]byte(string(sDec) + Created.Format(time.RFC3339Nano) + Password))
|
||||
hasher.Write([]byte(string(sDec) + Created + Password))
|
||||
|
||||
return base64.StdEncoding.EncodeToString(hasher.Sum(nil))
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package networking
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"bytes"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SendSoap send soap message
|
||||
func SendSoap(endpoint, message string) (*http.Response, error) {
|
||||
httpClient := new(http.Client)
|
||||
|
||||
@@ -13,5 +15,14 @@ func SendSoap(endpoint, message string) (*http.Response, error) {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
return resp,nil
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// SendSoapWithTimeout send soap message with timeOut
|
||||
func SendSoapWithTimeout(endpoint string, message []byte, timeout time.Duration) (*http.Response, error) {
|
||||
httpClient := &http.Client{
|
||||
Timeout: timeout,
|
||||
}
|
||||
|
||||
return httpClient.Post(endpoint, "application/soap+xml; charset=utf-8", bytes.NewReader(message))
|
||||
}
|
||||
|
||||
@@ -10,9 +10,11 @@ package wsdiscovery
|
||||
*******************************************************/
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
@@ -85,7 +87,9 @@ func sendUDPMulticast(msg string, interfaceName string) []string {
|
||||
b := make([]byte, bufSize)
|
||||
n, _, _, err := p.ReadFrom(b)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if !errors.Is(err, os.ErrDeadlineExceeded) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
result = append(result, string(b[0:n]))
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
iso8601 "github.com/yakovlevdmv/Golang-iso8601-duration"
|
||||
"github.com/use-go/onvif/xsd/iso8601"
|
||||
)
|
||||
|
||||
/*
|
||||
|
||||
104
xsd/iso8601/iso8601_duration.go
Normal file
104
xsd/iso8601/iso8601_duration.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package iso8601
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
//Duration of iso8601
|
||||
type Duration struct {
|
||||
years string //= number of years
|
||||
months string //= number of months
|
||||
days string //= number of days
|
||||
// Time section
|
||||
hours string //= the number of hours
|
||||
minutes string //= the number of minutes
|
||||
seconds string //= the number of seconds
|
||||
}
|
||||
|
||||
//NewDuration return duration
|
||||
func NewDuration(years, months, days, hours, minutes, seconds string) (*Duration, error) {
|
||||
// Pattern for Years, Months, Days, Hours and Minutes components
|
||||
pattern1 := "^$|[0-9]+"
|
||||
// Pattern for Seconds component
|
||||
pattern2 := "^$|[0-9]+(\\.[0-9]+)?"
|
||||
|
||||
matched, err := regexp.MatchString(pattern1, years)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("years value = " + years + " does not match pattern " + pattern1)
|
||||
}
|
||||
|
||||
matched, err = regexp.MatchString(pattern1, months)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("months value = " + months + " does not match pattern " + pattern1)
|
||||
}
|
||||
|
||||
matched, err = regexp.MatchString(pattern1, days)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("months value = " + days + " does not match pattern " + pattern1)
|
||||
}
|
||||
|
||||
matched, err = regexp.MatchString(pattern1, hours)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("months value = " + hours + " does not match pattern " + pattern1)
|
||||
}
|
||||
|
||||
matched, err = regexp.MatchString(pattern1, minutes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("months value = " + minutes + " does not match pattern " + pattern1)
|
||||
}
|
||||
|
||||
matched, err = regexp.MatchString(pattern2, seconds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !matched {
|
||||
return nil, errors.New("years value = " + seconds + " does not match pattern " + pattern2)
|
||||
}
|
||||
|
||||
return &Duration{years: years, months: months, hours: hours, days: days, minutes: minutes, seconds: seconds}, nil
|
||||
}
|
||||
|
||||
//ISO8601Duration to string
|
||||
func (duration Duration) ISO8601Duration() string {
|
||||
var result string
|
||||
result += "P" // time duration designator
|
||||
//years
|
||||
if duration.years != "" {
|
||||
result += duration.years + "Y"
|
||||
}
|
||||
if duration.months != "" {
|
||||
result += duration.months + "M"
|
||||
}
|
||||
if duration.days != "" {
|
||||
result += duration.days + "D"
|
||||
}
|
||||
|
||||
if duration.hours != "" && duration.minutes != "" && duration.seconds != "" {
|
||||
result += "T"
|
||||
if duration.hours != "" {
|
||||
result += duration.hours + "H"
|
||||
}
|
||||
if duration.minutes != "" {
|
||||
result += duration.minutes + "M"
|
||||
}
|
||||
if duration.seconds != "" {
|
||||
result += duration.seconds + "S"
|
||||
}
|
||||
}
|
||||
|
||||
if len(result) == 1 {
|
||||
result += "T0S"
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user