@ngrok/ngrok 0.5.2 → 0.7.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 +41 -28
- package/CODE_OF_CONDUCT.md +45 -0
- package/Cargo.toml +8 -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 +59 -22
- package/docs/classes/NgrokLabeledTunnelBuilder.html +42 -5
- package/docs/classes/NgrokSession.html +24 -9
- package/docs/classes/NgrokSessionBuilder.html +20 -17
- package/docs/classes/NgrokTcpTunnelBuilder.html +46 -9
- package/docs/classes/NgrokTlsTunnelBuilder.html +48 -11
- package/docs/classes/NgrokTunnel.html +33 -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 +30 -6
- package/index.js +100 -22
- 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 +17 -9
- package/src/http.rs +20 -22
- package/src/lib.rs +21 -0
- package/src/session.rs +61 -50
- package/src/tcp.rs +1 -1
- package/src/tls.rs +3 -5
- package/src/tunnel.rs +232 -110
- package/src/tunnel_builder.rs +34 -7
- package/trailer.js +96 -21
package/src/tunnel.rs
CHANGED
|
@@ -7,29 +7,41 @@ use std::{
|
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
use async_trait::async_trait;
|
|
10
|
+
use futures::prelude::*;
|
|
10
11
|
use lazy_static::lazy_static;
|
|
11
12
|
use napi::bindgen_prelude::*;
|
|
12
13
|
use napi_derive::napi;
|
|
13
14
|
use ngrok::{
|
|
15
|
+
forwarder::Forwarder,
|
|
14
16
|
prelude::*,
|
|
15
17
|
session::ConnectError,
|
|
16
18
|
tunnel::{
|
|
17
19
|
HttpTunnel,
|
|
18
20
|
LabeledTunnel,
|
|
19
|
-
ProtoTunnel,
|
|
20
21
|
TcpTunnel,
|
|
21
22
|
TlsTunnel,
|
|
22
|
-
UrlTunnel,
|
|
23
23
|
},
|
|
24
24
|
Session,
|
|
25
25
|
};
|
|
26
|
-
use
|
|
26
|
+
use regex::Regex;
|
|
27
|
+
use tokio::{
|
|
28
|
+
sync::Mutex,
|
|
29
|
+
task::JoinHandle,
|
|
30
|
+
};
|
|
27
31
|
use tracing::{
|
|
28
32
|
debug,
|
|
29
33
|
info,
|
|
30
34
|
};
|
|
35
|
+
use url::Url;
|
|
36
|
+
|
|
37
|
+
use crate::{
|
|
38
|
+
napi_err,
|
|
39
|
+
napi_ngrok_err,
|
|
40
|
+
};
|
|
31
41
|
|
|
32
|
-
|
|
42
|
+
// no forward host section to allow for relative unix paths
|
|
43
|
+
pub(crate) const UNIX_PREFIX: &str = "unix:";
|
|
44
|
+
pub(crate) const TCP_PREFIX: &str = "tcp://";
|
|
33
45
|
|
|
34
46
|
lazy_static! {
|
|
35
47
|
// tunnel references to be kept until explicit close to prevent nodejs gc from dropping them.
|
|
@@ -40,17 +52,30 @@ lazy_static! {
|
|
|
40
52
|
|
|
41
53
|
/// Stores the tunnel and session references to be kept until explicit close.
|
|
42
54
|
struct Storage {
|
|
43
|
-
tunnel: Arc<Mutex<dyn ExtendedTunnel
|
|
44
|
-
|
|
55
|
+
tunnel: Option<Arc<Mutex<dyn ExtendedTunnel>>>,
|
|
56
|
+
forwarder: Option<Arc<Mutex<dyn ExtendedForwarder>>>,
|
|
57
|
+
session: Session,
|
|
58
|
+
tun_meta: Arc<TunnelMetadata>,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
struct TunnelMetadata {
|
|
62
|
+
id: String,
|
|
63
|
+
forwards_to: String,
|
|
64
|
+
metadata: String,
|
|
45
65
|
url: Option<String>,
|
|
66
|
+
proto: Option<String>,
|
|
67
|
+
labels: HashMap<String, String>,
|
|
46
68
|
}
|
|
47
69
|
|
|
48
70
|
/// The TunnelExt cannot be turned into an object since it contains generics, so implementing
|
|
49
71
|
/// a proxy trait without generics which can be the dyn type stored in the global map.
|
|
50
72
|
#[async_trait]
|
|
51
|
-
pub trait ExtendedTunnel:
|
|
52
|
-
async fn
|
|
53
|
-
|
|
73
|
+
pub trait ExtendedTunnel: Send {
|
|
74
|
+
async fn fwd(&mut self, url: Url) -> CoreResult<(), io::Error>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
pub trait ExtendedForwarder: Send {
|
|
78
|
+
fn get_join(&mut self) -> &mut JoinHandle<CoreResult<(), io::Error>>;
|
|
54
79
|
}
|
|
55
80
|
|
|
56
81
|
/// An ngrok tunnel.
|
|
@@ -58,20 +83,14 @@ pub trait ExtendedTunnel: Tunnel {
|
|
|
58
83
|
/// @group Tunnel and Sessions
|
|
59
84
|
#[napi(custom_finalize)]
|
|
60
85
|
#[allow(dead_code)]
|
|
61
|
-
pub
|
|
62
|
-
id: String,
|
|
63
|
-
forwards_to: String,
|
|
64
|
-
metadata: String,
|
|
65
|
-
url: Option<String>,
|
|
66
|
-
proto: Option<String>,
|
|
86
|
+
pub struct NgrokTunnel {
|
|
67
87
|
session: Session,
|
|
68
|
-
|
|
88
|
+
tun_meta: Arc<TunnelMetadata>,
|
|
69
89
|
}
|
|
70
90
|
|
|
71
91
|
macro_rules! make_tunnel_type {
|
|
72
92
|
// the common (non-labeled) branch
|
|
73
93
|
($(#[$outer:meta])* $wrapper:ident, $tunnel:tt, common) => {
|
|
74
|
-
|
|
75
94
|
$(#[$outer])*
|
|
76
95
|
#[allow(dead_code)]
|
|
77
96
|
pub(crate) struct $wrapper {
|
|
@@ -82,27 +101,48 @@ macro_rules! make_tunnel_type {
|
|
|
82
101
|
impl $wrapper {
|
|
83
102
|
pub(crate) async fn new_tunnel(session: Session, raw_tunnel: $tunnel) -> NgrokTunnel {
|
|
84
103
|
let id = raw_tunnel.id().to_string();
|
|
85
|
-
let
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
104
|
+
let tun_meta = Arc::new(TunnelMetadata {
|
|
105
|
+
id: id.clone(),
|
|
106
|
+
forwards_to: raw_tunnel.forwards_to().to_string(),
|
|
107
|
+
metadata: raw_tunnel.metadata().to_string(),
|
|
108
|
+
url: Some(raw_tunnel.url().to_string()),
|
|
109
|
+
proto: Some(raw_tunnel.proto().to_string()),
|
|
110
|
+
labels: HashMap::new(),
|
|
111
|
+
});
|
|
112
|
+
info!("Created tunnel {id:?} with url {:?}", raw_tunnel.url());
|
|
90
113
|
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
91
|
-
|
|
92
|
-
tunnel: Arc::new(Mutex::new(raw_tunnel)),
|
|
93
|
-
|
|
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),
|
|
114
|
+
let storage = Arc::new(Storage {
|
|
115
|
+
tunnel: Some(Arc::new(Mutex::new(raw_tunnel))),
|
|
116
|
+
forwarder: None,
|
|
103
117
|
session,
|
|
118
|
+
tun_meta,
|
|
119
|
+
});
|
|
120
|
+
GLOBAL_TUNNELS.lock().await.insert(id, storage.clone());
|
|
121
|
+
// create the user-facing object
|
|
122
|
+
NgrokTunnel::from_storage(&storage)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
pub(crate) async fn new_forwarder(session: Session, forwarder: Forwarder<$tunnel>) -> NgrokTunnel {
|
|
126
|
+
let id = forwarder.id().to_string();
|
|
127
|
+
let tun_meta = Arc::new(TunnelMetadata {
|
|
128
|
+
id: id.clone(),
|
|
129
|
+
forwards_to: forwarder.forwards_to().to_string(),
|
|
130
|
+
metadata: forwarder.metadata().to_string(),
|
|
131
|
+
url: Some(forwarder.url().to_string()),
|
|
132
|
+
proto: Some(forwarder.proto().to_string()),
|
|
104
133
|
labels: HashMap::new(),
|
|
105
|
-
}
|
|
134
|
+
});
|
|
135
|
+
info!("Created tunnel {id:?} with url {:?}", forwarder.url());
|
|
136
|
+
// keep a tunnel reference until an explicit call to close to prevent python gc dropping it
|
|
137
|
+
let storage = Arc::new(Storage {
|
|
138
|
+
tunnel: None,
|
|
139
|
+
forwarder: Some(Arc::new(Mutex::new(forwarder))),
|
|
140
|
+
session,
|
|
141
|
+
tun_meta,
|
|
142
|
+
});
|
|
143
|
+
GLOBAL_TUNNELS.lock().await.insert(id, storage.clone());
|
|
144
|
+
// create the user-facing object
|
|
145
|
+
NgrokTunnel::from_storage(&storage)
|
|
106
146
|
}
|
|
107
147
|
}
|
|
108
148
|
|
|
@@ -120,26 +160,48 @@ macro_rules! make_tunnel_type {
|
|
|
120
160
|
impl $wrapper {
|
|
121
161
|
pub(crate) async fn new_tunnel(session: Session, raw_tunnel: $tunnel) -> NgrokTunnel {
|
|
122
162
|
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())),
|
|
163
|
+
let tun_meta = Arc::new(TunnelMetadata {
|
|
164
|
+
id: id.clone(),
|
|
165
|
+
forwards_to: raw_tunnel.forwards_to().to_string(),
|
|
166
|
+
metadata: raw_tunnel.metadata().to_string(),
|
|
131
167
|
url: None,
|
|
132
|
-
|
|
168
|
+
proto: None,
|
|
169
|
+
labels: raw_tunnel.labels().clone(),
|
|
170
|
+
});
|
|
171
|
+
info!("Created tunnel {id:?} with labels {:?}", tun_meta.labels);
|
|
172
|
+
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
173
|
+
let storage = Arc::new(Storage {
|
|
174
|
+
tunnel: Some(Arc::new(Mutex::new(raw_tunnel))),
|
|
175
|
+
forwarder: None,
|
|
176
|
+
session,
|
|
177
|
+
tun_meta,
|
|
178
|
+
});
|
|
179
|
+
GLOBAL_TUNNELS.lock().await.insert(id, storage.clone());
|
|
133
180
|
// create the user-facing object
|
|
134
|
-
NgrokTunnel
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
181
|
+
NgrokTunnel::from_storage(&storage)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
pub(crate) async fn new_forwarder(session: Session, forwarder: Forwarder<$tunnel>) -> NgrokTunnel {
|
|
185
|
+
let id = forwarder.id().to_string();
|
|
186
|
+
let tun_meta = Arc::new(TunnelMetadata {
|
|
187
|
+
id: id.clone(),
|
|
188
|
+
forwards_to: forwarder.forwards_to().to_string(),
|
|
189
|
+
metadata: forwarder.metadata().to_string(),
|
|
138
190
|
url: None,
|
|
139
191
|
proto: None,
|
|
192
|
+
labels: forwarder.labels().clone(),
|
|
193
|
+
});
|
|
194
|
+
info!("Created tunnel {id:?} with labels {:?}", tun_meta.labels);
|
|
195
|
+
// keep a tunnel reference until an explicit call to close to prevent python gc dropping it
|
|
196
|
+
let storage = Arc::new(Storage {
|
|
197
|
+
tunnel: None,
|
|
198
|
+
forwarder: Some(Arc::new(Mutex::new(forwarder))),
|
|
140
199
|
session,
|
|
141
|
-
|
|
142
|
-
}
|
|
200
|
+
tun_meta,
|
|
201
|
+
});
|
|
202
|
+
GLOBAL_TUNNELS.lock().await.insert(id, storage.clone());
|
|
203
|
+
// create the user-facing object
|
|
204
|
+
NgrokTunnel::from_storage(&storage)
|
|
143
205
|
}
|
|
144
206
|
}
|
|
145
207
|
|
|
@@ -150,11 +212,15 @@ macro_rules! make_tunnel_type {
|
|
|
150
212
|
($wrapper:ident, $tunnel:tt) => {
|
|
151
213
|
#[async_trait]
|
|
152
214
|
impl ExtendedTunnel for $tunnel {
|
|
153
|
-
|
|
154
|
-
|
|
215
|
+
#[allow(deprecated)]
|
|
216
|
+
async fn fwd(&mut self, url: Url) -> CoreResult<(), io::Error> {
|
|
217
|
+
ngrok::prelude::TunnelExt::forward(self, url).await
|
|
155
218
|
}
|
|
156
|
-
|
|
157
|
-
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
impl ExtendedForwarder for Forwarder<$tunnel> {
|
|
222
|
+
fn get_join(&mut self) -> &mut JoinHandle<CoreResult<(), io::Error>> {
|
|
223
|
+
self.join()
|
|
158
224
|
}
|
|
159
225
|
}
|
|
160
226
|
};
|
|
@@ -163,28 +229,37 @@ macro_rules! make_tunnel_type {
|
|
|
163
229
|
#[napi]
|
|
164
230
|
#[allow(dead_code)]
|
|
165
231
|
impl NgrokTunnel {
|
|
232
|
+
/// Create NgrokTunnel from Storage
|
|
233
|
+
fn from_storage(storage: &Arc<Storage>) -> NgrokTunnel {
|
|
234
|
+
// create the user-facing object
|
|
235
|
+
NgrokTunnel {
|
|
236
|
+
session: storage.session.clone(),
|
|
237
|
+
tun_meta: storage.tun_meta.clone(),
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
166
241
|
/// The URL that this tunnel backs.
|
|
167
242
|
#[napi]
|
|
168
243
|
pub fn url(&self) -> Option<String> {
|
|
169
|
-
self.url.clone()
|
|
244
|
+
self.tun_meta.url.clone()
|
|
170
245
|
}
|
|
171
246
|
|
|
172
247
|
/// The protocol of the endpoint that this tunnel backs.
|
|
173
248
|
#[napi]
|
|
174
249
|
pub fn proto(&self) -> Option<String> {
|
|
175
|
-
self.proto.clone()
|
|
250
|
+
self.tun_meta.proto.clone()
|
|
176
251
|
}
|
|
177
252
|
|
|
178
253
|
/// The labels this tunnel was started with.
|
|
179
254
|
#[napi]
|
|
180
255
|
pub fn labels(&self) -> HashMap<String, String> {
|
|
181
|
-
self.labels.clone()
|
|
256
|
+
self.tun_meta.labels.clone()
|
|
182
257
|
}
|
|
183
258
|
|
|
184
259
|
/// Returns a tunnel's unique ID.
|
|
185
260
|
#[napi]
|
|
186
261
|
pub fn id(&self) -> String {
|
|
187
|
-
self.id.clone()
|
|
262
|
+
self.tun_meta.id.clone()
|
|
188
263
|
}
|
|
189
264
|
|
|
190
265
|
/// Returns a human-readable string presented in the ngrok dashboard
|
|
@@ -193,27 +268,40 @@ impl NgrokTunnel {
|
|
|
193
268
|
/// explicitly.
|
|
194
269
|
#[napi]
|
|
195
270
|
pub fn forwards_to(&self) -> String {
|
|
196
|
-
self.forwards_to.clone()
|
|
271
|
+
self.tun_meta.forwards_to.clone()
|
|
197
272
|
}
|
|
198
273
|
|
|
199
274
|
/// Returns the arbitrary metadata string for this tunnel.
|
|
200
275
|
#[napi]
|
|
201
276
|
pub fn metadata(&self) -> String {
|
|
202
|
-
self.metadata.clone()
|
|
277
|
+
self.tun_meta.metadata.clone()
|
|
203
278
|
}
|
|
204
279
|
|
|
205
|
-
/// Forward incoming tunnel connections
|
|
280
|
+
/// Forward incoming tunnel connections. This can be either a TCP address or a file socket path.
|
|
281
|
+
/// For file socket paths on Linux/Darwin, addr can be a unix domain socket path, e.g. "/tmp/ngrok.sock"
|
|
282
|
+
/// On Windows, addr can be a named pipe, e.e. "\\\\.\\pipe\\an_ngrok_pipe
|
|
206
283
|
#[napi]
|
|
207
|
-
pub async fn
|
|
208
|
-
|
|
284
|
+
pub async fn forward(&self, addr: String) -> Result<()> {
|
|
285
|
+
forward(&self.tun_meta.id, addr).await
|
|
209
286
|
}
|
|
210
287
|
|
|
211
|
-
///
|
|
212
|
-
/// On Linux/Darwin addr can be a unix domain socket path, e.g. "/tmp/ngrok.sock"
|
|
213
|
-
/// On Windows addr can be a named pipe, e.e. "\\.\pipe\an_ngrok_pipe"
|
|
288
|
+
/// Wait for the forwarding task to exit.
|
|
214
289
|
#[napi]
|
|
215
|
-
pub async fn
|
|
216
|
-
|
|
290
|
+
pub async fn join(&self) -> Result<()> {
|
|
291
|
+
let id = self.tun_meta.id.clone();
|
|
292
|
+
let forwarder_option = &get_storage_by_id(&id).await?.forwarder;
|
|
293
|
+
if let Some(forwarder_mutex) = forwarder_option {
|
|
294
|
+
forwarder_mutex
|
|
295
|
+
.lock()
|
|
296
|
+
.await
|
|
297
|
+
.get_join()
|
|
298
|
+
.fuse()
|
|
299
|
+
.await
|
|
300
|
+
.map_err(|e| napi_err(format!("error on join: {e:?}")))?
|
|
301
|
+
.map_err(|e| napi_err(format!("error on join: {e:?}")))
|
|
302
|
+
} else {
|
|
303
|
+
Err(napi_err("Tunnel is not joinable"))
|
|
304
|
+
}
|
|
217
305
|
}
|
|
218
306
|
|
|
219
307
|
/// Close the tunnel.
|
|
@@ -223,18 +311,18 @@ impl NgrokTunnel {
|
|
|
223
311
|
/// tunnel's ID.
|
|
224
312
|
#[napi]
|
|
225
313
|
pub async fn close(&self) -> Result<()> {
|
|
226
|
-
debug!("NgrokTunnel closing, id: {}", self.id);
|
|
314
|
+
debug!("NgrokTunnel closing, id: {}", self.tun_meta.id);
|
|
227
315
|
|
|
228
316
|
// we may not be able to lock our reference to the tunnel due to the forward_* calls which
|
|
229
317
|
// continuously accept-loop while the tunnel is active, so calling close on the Session.
|
|
230
318
|
let res = self
|
|
231
319
|
.session
|
|
232
|
-
.close_tunnel(self.id.clone())
|
|
320
|
+
.close_tunnel(self.tun_meta.id.clone())
|
|
233
321
|
.await
|
|
234
|
-
.map_err(|e|
|
|
322
|
+
.map_err(|e| napi_ngrok_err("error closing tunnel", &e));
|
|
235
323
|
|
|
236
324
|
// drop our internal reference to the tunnel after awaiting close
|
|
237
|
-
GLOBAL_TUNNELS.lock().await.remove(&self.id);
|
|
325
|
+
GLOBAL_TUNNELS.lock().await.remove(&self.tun_meta.id);
|
|
238
326
|
|
|
239
327
|
res
|
|
240
328
|
}
|
|
@@ -243,7 +331,7 @@ impl NgrokTunnel {
|
|
|
243
331
|
#[allow(unused_mut)]
|
|
244
332
|
impl ObjectFinalize for NgrokTunnel {
|
|
245
333
|
fn finalize(mut self, _env: Env) -> Result<()> {
|
|
246
|
-
debug!("NgrokTunnel finalize, id: {}", self.id);
|
|
334
|
+
debug!("NgrokTunnel finalize, id: {}", self.tun_meta.id);
|
|
247
335
|
Ok(())
|
|
248
336
|
}
|
|
249
337
|
}
|
|
@@ -273,32 +361,32 @@ make_tunnel_type! {
|
|
|
273
361
|
NgrokLabeledTunnel, LabeledTunnel, label
|
|
274
362
|
}
|
|
275
363
|
|
|
276
|
-
pub async fn
|
|
277
|
-
|
|
278
|
-
let
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
.
|
|
284
|
-
|
|
364
|
+
pub async fn forward(id: &String, mut addr: String) -> Result<()> {
|
|
365
|
+
let tun_option = &get_storage_by_id(id).await?.tunnel;
|
|
366
|
+
if let Some(tun) = tun_option {
|
|
367
|
+
// if addr is not a full url, choose a default protocol
|
|
368
|
+
lazy_static! {
|
|
369
|
+
static ref RE: Regex = Regex::new(r"^[a-z0-9\-\.]+:\d+$").unwrap();
|
|
370
|
+
}
|
|
371
|
+
if !addr.contains(':') || RE.find(&addr).is_some() {
|
|
372
|
+
if addr.contains('/') {
|
|
373
|
+
addr = format!("{UNIX_PREFIX}{addr}")
|
|
374
|
+
} else {
|
|
375
|
+
addr = format!("{TCP_PREFIX}{addr}")
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
// parse to a url
|
|
379
|
+
let url = Url::parse(addr.as_str())
|
|
380
|
+
.map_err(|e| napi_err(format!("Cannot parse address: {addr}, error: {e}")))?;
|
|
285
381
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
382
|
+
info!("Tunnel {id:?} forwarding to {:?}", url.to_string());
|
|
383
|
+
let res = tun.lock().await.fwd(url).await;
|
|
289
384
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
.lock()
|
|
296
|
-
.await
|
|
297
|
-
.fwd_pipe(addr)
|
|
298
|
-
.await;
|
|
299
|
-
|
|
300
|
-
debug!("forward_pipe returning");
|
|
301
|
-
canceled_is_ok(res)
|
|
385
|
+
debug!("forward returning");
|
|
386
|
+
canceled_is_ok(res)
|
|
387
|
+
} else {
|
|
388
|
+
Err(napi_err("tunnel is not forwardable"))
|
|
389
|
+
}
|
|
302
390
|
}
|
|
303
391
|
|
|
304
392
|
fn canceled_is_ok(input: CoreResult<(), io::Error>) -> Result<()> {
|
|
@@ -320,15 +408,6 @@ fn canceled_is_ok(input: CoreResult<(), io::Error>) -> Result<()> {
|
|
|
320
408
|
}
|
|
321
409
|
}
|
|
322
410
|
|
|
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
411
|
async fn get_storage_by_id(id: &String) -> Result<Arc<Storage>> {
|
|
333
412
|
// we must clone the Arc before any locking so there is a local reference
|
|
334
413
|
// to the mutex to unlock if the tunnel wrapper struct is dropped.
|
|
@@ -351,15 +430,13 @@ pub(crate) async fn close_url(url: Option<String>) -> Result<()> {
|
|
|
351
430
|
let tunnels = GLOBAL_TUNNELS.lock().await;
|
|
352
431
|
for (id, storage) in tunnels.iter() {
|
|
353
432
|
debug!("tunnel: {}", id);
|
|
354
|
-
if url.as_ref().is_none() || url == storage.url {
|
|
433
|
+
if url.as_ref().is_none() || url == storage.tun_meta.url {
|
|
355
434
|
debug!("closing tunnel: {}", id);
|
|
356
435
|
storage
|
|
357
436
|
.session
|
|
358
|
-
.lock()
|
|
359
|
-
.await
|
|
360
437
|
.close_tunnel(id)
|
|
361
438
|
.await
|
|
362
|
-
.map_err(|e|
|
|
439
|
+
.map_err(|e| napi_ngrok_err("error closing tunnel", &e))?;
|
|
363
440
|
close_ids.push(id.clone());
|
|
364
441
|
}
|
|
365
442
|
}
|
|
@@ -371,3 +448,48 @@ pub(crate) async fn close_url(url: Option<String>) -> Result<()> {
|
|
|
371
448
|
}
|
|
372
449
|
Ok(())
|
|
373
450
|
}
|
|
451
|
+
|
|
452
|
+
/// Make a list of all tunnels by iterating over the global tunnel map and creating an NgrokTunnel from each.
|
|
453
|
+
pub(crate) async fn search_tunnels(
|
|
454
|
+
session_id: Option<String>,
|
|
455
|
+
url: Option<String>,
|
|
456
|
+
) -> Vec<NgrokTunnel> {
|
|
457
|
+
let mut tunnels: Vec<NgrokTunnel> = vec![];
|
|
458
|
+
for (_, storage) in GLOBAL_TUNNELS.lock().await.iter() {
|
|
459
|
+
// filter by session_id, if provided
|
|
460
|
+
if let Some(session_id) = session_id.as_ref() {
|
|
461
|
+
if session_id.ne(&storage.session.id()) {
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
// filter by url, if provided
|
|
466
|
+
if url.is_some() && url.ne(&storage.tun_meta.url) {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
// create a new NgrokTunnel from the storage
|
|
470
|
+
tunnels.push(NgrokTunnel::from_storage(storage));
|
|
471
|
+
}
|
|
472
|
+
tunnels
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/// Retrieve a list of non-closed tunnels, in no particular order.
|
|
476
|
+
#[napi]
|
|
477
|
+
pub async fn tunnels() -> Vec<NgrokTunnel> {
|
|
478
|
+
search_tunnels(None, None).await
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/// Retrieve tunnel using the id
|
|
482
|
+
#[napi]
|
|
483
|
+
pub async fn get_tunnel(id: String) -> Option<NgrokTunnel> {
|
|
484
|
+
GLOBAL_TUNNELS
|
|
485
|
+
.lock()
|
|
486
|
+
.await
|
|
487
|
+
.get(&id)
|
|
488
|
+
.map(NgrokTunnel::from_storage)
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/// Retrieve tunnel using the url
|
|
492
|
+
#[napi]
|
|
493
|
+
pub async fn get_tunnel_by_url(url: String) -> Option<NgrokTunnel> {
|
|
494
|
+
search_tunnels(None, Some(url)).await.into_iter().next()
|
|
495
|
+
}
|
package/src/tunnel_builder.rs
CHANGED
|
@@ -18,9 +18,11 @@ use ngrok::{
|
|
|
18
18
|
};
|
|
19
19
|
use parking_lot::Mutex;
|
|
20
20
|
use tracing::debug;
|
|
21
|
+
use url::Url;
|
|
21
22
|
|
|
22
23
|
use crate::{
|
|
23
24
|
napi_err,
|
|
25
|
+
napi_ngrok_err,
|
|
24
26
|
tunnel::{
|
|
25
27
|
NgrokHttpTunnel,
|
|
26
28
|
NgrokLabeledTunnel,
|
|
@@ -54,7 +56,7 @@ macro_rules! make_tunnel_builder {
|
|
|
54
56
|
#[napi]
|
|
55
57
|
pub fn metadata(&mut self, metadata: String) -> &Self {
|
|
56
58
|
let mut builder = self.tunnel_builder.lock();
|
|
57
|
-
|
|
59
|
+
builder.metadata(metadata);
|
|
58
60
|
self
|
|
59
61
|
}
|
|
60
62
|
|
|
@@ -66,7 +68,7 @@ macro_rules! make_tunnel_builder {
|
|
|
66
68
|
let result = tun
|
|
67
69
|
.listen()
|
|
68
70
|
.await
|
|
69
|
-
.map_err(|e|
|
|
71
|
+
.map_err(|e| napi_ngrok_err("failed to start tunnel", &e));
|
|
70
72
|
|
|
71
73
|
// create the wrapping tunnel object via its async new()
|
|
72
74
|
match result {
|
|
@@ -74,6 +76,31 @@ macro_rules! make_tunnel_builder {
|
|
|
74
76
|
Err(val) => Err(val),
|
|
75
77
|
}
|
|
76
78
|
}
|
|
79
|
+
|
|
80
|
+
/// Begin listening for new connections on this tunnel and forwarding them to the given url.
|
|
81
|
+
#[napi]
|
|
82
|
+
pub async fn listen_and_forward(&self, to_url: String) -> Result<NgrokTunnel> {
|
|
83
|
+
let url = Url::parse(&to_url).map_err(|e| napi_err(format!("Url forward argument parse failure, {e}")))?;
|
|
84
|
+
let session = self.session.lock().clone();
|
|
85
|
+
let builder = self.tunnel_builder.lock().clone();
|
|
86
|
+
|
|
87
|
+
let result = builder
|
|
88
|
+
.listen_and_forward(url)
|
|
89
|
+
.await
|
|
90
|
+
.map_err(|e| napi_ngrok_err("failed to start tunnel", &e));
|
|
91
|
+
|
|
92
|
+
// create the wrapping tunnel object via its async new()
|
|
93
|
+
match result {
|
|
94
|
+
Ok(raw_fwd) => Ok($tunnel::new_forwarder(session, raw_fwd).await),
|
|
95
|
+
Err(val) => Err(val),
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// Begin listening for new connections on this tunnel and forwarding them to the given server.
|
|
100
|
+
#[napi(ts_args_type = "server: any")]
|
|
101
|
+
pub async fn listen_and_serve(&self, server: String) -> Result<NgrokTunnel> {
|
|
102
|
+
Err(napi_err(format!("listen_and_serve implemented in wrapper, {server}")))
|
|
103
|
+
}
|
|
77
104
|
}
|
|
78
105
|
|
|
79
106
|
impl ObjectFinalize for $wrapper {
|
|
@@ -95,7 +122,7 @@ macro_rules! make_tunnel_builder {
|
|
|
95
122
|
#[napi]
|
|
96
123
|
pub fn allow_cidr(&mut self, cidr: String) -> &Self {
|
|
97
124
|
let mut builder = self.tunnel_builder.lock();
|
|
98
|
-
|
|
125
|
+
builder.allow_cidr(cidr);
|
|
99
126
|
self
|
|
100
127
|
}
|
|
101
128
|
/// Restriction placed on the origin of incoming connections to the edge to deny these CIDR ranges.
|
|
@@ -103,14 +130,14 @@ macro_rules! make_tunnel_builder {
|
|
|
103
130
|
#[napi]
|
|
104
131
|
pub fn deny_cidr(&mut self, cidr: String) -> &Self {
|
|
105
132
|
let mut builder = self.tunnel_builder.lock();
|
|
106
|
-
|
|
133
|
+
builder.deny_cidr(cidr);
|
|
107
134
|
self
|
|
108
135
|
}
|
|
109
136
|
/// The version of PROXY protocol to use with this tunnel "1", "2", or "" if not using.
|
|
110
137
|
#[napi]
|
|
111
138
|
pub fn proxy_proto(&mut self, proxy_proto: String) -> &Self {
|
|
112
139
|
let mut builder = self.tunnel_builder.lock();
|
|
113
|
-
|
|
140
|
+
builder.proxy_proto(
|
|
114
141
|
ProxyProto::from_str(proxy_proto.as_str())
|
|
115
142
|
.unwrap_or_else(|_| panic!("Unknown proxy protocol: {:?}", proxy_proto)),
|
|
116
143
|
);
|
|
@@ -121,7 +148,7 @@ macro_rules! make_tunnel_builder {
|
|
|
121
148
|
#[napi]
|
|
122
149
|
pub fn forwards_to(&mut self, forwards_to: String) -> &Self {
|
|
123
150
|
let mut builder = self.tunnel_builder.lock();
|
|
124
|
-
|
|
151
|
+
builder.forwards_to(forwards_to);
|
|
125
152
|
self
|
|
126
153
|
}
|
|
127
154
|
}
|
|
@@ -135,7 +162,7 @@ macro_rules! make_tunnel_builder {
|
|
|
135
162
|
#[napi]
|
|
136
163
|
pub fn label(&mut self, label: String, value: String) -> &Self {
|
|
137
164
|
let mut builder = self.tunnel_builder.lock();
|
|
138
|
-
|
|
165
|
+
builder.label(label, value);
|
|
139
166
|
self
|
|
140
167
|
}
|
|
141
168
|
}
|