forked from yakovlevdmv/WS-Discovery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
discovery.go
115 lines (93 loc) · 2.57 KB
/
discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package onvif
import (
"bytes"
"net"
"net/url"
"path"
"sort"
"strings"
"github.com/beevik/etree"
"github.com/google/uuid"
)
type Device struct {
XAddr string
Hardware string
Name string
Location string
}
// Discovery onvif devices by type NetworkVideoTransmitter
func Discovery(iface *net.Interface) (devices []Device, err error) {
msg := buildProbeMessage(uuid.New().String(), nil, []string{"dn:NetworkVideoTransmitter"}, map[string]string{"dn": "http://www.onvif.org/ver10/network/wsdl"})
resp, err := sendUDPMulticast(iface, []byte(msg.String()))
if err != nil {
return nil, err
}
for _, j := range resp {
doc := etree.NewDocument()
if err := doc.ReadFromString(j); err != nil {
continue
}
addr, ok := lookupXaddr(doc)
if !ok {
continue
}
hardware, name, location := lookupScopes(doc)
// remove from name hardware value
name = strings.Replace(name, hardware, "", -1)
name = strings.TrimSpace(name)
devices = append(devices, Device{
XAddr: addr,
Hardware: hardware,
Name: name,
Location: location,
})
}
sort.Slice(devices, func(i, j int) bool {
ip1 := net.ParseIP(devices[i].XAddr)
ip2 := net.ParseIP(devices[j].XAddr)
return bytes.Compare(ip1, ip2) < 0
})
return
}
// lookup xaddrs by path ./Body/ProbeMatches/ProbeMatch/XAddrs
// ex: <d:XAddrs>http://${IPv4-addr}/onvif/device_service http://[${IPv6-addr}]/onvif/device_service ... </d:XAddrs>
func lookupXaddr(doc *etree.Document) (xaddr string, ok bool) {
elem := doc.Root().FindElement("./Body/ProbeMatches/ProbeMatch/XAddrs")
if elem == nil {
return
}
for _, onvifurl := range strings.Split(elem.Text(), " ") {
u, err := url.Parse(onvifurl)
if err != nil {
continue
}
xaddr = u.Hostname()
ok = true
break
}
return
}
// lookup scopes by path ./Body/ProbeMatches/ProbeMatch/Scopes
// ex: <d:Scopes>onvif://www.onvif.org/type/video_encoder onvif://www.onvif.org/hardware/DS-2CD2042WD-I onvif://www.onvif.org/name/HIKVISION%20DS-2CD2042WD-I</d:Scopes>
func lookupScopes(doc *etree.Document) (hardware, name, location string) {
elem := doc.Root().FindElement("./Body/ProbeMatches/ProbeMatch/Scopes")
if elem == nil {
return
}
for _, scope := range strings.Split(elem.Text(), " ") {
u, err := url.Parse(scope)
if err != nil {
continue
}
upath := strings.ToLower(u.Path)
switch {
case strings.Contains(upath, "hardware"):
_, hardware = path.Split(u.Path)
case strings.Contains(upath, "name"):
_, name = path.Split(u.Path)
case strings.Contains(upath, "location"):
_, location = path.Split(u.Path)
}
}
return
}