-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery_service.ts
115 lines (90 loc) · 4.69 KB
/
discovery_service.ts
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
/// <reference path="../rpos-gateway.d.ts"/>
/// <reference path="../typings/main.d.ts"/>
/*
The MIT License(MIT)
Copyright(c) 2016 Roger Hardiman
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 tothe 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.
*/
/* WS-Discovery */
/* Listens on Port 3702 on 239.255.255.0 for UDP WS-Discovery Messages */
import dgram = require('dgram');
import uuid = require('uuid');
import xml2js = require('xml2js');
class DiscoveryService {
config: string;
constructor(config: string) {
this.config = config;
}
start() {
var discover_socket = dgram.createSocket('udp4');
discover_socket.on('error', (err) => {
throw err;
});
discover_socket.on('message', (received_msg, rinfo) => {
console.debug("Discovery received");
// Filter xmlns namespaces from XML before calling XML2JS
let filtered_msg = received_msg.toString().replace(/xmlns(.*?)=(".*?")/g, '');
var parseString = xml2js.parseString;
var strip = xml2js['processors'].stripPrefix;
parseString(filtered_msg, { tagNameProcessors: [strip] }, (err, result) => {
let probe_uuid = result['Envelope']['Header'][0]['MessageID'][0];
let probe_type = "";
try {
probe_type = result['Envelope']['Body'][0]['Probe'][0]['Types'][0];
} catch (err) {
probe_type = ""; // For a VMS that does not send Types
}
if (probe_type === "" || probe_type.indexOf("NetworkVideoTransmitter") > -1) {
let reply = `<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:d="http://schemas.xmlsoap.org/ws/2005/04/discovery"
xmlns:dn="http://www.onvif.org/ver10/network/wsdl">
<SOAP-ENV:Header>
<wsa:MessageID>uuid:${uuid.v1()}</wsa:MessageID>
<wsa:RelatesTo>${probe_uuid}</wsa:RelatesTo>
<wsa:To SOAP-ENV:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsa:Action SOAP-ENV:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2005/04/discovery/ProbeMatches</wsa:Action>
<d:AppSequence SOAP-ENV:mustUnderstand="true" MessageNumber="68" InstanceId="1460972484"/>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<d:ProbeMatches>
<d:ProbeMatch>
<wsa:EndpointReference>
<wsa:Address>urn:uuid:${'hello-world-uuid'}</wsa:Address>
</wsa:EndpointReference>
<d:Types>dn:NetworkVideoTransmitter</d:Types>
<d:Scopes> onvif://www.onvif.org/type/video_encoder onvif://www.onvif.org/type/ptz onvif://www.onvif.org/hardware/BMI onvif://www.onvif.org/name/GateWay onvif://www.onvif.org/location/beijing </d:Scopes>
<d:XAddrs>http://${'hello-world'}/onvif/device_service</d:XAddrs>
<d:MetadataVersion>1</d:MetadataVersion>
</d:ProbeMatch>
</d:ProbeMatches>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>`;
let reply_bytes = new Buffer(reply);
return discover_socket.send(reply_bytes, 0, reply_bytes.length, rinfo.port, rinfo.address);
}
});
});
discover_socket.bind(3702, '239.255.255.250', () => {
discover_socket.addMembership('239.255.255.250');
});
console.info("discovery_service started");
};
} // end class Discovery
export = DiscoveryService;