17 Commits

Author SHA1 Message Date
Edward
48daf5acb5 Rename license to LICENSE 2021-01-31 16:01:46 +08:00
Edward
d85a0742b9 Create license 2021-01-31 16:00:28 +08:00
Edward
24a85afc84 Merge pull request #4 from GreenLightning/master
Do not log "i/o timeout" errors
2021-01-29 19:37:38 +08:00
Green Lightning
c8ac58ebd7 Require Go 1.15 2020-10-23 11:51:21 +02:00
Green Lightning
4ec21b20e0 Do not log "i/o timeout" errors 2020-10-21 00:09:00 +02:00
Edward
4e696ec65a Merge pull request #3 from GreenLightning/fix-auth
Fix authentication
2020-08-17 18:39:23 +08:00
Green Lightning
e1345d5e6b Fix authentication
Because time.Now() is called twice, it may return different results on a
slow machine, causing an invalid authentication header to be generated.
2020-08-16 17:31:35 +02:00
Edward
918ce541d2 Update README.md 2020-05-26 10:39:28 +08:00
Edward
15e268c1b7 Update README.md 2020-05-09 16:06:23 +08:00
Edward
dce0d0faad add an example by testing 2020-05-01 22:58:49 +08:00
Edward
be84cab1fe add SendSoapWithTimeout 2020-05-01 22:58:33 +08:00
Edward
c10b8ca105 update mod 2020-05-01 22:42:53 +08:00
Edward
e6593ad04f introduce iso8601_duration directly 2020-05-01 22:42:40 +08:00
Edward
702fded39b Update README.md 2020-05-01 22:35:50 +08:00
Edward
298c2dabb9 Update README.md 2020-04-29 11:09:08 +08:00
Edward
bedd8886e4 Update README.md 2020-04-29 11:08:05 +08:00
Edward
ddb7588318 Update README.md 2020-04-29 11:07:53 +08:00
10 changed files with 241 additions and 18 deletions

21
LICENSE Normal file
View 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.

View File

@@ -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)

View 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
View File

@@ -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
View File

@@ -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=

View File

@@ -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))
}

View File

@@ -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))
}

View File

@@ -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]))

View File

@@ -11,7 +11,7 @@ import (
"strings"
"time"
iso8601 "github.com/yakovlevdmv/Golang-iso8601-duration"
"github.com/use-go/onvif/xsd/iso8601"
)
/*

View 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
}