generated from mapeje/node-red-axis-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxis-digest.js
175 lines (166 loc) · 4.81 KB
/
axis-digest.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//Copyright (c) 2023 Fred Juhlin
// Provide digest authentication
const digestAuth = require("@mreal/digest-auth");
const got = require("got");
var exports = module.exports = {};
exports.get = function( device, path, resonseType, callback ) {
if( !device || !device.hasOwnProperty("address") || !device.hasOwnProperty("user") || !device.hasOwnProperty("password") ) {
callback(true,{
statusCode: 0,
statusMessage: "Invalid device",
body: "Missing address,user or password"
});
return;
}
var protocol = device.protocol || "http";
var url = protocol + "://" + device.address + path;
console.log(url);
var client = got.extend({
hooks:{
afterResponse: [
(res, retry) => {
const options = res.request.options;
const digestHeader = res.headers["www-authenticate"];
if (!digestHeader){
return res;
}
const incomingDigest = digestAuth.ClientDigestAuth.analyze( digestHeader );
const digest = digestAuth.ClientDigestAuth.generateProtectionAuth( incomingDigest, device.user, device.password,{
method: options.method,
uri: options.url.pathname,
counter: 1
});
options.headers.authorization = digest.raw;
return retry(options);
}
]
}
});
(async () => {
try {
const response = await client.get( url,{
responseType: resonseType,
https:{rejectUnauthorized: false}
});
callback(false, response.body );
}
catch (error) {
if( error.code === 'ECONNREFUSED' ) {
callback( true, {
statusCode: error.code,
statusMessage: "Connection refused",
body: "Port is not active or blocked by firewall"
});
return;
}
if( error.code === 'EHOSTUNREACH' ) {
callback( true, {
statusCode: error.code,
statusMessage: "Unreachable",
body: "Host does not respond"
});
return;
}
if( error.code === 'ETIMEDOUT' ) {
callback( true, {
statusCode: error.code,
statusMessage: "Timeout",
body: "Host does not respond"
});
return;
}
callback( true, {
statusCode: error && error.response ? error.response.statusCode:0,
statusMessage: error && error.response ? error.response.statusMessage:"Unkown error",
body: error && error.response ? error.response.body:""
});
}
})();
}
exports.post = function( device, path, body, responseType, callback ) {
if( !device || !device.hasOwnProperty("address") || !device.hasOwnProperty("user") || !device.hasOwnProperty("password") ) {
callback(true,{
statusCode: 0,
statusMessage: "Invalid input",
body: "Missing address,user or password"
});
return;
}
var json = null;
if( typeof body === "object" )
json = body;
if( typeof body === "string" && (body[0]==='{' || body[0]==='[' ))
json = JSON.parse(body);
var protocol = device.protocol || "http";
var url = protocol + "://" + device.address + path;
var client = got.extend({
hooks:{
afterResponse: [
(res, retry) => {
const options = res.request.options;
const digestHeader = res.headers["www-authenticate"];
if (!digestHeader){
return res;
}
const incomingDigest = digestAuth.ClientDigestAuth.analyze( digestHeader );
const digest = digestAuth.ClientDigestAuth.generateProtectionAuth( incomingDigest, device.user, device.password,{
method: options.method,
uri: options.url.pathname,
counter: 1
});
options.headers.authorization = digest.raw;
return retry(options);
}
]
}
});
(async () => {
try {
var response = 0;
if( json ) {
response = await client.post( url, {
json: json,
responseType: "json",
https: {rejectUnauthorized: false}
});
} else {
response = await client.post( url, {
body: body,
responseType: responseType,
https: {rejectUnauthorized: false}
});
}
callback(false, response.body );
} catch (error) {
if( error.code === 'ECONNREFUSED' ) {
callback( true, {
statusCode: error.code,
statusMessage: "Connection refused",
body: "Port is not active or blocked by firewall"
});
return;
}
if( error.code === 'EHOSTUNREACH' ) {
callback( true, {
statusCode: error.code,
statusMessage: "Unreachable",
body: "Host does not respond"
});
return;
}
if( error.code === 'ETIMEDOUT' ) {
callback( true, {
statusCode: error.code,
statusMessage: "Timeout",
body: "Host does not respond"
});
return;
}
callback( true, {
statusCode: error && error.response ? error.response.statusCode:0,
statusMessage: error && error.response ? error.response.statusMessage:"Unkown error",
body: error && error.response ? error.response.body:""
} );
}
})();
}