-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
141 lines (111 loc) · 3.87 KB
/
client.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"use strict";
const libxmljs = require("libxmljs");
const methods = require("./methods");
const logger = require("./logger");
const NAMESPACES = {
"soap-enc": "http://schemas.xmlsoap.org/soap/encoding/",
"soap-env": "http://schemas.xmlsoap.org/soap/envelope/",
"xsd": "http://www.w3.org/2001/XMLSchema",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
"cwmp": "urn:dslforum-org:cwmp-1-0"
};
let http = null;
let requestOptions = null;
let globalSettings = null;
let httpAgent = null;
function createSoapDocument(soapHeader) {
let xml = libxmljs.Document();
let env = xml.node("soap-env:Envelope");
for (let prefix in NAMESPACES)
env.defineNamespace(prefix, NAMESPACES[prefix]);
let header = env.node("soap-env:Header");
if (soapHeader) {
soapHeader = "<body>"+soapHeader+"</body>";
let parsedSoapHeader = libxmljs.parseXml(soapHeader);
for (let head of parsedSoapHeader.get("/body").childNodes()) {
header.addChild(head);
}
}
let body = env.node("soap-env:Body");
body._attr("SOAP-ENV:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/")
return xml;
}
function sendRequest(xml, soapAction, callback) {
let headers = {};
let body = "";
if (xml)
body = xml.toString();
headers["Content-Length"] = body.length;
headers["Content-Type"] = "text/xml; charset=\"utf-8\"";
if (soapAction) {
headers["SOAPServer"] = "";
headers["SOAPAction"] = soapAction;
}
if (globalSettings.cookie)
headers["Cookie"] = globalSettings.cookie;
let options = {
method: "POST",
headers: headers,
agent: httpAgent
};
Object.assign(options, requestOptions);
let request = http.request(options, function(response) {
let chunks = [];
let bytes = 0;
response.on("data", function(chunk) {
chunks.push(chunk);
return bytes += chunk.length;
});
return response.on("end", function() {
let offset = 0;
body = new Buffer(bytes);
chunks.forEach(function(chunk) {
chunk.copy(body, offset, 0, chunk.length);
return offset += chunk.length;
});
if (+response.headers["Content-Length"] > 0 || body.length > 0)
xml = libxmljs.parseXml(body);
else
xml = null;
if (response.headers["set-cookie"])
globalSettings.cookie = response.headers["set-cookie"];
return callback(xml);
});
});
request.setTimeout(30000, function(err) {
throw new Error("Socket timed out");
});
return request.end(body);
}
function request(entry, settings, finalCB) {
requestOptions = require("url").parse(settings.url);
http = require(requestOptions.protocol.slice(0, -1));
httpAgent = new http.Agent({keepAlive: true, maxSockets: settings.parallel});
globalSettings = settings;
const xmlOut = createSoapDocument(settings.soapHeader);
methods.GetParameterValues(settings, entry, xmlOut, function(res) {
sendRequest(res, "cwmp:GetParameterValues", function(xml) {
// Check if 404 or 403, hack fix
if (xml.root().find("string(//title)").indexOf("403") >= 0 || xml.root().find("string(//title)").indexOf("404") >= 0) {
console.error("Invalid URL".red);
process.exit(1);
}
// Check if there is a CWMP error
if (xml.root().find("string(//faultcode)").toString() !== "") {
logger.warn(" - unreadable")
logger.v(" [Fault Response] ".yellow +xml.root().find("string(//faultcode)").toString()+" - "+xml.root().find("string(//faultstring)").toString());
logger.vv(xml.root().toString());
} else {
// Read response values
logger.vv('\n'+xml.root().toString())
var value = xml.root().find("string(//ParameterValueStruct/Value)").toString();
if (value !== "") // We found a match!
logger.logEntry(entry, value);
else
logger.warn(" - empty");
}
finalCB();
});
});
}
exports.request = request;