@ngrok/ngrok 0.6.0 → 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 +5 -0
- package/Cargo.toml +5 -2
- package/README.md +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/NgrokHttpTunnelBuilder.html +55 -21
- package/docs/classes/NgrokLabeledTunnelBuilder.html +38 -4
- package/docs/classes/NgrokSession.html +8 -8
- package/docs/classes/NgrokSessionBuilder.html +16 -16
- package/docs/classes/NgrokTcpTunnelBuilder.html +42 -8
- package/docs/classes/NgrokTlsTunnelBuilder.html +44 -10
- package/docs/classes/NgrokTunnel.html +21 -9
- package/docs/classes/UpdateRequest.html +3 -3
- package/docs/functions/authtoken.html +1 -1
- package/docs/functions/connect.html +1 -1
- package/docs/functions/consoleLog.html +1 -1
- package/docs/functions/disconnect.html +1 -1
- package/docs/functions/getTunnel.html +1 -1
- package/docs/functions/getTunnelByUrl.html +1 -1
- package/docs/functions/kill.html +1 -1
- package/docs/functions/listen.html +1 -1
- package/docs/functions/listenable.html +1 -1
- package/docs/functions/loggingCallback.html +1 -1
- package/docs/functions/tunnels.html +1 -1
- package/docs/interfaces/Config.html +53 -53
- package/index.d.ts +18 -0
- package/index.js +12 -0
- package/package.json +14 -14
- package/src/connect.rs +7 -4
- package/src/http.rs +20 -22
- package/src/lib.rs +1 -1
- package/src/session.rs +45 -45
- package/src/tcp.rs +1 -1
- package/src/tls.rs +3 -5
- package/src/tunnel.rs +118 -31
- package/src/tunnel_builder.rs +33 -6
- package/trailer.js +12 -0
package/src/tunnel.rs
CHANGED
|
@@ -7,35 +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;
|
|
31
36
|
|
|
32
37
|
use crate::{
|
|
33
|
-
connect::PIPE_PREFIX,
|
|
34
38
|
napi_err,
|
|
35
39
|
napi_ngrok_err,
|
|
36
40
|
};
|
|
37
41
|
|
|
42
|
+
// no forward host section to allow for relative unix paths
|
|
38
43
|
pub(crate) const UNIX_PREFIX: &str = "unix:";
|
|
44
|
+
pub(crate) const TCP_PREFIX: &str = "tcp://";
|
|
39
45
|
|
|
40
46
|
lazy_static! {
|
|
41
47
|
// tunnel references to be kept until explicit close to prevent nodejs gc from dropping them.
|
|
@@ -46,7 +52,8 @@ lazy_static! {
|
|
|
46
52
|
|
|
47
53
|
/// Stores the tunnel and session references to be kept until explicit close.
|
|
48
54
|
struct Storage {
|
|
49
|
-
tunnel: Arc<Mutex<dyn ExtendedTunnel
|
|
55
|
+
tunnel: Option<Arc<Mutex<dyn ExtendedTunnel>>>,
|
|
56
|
+
forwarder: Option<Arc<Mutex<dyn ExtendedForwarder>>>,
|
|
50
57
|
session: Session,
|
|
51
58
|
tun_meta: Arc<TunnelMetadata>,
|
|
52
59
|
}
|
|
@@ -63,9 +70,12 @@ struct TunnelMetadata {
|
|
|
63
70
|
/// The TunnelExt cannot be turned into an object since it contains generics, so implementing
|
|
64
71
|
/// a proxy trait without generics which can be the dyn type stored in the global map.
|
|
65
72
|
#[async_trait]
|
|
66
|
-
pub trait ExtendedTunnel:
|
|
67
|
-
async fn
|
|
68
|
-
|
|
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>>;
|
|
69
79
|
}
|
|
70
80
|
|
|
71
81
|
/// An ngrok tunnel.
|
|
@@ -102,7 +112,31 @@ macro_rules! make_tunnel_type {
|
|
|
102
112
|
info!("Created tunnel {id:?} with url {:?}", raw_tunnel.url());
|
|
103
113
|
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
104
114
|
let storage = Arc::new(Storage {
|
|
105
|
-
tunnel: Arc::new(Mutex::new(raw_tunnel)),
|
|
115
|
+
tunnel: Some(Arc::new(Mutex::new(raw_tunnel))),
|
|
116
|
+
forwarder: None,
|
|
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()),
|
|
133
|
+
labels: HashMap::new(),
|
|
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))),
|
|
106
140
|
session,
|
|
107
141
|
tun_meta,
|
|
108
142
|
});
|
|
@@ -137,7 +171,31 @@ macro_rules! make_tunnel_type {
|
|
|
137
171
|
info!("Created tunnel {id:?} with labels {:?}", tun_meta.labels);
|
|
138
172
|
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
139
173
|
let storage = Arc::new(Storage {
|
|
140
|
-
tunnel: Arc::new(Mutex::new(raw_tunnel)),
|
|
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());
|
|
180
|
+
// create the user-facing object
|
|
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(),
|
|
190
|
+
url: None,
|
|
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))),
|
|
141
199
|
session,
|
|
142
200
|
tun_meta,
|
|
143
201
|
});
|
|
@@ -154,11 +212,15 @@ macro_rules! make_tunnel_type {
|
|
|
154
212
|
($wrapper:ident, $tunnel:tt) => {
|
|
155
213
|
#[async_trait]
|
|
156
214
|
impl ExtendedTunnel for $tunnel {
|
|
157
|
-
|
|
158
|
-
|
|
215
|
+
#[allow(deprecated)]
|
|
216
|
+
async fn fwd(&mut self, url: Url) -> CoreResult<(), io::Error> {
|
|
217
|
+
ngrok::prelude::TunnelExt::forward(self, url).await
|
|
159
218
|
}
|
|
160
|
-
|
|
161
|
-
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
impl ExtendedForwarder for Forwarder<$tunnel> {
|
|
222
|
+
fn get_join(&mut self) -> &mut JoinHandle<CoreResult<(), io::Error>> {
|
|
223
|
+
self.join()
|
|
162
224
|
}
|
|
163
225
|
}
|
|
164
226
|
};
|
|
@@ -223,6 +285,25 @@ impl NgrokTunnel {
|
|
|
223
285
|
forward(&self.tun_meta.id, addr).await
|
|
224
286
|
}
|
|
225
287
|
|
|
288
|
+
/// Wait for the forwarding task to exit.
|
|
289
|
+
#[napi]
|
|
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
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
226
307
|
/// Close the tunnel.
|
|
227
308
|
///
|
|
228
309
|
/// This is an RPC call that must be `.await`ed.
|
|
@@ -280,26 +361,32 @@ make_tunnel_type! {
|
|
|
280
361
|
NgrokLabeledTunnel, LabeledTunnel, label
|
|
281
362
|
}
|
|
282
363
|
|
|
283
|
-
pub async fn forward(id: &String, addr: String) -> Result<()> {
|
|
284
|
-
let
|
|
285
|
-
let
|
|
286
|
-
|
|
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}")))?;
|
|
287
381
|
|
|
288
|
-
|
|
289
|
-
let
|
|
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);
|
|
382
|
+
info!("Tunnel {id:?} forwarding to {:?}", url.to_string());
|
|
383
|
+
let res = tun.lock().await.fwd(url).await;
|
|
293
384
|
|
|
294
|
-
|
|
295
|
-
|
|
385
|
+
debug!("forward returning");
|
|
386
|
+
canceled_is_ok(res)
|
|
296
387
|
} else {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
};
|
|
300
|
-
|
|
301
|
-
debug!("forward returning");
|
|
302
|
-
canceled_is_ok(res)
|
|
388
|
+
Err(napi_err("tunnel is not forwardable"))
|
|
389
|
+
}
|
|
303
390
|
}
|
|
304
391
|
|
|
305
392
|
fn canceled_is_ok(input: CoreResult<(), io::Error>) -> Result<()> {
|
package/src/tunnel_builder.rs
CHANGED
|
@@ -18,8 +18,10 @@ 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::{
|
|
24
|
+
napi_err,
|
|
23
25
|
napi_ngrok_err,
|
|
24
26
|
tunnel::{
|
|
25
27
|
NgrokHttpTunnel,
|
|
@@ -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
|
|
|
@@ -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
|
}
|
package/trailer.js
CHANGED
|
@@ -22,6 +22,11 @@ NgrokTcpTunnelBuilder.prototype.listen = ngrokBind;
|
|
|
22
22
|
NgrokTlsTunnelBuilder.prototype.listen = ngrokBind;
|
|
23
23
|
NgrokLabeledTunnelBuilder.prototype.listen = ngrokBind;
|
|
24
24
|
|
|
25
|
+
NgrokHttpTunnelBuilder.prototype.listenAndServe = listenAndServe;
|
|
26
|
+
NgrokTcpTunnelBuilder.prototype.listenAndServe = listenAndServe;
|
|
27
|
+
NgrokTlsTunnelBuilder.prototype.listenAndServe = listenAndServe;
|
|
28
|
+
NgrokLabeledTunnelBuilder.prototype.listenAndServe = listenAndServe;
|
|
29
|
+
|
|
25
30
|
// Wrap session connect to fill in exception's errorCode
|
|
26
31
|
async function ngrokSessionConnect() {
|
|
27
32
|
try {
|
|
@@ -50,6 +55,13 @@ async function ngrokBind(bind) {
|
|
|
50
55
|
}
|
|
51
56
|
}
|
|
52
57
|
|
|
58
|
+
/// Begin listening for new connections on this tunnel and forwarding them to the given server.
|
|
59
|
+
async function listenAndServe(server) {
|
|
60
|
+
const tunnel = await this._listen();
|
|
61
|
+
tunnel.socket = await ngrokListen(server, tunnel);
|
|
62
|
+
return tunnel;
|
|
63
|
+
}
|
|
64
|
+
|
|
53
65
|
function populateErrorCode(err) {
|
|
54
66
|
if (err.message) {
|
|
55
67
|
const regex = /error_code: (ERR_NGROK_\d+)$/;
|