@ngrok/ngrok 0.5.2 → 0.6.0
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/CHANGELOG.md +36 -28
- package/CODE_OF_CONDUCT.md +45 -0
- package/Cargo.toml +5 -2
- package/README.md +161 -21
- package/docs/assets/highlight.css +13 -6
- package/docs/assets/search.js +1 -1
- package/docs/classes/NgrokHttpTunnelBuilder.html +25 -22
- package/docs/classes/NgrokLabeledTunnelBuilder.html +8 -5
- package/docs/classes/NgrokSession.html +24 -9
- package/docs/classes/NgrokSessionBuilder.html +20 -17
- package/docs/classes/NgrokTcpTunnelBuilder.html +12 -9
- package/docs/classes/NgrokTlsTunnelBuilder.html +14 -11
- package/docs/classes/NgrokTunnel.html +21 -35
- package/docs/classes/UpdateRequest.html +7 -4
- package/docs/functions/authtoken.html +5 -2
- package/docs/functions/connect.html +5 -2
- package/docs/functions/consoleLog.html +5 -2
- package/docs/functions/disconnect.html +5 -2
- package/docs/functions/getTunnel.html +88 -0
- package/docs/functions/getTunnelByUrl.html +88 -0
- package/docs/functions/kill.html +5 -2
- package/docs/functions/listen.html +5 -2
- package/docs/functions/listenable.html +5 -2
- package/docs/functions/loggingCallback.html +5 -2
- package/docs/functions/tunnels.html +83 -0
- package/docs/index.html +74 -13
- package/docs/interfaces/Config.html +57 -54
- package/docs/modules.html +7 -1
- package/examples/fastify/ngrok-fastify.js +1 -1
- package/examples/hapi/ngrok-hapi.js +1 -1
- package/examples/nestjs/src/main.ts +1 -1
- package/examples/nextjs/ngrok.config.js +1 -1
- package/examples/ngrok-connect-full.js +22 -23
- package/examples/ngrok-connect-minimal.js +3 -2
- package/examples/ngrok-http-full.js +1 -1
- package/examples/ngrok-http-minimum.js +1 -1
- package/examples/ngrok-labeled.js +1 -1
- package/examples/ngrok-tcp.js +1 -1
- package/examples/ngrok-tls.js +1 -1
- package/examples/ngrok-typescript.ts +1 -1
- package/examples/ngrok-windows-pipe.js +1 -1
- package/examples/remix/ngrok.config.js +1 -1
- package/examples/svelte/ngrok.config.cjs +1 -1
- package/examples/vue/ngrok.config.ts +1 -1
- package/index.d.ts +12 -6
- package/index.js +89 -23
- package/package.json +14 -14
- package/scripts/gc-node-drop.js +1 -1
- package/scripts/gc-rust-drop.js +1 -1
- package/src/connect.rs +13 -8
- package/src/lib.rs +21 -0
- package/src/session.rs +17 -6
- package/src/tunnel.rs +139 -104
- package/src/tunnel_builder.rs +2 -2
- package/trailer.js +85 -22
package/src/session.rs
CHANGED
|
@@ -34,8 +34,12 @@ use tracing::{
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
use crate::{
|
|
37
|
-
|
|
38
|
-
tunnel::
|
|
37
|
+
napi_ngrok_err,
|
|
38
|
+
tunnel::{
|
|
39
|
+
remove_global_tunnel,
|
|
40
|
+
search_tunnels,
|
|
41
|
+
NgrokTunnel,
|
|
42
|
+
},
|
|
39
43
|
tunnel_builder::{
|
|
40
44
|
NgrokHttpTunnelBuilder,
|
|
41
45
|
NgrokLabeledTunnelBuilder,
|
|
@@ -406,12 +410,12 @@ impl NgrokSessionBuilder {
|
|
|
406
410
|
.connect()
|
|
407
411
|
.await
|
|
408
412
|
.map(|s| {
|
|
409
|
-
info!("Session created");
|
|
413
|
+
info!("Session created {:?}", s.id());
|
|
410
414
|
NgrokSession {
|
|
411
415
|
raw_session: Arc::new(SyncMutex::new(s)),
|
|
412
416
|
}
|
|
413
417
|
})
|
|
414
|
-
.map_err(|e|
|
|
418
|
+
.map_err(|e| napi_ngrok_err("failed to connect session", &e))
|
|
415
419
|
}
|
|
416
420
|
}
|
|
417
421
|
|
|
@@ -455,6 +459,13 @@ impl NgrokSession {
|
|
|
455
459
|
NgrokLabeledTunnelBuilder::new(session.clone(), session.labeled_tunnel())
|
|
456
460
|
}
|
|
457
461
|
|
|
462
|
+
/// Retrieve a list of this session's non-closed tunnels, in no particular order.
|
|
463
|
+
#[napi]
|
|
464
|
+
pub async fn tunnels(&self) -> Vec<NgrokTunnel> {
|
|
465
|
+
let session_id = self.raw_session.lock().id();
|
|
466
|
+
search_tunnels(Some(session_id), None).await
|
|
467
|
+
}
|
|
468
|
+
|
|
458
469
|
/// Close a tunnel with the given ID.
|
|
459
470
|
#[napi]
|
|
460
471
|
pub async fn close_tunnel(&self, id: String) -> Result<()> {
|
|
@@ -463,7 +474,7 @@ impl NgrokSession {
|
|
|
463
474
|
let res = session
|
|
464
475
|
.close_tunnel(id.clone())
|
|
465
476
|
.await
|
|
466
|
-
.map_err(|e|
|
|
477
|
+
.map_err(|e| napi_ngrok_err("failed to close tunnel", &e));
|
|
467
478
|
|
|
468
479
|
if res.is_ok() {
|
|
469
480
|
// remove our reference to allow it to drop
|
|
@@ -479,7 +490,7 @@ impl NgrokSession {
|
|
|
479
490
|
session
|
|
480
491
|
.close()
|
|
481
492
|
.await
|
|
482
|
-
.map_err(|e|
|
|
493
|
+
.map_err(|e| napi_ngrok_err("failed to close session", &e))
|
|
483
494
|
}
|
|
484
495
|
}
|
|
485
496
|
|
package/src/tunnel.rs
CHANGED
|
@@ -29,7 +29,13 @@ use tracing::{
|
|
|
29
29
|
info,
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
use crate::
|
|
32
|
+
use crate::{
|
|
33
|
+
connect::PIPE_PREFIX,
|
|
34
|
+
napi_err,
|
|
35
|
+
napi_ngrok_err,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
pub(crate) const UNIX_PREFIX: &str = "unix:";
|
|
33
39
|
|
|
34
40
|
lazy_static! {
|
|
35
41
|
// tunnel references to be kept until explicit close to prevent nodejs gc from dropping them.
|
|
@@ -41,8 +47,17 @@ lazy_static! {
|
|
|
41
47
|
/// Stores the tunnel and session references to be kept until explicit close.
|
|
42
48
|
struct Storage {
|
|
43
49
|
tunnel: Arc<Mutex<dyn ExtendedTunnel>>,
|
|
44
|
-
session:
|
|
50
|
+
session: Session,
|
|
51
|
+
tun_meta: Arc<TunnelMetadata>,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
struct TunnelMetadata {
|
|
55
|
+
id: String,
|
|
56
|
+
forwards_to: String,
|
|
57
|
+
metadata: String,
|
|
45
58
|
url: Option<String>,
|
|
59
|
+
proto: Option<String>,
|
|
60
|
+
labels: HashMap<String, String>,
|
|
46
61
|
}
|
|
47
62
|
|
|
48
63
|
/// The TunnelExt cannot be turned into an object since it contains generics, so implementing
|
|
@@ -58,20 +73,14 @@ pub trait ExtendedTunnel: Tunnel {
|
|
|
58
73
|
/// @group Tunnel and Sessions
|
|
59
74
|
#[napi(custom_finalize)]
|
|
60
75
|
#[allow(dead_code)]
|
|
61
|
-
pub
|
|
62
|
-
id: String,
|
|
63
|
-
forwards_to: String,
|
|
64
|
-
metadata: String,
|
|
65
|
-
url: Option<String>,
|
|
66
|
-
proto: Option<String>,
|
|
76
|
+
pub struct NgrokTunnel {
|
|
67
77
|
session: Session,
|
|
68
|
-
|
|
78
|
+
tun_meta: Arc<TunnelMetadata>,
|
|
69
79
|
}
|
|
70
80
|
|
|
71
81
|
macro_rules! make_tunnel_type {
|
|
72
82
|
// the common (non-labeled) branch
|
|
73
83
|
($(#[$outer:meta])* $wrapper:ident, $tunnel:tt, common) => {
|
|
74
|
-
|
|
75
84
|
$(#[$outer])*
|
|
76
85
|
#[allow(dead_code)]
|
|
77
86
|
pub(crate) struct $wrapper {
|
|
@@ -82,27 +91,24 @@ macro_rules! make_tunnel_type {
|
|
|
82
91
|
impl $wrapper {
|
|
83
92
|
pub(crate) async fn new_tunnel(session: Session, raw_tunnel: $tunnel) -> NgrokTunnel {
|
|
84
93
|
let id = raw_tunnel.id().to_string();
|
|
85
|
-
let
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
94
|
+
let tun_meta = Arc::new(TunnelMetadata {
|
|
95
|
+
id: id.clone(),
|
|
96
|
+
forwards_to: raw_tunnel.forwards_to().to_string(),
|
|
97
|
+
metadata: raw_tunnel.metadata().to_string(),
|
|
98
|
+
url: Some(raw_tunnel.url().to_string()),
|
|
99
|
+
proto: Some(raw_tunnel.proto().to_string()),
|
|
100
|
+
labels: HashMap::new(),
|
|
101
|
+
});
|
|
102
|
+
info!("Created tunnel {id:?} with url {:?}", raw_tunnel.url());
|
|
90
103
|
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
91
|
-
|
|
104
|
+
let storage = Arc::new(Storage {
|
|
92
105
|
tunnel: Arc::new(Mutex::new(raw_tunnel)),
|
|
93
|
-
session: Arc::new(Mutex::new(session.clone())),
|
|
94
|
-
url: Some(url.clone()),
|
|
95
|
-
}));
|
|
96
|
-
// create the user-facing object
|
|
97
|
-
NgrokTunnel {
|
|
98
|
-
id,
|
|
99
|
-
forwards_to,
|
|
100
|
-
metadata,
|
|
101
|
-
url: Some(url),
|
|
102
|
-
proto: Some(proto),
|
|
103
106
|
session,
|
|
104
|
-
|
|
105
|
-
}
|
|
107
|
+
tun_meta,
|
|
108
|
+
});
|
|
109
|
+
GLOBAL_TUNNELS.lock().await.insert(id, storage.clone());
|
|
110
|
+
// create the user-facing object
|
|
111
|
+
NgrokTunnel::from_storage(&storage)
|
|
106
112
|
}
|
|
107
113
|
}
|
|
108
114
|
|
|
@@ -120,26 +126,24 @@ macro_rules! make_tunnel_type {
|
|
|
120
126
|
impl $wrapper {
|
|
121
127
|
pub(crate) async fn new_tunnel(session: Session, raw_tunnel: $tunnel) -> NgrokTunnel {
|
|
122
128
|
let id = raw_tunnel.id().to_string();
|
|
123
|
-
let
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
128
|
-
GLOBAL_TUNNELS.lock().await.insert(id.clone(), Arc::new(Storage {
|
|
129
|
-
tunnel: Arc::new(Mutex::new(raw_tunnel)),
|
|
130
|
-
session: Arc::new(Mutex::new(session.clone())),
|
|
131
|
-
url: None,
|
|
132
|
-
}));
|
|
133
|
-
// create the user-facing object
|
|
134
|
-
NgrokTunnel {
|
|
135
|
-
id,
|
|
136
|
-
forwards_to,
|
|
137
|
-
metadata,
|
|
129
|
+
let tun_meta = Arc::new(TunnelMetadata {
|
|
130
|
+
id: id.clone(),
|
|
131
|
+
forwards_to: raw_tunnel.forwards_to().to_string(),
|
|
132
|
+
metadata: raw_tunnel.metadata().to_string(),
|
|
138
133
|
url: None,
|
|
139
134
|
proto: None,
|
|
135
|
+
labels: raw_tunnel.labels().clone(),
|
|
136
|
+
});
|
|
137
|
+
info!("Created tunnel {id:?} with labels {:?}", tun_meta.labels);
|
|
138
|
+
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
139
|
+
let storage = Arc::new(Storage {
|
|
140
|
+
tunnel: Arc::new(Mutex::new(raw_tunnel)),
|
|
140
141
|
session,
|
|
141
|
-
|
|
142
|
-
}
|
|
142
|
+
tun_meta,
|
|
143
|
+
});
|
|
144
|
+
GLOBAL_TUNNELS.lock().await.insert(id, storage.clone());
|
|
145
|
+
// create the user-facing object
|
|
146
|
+
NgrokTunnel::from_storage(&storage)
|
|
143
147
|
}
|
|
144
148
|
}
|
|
145
149
|
|
|
@@ -163,28 +167,37 @@ macro_rules! make_tunnel_type {
|
|
|
163
167
|
#[napi]
|
|
164
168
|
#[allow(dead_code)]
|
|
165
169
|
impl NgrokTunnel {
|
|
170
|
+
/// Create NgrokTunnel from Storage
|
|
171
|
+
fn from_storage(storage: &Arc<Storage>) -> NgrokTunnel {
|
|
172
|
+
// create the user-facing object
|
|
173
|
+
NgrokTunnel {
|
|
174
|
+
session: storage.session.clone(),
|
|
175
|
+
tun_meta: storage.tun_meta.clone(),
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
166
179
|
/// The URL that this tunnel backs.
|
|
167
180
|
#[napi]
|
|
168
181
|
pub fn url(&self) -> Option<String> {
|
|
169
|
-
self.url.clone()
|
|
182
|
+
self.tun_meta.url.clone()
|
|
170
183
|
}
|
|
171
184
|
|
|
172
185
|
/// The protocol of the endpoint that this tunnel backs.
|
|
173
186
|
#[napi]
|
|
174
187
|
pub fn proto(&self) -> Option<String> {
|
|
175
|
-
self.proto.clone()
|
|
188
|
+
self.tun_meta.proto.clone()
|
|
176
189
|
}
|
|
177
190
|
|
|
178
191
|
/// The labels this tunnel was started with.
|
|
179
192
|
#[napi]
|
|
180
193
|
pub fn labels(&self) -> HashMap<String, String> {
|
|
181
|
-
self.labels.clone()
|
|
194
|
+
self.tun_meta.labels.clone()
|
|
182
195
|
}
|
|
183
196
|
|
|
184
197
|
/// Returns a tunnel's unique ID.
|
|
185
198
|
#[napi]
|
|
186
199
|
pub fn id(&self) -> String {
|
|
187
|
-
self.id.clone()
|
|
200
|
+
self.tun_meta.id.clone()
|
|
188
201
|
}
|
|
189
202
|
|
|
190
203
|
/// Returns a human-readable string presented in the ngrok dashboard
|
|
@@ -193,27 +206,21 @@ impl NgrokTunnel {
|
|
|
193
206
|
/// explicitly.
|
|
194
207
|
#[napi]
|
|
195
208
|
pub fn forwards_to(&self) -> String {
|
|
196
|
-
self.forwards_to.clone()
|
|
209
|
+
self.tun_meta.forwards_to.clone()
|
|
197
210
|
}
|
|
198
211
|
|
|
199
212
|
/// Returns the arbitrary metadata string for this tunnel.
|
|
200
213
|
#[napi]
|
|
201
214
|
pub fn metadata(&self) -> String {
|
|
202
|
-
self.metadata.clone()
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/// Forward incoming tunnel connections to the provided TCP address.
|
|
206
|
-
#[napi]
|
|
207
|
-
pub async fn forward_tcp(&self, addr: String) -> Result<()> {
|
|
208
|
-
forward_tcp(&self.id, addr).await
|
|
215
|
+
self.tun_meta.metadata.clone()
|
|
209
216
|
}
|
|
210
217
|
|
|
211
|
-
/// Forward incoming tunnel connections
|
|
212
|
-
///
|
|
213
|
-
///
|
|
218
|
+
/// Forward incoming tunnel connections. This can be either a TCP address or a file socket path.
|
|
219
|
+
/// For file socket paths on Linux/Darwin, addr can be a unix domain socket path, e.g. "/tmp/ngrok.sock"
|
|
220
|
+
/// On Windows, addr can be a named pipe, e.e. "\\\\.\\pipe\\an_ngrok_pipe
|
|
214
221
|
#[napi]
|
|
215
|
-
pub async fn
|
|
216
|
-
|
|
222
|
+
pub async fn forward(&self, addr: String) -> Result<()> {
|
|
223
|
+
forward(&self.tun_meta.id, addr).await
|
|
217
224
|
}
|
|
218
225
|
|
|
219
226
|
/// Close the tunnel.
|
|
@@ -223,18 +230,18 @@ impl NgrokTunnel {
|
|
|
223
230
|
/// tunnel's ID.
|
|
224
231
|
#[napi]
|
|
225
232
|
pub async fn close(&self) -> Result<()> {
|
|
226
|
-
debug!("NgrokTunnel closing, id: {}", self.id);
|
|
233
|
+
debug!("NgrokTunnel closing, id: {}", self.tun_meta.id);
|
|
227
234
|
|
|
228
235
|
// we may not be able to lock our reference to the tunnel due to the forward_* calls which
|
|
229
236
|
// continuously accept-loop while the tunnel is active, so calling close on the Session.
|
|
230
237
|
let res = self
|
|
231
238
|
.session
|
|
232
|
-
.close_tunnel(self.id.clone())
|
|
239
|
+
.close_tunnel(self.tun_meta.id.clone())
|
|
233
240
|
.await
|
|
234
|
-
.map_err(|e|
|
|
241
|
+
.map_err(|e| napi_ngrok_err("error closing tunnel", &e));
|
|
235
242
|
|
|
236
243
|
// drop our internal reference to the tunnel after awaiting close
|
|
237
|
-
GLOBAL_TUNNELS.lock().await.remove(&self.id);
|
|
244
|
+
GLOBAL_TUNNELS.lock().await.remove(&self.tun_meta.id);
|
|
238
245
|
|
|
239
246
|
res
|
|
240
247
|
}
|
|
@@ -243,7 +250,7 @@ impl NgrokTunnel {
|
|
|
243
250
|
#[allow(unused_mut)]
|
|
244
251
|
impl ObjectFinalize for NgrokTunnel {
|
|
245
252
|
fn finalize(mut self, _env: Env) -> Result<()> {
|
|
246
|
-
debug!("NgrokTunnel finalize, id: {}", self.id);
|
|
253
|
+
debug!("NgrokTunnel finalize, id: {}", self.tun_meta.id);
|
|
247
254
|
Ok(())
|
|
248
255
|
}
|
|
249
256
|
}
|
|
@@ -273,31 +280,25 @@ make_tunnel_type! {
|
|
|
273
280
|
NgrokLabeledTunnel, LabeledTunnel, label
|
|
274
281
|
}
|
|
275
282
|
|
|
276
|
-
pub async fn
|
|
277
|
-
|
|
278
|
-
let
|
|
279
|
-
.
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
.
|
|
283
|
-
|
|
284
|
-
.
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
.await?
|
|
294
|
-
.tunnel
|
|
295
|
-
.lock()
|
|
296
|
-
.await
|
|
297
|
-
.fwd_pipe(addr)
|
|
298
|
-
.await;
|
|
283
|
+
pub async fn forward(id: &String, addr: String) -> Result<()> {
|
|
284
|
+
let tun = &get_storage_by_id(id).await?.tunnel;
|
|
285
|
+
let is_pipe =
|
|
286
|
+
addr.starts_with(PIPE_PREFIX) || addr.starts_with(UNIX_PREFIX) || addr.contains('/');
|
|
287
|
+
|
|
288
|
+
let res = if is_pipe {
|
|
289
|
+
let mut tun_addr: &str = addr.as_str();
|
|
290
|
+
// remove the "pipe:" and "unix:" prefix
|
|
291
|
+
tun_addr = tun_addr.strip_prefix(PIPE_PREFIX).unwrap_or(tun_addr);
|
|
292
|
+
tun_addr = tun_addr.strip_prefix(UNIX_PREFIX).unwrap_or(tun_addr);
|
|
293
|
+
|
|
294
|
+
info!("Tunnel {id:?} Pipe forwarding to {tun_addr:?}");
|
|
295
|
+
tun.lock().await.fwd_pipe(tun_addr.to_string()).await
|
|
296
|
+
} else {
|
|
297
|
+
info!("Tunnel {id:?} TCP forwarding to {addr:?}");
|
|
298
|
+
tun.lock().await.fwd_tcp(addr).await
|
|
299
|
+
};
|
|
299
300
|
|
|
300
|
-
debug!("
|
|
301
|
+
debug!("forward returning");
|
|
301
302
|
canceled_is_ok(res)
|
|
302
303
|
}
|
|
303
304
|
|
|
@@ -320,15 +321,6 @@ fn canceled_is_ok(input: CoreResult<(), io::Error>) -> Result<()> {
|
|
|
320
321
|
}
|
|
321
322
|
}
|
|
322
323
|
|
|
323
|
-
/// Get url using the tunnel id
|
|
324
|
-
pub(crate) async fn get_url(id: &String) -> Option<String> {
|
|
325
|
-
GLOBAL_TUNNELS
|
|
326
|
-
.lock()
|
|
327
|
-
.await
|
|
328
|
-
.get(id)
|
|
329
|
-
.and_then(|s| s.url.clone())
|
|
330
|
-
}
|
|
331
|
-
|
|
332
324
|
async fn get_storage_by_id(id: &String) -> Result<Arc<Storage>> {
|
|
333
325
|
// we must clone the Arc before any locking so there is a local reference
|
|
334
326
|
// to the mutex to unlock if the tunnel wrapper struct is dropped.
|
|
@@ -351,15 +343,13 @@ pub(crate) async fn close_url(url: Option<String>) -> Result<()> {
|
|
|
351
343
|
let tunnels = GLOBAL_TUNNELS.lock().await;
|
|
352
344
|
for (id, storage) in tunnels.iter() {
|
|
353
345
|
debug!("tunnel: {}", id);
|
|
354
|
-
if url.as_ref().is_none() || url == storage.url {
|
|
346
|
+
if url.as_ref().is_none() || url == storage.tun_meta.url {
|
|
355
347
|
debug!("closing tunnel: {}", id);
|
|
356
348
|
storage
|
|
357
349
|
.session
|
|
358
|
-
.lock()
|
|
359
|
-
.await
|
|
360
350
|
.close_tunnel(id)
|
|
361
351
|
.await
|
|
362
|
-
.map_err(|e|
|
|
352
|
+
.map_err(|e| napi_ngrok_err("error closing tunnel", &e))?;
|
|
363
353
|
close_ids.push(id.clone());
|
|
364
354
|
}
|
|
365
355
|
}
|
|
@@ -371,3 +361,48 @@ pub(crate) async fn close_url(url: Option<String>) -> Result<()> {
|
|
|
371
361
|
}
|
|
372
362
|
Ok(())
|
|
373
363
|
}
|
|
364
|
+
|
|
365
|
+
/// Make a list of all tunnels by iterating over the global tunnel map and creating an NgrokTunnel from each.
|
|
366
|
+
pub(crate) async fn search_tunnels(
|
|
367
|
+
session_id: Option<String>,
|
|
368
|
+
url: Option<String>,
|
|
369
|
+
) -> Vec<NgrokTunnel> {
|
|
370
|
+
let mut tunnels: Vec<NgrokTunnel> = vec![];
|
|
371
|
+
for (_, storage) in GLOBAL_TUNNELS.lock().await.iter() {
|
|
372
|
+
// filter by session_id, if provided
|
|
373
|
+
if let Some(session_id) = session_id.as_ref() {
|
|
374
|
+
if session_id.ne(&storage.session.id()) {
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
// filter by url, if provided
|
|
379
|
+
if url.is_some() && url.ne(&storage.tun_meta.url) {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
// create a new NgrokTunnel from the storage
|
|
383
|
+
tunnels.push(NgrokTunnel::from_storage(storage));
|
|
384
|
+
}
|
|
385
|
+
tunnels
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/// Retrieve a list of non-closed tunnels, in no particular order.
|
|
389
|
+
#[napi]
|
|
390
|
+
pub async fn tunnels() -> Vec<NgrokTunnel> {
|
|
391
|
+
search_tunnels(None, None).await
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/// Retrieve tunnel using the id
|
|
395
|
+
#[napi]
|
|
396
|
+
pub async fn get_tunnel(id: String) -> Option<NgrokTunnel> {
|
|
397
|
+
GLOBAL_TUNNELS
|
|
398
|
+
.lock()
|
|
399
|
+
.await
|
|
400
|
+
.get(&id)
|
|
401
|
+
.map(NgrokTunnel::from_storage)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/// Retrieve tunnel using the url
|
|
405
|
+
#[napi]
|
|
406
|
+
pub async fn get_tunnel_by_url(url: String) -> Option<NgrokTunnel> {
|
|
407
|
+
search_tunnels(None, Some(url)).await.into_iter().next()
|
|
408
|
+
}
|
package/src/tunnel_builder.rs
CHANGED
|
@@ -20,7 +20,7 @@ use parking_lot::Mutex;
|
|
|
20
20
|
use tracing::debug;
|
|
21
21
|
|
|
22
22
|
use crate::{
|
|
23
|
-
|
|
23
|
+
napi_ngrok_err,
|
|
24
24
|
tunnel::{
|
|
25
25
|
NgrokHttpTunnel,
|
|
26
26
|
NgrokLabeledTunnel,
|
|
@@ -66,7 +66,7 @@ macro_rules! make_tunnel_builder {
|
|
|
66
66
|
let result = tun
|
|
67
67
|
.listen()
|
|
68
68
|
.await
|
|
69
|
-
.map_err(|e|
|
|
69
|
+
.map_err(|e| napi_ngrok_err("failed to start tunnel", &e));
|
|
70
70
|
|
|
71
71
|
// create the wrapping tunnel object via its async new()
|
|
72
72
|
match result {
|
package/trailer.js
CHANGED
|
@@ -5,6 +5,11 @@
|
|
|
5
5
|
const net = require("net");
|
|
6
6
|
const fs = require("fs");
|
|
7
7
|
const os = require("os");
|
|
8
|
+
const path = require("path");
|
|
9
|
+
|
|
10
|
+
// wrap connect with the code for extending exception with error code
|
|
11
|
+
NgrokSessionBuilder.prototype._connect = NgrokSessionBuilder.prototype.connect;
|
|
12
|
+
NgrokSessionBuilder.prototype.connect = ngrokSessionConnect;
|
|
8
13
|
|
|
9
14
|
// wrap listen with the bind code for passing to net.Server.listen()
|
|
10
15
|
NgrokHttpTunnelBuilder.prototype._listen = NgrokHttpTunnelBuilder.prototype.listen;
|
|
@@ -17,17 +22,42 @@ NgrokTcpTunnelBuilder.prototype.listen = ngrokBind;
|
|
|
17
22
|
NgrokTlsTunnelBuilder.prototype.listen = ngrokBind;
|
|
18
23
|
NgrokLabeledTunnelBuilder.prototype.listen = ngrokBind;
|
|
19
24
|
|
|
25
|
+
// Wrap session connect to fill in exception's errorCode
|
|
26
|
+
async function ngrokSessionConnect() {
|
|
27
|
+
try {
|
|
28
|
+
return await this._connect();
|
|
29
|
+
} catch (err) {
|
|
30
|
+
populateErrorCode(err);
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
20
35
|
// Begin listening for new connections on this tunnel,
|
|
21
36
|
// and bind to a local socket so this tunnel can be
|
|
22
37
|
// passed into net.Server.listen().
|
|
23
38
|
async function ngrokBind(bind) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
39
|
+
try {
|
|
40
|
+
const tunnel = await this._listen();
|
|
41
|
+
if (bind !== false) {
|
|
42
|
+
const socket = await randomTcpSocket();
|
|
43
|
+
tunnel.socket = socket;
|
|
44
|
+
defineTunnelHandle(tunnel, socket);
|
|
45
|
+
}
|
|
46
|
+
return tunnel;
|
|
47
|
+
} catch (err) {
|
|
48
|
+
populateErrorCode(err);
|
|
49
|
+
throw err;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function populateErrorCode(err) {
|
|
54
|
+
if (err.message) {
|
|
55
|
+
const regex = /error_code: (ERR_NGROK_\d+)$/;
|
|
56
|
+
const errorCode = err.message.match(regex);
|
|
57
|
+
if (errorCode && errorCode.length > 1) {
|
|
58
|
+
err.errorCode = errorCode[1];
|
|
59
|
+
}
|
|
29
60
|
}
|
|
30
|
-
return tunnel;
|
|
31
61
|
}
|
|
32
62
|
|
|
33
63
|
// add a 'handle' getter to the tunnel so it can be
|
|
@@ -38,7 +68,7 @@ function defineTunnelHandle(tunnel, socket) {
|
|
|
38
68
|
Object.defineProperty(tunnel, "handle", {
|
|
39
69
|
get: function () {
|
|
40
70
|
// turn on forwarding now that it has been requested
|
|
41
|
-
tunnel.
|
|
71
|
+
tunnel.forward("localhost:" + socket.address().port);
|
|
42
72
|
return socket._handle;
|
|
43
73
|
},
|
|
44
74
|
});
|
|
@@ -113,30 +143,56 @@ async function ngrokLinkTcp(tunnel, server) {
|
|
|
113
143
|
// random local port
|
|
114
144
|
const socket = await asyncListen(server, { host: "localhost", port: 0 });
|
|
115
145
|
// forward to socket
|
|
116
|
-
tunnel.
|
|
146
|
+
tunnel.forward("localhost:" + socket.address().port);
|
|
117
147
|
return socket;
|
|
118
148
|
}
|
|
119
149
|
|
|
120
|
-
|
|
150
|
+
function generatePipeFilename(tunnel, server) {
|
|
121
151
|
var proposed = "tun-" + tunnel.id() + ".sock";
|
|
152
|
+
|
|
153
|
+
// windows leaves little choice
|
|
122
154
|
if (platform == "win32") {
|
|
123
|
-
|
|
155
|
+
return "\\\\.\\pipe\\" + proposed;
|
|
124
156
|
}
|
|
125
|
-
|
|
157
|
+
|
|
158
|
+
// try to make a directory in the current working directory
|
|
159
|
+
const dir = ".ngrok";
|
|
126
160
|
try {
|
|
127
|
-
fs.
|
|
128
|
-
filename = proposed;
|
|
161
|
+
fs.mkdirSync(dir);
|
|
129
162
|
} catch (err) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
163
|
+
// move on
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
fs.accessSync(dir, fs.constants.W_OK);
|
|
167
|
+
return dir + path.sep + proposed;
|
|
168
|
+
} catch (err) {
|
|
169
|
+
// move on
|
|
134
170
|
}
|
|
135
171
|
|
|
136
|
-
|
|
137
|
-
|
|
172
|
+
// try the OS temp directory, being careful not to exceed the maximum path length for unix sockets
|
|
173
|
+
// https://linux.die.net/man/7/unix
|
|
174
|
+
// https://unix.stackexchange.com/a/367012
|
|
175
|
+
if (os.tmpdir().length < 90) {
|
|
176
|
+
try {
|
|
177
|
+
fs.accessSync(os.tmpdir(), fs.constants.W_OK);
|
|
178
|
+
filepath = os.tmpdir() + path.sep + proposed;
|
|
179
|
+
if (filepath.length > 100) {
|
|
180
|
+
// truncate
|
|
181
|
+
filepath = filepath.substring(0, 100);
|
|
182
|
+
}
|
|
183
|
+
return filepath;
|
|
184
|
+
} catch (err) {
|
|
185
|
+
// move on
|
|
186
|
+
}
|
|
138
187
|
}
|
|
139
188
|
|
|
189
|
+
// fallback to current working directory. allow any exception to propagate
|
|
190
|
+
fs.accessSync(process.cwd(), fs.constants.W_OK);
|
|
191
|
+
return proposed;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function ngrokLinkPipe(tunnel, server) {
|
|
195
|
+
var filename = generatePipeFilename(tunnel);
|
|
140
196
|
// begin listening
|
|
141
197
|
const socket = await asyncListen(server, { path: filename });
|
|
142
198
|
// tighten permissions
|
|
@@ -148,7 +204,8 @@ async function ngrokLinkPipe(tunnel, server) {
|
|
|
148
204
|
console.debug("Cannot change permissions of file: " + filename);
|
|
149
205
|
}
|
|
150
206
|
// forward tunnel
|
|
151
|
-
|
|
207
|
+
const proto = platform == "win32" ? "pipe:" : "unix:";
|
|
208
|
+
tunnel.forward(proto + filename);
|
|
152
209
|
socket.path = filename; // surface to caller
|
|
153
210
|
|
|
154
211
|
return socket;
|
|
@@ -188,9 +245,10 @@ function consoleLog(level) {
|
|
|
188
245
|
// wrap connect with code to vectorize and split out functions
|
|
189
246
|
const _connect = connect;
|
|
190
247
|
async function ngrokConnect(config) {
|
|
248
|
+
if (config == undefined) config = 80;
|
|
191
249
|
if (Number.isInteger(config) || typeof config === "string" || config instanceof String) {
|
|
192
250
|
address = String(config);
|
|
193
|
-
if (!address.includes(":")) {
|
|
251
|
+
if (Number.isInteger(config) && !address.includes(":")) {
|
|
194
252
|
address = `localhost:${address}`;
|
|
195
253
|
}
|
|
196
254
|
config = { addr: address };
|
|
@@ -264,7 +322,12 @@ async function ngrokConnect(config) {
|
|
|
264
322
|
config["onStatusChange"] = true;
|
|
265
323
|
}
|
|
266
324
|
// call into rust
|
|
267
|
-
|
|
325
|
+
try {
|
|
326
|
+
return await _connect(config, on_log_event, on_connection, on_disconnection);
|
|
327
|
+
} catch (err) {
|
|
328
|
+
populateErrorCode(err);
|
|
329
|
+
throw err;
|
|
330
|
+
}
|
|
268
331
|
}
|
|
269
332
|
|
|
270
333
|
function undot(config, dotKey) {
|