@ngrok/ngrok 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js ADDED
@@ -0,0 +1,352 @@
1
+ const { existsSync, readFileSync } = require('fs')
2
+ const { join } = require('path')
3
+
4
+ const { platform, arch } = process
5
+
6
+ let nativeBinding = null
7
+ let localFileExisted = false
8
+ let loadError = null
9
+
10
+ function isMusl() {
11
+ // For Node 10
12
+ if (!process.report || typeof process.report.getReport !== 'function') {
13
+ try {
14
+ const lddPath = require('child_process').execSync('which ldd').toString().trim();
15
+ return readFileSync(lddPath, 'utf8').includes('musl')
16
+ } catch (e) {
17
+ return true
18
+ }
19
+ } else {
20
+ const { glibcVersionRuntime } = process.report.getReport().header
21
+ return !glibcVersionRuntime
22
+ }
23
+ }
24
+
25
+ switch (platform) {
26
+ case 'android':
27
+ switch (arch) {
28
+ case 'arm64':
29
+ localFileExisted = existsSync(join(__dirname, 'ngrok.android-arm64.node'))
30
+ try {
31
+ if (localFileExisted) {
32
+ nativeBinding = require('./ngrok.android-arm64.node')
33
+ } else {
34
+ nativeBinding = require('@ngrok/ngrok-android-arm64')
35
+ }
36
+ } catch (e) {
37
+ loadError = e
38
+ }
39
+ break
40
+ case 'arm':
41
+ localFileExisted = existsSync(join(__dirname, 'ngrok.android-arm-eabi.node'))
42
+ try {
43
+ if (localFileExisted) {
44
+ nativeBinding = require('./ngrok.android-arm-eabi.node')
45
+ } else {
46
+ nativeBinding = require('@ngrok/ngrok-android-arm-eabi')
47
+ }
48
+ } catch (e) {
49
+ loadError = e
50
+ }
51
+ break
52
+ default:
53
+ throw new Error(`Unsupported architecture on Android ${arch}`)
54
+ }
55
+ break
56
+ case 'win32':
57
+ switch (arch) {
58
+ case 'x64':
59
+ localFileExisted = existsSync(
60
+ join(__dirname, 'ngrok.win32-x64-msvc.node')
61
+ )
62
+ try {
63
+ if (localFileExisted) {
64
+ nativeBinding = require('./ngrok.win32-x64-msvc.node')
65
+ } else {
66
+ nativeBinding = require('@ngrok/ngrok-win32-x64-msvc')
67
+ }
68
+ } catch (e) {
69
+ loadError = e
70
+ }
71
+ break
72
+ case 'ia32':
73
+ localFileExisted = existsSync(
74
+ join(__dirname, 'ngrok.win32-ia32-msvc.node')
75
+ )
76
+ try {
77
+ if (localFileExisted) {
78
+ nativeBinding = require('./ngrok.win32-ia32-msvc.node')
79
+ } else {
80
+ nativeBinding = require('@ngrok/ngrok-win32-ia32-msvc')
81
+ }
82
+ } catch (e) {
83
+ loadError = e
84
+ }
85
+ break
86
+ case 'arm64':
87
+ localFileExisted = existsSync(
88
+ join(__dirname, 'ngrok.win32-arm64-msvc.node')
89
+ )
90
+ try {
91
+ if (localFileExisted) {
92
+ nativeBinding = require('./ngrok.win32-arm64-msvc.node')
93
+ } else {
94
+ nativeBinding = require('@ngrok/ngrok-win32-arm64-msvc')
95
+ }
96
+ } catch (e) {
97
+ loadError = e
98
+ }
99
+ break
100
+ default:
101
+ throw new Error(`Unsupported architecture on Windows: ${arch}`)
102
+ }
103
+ break
104
+ case 'darwin':
105
+ localFileExisted = existsSync(join(__dirname, 'ngrok.darwin-universal.node'))
106
+ try {
107
+ if (localFileExisted) {
108
+ nativeBinding = require('./ngrok.darwin-universal.node')
109
+ } else {
110
+ nativeBinding = require('@ngrok/ngrok-darwin-universal')
111
+ }
112
+ break
113
+ } catch {}
114
+ switch (arch) {
115
+ case 'x64':
116
+ localFileExisted = existsSync(join(__dirname, 'ngrok.darwin-x64.node'))
117
+ try {
118
+ if (localFileExisted) {
119
+ nativeBinding = require('./ngrok.darwin-x64.node')
120
+ } else {
121
+ nativeBinding = require('@ngrok/ngrok-darwin-x64')
122
+ }
123
+ } catch (e) {
124
+ loadError = e
125
+ }
126
+ break
127
+ case 'arm64':
128
+ localFileExisted = existsSync(
129
+ join(__dirname, 'ngrok.darwin-arm64.node')
130
+ )
131
+ try {
132
+ if (localFileExisted) {
133
+ nativeBinding = require('./ngrok.darwin-arm64.node')
134
+ } else {
135
+ nativeBinding = require('@ngrok/ngrok-darwin-arm64')
136
+ }
137
+ } catch (e) {
138
+ loadError = e
139
+ }
140
+ break
141
+ default:
142
+ throw new Error(`Unsupported architecture on macOS: ${arch}`)
143
+ }
144
+ break
145
+ case 'freebsd':
146
+ if (arch !== 'x64') {
147
+ throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
148
+ }
149
+ localFileExisted = existsSync(join(__dirname, 'ngrok.freebsd-x64.node'))
150
+ try {
151
+ if (localFileExisted) {
152
+ nativeBinding = require('./ngrok.freebsd-x64.node')
153
+ } else {
154
+ nativeBinding = require('@ngrok/ngrok-freebsd-x64')
155
+ }
156
+ } catch (e) {
157
+ loadError = e
158
+ }
159
+ break
160
+ case 'linux':
161
+ switch (arch) {
162
+ case 'x64':
163
+ if (isMusl()) {
164
+ localFileExisted = existsSync(
165
+ join(__dirname, 'ngrok.linux-x64-musl.node')
166
+ )
167
+ try {
168
+ if (localFileExisted) {
169
+ nativeBinding = require('./ngrok.linux-x64-musl.node')
170
+ } else {
171
+ nativeBinding = require('@ngrok/ngrok-linux-x64-musl')
172
+ }
173
+ } catch (e) {
174
+ loadError = e
175
+ }
176
+ } else {
177
+ localFileExisted = existsSync(
178
+ join(__dirname, 'ngrok.linux-x64-gnu.node')
179
+ )
180
+ try {
181
+ if (localFileExisted) {
182
+ nativeBinding = require('./ngrok.linux-x64-gnu.node')
183
+ } else {
184
+ nativeBinding = require('@ngrok/ngrok-linux-x64-gnu')
185
+ }
186
+ } catch (e) {
187
+ loadError = e
188
+ }
189
+ }
190
+ break
191
+ case 'arm64':
192
+ if (isMusl()) {
193
+ localFileExisted = existsSync(
194
+ join(__dirname, 'ngrok.linux-arm64-musl.node')
195
+ )
196
+ try {
197
+ if (localFileExisted) {
198
+ nativeBinding = require('./ngrok.linux-arm64-musl.node')
199
+ } else {
200
+ nativeBinding = require('@ngrok/ngrok-linux-arm64-musl')
201
+ }
202
+ } catch (e) {
203
+ loadError = e
204
+ }
205
+ } else {
206
+ localFileExisted = existsSync(
207
+ join(__dirname, 'ngrok.linux-arm64-gnu.node')
208
+ )
209
+ try {
210
+ if (localFileExisted) {
211
+ nativeBinding = require('./ngrok.linux-arm64-gnu.node')
212
+ } else {
213
+ nativeBinding = require('@ngrok/ngrok-linux-arm64-gnu')
214
+ }
215
+ } catch (e) {
216
+ loadError = e
217
+ }
218
+ }
219
+ break
220
+ case 'arm':
221
+ localFileExisted = existsSync(
222
+ join(__dirname, 'ngrok.linux-arm-gnueabihf.node')
223
+ )
224
+ try {
225
+ if (localFileExisted) {
226
+ nativeBinding = require('./ngrok.linux-arm-gnueabihf.node')
227
+ } else {
228
+ nativeBinding = require('@ngrok/ngrok-linux-arm-gnueabihf')
229
+ }
230
+ } catch (e) {
231
+ loadError = e
232
+ }
233
+ break
234
+ default:
235
+ throw new Error(`Unsupported architecture on Linux: ${arch}`)
236
+ }
237
+ break
238
+ default:
239
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
240
+ }
241
+
242
+ if (!nativeBinding) {
243
+ if (loadError) {
244
+ throw loadError
245
+ }
246
+ throw new Error(`Failed to load native binding`)
247
+ }
248
+
249
+ const { NgrokSessionBuilder, tracingSubscriber, NgrokSession, NgrokHttpTunnel, NgrokTcpTunnel, NgrokTlsTunnel, NgrokLabeledTunnel, NgrokHttpTunnelBuilder, NgrokTcpTunnelBuilder, NgrokTlsTunnelBuilder, NgrokLabeledTunnelBuilder } = nativeBinding
250
+
251
+ module.exports.NgrokSessionBuilder = NgrokSessionBuilder
252
+ module.exports.tracingSubscriber = tracingSubscriber
253
+ module.exports.NgrokSession = NgrokSession
254
+ module.exports.NgrokHttpTunnel = NgrokHttpTunnel
255
+ module.exports.NgrokTcpTunnel = NgrokTcpTunnel
256
+ module.exports.NgrokTlsTunnel = NgrokTlsTunnel
257
+ module.exports.NgrokLabeledTunnel = NgrokLabeledTunnel
258
+ module.exports.NgrokHttpTunnelBuilder = NgrokHttpTunnelBuilder
259
+ module.exports.NgrokTcpTunnelBuilder = NgrokTcpTunnelBuilder
260
+ module.exports.NgrokTlsTunnelBuilder = NgrokTlsTunnelBuilder
261
+ module.exports.NgrokLabeledTunnelBuilder = NgrokLabeledTunnelBuilder
262
+ //
263
+ // javascript trailer
264
+ //
265
+
266
+ const net = require('net');
267
+ const fs = require('fs');
268
+ const os = require('os');
269
+
270
+ async function defaultTunnel() {
271
+ // set up a default session and tunnel
272
+ var builder = new NgrokSessionBuilder();
273
+ builder.authtokenFromEnv();
274
+ var session = await builder.connect();
275
+ var tunnel = await session.httpEndpoint().listen();
276
+ tunnel.session = session; // surface to caller
277
+ return tunnel;
278
+ }
279
+
280
+ // get a ngrok-connect socket, optionally passing in a pre-existing tunnel
281
+ async function getSocket(tunnel) {
282
+ if (!tunnel) {
283
+ tunnel = await defaultTunnel();
284
+ }
285
+ // use tcp socket with random local port
286
+ const server = new net.Server();
287
+ server.listen(0);
288
+ // forward to this socket
289
+ tunnel.forwardTcp('localhost:' + server.address().port);
290
+ // surface to caller
291
+ server.tunnel = tunnel;
292
+ return server;
293
+ }
294
+
295
+ // bind a server to a ngrok tunnel, optionally passing in a pre-existing tunnel
296
+ async function ngrokListen(server, tunnel) {
297
+ if (!tunnel) {
298
+ tunnel = await defaultTunnel();
299
+ }
300
+ // todo: abstract socket on linux: https://stackoverflow.com/a/60014174
301
+ // todo: named pipe on windows: https://nodejs.org/api/net.html#ipc-support
302
+
303
+ // attempt unix socket
304
+ ngrokLinkUnix(tunnel, server)
305
+ .then(() => {})
306
+ .catch(function(err) {
307
+ console.debug("Using TCP socket. " + err);
308
+ // fallback to tcp socket
309
+ ngrokLinkTcp(tunnel, server);
310
+ });
311
+
312
+ server.tunnel = tunnel; // surface to caller
313
+ }
314
+
315
+ async function ngrokLinkTcp(tunnel, server) {
316
+ // random local port
317
+ server.listen(0);
318
+ // forward to socket
319
+ tunnel.forwardTcp('localhost:' + server.address().port);
320
+ }
321
+
322
+ async function ngrokLinkUnix(tunnel, server) {
323
+ const proposed = "tun-" + tunnel.id() + ".sock";
324
+ var filename;
325
+ try {
326
+ fs.accessSync(process.cwd(), fs.constants.W_OK);
327
+ filename = proposed;
328
+ } catch (err) {
329
+ console.debug("Cannot write to: " + process.cwd());
330
+ // try tmp. allow any exception to propagate
331
+ fs.accessSync(os.tmpdir(), fs.constants.W_OK);
332
+ filename = os.tmpdir() + proposed;
333
+ }
334
+
335
+ if (!filename) {
336
+ throw new Error("no writeable directory found");
337
+ }
338
+
339
+ // begin listening
340
+ server.listen({path: filename});
341
+ // tighten permissions
342
+ try {
343
+ fs.chmodSync(filename, fs.constants.S_IRWXU);
344
+ } catch (err) {
345
+ console.debug("Cannot change permissions of file: " + filename);
346
+ }
347
+ // forward tunnel
348
+ tunnel.forwardUnix(filename);
349
+ }
350
+
351
+ module.exports.getSocket = getSocket;
352
+ module.exports.listen = ngrokListen;
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@ngrok/ngrok",
3
+ "version": "0.0.6",
4
+ "main": "index.js",
5
+ "types": "index.d.ts",
6
+ "napi": {
7
+ "name": "ngrok",
8
+ "triples": {
9
+ "additional": [
10
+ "aarch64-apple-darwin",
11
+ "aarch64-linux-android",
12
+ "aarch64-unknown-linux-gnu",
13
+ "aarch64-unknown-linux-musl",
14
+ "aarch64-pc-windows-msvc",
15
+ "armv7-unknown-linux-gnueabihf",
16
+ "x86_64-unknown-linux-musl",
17
+ "x86_64-unknown-freebsd",
18
+ "i686-pc-windows-msvc",
19
+ "armv7-linux-androideabi",
20
+ "universal-apple-darwin"
21
+ ]
22
+ }
23
+ },
24
+ "license": "MIT",
25
+ "devDependencies": {
26
+ "@napi-rs/cli": "^2.14.1",
27
+ "ava": "^4.3.3",
28
+ "express": "^4.18.2"
29
+ },
30
+ "ava": {
31
+ "timeout": "3m"
32
+ },
33
+ "engines": {
34
+ "node": ">= 10"
35
+ },
36
+ "scripts": {
37
+ "artifacts": "napi artifacts",
38
+ "build": "napi build --platform --release --js gen.js --pipe 'node post-build.js trailer index'",
39
+ "build:debug": "napi build --platform",
40
+ "prepublishOnly": "napi prepublish -t npm",
41
+ "test": "ava",
42
+ "universal": "napi universal",
43
+ "version": "napi version"
44
+ },
45
+ "packageManager": "yarn@3.3.1",
46
+ "description": "The ngrok agent in library form, suitable for integrating directly into your NodeJS application.",
47
+ "repository": "https://github.com/ngrok/ngrok-js",
48
+ "optionalDependencies": {
49
+ "@ngrok/ngrok-win32-x64-msvc": "0.0.6",
50
+ "@ngrok/ngrok-darwin-x64": "0.0.6",
51
+ "@ngrok/ngrok-linux-x64-gnu": "0.0.6",
52
+ "@ngrok/ngrok-darwin-arm64": "0.0.6",
53
+ "@ngrok/ngrok-android-arm64": "0.0.6",
54
+ "@ngrok/ngrok-linux-arm64-gnu": "0.0.6",
55
+ "@ngrok/ngrok-linux-arm64-musl": "0.0.6",
56
+ "@ngrok/ngrok-win32-arm64-msvc": "0.0.6",
57
+ "@ngrok/ngrok-linux-arm-gnueabihf": "0.0.6",
58
+ "@ngrok/ngrok-linux-x64-musl": "0.0.6",
59
+ "@ngrok/ngrok-freebsd-x64": "0.0.6",
60
+ "@ngrok/ngrok-win32-ia32-msvc": "0.0.6",
61
+ "@ngrok/ngrok-android-arm-eabi": "0.0.6",
62
+ "@ngrok/ngrok-darwin-universal": "0.0.6"
63
+ }
64
+ }
package/post-build.js ADDED
@@ -0,0 +1,31 @@
1
+ // basically "cat gen.js trailer.js > index.js" but in nodejs to be cross-platform
2
+ // called from 'yarn build', configured in package.json
3
+ // e.g.: node post-build.js trailer index <input_file_path>
4
+
5
+ var args = process.argv.slice(2);
6
+ var trailer_prefix = args[0];
7
+ var output_prefix = args[1];
8
+ var input_path = args[2];
9
+
10
+ // remove this abort when adding to typescript is needed
11
+ if (input_path.endsWith('.d.ts')) {
12
+ console.log("skipping .d.ts file: " + input_path);
13
+ return;
14
+ }
15
+
16
+ console.log("trailer_prefix: '" + trailer_prefix + "' output_prefix: '" + output_prefix
17
+ + "' input_path: '" + input_path + "'");
18
+ path = require("path");
19
+ input_filename = path.basename(input_path);
20
+ input_suffix = input_filename.substring(input_filename.indexOf('.'), input_filename.length);
21
+ output_filename = output_prefix + input_suffix
22
+
23
+ console.log("writing: " + output_filename);
24
+ var fs = require('fs');
25
+ // input to output
26
+ var data = fs.readFileSync(input_path, 'utf8');
27
+ fs.writeFileSync(output_filename, data, {encoding: 'utf8', flag: 'w'});
28
+ // trailer to output
29
+ data = fs.readFileSync(trailer_prefix + input_suffix, 'utf8');
30
+ fs.writeFileSync(output_filename, data, {encoding: 'utf8', flag: 'a'});
31
+ console.log("post-build complete");
package/src/http.rs ADDED
@@ -0,0 +1,157 @@
1
+ use std::str::FromStr;
2
+
3
+ use bytes::Bytes;
4
+ use napi::bindgen_prelude::Uint8Array;
5
+ use napi_derive::napi;
6
+ use ngrok::config::{
7
+ OauthOptions,
8
+ OidcOptions,
9
+ Scheme,
10
+ };
11
+
12
+ use crate::tunnel_builder::NgrokHttpTunnelBuilder;
13
+
14
+ #[napi]
15
+ #[allow(dead_code)]
16
+ impl NgrokHttpTunnelBuilder {
17
+ /// The scheme that this edge should use.
18
+ /// Defaults to [Scheme::HTTPS].
19
+ #[napi]
20
+ pub fn scheme(&mut self, scheme: String) -> &Self {
21
+ self.tunnel_builder = self.tunnel_builder.clone().scheme(
22
+ Scheme::from_str(scheme.as_str())
23
+ .unwrap_or_else(|_| panic!("Unknown scheme: {scheme:?}")),
24
+ );
25
+ self
26
+ }
27
+ /// The domain to request for this edge.
28
+ #[napi]
29
+ pub fn domain(&mut self, domain: String) -> &Self {
30
+ self.tunnel_builder = self.tunnel_builder.clone().domain(domain);
31
+ self
32
+ }
33
+ /// Certificates to use for client authentication at the ngrok edge.
34
+ #[napi]
35
+ pub fn mutual_tlsca(&mut self, mutual_tlsca: Uint8Array) -> &Self {
36
+ self.tunnel_builder = self
37
+ .tunnel_builder
38
+ .clone()
39
+ .mutual_tlsca(Bytes::from(mutual_tlsca.to_vec()));
40
+ self
41
+ }
42
+ /// Enable gzip compression for HTTP responses.
43
+ #[napi]
44
+ pub fn compression(&mut self) -> &Self {
45
+ self.tunnel_builder = self.tunnel_builder.clone().compression();
46
+ self
47
+ }
48
+ /// Convert incoming websocket connections to TCP-like streams.
49
+ #[napi]
50
+ pub fn websocket_tcp_conversion(&mut self) -> &Self {
51
+ self.tunnel_builder = self.tunnel_builder.clone().websocket_tcp_conversion();
52
+ self
53
+ }
54
+ /// Reject requests when 5XX responses exceed this ratio.
55
+ /// Disabled when 0.
56
+ #[napi]
57
+ pub fn circuit_breaker(&mut self, circuit_breaker: f64) -> &Self {
58
+ self.tunnel_builder = self.tunnel_builder.clone().circuit_breaker(circuit_breaker);
59
+ self
60
+ }
61
+
62
+ /// with_request_header adds a header to all requests to this edge.
63
+ #[napi]
64
+ pub fn request_header(&mut self, name: String, value: String) -> &Self {
65
+ self.tunnel_builder = self.tunnel_builder.clone().request_header(name, value);
66
+ self
67
+ }
68
+ /// with_response_header adds a header to all responses coming from this edge.
69
+ #[napi]
70
+ pub fn response_header(&mut self, name: String, value: String) -> &Self {
71
+ self.tunnel_builder = self.tunnel_builder.clone().response_header(name, value);
72
+ self
73
+ }
74
+ /// with_remove_request_header removes a header from requests to this edge.
75
+ #[napi]
76
+ pub fn remove_request_header(&mut self, name: String) -> &Self {
77
+ self.tunnel_builder = self.tunnel_builder.clone().remove_request_header(name);
78
+ self
79
+ }
80
+ /// with_remove_response_header removes a header from responses from this edge.
81
+ #[napi]
82
+ pub fn remove_response_header(&mut self, name: String) -> &Self {
83
+ self.tunnel_builder = self.tunnel_builder.clone().remove_response_header(name);
84
+ self
85
+ }
86
+
87
+ /// Credentials for basic authentication.
88
+ /// If not called, basic authentication is disabled.
89
+ #[napi]
90
+ pub fn basic_auth(&mut self, username: String, password: String) -> &Self {
91
+ self.tunnel_builder = self.tunnel_builder.clone().basic_auth(username, password);
92
+ self
93
+ }
94
+
95
+ /// OAuth configuration.
96
+ /// If not called, OAuth is disabled.
97
+ #[napi]
98
+ pub fn oauth(
99
+ &mut self,
100
+ provider: String,
101
+ allow_emails: Vec<String>,
102
+ allow_domains: Vec<String>,
103
+ scopes: Vec<String>,
104
+ ) -> &Self {
105
+ let mut oauth = OauthOptions::new(provider);
106
+ allow_emails.iter().for_each(|v| {
107
+ oauth = oauth.clone().allow_email(v);
108
+ });
109
+ allow_domains.iter().for_each(|v| {
110
+ oauth = oauth.clone().allow_domain(v);
111
+ });
112
+ scopes.iter().for_each(|v| {
113
+ oauth = oauth.clone().scope(v);
114
+ });
115
+
116
+ self.tunnel_builder = self.tunnel_builder.clone().oauth(oauth);
117
+ self
118
+ }
119
+
120
+ /// OIDC configuration.
121
+ /// If not called, OIDC is disabled.
122
+ #[napi]
123
+ pub fn oidc(
124
+ &mut self,
125
+ issuer_url: String,
126
+ client_id: String,
127
+ client_secret: String,
128
+ allow_emails: Vec<String>,
129
+ allow_domains: Vec<String>,
130
+ scopes: Vec<String>,
131
+ ) -> &Self {
132
+ let mut oidc = OidcOptions::new(issuer_url, client_id, client_secret);
133
+ allow_emails.iter().for_each(|v| {
134
+ oidc = oidc.clone().allow_email(v);
135
+ });
136
+ allow_domains.iter().for_each(|v| {
137
+ oidc = oidc.clone().allow_domain(v);
138
+ });
139
+ scopes.iter().for_each(|v| {
140
+ oidc = oidc.clone().scope(v);
141
+ });
142
+
143
+ self.tunnel_builder = self.tunnel_builder.clone().oidc(oidc);
144
+ self
145
+ }
146
+
147
+ /// WebhookVerification configuration.
148
+ /// If not called, WebhookVerification is disabled.
149
+ #[napi]
150
+ pub fn webhook_verification(&mut self, provider: String, secret: String) -> &Self {
151
+ self.tunnel_builder = self
152
+ .tunnel_builder
153
+ .clone()
154
+ .webhook_verification(provider, secret);
155
+ self
156
+ }
157
+ }
package/src/lib.rs ADDED
@@ -0,0 +1,6 @@
1
+ pub mod http;
2
+ pub mod session;
3
+ pub mod tcp;
4
+ pub mod tls;
5
+ pub mod tunnel;
6
+ pub mod tunnel_builder;