@ngrok/ngrok 0.5.1 → 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/.gitattributes +3 -0
- package/CHANGELOG.md +39 -27
- 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 +32 -29
- package/docs/classes/NgrokLabeledTunnelBuilder.html +9 -6
- package/docs/classes/NgrokSession.html +24 -9
- package/docs/classes/NgrokSessionBuilder.html +21 -18
- package/docs/classes/NgrokTcpTunnelBuilder.html +13 -10
- package/docs/classes/NgrokTlsTunnelBuilder.html +15 -12
- package/docs/classes/NgrokTunnel.html +25 -39
- 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 +6 -3
- 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 +11 -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 +15 -15
- 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 +164 -107
- package/src/tunnel_builder.rs +2 -2
- package/trailer.js +85 -22
- package/tsconfig.json +4 -2
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
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
use core::result::Result as CoreResult;
|
|
1
2
|
use std::{
|
|
2
3
|
collections::HashMap,
|
|
4
|
+
error::Error,
|
|
3
5
|
io,
|
|
4
6
|
sync::Arc,
|
|
5
7
|
};
|
|
@@ -10,6 +12,7 @@ use napi::bindgen_prelude::*;
|
|
|
10
12
|
use napi_derive::napi;
|
|
11
13
|
use ngrok::{
|
|
12
14
|
prelude::*,
|
|
15
|
+
session::ConnectError,
|
|
13
16
|
tunnel::{
|
|
14
17
|
HttpTunnel,
|
|
15
18
|
LabeledTunnel,
|
|
@@ -26,7 +29,13 @@ use tracing::{
|
|
|
26
29
|
info,
|
|
27
30
|
};
|
|
28
31
|
|
|
29
|
-
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:";
|
|
30
39
|
|
|
31
40
|
lazy_static! {
|
|
32
41
|
// tunnel references to be kept until explicit close to prevent nodejs gc from dropping them.
|
|
@@ -38,16 +47,25 @@ lazy_static! {
|
|
|
38
47
|
/// Stores the tunnel and session references to be kept until explicit close.
|
|
39
48
|
struct Storage {
|
|
40
49
|
tunnel: Arc<Mutex<dyn ExtendedTunnel>>,
|
|
41
|
-
session:
|
|
50
|
+
session: Session,
|
|
51
|
+
tun_meta: Arc<TunnelMetadata>,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
struct TunnelMetadata {
|
|
55
|
+
id: String,
|
|
56
|
+
forwards_to: String,
|
|
57
|
+
metadata: String,
|
|
42
58
|
url: Option<String>,
|
|
59
|
+
proto: Option<String>,
|
|
60
|
+
labels: HashMap<String, String>,
|
|
43
61
|
}
|
|
44
62
|
|
|
45
63
|
/// The TunnelExt cannot be turned into an object since it contains generics, so implementing
|
|
46
64
|
/// a proxy trait without generics which can be the dyn type stored in the global map.
|
|
47
65
|
#[async_trait]
|
|
48
66
|
pub trait ExtendedTunnel: Tunnel {
|
|
49
|
-
async fn fwd_tcp(&mut self, addr: String) ->
|
|
50
|
-
async fn fwd_pipe(&mut self, addr: String) ->
|
|
67
|
+
async fn fwd_tcp(&mut self, addr: String) -> CoreResult<(), io::Error>;
|
|
68
|
+
async fn fwd_pipe(&mut self, addr: String) -> CoreResult<(), io::Error>;
|
|
51
69
|
}
|
|
52
70
|
|
|
53
71
|
/// An ngrok tunnel.
|
|
@@ -55,20 +73,14 @@ pub trait ExtendedTunnel: Tunnel {
|
|
|
55
73
|
/// @group Tunnel and Sessions
|
|
56
74
|
#[napi(custom_finalize)]
|
|
57
75
|
#[allow(dead_code)]
|
|
58
|
-
pub
|
|
59
|
-
id: String,
|
|
60
|
-
forwards_to: String,
|
|
61
|
-
metadata: String,
|
|
62
|
-
url: Option<String>,
|
|
63
|
-
proto: Option<String>,
|
|
76
|
+
pub struct NgrokTunnel {
|
|
64
77
|
session: Session,
|
|
65
|
-
|
|
78
|
+
tun_meta: Arc<TunnelMetadata>,
|
|
66
79
|
}
|
|
67
80
|
|
|
68
81
|
macro_rules! make_tunnel_type {
|
|
69
82
|
// the common (non-labeled) branch
|
|
70
83
|
($(#[$outer:meta])* $wrapper:ident, $tunnel:tt, common) => {
|
|
71
|
-
|
|
72
84
|
$(#[$outer])*
|
|
73
85
|
#[allow(dead_code)]
|
|
74
86
|
pub(crate) struct $wrapper {
|
|
@@ -79,27 +91,24 @@ macro_rules! make_tunnel_type {
|
|
|
79
91
|
impl $wrapper {
|
|
80
92
|
pub(crate) async fn new_tunnel(session: Session, raw_tunnel: $tunnel) -> NgrokTunnel {
|
|
81
93
|
let id = raw_tunnel.id().to_string();
|
|
82
|
-
let
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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());
|
|
87
103
|
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
88
|
-
|
|
104
|
+
let storage = Arc::new(Storage {
|
|
89
105
|
tunnel: Arc::new(Mutex::new(raw_tunnel)),
|
|
90
|
-
session: Arc::new(Mutex::new(session.clone())),
|
|
91
|
-
url: Some(url.clone()),
|
|
92
|
-
}));
|
|
93
|
-
// create the user-facing object
|
|
94
|
-
NgrokTunnel {
|
|
95
|
-
id,
|
|
96
|
-
forwards_to,
|
|
97
|
-
metadata,
|
|
98
|
-
url: Some(url),
|
|
99
|
-
proto: Some(proto),
|
|
100
106
|
session,
|
|
101
|
-
|
|
102
|
-
}
|
|
107
|
+
tun_meta,
|
|
108
|
+
});
|
|
109
|
+
GLOBAL_TUNNELS.lock().await.insert(id, storage.clone());
|
|
110
|
+
// create the user-facing object
|
|
111
|
+
NgrokTunnel::from_storage(&storage)
|
|
103
112
|
}
|
|
104
113
|
}
|
|
105
114
|
|
|
@@ -117,26 +126,24 @@ macro_rules! make_tunnel_type {
|
|
|
117
126
|
impl $wrapper {
|
|
118
127
|
pub(crate) async fn new_tunnel(session: Session, raw_tunnel: $tunnel) -> NgrokTunnel {
|
|
119
128
|
let id = raw_tunnel.id().to_string();
|
|
120
|
-
let
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
125
|
-
GLOBAL_TUNNELS.lock().await.insert(id.clone(), Arc::new(Storage {
|
|
126
|
-
tunnel: Arc::new(Mutex::new(raw_tunnel)),
|
|
127
|
-
session: Arc::new(Mutex::new(session.clone())),
|
|
128
|
-
url: None,
|
|
129
|
-
}));
|
|
130
|
-
// create the user-facing object
|
|
131
|
-
NgrokTunnel {
|
|
132
|
-
id,
|
|
133
|
-
forwards_to,
|
|
134
|
-
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(),
|
|
135
133
|
url: None,
|
|
136
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)),
|
|
137
141
|
session,
|
|
138
|
-
|
|
139
|
-
}
|
|
142
|
+
tun_meta,
|
|
143
|
+
});
|
|
144
|
+
GLOBAL_TUNNELS.lock().await.insert(id, storage.clone());
|
|
145
|
+
// create the user-facing object
|
|
146
|
+
NgrokTunnel::from_storage(&storage)
|
|
140
147
|
}
|
|
141
148
|
}
|
|
142
149
|
|
|
@@ -147,10 +154,10 @@ macro_rules! make_tunnel_type {
|
|
|
147
154
|
($wrapper:ident, $tunnel:tt) => {
|
|
148
155
|
#[async_trait]
|
|
149
156
|
impl ExtendedTunnel for $tunnel {
|
|
150
|
-
async fn fwd_tcp(&mut self, addr: String) ->
|
|
157
|
+
async fn fwd_tcp(&mut self, addr: String) -> CoreResult<(), io::Error> {
|
|
151
158
|
self.forward_tcp(addr).await
|
|
152
159
|
}
|
|
153
|
-
async fn fwd_pipe(&mut self, addr: String) ->
|
|
160
|
+
async fn fwd_pipe(&mut self, addr: String) -> CoreResult<(), io::Error> {
|
|
154
161
|
self.forward_pipe(addr).await
|
|
155
162
|
}
|
|
156
163
|
}
|
|
@@ -160,28 +167,37 @@ macro_rules! make_tunnel_type {
|
|
|
160
167
|
#[napi]
|
|
161
168
|
#[allow(dead_code)]
|
|
162
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
|
+
|
|
163
179
|
/// The URL that this tunnel backs.
|
|
164
180
|
#[napi]
|
|
165
181
|
pub fn url(&self) -> Option<String> {
|
|
166
|
-
self.url.clone()
|
|
182
|
+
self.tun_meta.url.clone()
|
|
167
183
|
}
|
|
168
184
|
|
|
169
185
|
/// The protocol of the endpoint that this tunnel backs.
|
|
170
186
|
#[napi]
|
|
171
187
|
pub fn proto(&self) -> Option<String> {
|
|
172
|
-
self.proto.clone()
|
|
188
|
+
self.tun_meta.proto.clone()
|
|
173
189
|
}
|
|
174
190
|
|
|
175
191
|
/// The labels this tunnel was started with.
|
|
176
192
|
#[napi]
|
|
177
193
|
pub fn labels(&self) -> HashMap<String, String> {
|
|
178
|
-
self.labels.clone()
|
|
194
|
+
self.tun_meta.labels.clone()
|
|
179
195
|
}
|
|
180
196
|
|
|
181
197
|
/// Returns a tunnel's unique ID.
|
|
182
198
|
#[napi]
|
|
183
199
|
pub fn id(&self) -> String {
|
|
184
|
-
self.id.clone()
|
|
200
|
+
self.tun_meta.id.clone()
|
|
185
201
|
}
|
|
186
202
|
|
|
187
203
|
/// Returns a human-readable string presented in the ngrok dashboard
|
|
@@ -190,27 +206,21 @@ impl NgrokTunnel {
|
|
|
190
206
|
/// explicitly.
|
|
191
207
|
#[napi]
|
|
192
208
|
pub fn forwards_to(&self) -> String {
|
|
193
|
-
self.forwards_to.clone()
|
|
209
|
+
self.tun_meta.forwards_to.clone()
|
|
194
210
|
}
|
|
195
211
|
|
|
196
212
|
/// Returns the arbitrary metadata string for this tunnel.
|
|
197
213
|
#[napi]
|
|
198
214
|
pub fn metadata(&self) -> String {
|
|
199
|
-
self.metadata.clone()
|
|
215
|
+
self.tun_meta.metadata.clone()
|
|
200
216
|
}
|
|
201
217
|
|
|
202
|
-
/// Forward incoming tunnel connections
|
|
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
|
|
203
221
|
#[napi]
|
|
204
|
-
pub async fn
|
|
205
|
-
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/// Forward incoming tunnel connections to the provided file socket path.
|
|
209
|
-
/// On Linux/Darwin addr can be a unix domain socket path, e.g. "/tmp/ngrok.sock"
|
|
210
|
-
/// On Windows addr can be a named pipe, e.e. "\\.\pipe\an_ngrok_pipe"
|
|
211
|
-
#[napi]
|
|
212
|
-
pub async fn forward_pipe(&self, addr: String) -> Result<()> {
|
|
213
|
-
forward_pipe(&self.id, addr).await
|
|
222
|
+
pub async fn forward(&self, addr: String) -> Result<()> {
|
|
223
|
+
forward(&self.tun_meta.id, addr).await
|
|
214
224
|
}
|
|
215
225
|
|
|
216
226
|
/// Close the tunnel.
|
|
@@ -220,18 +230,18 @@ impl NgrokTunnel {
|
|
|
220
230
|
/// tunnel's ID.
|
|
221
231
|
#[napi]
|
|
222
232
|
pub async fn close(&self) -> Result<()> {
|
|
223
|
-
debug!("NgrokTunnel closing, id: {}", self.id);
|
|
233
|
+
debug!("NgrokTunnel closing, id: {}", self.tun_meta.id);
|
|
224
234
|
|
|
225
235
|
// we may not be able to lock our reference to the tunnel due to the forward_* calls which
|
|
226
236
|
// continuously accept-loop while the tunnel is active, so calling close on the Session.
|
|
227
237
|
let res = self
|
|
228
238
|
.session
|
|
229
|
-
.close_tunnel(self.id.clone())
|
|
239
|
+
.close_tunnel(self.tun_meta.id.clone())
|
|
230
240
|
.await
|
|
231
|
-
.map_err(|e|
|
|
241
|
+
.map_err(|e| napi_ngrok_err("error closing tunnel", &e));
|
|
232
242
|
|
|
233
243
|
// drop our internal reference to the tunnel after awaiting close
|
|
234
|
-
GLOBAL_TUNNELS.lock().await.remove(&self.id);
|
|
244
|
+
GLOBAL_TUNNELS.lock().await.remove(&self.tun_meta.id);
|
|
235
245
|
|
|
236
246
|
res
|
|
237
247
|
}
|
|
@@ -240,7 +250,7 @@ impl NgrokTunnel {
|
|
|
240
250
|
#[allow(unused_mut)]
|
|
241
251
|
impl ObjectFinalize for NgrokTunnel {
|
|
242
252
|
fn finalize(mut self, _env: Env) -> Result<()> {
|
|
243
|
-
debug!("NgrokTunnel finalize, id: {}", self.id);
|
|
253
|
+
debug!("NgrokTunnel finalize, id: {}", self.tun_meta.id);
|
|
244
254
|
Ok(())
|
|
245
255
|
}
|
|
246
256
|
}
|
|
@@ -270,41 +280,45 @@ make_tunnel_type! {
|
|
|
270
280
|
NgrokLabeledTunnel, LabeledTunnel, label
|
|
271
281
|
}
|
|
272
282
|
|
|
273
|
-
pub async fn
|
|
274
|
-
|
|
275
|
-
let
|
|
276
|
-
.
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
.
|
|
280
|
-
|
|
281
|
-
.
|
|
282
|
-
.
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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
|
+
};
|
|
286
300
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
let res = get_storage_by_id(id)
|
|
290
|
-
.await?
|
|
291
|
-
.tunnel
|
|
292
|
-
.lock()
|
|
293
|
-
.await
|
|
294
|
-
.fwd_pipe(addr)
|
|
295
|
-
.await
|
|
296
|
-
.map_err(|e| napi_err(format!("cannot forward pipe: {e:?}")));
|
|
297
|
-
debug!("forward_pipe returning");
|
|
298
|
-
res
|
|
301
|
+
debug!("forward returning");
|
|
302
|
+
canceled_is_ok(res)
|
|
299
303
|
}
|
|
300
304
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
305
|
+
fn canceled_is_ok(input: CoreResult<(), io::Error>) -> Result<()> {
|
|
306
|
+
match input {
|
|
307
|
+
Ok(_) => Ok(()),
|
|
308
|
+
Err(e) => {
|
|
309
|
+
if let Some(source) = e
|
|
310
|
+
.source()
|
|
311
|
+
.and_then(|s| s.downcast_ref::<Arc<ConnectError>>())
|
|
312
|
+
{
|
|
313
|
+
if let ConnectError::Canceled = **source {
|
|
314
|
+
debug!("Reconnect was canceled, session is closing, returning Ok");
|
|
315
|
+
return Ok(());
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
Err(napi_err(format!("error forwarding: {e:?}")))
|
|
320
|
+
}
|
|
321
|
+
}
|
|
308
322
|
}
|
|
309
323
|
|
|
310
324
|
async fn get_storage_by_id(id: &String) -> Result<Arc<Storage>> {
|
|
@@ -329,15 +343,13 @@ pub(crate) async fn close_url(url: Option<String>) -> Result<()> {
|
|
|
329
343
|
let tunnels = GLOBAL_TUNNELS.lock().await;
|
|
330
344
|
for (id, storage) in tunnels.iter() {
|
|
331
345
|
debug!("tunnel: {}", id);
|
|
332
|
-
if url.as_ref().is_none() || url == storage.url {
|
|
346
|
+
if url.as_ref().is_none() || url == storage.tun_meta.url {
|
|
333
347
|
debug!("closing tunnel: {}", id);
|
|
334
348
|
storage
|
|
335
349
|
.session
|
|
336
|
-
.lock()
|
|
337
|
-
.await
|
|
338
350
|
.close_tunnel(id)
|
|
339
351
|
.await
|
|
340
|
-
.map_err(|e|
|
|
352
|
+
.map_err(|e| napi_ngrok_err("error closing tunnel", &e))?;
|
|
341
353
|
close_ids.push(id.clone());
|
|
342
354
|
}
|
|
343
355
|
}
|
|
@@ -349,3 +361,48 @@ pub(crate) async fn close_url(url: Option<String>) -> Result<()> {
|
|
|
349
361
|
}
|
|
350
362
|
Ok(())
|
|
351
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) {
|