forked from mchristopher/PokemonGo-DesktopMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngrok.js
234 lines (201 loc) · 4.91 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
var request = require('request');
var spawn = require('child_process').spawn;
var Emitter = require('events').EventEmitter;
var lock = require('lock')();
var async = require('async');
var uuid = require('node-uuid');
var xtend = require('xtend');
var os = require('os');
var path = require('path');
var bin_name = 'ngrok-' + os.platform();
var bin = path.join(__dirname, 'bin',
bin_name + (os.platform() === 'win32' ? '.exe' : ''));
var ready = /starting web service.*addr=(\d+\.\d+\.\d+\.\d+:\d+)/;
var noop = function() {};
var emitter = new Emitter().on('error', noop);
var ngrok, api, tunnels = {};
function connect(opts, cb) {
if (typeof opts === 'function') {
cb = opts;
}
cb = cb || noop;
opts = defaults(opts);
if (api) {
return runTunnel(opts, cb);
}
lock('ngrok', function(release) {
function run(err) {
if (err) {
emitter.emit('error', err);
return cb(err);
}
runNgrok(opts, release(function(err) {
if (err) {
emitter.emit('error', err);
return cb(err);
}
runTunnel(opts, cb)
}));
}
opts.authtoken ?
authtoken(opts.authtoken, run) :
run(null);
});
}
function defaults(opts) {
opts = opts || {proto: 'http', addr: 80};
if (typeof opts === 'function') {
opts = {proto: 'http', addr: 80};
}
if (typeof opts !== 'object') {
opts = {proto: 'http', addr: opts};
}
if (!opts.proto) {
opts.proto = 'http';
}
if (!opts.addr) {
opts.addr = opts.port || opts.host || 80;
}
if (opts.httpauth) {
opts.auth = opts.httpauth;
}
if (['us', 'eu', 'au', 'ap'].indexOf(opts.region) === -1) {
opts.region = 'us';
}
return opts;
}
function runNgrok(opts, cb) {
if (api) {
return cb();
}
ngrok = spawn(
bin,
['start', '--none', '--log=stdout', '--region=' + opts.region],
{cwd: __dirname + '/bin'});
ngrok.stdout.on('data', function (data) {
var addr = data.toString().match(ready);
if (addr) {
api = request.defaults({
baseUrl: 'http://' + addr[1],
json: true
});
cb();
}
});
ngrok.stderr.on('data', function (data) {
var info = data.toString().substring(0, 10000);
return cb(new Error(info));
});
ngrok.on('close', function () {
return emitter.emit('close');
});
process.on('exit', function() {
kill();
});
}
function runTunnel(opts, cb) {
_runTunnel(opts, function(err, url) {
if (err) {
emitter.emit('error', err);
return cb(err);
}
emitter.emit('connect', url);
return cb(null, url);
});
}
function _runTunnel(opts, cb) {
var retries = 100;
opts.name = String(opts.name || uuid.v4());
var retry = function() {
api.post(
{url: 'api/tunnels', json: opts},
function(err, resp, body) {
if (err) {
return cb(err);
}
var notReady = resp.statusCode === 500 && /panic/.test(body) ||
resp.statusCode === 502 && body.details &&
body.details.err === 'tunnel session not ready yet';
if (notReady) {
return retries-- ?
setTimeout(retry, 200) :
cb(new Error(body));
}
var url = body && body.public_url;
if (!url) {
var err = xtend(new Error(body.msg || 'failed to start tunnel'), body);
return cb(err);
}
tunnels[url] = body.uri;
if (opts.proto === 'http' && opts.bind_tls !== false) {
tunnels[url.replace('https', 'http')] = body.uri + ' (http)';
}
return cb(null, url);
});
};
retry();
}
function authtoken(token, cb) {
cb = cb || noop;
var a = spawn(
bin,
['authtoken', token],
{cwd: __dirname + '/bin'});
a.stdout.once('data', done.bind(null, null, token));
a.stderr.once('data', done.bind(null, new Error('cant set authtoken')));
function done(err, token) {
cb(err, token);
a.kill();
}
}
function disconnect(url, cb) {
cb = cb || noop;
if (typeof url === 'function') {
cb = url;
url = null;
}
if (!api) {
return cb();
}
if (url) {
return api.del(
tunnels[url],
function(err, resp, body) {
if (err || resp.statusCode !== 204) {
return cb(err || new Error(body));
}
delete tunnels[url];
emitter.emit('disconnect', url);
return cb();
});
}
return async.each(
Object.keys(tunnels),
disconnect,
function(err) {
if (err) {
emitter.emit('error', err);
return cb(err);
}
emitter.emit('disconnect');
return cb();
});
}
function kill(cb) {
cb = cb || noop;
if (!ngrok) {
return cb();
}
ngrok.on('exit', function() {
api = null;
tunnels = {};
emitter.emit('disconnect');
return cb();
});
return ngrok.kill();
}
emitter.connect = connect;
emitter.disconnect = disconnect;
emitter.authtoken = authtoken;
emitter.kill = kill;
module.exports = emitter;