add wsdiscovery

This commit is contained in:
Edward
2020-04-29 10:52:08 +08:00
parent ea0aed2588
commit 8827b21d01
2 changed files with 178 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
package wsdiscovery
/*******************************************************
* Copyright (C) 2018 Palanjyan Zhorzhik
*
* This file is part of ws-discovery project.
*
* ws-discovery can be copied and/or distributed without the express
* permission of Palanjyan Zhorzhik
*******************************************************/
import (
"fmt"
"log"
"net"
"time"
"github.com/gofrs/uuid"
"golang.org/x/net/ipv4"
)
const bufSize = 8192
//SendProbe to device
func SendProbe(interfaceName string, scopes, types []string, namespaces map[string]string) []string {
// Creating UUID Version 4
uuidV4 := uuid.Must(uuid.NewV4())
//fmt.Printf("UUIDv4: %s\n", uuidV4)
probeSOAP := buildProbeMessage(uuidV4.String(), scopes, types, namespaces)
//probeSOAP = `<?xml version="1.0" encoding="UTF-8"?>
//<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">
//<Header>
//<a:Action mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</a:Action>
//<a:MessageID>uuid:78a2ed98-bc1f-4b08-9668-094fcba81e35</a:MessageID><a:ReplyTo>
//<a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
//</a:ReplyTo><a:To mustUnderstand="1">urn:schemas-xmlsoap-org:ws:2005:04:discovery</a:To>
//</Header>
//<Body><Probe xmlns="http://schemas.xmlsoap.org/ws/2005/04/discovery">
//<d:Types xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery" xmlns:dp0="http://www.onvif.org/ver10/network/wsdl">dp0:NetworkVideoTransmitter</d:Types>
//</Probe>
//</Body>
//</Envelope>`
return sendUDPMulticast(probeSOAP.String(), interfaceName)
}
func sendUDPMulticast(msg string, interfaceName string) []string {
var result []string
data := []byte(msg)
iface, err := net.InterfaceByName(interfaceName)
if err != nil {
fmt.Println(err)
}
group := net.IPv4(239, 255, 255, 250)
c, err := net.ListenPacket("udp4", "0.0.0.0:1024")
if err != nil {
fmt.Println(err)
}
defer c.Close()
p := ipv4.NewPacketConn(c)
if err := p.JoinGroup(iface, &net.UDPAddr{IP: group}); err != nil {
fmt.Println(err)
}
dst := &net.UDPAddr{IP: group, Port: 3702}
for _, ifi := range []*net.Interface{iface} {
if err := p.SetMulticastInterface(ifi); err != nil {
fmt.Println(err)
}
p.SetMulticastTTL(2)
if _, err := p.WriteTo(data, nil, dst); err != nil {
fmt.Println(err)
}
}
if err := p.SetReadDeadline(time.Now().Add(time.Second * 1)); err != nil {
log.Fatal(err)
}
for {
b := make([]byte, bufSize)
n, _, _, err := p.ReadFrom(b)
if err != nil {
fmt.Println(err)
break
}
result = append(result, string(b[0:n]))
}
return result
}

View File

@@ -0,0 +1,84 @@
package wsdiscovery
import (
"strings"
"github.com/beevik/etree"
"github.com/use-go/onvif/gosoap"
)
func buildProbeMessage(uuidV4 string, scopes, types []string, nmsp map[string]string) gosoap.SoapMessage {
//Список namespace
namespaces := make(map[string]string)
namespaces["a"] = "http://schemas.xmlsoap.org/ws/2004/08/addressing"
//namespaces["d"] = "http://schemas.xmlsoap.org/ws/2005/04/discovery"
probeMessage := gosoap.NewEmptySOAP()
probeMessage.AddRootNamespaces(namespaces)
//if len(nmsp) != 0 {
// probeMessage.AddRootNamespaces(nmsp)
//}
//fmt.Println(probeMessage.String())
//Содержимое Head
var headerContent []*etree.Element
action := etree.NewElement("a:Action")
action.SetText("http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe")
action.CreateAttr("mustUnderstand", "1")
msgID := etree.NewElement("a:MessageID")
msgID.SetText("uuid:" + uuidV4)
replyTo := etree.NewElement("a:ReplyTo")
replyTo.CreateElement("a:Address").SetText("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous")
to := etree.NewElement("a:To")
to.SetText("urn:schemas-xmlsoap-org:ws:2005:04:discovery")
to.CreateAttr("mustUnderstand", "1")
headerContent = append(headerContent, action, msgID, replyTo, to)
probeMessage.AddHeaderContents(headerContent)
//Содержимое Body
probe := etree.NewElement("Probe")
probe.CreateAttr("xmlns", "http://schemas.xmlsoap.org/ws/2005/04/discovery")
if len(types) != 0 {
typesTag := etree.NewElement("d:Types")
if len(nmsp) != 0 {
for key, value := range nmsp {
typesTag.CreateAttr("xmlns:"+key, value)
}
}
typesTag.CreateAttr("xmlns:d", "http://schemas.xmlsoap.org/ws/2005/04/discovery")
//typesTag.CreateAttr("xmlns:dp0", "http://www.onvif.org/ver10/network/wsdl")
var typesString string
for _, j := range types {
typesString += j
typesString += " "
}
typesTag.SetText(strings.TrimSpace(typesString))
probe.AddChild(typesTag)
}
if len(scopes) != 0 {
scopesTag := etree.NewElement("d:Scopes")
var scopesString string
for _, j := range scopes {
scopesString += j
scopesString += " "
}
scopesTag.SetText(strings.TrimSpace(scopesString))
probe.AddChild(scopesTag)
}
probeMessage.AddBodyContent(probe)
return probeMessage
}