-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathngrok.js
244 lines (228 loc) · 8.07 KB
/
ngrok.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const ng = require('@ngrok/ngrok');
const Package = require('./package.json');
const ngp = require('@ngrok/ngrok/package.json');
module.exports = function (RED) {
function ngrok(config) {
const proto_types = ['http', 'https', 'tcp', 'tls'];
const bind_tls_types = ['https', 'true', 'http', 'false'];
RED.nodes.createNode(this, config);
var node = this;
node.creds = RED.nodes.getNode(config.creds);
node.port = config.port || '';
node.portType = config.portType || 'num';
node.host = config.host || '';
node.hostType = config.hostType || 'localhost';
node.proto = config.proto || 'http';
node.bind_tls = config.bind_tls;
node.bind_tlsType = config.bind_tlsType || 'https';
node.subdomain = config.subdomain;
node.subdomainType = config.subdomainType || 'none';
node.auth = config.auth;
node.authType = config.authType || 'str';
node.hostHeader = config.hostHeader || '';
node.hostHeaderType = config.hostHeaderType || 'none';
if (RED.nodes.getNode(config.creds) == null) {
node.authtoken = '';
} else {
node.authtoken = RED.nodes.getNode(config.creds).credentials.authtoken;
}
node.listener = null;
node.status({fill: 'grey', shape: 'ring', text: 'idle'});
node.on('input', function (msg) {
if (!node.authtoken || node.authtoken == '') {
node.error('Usage of ngrok requires a verified account and authtoken');
node.status({fill: 'red', shape: 'dot', text: 'error'});
return;
}
if (['false', 'off', '0'].indexOf(String(msg.payload).toLowerCase()) != -1) {
(async function () {
try {
if (node.listener) {
await node.listener.close();
node.listener = null;
} else {
// _domain
let _domain = RED.util.evaluateNodeProperty(node.subdomain, node.subdomainType, node, msg);
if (_domain.indexOf('.') > -1) {
await ng.disconnect(_domain);
}
}
} catch (_) {}
msg.payload = null;
node.send(msg);
node.status({fill: 'red', shape: 'ring', text: 'disconnected'});
})();
} else if (['true', 'on', '1'].indexOf(String(msg.payload).toLowerCase()) != -1) {
let _port, _host, _proto, _domain, _bind_tls, _auth, _host_header;
if (node.portType == 'node-red' || node.portType == '') {
_port = null;
} else {
_port = RED.util.evaluateNodeProperty(node.port, node.portType, node, msg);
}
if (!_port) {
_port = RED.settings.uiPort;
} //ensure port is something
if (node.hostType == 'localhost' || node.hostType == '') {
_host = '127.0.0.1';
} else {
_host = RED.util.evaluateNodeProperty(node.host, node.hostType, node, msg);
}
if (proto_types.indexOf(node.proto) >= 0) {
_proto = node.proto;
} else {
node.error('Invalid protocol type', msg);
node.status({fill: 'red', shape: 'dot', text: 'error'});
return;
}
if (String(_proto) != 'tcp') {
//binding
if (bind_tls_types.indexOf(node.bind_tlsType) >= 0) {
_bind_tls = node.bind_tlsType;
} else {
_bind_tls = RED.util.evaluateNodeProperty(node.bind_tls, node.bind_tlsType, node, msg);
}
//domain (node property is subdomain for backwards compatibility)
_domain = RED.util.evaluateNodeProperty(node.subdomain, node.subdomainType, node, msg);
//auth
if (node.authType == 'none') {
_auth = null;
} else {
_auth = RED.util.evaluateNodeProperty(node.auth, node.authType, node, msg);
}
//host-header
if (node.hostHeaderType == 'none') {
_host_header = null;
} else {
_host_header = RED.util.evaluateNodeProperty(node.hostHeader, node.hostHeaderType, node, msg);
}
//ensure binding is valid
switch (_bind_tls) {
case 'false':
case false:
case 'https':
_bind_tls = ['https'];
break;
case 'http':
_bind_tls = ['http'];
break;
default:
_bind_tls = ['https']; //default to secure
}
}
// metadata
const _red = RED.version();
const _pname = Package.name.trim();
const _pversion = Package.version.trim();
const _ngversion = ngp.version.trim();
const _nname = node.name;
const _nid = node.id;
var options = {
authtoken: node.authtoken,
schemes: _bind_tls,
host_header: _host_header,
session_metadata: `{"Node-RED":"${_red}","${_pname}":"${_pversion}","ngrok-javascript": ${_ngversion},"name":"${_nname},"id":"${_nid}"}`
};
if (_proto == 'https'){
options.addr = "https://"+ _host + ':' + _port
} else {
options.proto = _proto
options.addr =_host + ':' + _port
}
if (_domain.length !=0){
if (_domain.indexOf('.') > -1) {
options.domain = _domain;
} else {
options.domain = _domain+'.ngrok.io'; // Added for backwards compatibility
}
}
if (_auth) {
const auth = _auth.split(':');
if (!auth || auth.length !== 2) {
node.error('ngrok auth should be formatted as username:password', msg);
node.status({fill: 'red', shape: 'dot', text: 'error'});
return;
}
if (auth[1].length < 8) {
node.error('Password must be at least 8 characters');
node.status({fill: 'red', shape: 'dot', text: 'error'});
return;
}
options.basic_auth = _auth;
}
clean(options);
(async function () {
try {
//Disconnect once before reconnecting
if (node.listener) {
await node.listener.close();
node.listener = null;
} else {
if (options.domain) {
await ng.disconnect(options.domain);
}
}
node.debug(JSON.stringify(options))
node.listener = await ng.forward(options);
msg.payload = node.listener.url();
node.send(msg);
node.status({fill: 'green', shape: 'dot', text: msg.payload});
} catch ({message}) {
node.error(`Connect error: ${message}`);
node.status({fill: 'red', shape: 'dot', text: 'error'});
return;
}
})();
}
});
node.on('close', function (removed, done) {
(async function () {
try {
if (node.listener) {
await node.listener.close();
node.listener = null;
} else {
// _domain
let _domain = RED.util.evaluateNodeProperty(node.subdomain, node.subdomainType, node);
if (_domain.indexOf('.') > -1) {
await ng.disconnect(_domain);
}
}
} catch (_) {}
})();
done();
});
}
function ngrokauth(n) {
RED.nodes.createNode(this, n);
this.authtoken = n.authtoken;
this.name = n.name;
}
RED.nodes.registerType('ngrok', ngrok);
RED.nodes.registerType('ngrokauth', ngrokauth, {
credentials: {
authtoken: {type: 'text'}
}
});
RED.httpAdmin.post('/ngrokInject/:id', RED.auth.needsPermission('inject.write'), function (req, res) {
var node = RED.nodes.getNode(req.params.id);
if (node != null) {
try {
var state = req.body.on == 'true';
node.receive({payload: state});
res.sendStatus(200);
} catch (err) {
res.sendStatus(500);
node.error(RED._('inject.failed', {error: err.toString()}));
}
} else {
res.sendStatus(404);
}
});
};
function clean(obj) {
for (var propName in obj) {
if (obj[propName] === null || obj[propName] === undefined || obj[propName] === '') {
delete obj[propName];
}
}
}