@ngrok/ngrok 0.0.6 → 0.0.8
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 +16 -0
- package/CONTRIBUTING.md +4 -4
- package/Cargo.toml +5 -2
- package/README.md +11 -11
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +71 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1280 -0
- package/docs/classes/NgrokHttpTunnel.html +189 -0
- package/docs/classes/NgrokHttpTunnelBuilder.html +440 -0
- package/docs/classes/NgrokLabeledTunnel.html +177 -0
- package/docs/classes/NgrokLabeledTunnelBuilder.html +130 -0
- package/docs/classes/NgrokSession.html +142 -0
- package/docs/classes/NgrokSessionBuilder.html +303 -0
- package/docs/classes/NgrokTcpTunnel.html +189 -0
- package/docs/classes/NgrokTcpTunnelBuilder.html +199 -0
- package/docs/classes/NgrokTlsTunnel.html +189 -0
- package/docs/classes/NgrokTlsTunnelBuilder.html +235 -0
- package/docs/classes/UpdateRequest.html +92 -0
- package/docs/functions/consoleLog.html +73 -0
- package/docs/functions/listen.html +74 -0
- package/docs/functions/listenable.html +72 -0
- package/docs/functions/loggingCallback.html +91 -0
- package/docs/index.html +129 -0
- package/docs/modules.html +79 -0
- package/examples/ngrok-http-full.js +18 -8
- package/examples/ngrok-http-minimum.js +1 -3
- package/examples/ngrok-labeled.js +2 -4
- package/examples/ngrok-tcp.js +4 -6
- package/examples/ngrok-tls.js +6 -8
- package/examples/ngrok-typescript.ts +64 -0
- package/examples/ngrok-winston.js +24 -0
- package/index.d.ts +193 -41
- package/index.js +114 -27
- package/package.json +25 -17
- package/post-build.js +23 -16
- package/scripts/gc-node-drop.js +42 -0
- package/scripts/gc-rust-drop.js +47 -0
- package/src/http.rs +64 -42
- package/src/lib.rs +10 -0
- package/src/logging.rs +134 -0
- package/src/session.rs +239 -63
- package/src/tcp.rs +2 -1
- package/src/tls.rs +10 -18
- package/src/tunnel.rs +146 -22
- package/src/tunnel_builder.rs +46 -28
- package/trailer.d.ts +9 -0
- package/trailer.js +111 -25
- package/tsconfig.json +6 -0
- package/examples/ngrok-get-socket.js +0 -13
package/src/tunnel.rs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
use std::{
|
|
2
2
|
collections::HashMap,
|
|
3
|
+
io,
|
|
3
4
|
sync::Arc,
|
|
4
5
|
};
|
|
5
6
|
|
|
7
|
+
use async_trait::async_trait;
|
|
8
|
+
use lazy_static::lazy_static;
|
|
6
9
|
use napi::bindgen_prelude::*;
|
|
7
10
|
use napi_derive::napi;
|
|
8
11
|
use ngrok::{
|
|
@@ -15,32 +18,67 @@ use ngrok::{
|
|
|
15
18
|
TlsTunnel,
|
|
16
19
|
UrlTunnel,
|
|
17
20
|
},
|
|
21
|
+
Session,
|
|
18
22
|
};
|
|
19
23
|
use tokio::sync::Mutex;
|
|
20
|
-
use tracing::
|
|
24
|
+
use tracing::{
|
|
25
|
+
debug,
|
|
26
|
+
info,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
use crate::napi_err;
|
|
30
|
+
|
|
31
|
+
lazy_static! {
|
|
32
|
+
// tunnel references to be kept until explicit close to prevent nodejs gc from dropping them.
|
|
33
|
+
// the tunnel wrapper object, and the underlying tunnel, both have references to the Session
|
|
34
|
+
// so the Session is safe from premature dropping.
|
|
35
|
+
static ref GLOBAL_TUNNELS: Mutex<HashMap<String,Arc<Mutex<dyn ExtendedTunnel>>>> = Mutex::new(HashMap::new());
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// The TunnelExt cannot be turned into an object since it contains generics, so implementing
|
|
39
|
+
/// a proxy trait without generics which can be the dyn type stored in the global map.
|
|
40
|
+
#[async_trait]
|
|
41
|
+
pub trait ExtendedTunnel: Tunnel {
|
|
42
|
+
async fn fwd_tcp(&mut self, addr: String) -> core::result::Result<(), io::Error>;
|
|
43
|
+
#[cfg(not(target_os = "windows"))]
|
|
44
|
+
async fn fwd_unix(&mut self, addr: String) -> core::result::Result<(), io::Error>;
|
|
45
|
+
}
|
|
21
46
|
|
|
22
47
|
macro_rules! make_tunnel_type {
|
|
23
48
|
// the common (non-labeled) branch
|
|
24
49
|
($(#[$outer:meta])* $wrapper:ident, $tunnel:tt, common) => {
|
|
50
|
+
|
|
25
51
|
$(#[$outer])*
|
|
26
52
|
#[napi(custom_finalize)]
|
|
27
53
|
#[allow(dead_code)]
|
|
28
54
|
pub(crate) struct $wrapper {
|
|
29
55
|
id: String,
|
|
56
|
+
forwards_to: String,
|
|
57
|
+
metadata: String,
|
|
30
58
|
url: String,
|
|
31
59
|
proto: String,
|
|
32
|
-
|
|
60
|
+
session: Session,
|
|
33
61
|
}
|
|
34
62
|
|
|
35
63
|
#[napi]
|
|
36
64
|
#[allow(dead_code)]
|
|
37
65
|
impl $wrapper {
|
|
38
|
-
pub(crate) fn new(raw_tunnel: $tunnel) -> Self {
|
|
66
|
+
pub(crate) async fn new(session: Session, raw_tunnel: $tunnel) -> Self {
|
|
67
|
+
let id = raw_tunnel.id().to_string();
|
|
68
|
+
let forwards_to = raw_tunnel.forwards_to().to_string();
|
|
69
|
+
let metadata = raw_tunnel.metadata().to_string();
|
|
70
|
+
let url = raw_tunnel.url().to_string();
|
|
71
|
+
let proto = raw_tunnel.proto().to_string();
|
|
72
|
+
info!("Created tunnel {id:?} with url {url:?}");
|
|
73
|
+
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
74
|
+
GLOBAL_TUNNELS.lock().await.insert(id.clone(), Arc::new(Mutex::new(raw_tunnel)));
|
|
39
75
|
$wrapper {
|
|
40
|
-
id
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
76
|
+
id,
|
|
77
|
+
forwards_to,
|
|
78
|
+
metadata,
|
|
79
|
+
url,
|
|
80
|
+
proto,
|
|
81
|
+
session,
|
|
44
82
|
}
|
|
45
83
|
}
|
|
46
84
|
|
|
@@ -67,18 +105,29 @@ macro_rules! make_tunnel_type {
|
|
|
67
105
|
#[allow(dead_code)]
|
|
68
106
|
pub(crate) struct $wrapper {
|
|
69
107
|
id: String,
|
|
108
|
+
forwards_to: String,
|
|
109
|
+
metadata: String,
|
|
70
110
|
labels: HashMap<String,String>,
|
|
71
|
-
|
|
111
|
+
session: Session,
|
|
72
112
|
}
|
|
73
113
|
|
|
74
114
|
#[napi]
|
|
75
115
|
#[allow(dead_code)]
|
|
76
116
|
impl $wrapper {
|
|
77
|
-
pub(crate) fn new(raw_tunnel: $tunnel) -> Self {
|
|
117
|
+
pub(crate) async fn new(session: Session, raw_tunnel: $tunnel) -> Self {
|
|
118
|
+
let id = raw_tunnel.id().to_string();
|
|
119
|
+
let forwards_to = raw_tunnel.forwards_to().to_string();
|
|
120
|
+
let metadata = raw_tunnel.metadata().to_string();
|
|
121
|
+
let labels = raw_tunnel.labels().clone();
|
|
122
|
+
info!("Created tunnel {id:?} with labels {labels:?}");
|
|
123
|
+
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
124
|
+
GLOBAL_TUNNELS.lock().await.insert(id.clone(), Arc::new(Mutex::new(raw_tunnel)));
|
|
78
125
|
$wrapper {
|
|
79
|
-
id
|
|
80
|
-
|
|
81
|
-
|
|
126
|
+
id,
|
|
127
|
+
forwards_to,
|
|
128
|
+
metadata,
|
|
129
|
+
labels,
|
|
130
|
+
session,
|
|
82
131
|
}
|
|
83
132
|
}
|
|
84
133
|
|
|
@@ -94,47 +143,117 @@ macro_rules! make_tunnel_type {
|
|
|
94
143
|
|
|
95
144
|
// all tunnels get these
|
|
96
145
|
($wrapper:ident, $tunnel:tt) => {
|
|
146
|
+
#[async_trait]
|
|
147
|
+
impl ExtendedTunnel for $tunnel {
|
|
148
|
+
async fn fwd_tcp(&mut self, addr: String) -> core::result::Result<(), io::Error> {
|
|
149
|
+
self.forward_tcp(addr).await
|
|
150
|
+
}
|
|
151
|
+
#[cfg(not(target_os = "windows"))]
|
|
152
|
+
async fn fwd_unix(&mut self, addr: String) -> core::result::Result<(), io::Error> {
|
|
153
|
+
self.forward_unix(addr).await
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
97
157
|
#[napi]
|
|
98
158
|
#[allow(dead_code)]
|
|
99
159
|
impl $wrapper {
|
|
100
|
-
///
|
|
160
|
+
/// Returns a tunnel's unique ID.
|
|
101
161
|
#[napi]
|
|
102
162
|
pub fn id(&self) -> String {
|
|
103
163
|
self.id.clone()
|
|
104
164
|
}
|
|
105
165
|
|
|
166
|
+
/// Returns a human-readable string presented in the ngrok dashboard
|
|
167
|
+
/// and the Tunnels API. Use the [HttpTunnelBuilder::forwards_to],
|
|
168
|
+
/// [TcpTunnelBuilder::forwards_to], etc. to set this value
|
|
169
|
+
/// explicitly.
|
|
170
|
+
#[napi]
|
|
171
|
+
pub fn forwards_to(&self) -> String {
|
|
172
|
+
self.forwards_to.clone()
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/// Returns the arbitrary metadata string for this tunnel.
|
|
176
|
+
#[napi]
|
|
177
|
+
pub fn metadata(&self) -> String {
|
|
178
|
+
self.metadata.clone()
|
|
179
|
+
}
|
|
180
|
+
|
|
106
181
|
/// Forward incoming tunnel connections to the provided TCP address.
|
|
107
182
|
#[napi]
|
|
108
183
|
pub async fn forward_tcp(&self, addr: String) -> Result<()> {
|
|
109
|
-
self.
|
|
184
|
+
info!("Tunnel {:?} TCP forwarding to {addr:?}", &self.id);
|
|
185
|
+
// we must clone the Arc before locking so we have a local reference
|
|
186
|
+
// to the mutex to unlock if this struct is dropped.
|
|
187
|
+
let arc = GLOBAL_TUNNELS.lock().await
|
|
188
|
+
.get(&self.id)
|
|
189
|
+
.ok_or(napi_err("Tunnel is no longer running"))?
|
|
190
|
+
.clone(); // required clone
|
|
191
|
+
|
|
192
|
+
// doing this as a seperate statement so the GLOBAL_TUNNELS lock is dropped
|
|
193
|
+
let res = arc
|
|
110
194
|
.lock()
|
|
111
195
|
.await
|
|
112
|
-
.
|
|
196
|
+
.fwd_tcp(addr)
|
|
113
197
|
.await
|
|
114
|
-
.map_err(|e|
|
|
198
|
+
.map_err(|e| napi_err(format!("cannot forward tcp: {e:?}")));
|
|
199
|
+
debug!("forward_tcp returning");
|
|
200
|
+
res
|
|
115
201
|
}
|
|
116
202
|
|
|
117
203
|
/// Forward incoming tunnel connections to the provided Unix socket path.
|
|
118
204
|
#[napi]
|
|
119
205
|
pub async fn forward_unix(&self, addr: String) -> Result<()> {
|
|
206
|
+
info!("Tunnel {:?} pipe forwarding to {addr:?}", &self.id);
|
|
120
207
|
#[cfg(not(target_os = "windows"))]
|
|
121
208
|
{
|
|
122
|
-
|
|
209
|
+
// we must clone the Arc before locking so we have a local reference
|
|
210
|
+
// to the mutex to unlock if this struct is dropped.
|
|
211
|
+
let arc = GLOBAL_TUNNELS.lock().await
|
|
212
|
+
.get(&self.id)
|
|
213
|
+
.ok_or(napi_err("Tunnel is no longer running"))?
|
|
214
|
+
.clone(); // required clone
|
|
215
|
+
|
|
216
|
+
// doing this as a seperate statement so the GLOBAL_TUNNELS lock is dropped
|
|
217
|
+
let res = arc
|
|
123
218
|
.lock()
|
|
124
219
|
.await
|
|
125
|
-
.
|
|
220
|
+
.fwd_unix(addr)
|
|
126
221
|
.await
|
|
127
|
-
.map_err(|e|
|
|
222
|
+
.map_err(|e| napi_err(format!("cannot forward unix: {e:?}")));
|
|
223
|
+
debug!("forward_unix returning");
|
|
224
|
+
res
|
|
128
225
|
}
|
|
129
226
|
#[cfg(target_os = "windows")] {
|
|
130
|
-
Err(
|
|
227
|
+
Err(napi_err(format!("forward_unix not supported on windows")))
|
|
131
228
|
}
|
|
132
229
|
}
|
|
230
|
+
|
|
231
|
+
/// Close the tunnel.
|
|
232
|
+
///
|
|
233
|
+
/// This is an RPC call that must be `.await`ed.
|
|
234
|
+
/// It is equivalent to calling `Session::close_tunnel` with this
|
|
235
|
+
/// tunnel's ID.
|
|
236
|
+
#[napi]
|
|
237
|
+
pub async fn close(&self) -> Result<()> {
|
|
238
|
+
debug!("{} closing, id: {}", stringify!($wrapper), self.id);
|
|
239
|
+
|
|
240
|
+
// we may not be able to lock our reference to the tunnel due to the forward_* calls which
|
|
241
|
+
// continuously accept-loop while the tunnel is active, so calling close on the Session.
|
|
242
|
+
let res = self.session.close_tunnel(self.id.clone())
|
|
243
|
+
.await
|
|
244
|
+
.map_err(|e| napi_err(format!("error closing tunnel: {e:?}")));
|
|
245
|
+
|
|
246
|
+
// drop our internal reference to the tunnel after awaiting close
|
|
247
|
+
GLOBAL_TUNNELS.lock().await.remove(&self.id);
|
|
248
|
+
|
|
249
|
+
res
|
|
250
|
+
}
|
|
133
251
|
}
|
|
134
252
|
|
|
253
|
+
#[allow(unused_mut)]
|
|
135
254
|
impl ObjectFinalize for $wrapper {
|
|
136
|
-
fn finalize(self,
|
|
137
|
-
debug!("
|
|
255
|
+
fn finalize(mut self, _env: Env) -> Result<()> {
|
|
256
|
+
debug!("{} finalize, id: {}", stringify!($wrapper), self.id);
|
|
138
257
|
Ok(())
|
|
139
258
|
}
|
|
140
259
|
}
|
|
@@ -157,3 +276,8 @@ make_tunnel_type! {
|
|
|
157
276
|
/// A labeled ngrok tunnel.
|
|
158
277
|
NgrokLabeledTunnel, LabeledTunnel, label
|
|
159
278
|
}
|
|
279
|
+
|
|
280
|
+
/// Delete any reference to the tunnel id
|
|
281
|
+
pub(crate) async fn remove_global_tunnel(id: &String) {
|
|
282
|
+
GLOBAL_TUNNELS.lock().await.remove(id);
|
|
283
|
+
}
|
package/src/tunnel_builder.rs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
use std::
|
|
1
|
+
use std::{
|
|
2
|
+
str::FromStr,
|
|
3
|
+
sync::Arc,
|
|
4
|
+
};
|
|
2
5
|
|
|
3
6
|
use napi::bindgen_prelude::*;
|
|
4
7
|
use napi_derive::napi;
|
|
@@ -11,14 +14,19 @@ use ngrok::{
|
|
|
11
14
|
TlsTunnelBuilder,
|
|
12
15
|
},
|
|
13
16
|
prelude::*,
|
|
17
|
+
Session,
|
|
14
18
|
};
|
|
19
|
+
use parking_lot::Mutex;
|
|
15
20
|
use tracing::debug;
|
|
16
21
|
|
|
17
|
-
use crate::
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
use crate::{
|
|
23
|
+
napi_err,
|
|
24
|
+
tunnel::{
|
|
25
|
+
NgrokHttpTunnel,
|
|
26
|
+
NgrokLabeledTunnel,
|
|
27
|
+
NgrokTcpTunnel,
|
|
28
|
+
NgrokTlsTunnel,
|
|
29
|
+
},
|
|
22
30
|
};
|
|
23
31
|
|
|
24
32
|
macro_rules! make_tunnel_builder {
|
|
@@ -27,44 +35,49 @@ macro_rules! make_tunnel_builder {
|
|
|
27
35
|
#[napi(custom_finalize)]
|
|
28
36
|
#[allow(dead_code)]
|
|
29
37
|
pub(crate) struct $wrapper {
|
|
30
|
-
|
|
38
|
+
session: Arc<Mutex<Session>>,
|
|
39
|
+
pub(crate) tunnel_builder: Arc<Mutex<$builder>>,
|
|
31
40
|
}
|
|
32
41
|
|
|
33
42
|
#[napi]
|
|
34
43
|
#[allow(dead_code)]
|
|
35
44
|
impl $wrapper {
|
|
36
|
-
pub(crate) fn new(raw_tunnel_builder: $builder) -> Self {
|
|
45
|
+
pub(crate) fn new(session: Session, raw_tunnel_builder: $builder) -> Self {
|
|
37
46
|
$wrapper {
|
|
38
|
-
|
|
47
|
+
session: Arc::new(Mutex::new(session)),
|
|
48
|
+
tunnel_builder: Arc::new(Mutex::new(raw_tunnel_builder)),
|
|
39
49
|
}
|
|
40
50
|
}
|
|
41
51
|
|
|
42
52
|
/// Tunnel-specific opaque metadata. Viewable via the API.
|
|
43
53
|
#[napi]
|
|
44
54
|
pub fn metadata(&mut self, metadata: String) -> &Self {
|
|
45
|
-
|
|
55
|
+
let mut builder = self.tunnel_builder.lock();
|
|
56
|
+
*builder = builder.clone().metadata(metadata);
|
|
46
57
|
self
|
|
47
58
|
}
|
|
48
59
|
|
|
49
60
|
/// Begin listening for new connections on this tunnel.
|
|
50
61
|
#[napi]
|
|
51
|
-
pub async fn listen(&self) -> Result<$tunnel> {
|
|
52
|
-
self.
|
|
62
|
+
pub async fn listen(&self, _bind: Option<bool>) -> Result<$tunnel> {
|
|
63
|
+
let session = self.session.lock().clone();
|
|
64
|
+
let tun = self.tunnel_builder.lock().clone();
|
|
65
|
+
let result = tun
|
|
53
66
|
.listen()
|
|
54
67
|
.await
|
|
55
|
-
.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
68
|
+
.map_err(|e| napi_err(format!("failed to start tunnel: {e:?}")));
|
|
69
|
+
|
|
70
|
+
// create the wrapping tunnel object via its async new()
|
|
71
|
+
match result {
|
|
72
|
+
Ok(raw_tun) => Ok($tunnel::new(session, raw_tun).await),
|
|
73
|
+
Err(val) => Err(val),
|
|
74
|
+
}
|
|
62
75
|
}
|
|
63
76
|
}
|
|
64
77
|
|
|
65
78
|
impl ObjectFinalize for $wrapper {
|
|
66
79
|
fn finalize(self, mut _env: Env) -> Result<()> {
|
|
67
|
-
debug!("
|
|
80
|
+
debug!("{} finalize", stringify!($wrapper));
|
|
68
81
|
Ok(())
|
|
69
82
|
}
|
|
70
83
|
}
|
|
@@ -79,21 +92,24 @@ macro_rules! make_tunnel_builder {
|
|
|
79
92
|
/// Restriction placed on the origin of incoming connections to the edge to only allow these CIDR ranges.
|
|
80
93
|
/// Call multiple times to add additional CIDR ranges.
|
|
81
94
|
#[napi]
|
|
82
|
-
pub fn
|
|
83
|
-
|
|
95
|
+
pub fn allow_cidr(&mut self, cidr: String) -> &Self {
|
|
96
|
+
let mut builder = self.tunnel_builder.lock();
|
|
97
|
+
*builder = builder.clone().allow_cidr(cidr);
|
|
84
98
|
self
|
|
85
99
|
}
|
|
86
100
|
/// Restriction placed on the origin of incoming connections to the edge to deny these CIDR ranges.
|
|
87
101
|
/// Call multiple times to add additional CIDR ranges.
|
|
88
102
|
#[napi]
|
|
89
|
-
pub fn
|
|
90
|
-
|
|
103
|
+
pub fn deny_cidr(&mut self, cidr: String) -> &Self {
|
|
104
|
+
let mut builder = self.tunnel_builder.lock();
|
|
105
|
+
*builder = builder.clone().deny_cidr(cidr);
|
|
91
106
|
self
|
|
92
107
|
}
|
|
93
|
-
/// The version of PROXY protocol to use with this tunnel,
|
|
108
|
+
/// The version of PROXY protocol to use with this tunnel "1", "2", or "" if not using.
|
|
94
109
|
#[napi]
|
|
95
110
|
pub fn proxy_proto(&mut self, proxy_proto: String) -> &Self {
|
|
96
|
-
|
|
111
|
+
let mut builder = self.tunnel_builder.lock();
|
|
112
|
+
*builder = builder.clone().proxy_proto(
|
|
97
113
|
ProxyProto::from_str(proxy_proto.as_str())
|
|
98
114
|
.unwrap_or_else(|_| panic!("Unknown proxy protocol: {:?}", proxy_proto)),
|
|
99
115
|
);
|
|
@@ -103,7 +119,8 @@ macro_rules! make_tunnel_builder {
|
|
|
103
119
|
/// bearing on tunnel behavior.
|
|
104
120
|
#[napi]
|
|
105
121
|
pub fn forwards_to(&mut self, forwards_to: String) -> &Self {
|
|
106
|
-
|
|
122
|
+
let mut builder = self.tunnel_builder.lock();
|
|
123
|
+
*builder = builder.clone().forwards_to(forwards_to);
|
|
107
124
|
self
|
|
108
125
|
}
|
|
109
126
|
}
|
|
@@ -116,7 +133,8 @@ macro_rules! make_tunnel_builder {
|
|
|
116
133
|
/// Add a label, value pair for this tunnel.
|
|
117
134
|
#[napi]
|
|
118
135
|
pub fn label(&mut self, label: String, value: String) -> &Self {
|
|
119
|
-
|
|
136
|
+
let mut builder = self.tunnel_builder.lock();
|
|
137
|
+
*builder = builder.clone().label(label, value);
|
|
120
138
|
self
|
|
121
139
|
}
|
|
122
140
|
}
|
package/trailer.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Generate, or convert a given tunnel, into one that can be passed into net.Server.listen(). */
|
|
2
|
+
export function listenable(tunnel?: (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)): (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)
|
|
3
|
+
/** Start the given net.Server listening to a generated, or passed in, tunnel. */
|
|
4
|
+
export function listen(server: import("net").Server, tunnel?: (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)): (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)
|
|
5
|
+
/**
|
|
6
|
+
* Register a console.log callback for ngrok INFO logging.
|
|
7
|
+
* Optionally set the logging level to one of ERROR, WARN, INFO, DEBUG, or TRACE.
|
|
8
|
+
*/
|
|
9
|
+
export function consoleLog(level?: String)
|
package/trailer.js
CHANGED
|
@@ -6,6 +6,63 @@ const net = require('net');
|
|
|
6
6
|
const fs = require('fs');
|
|
7
7
|
const os = require('os');
|
|
8
8
|
|
|
9
|
+
// wrap listen with the bind code for passing to net.Server.listen()
|
|
10
|
+
NgrokHttpTunnelBuilder.prototype._listen = NgrokHttpTunnelBuilder.prototype.listen;
|
|
11
|
+
NgrokTcpTunnelBuilder.prototype._listen = NgrokTcpTunnelBuilder.prototype.listen;
|
|
12
|
+
NgrokTlsTunnelBuilder.prototype._listen = NgrokTlsTunnelBuilder.prototype.listen;
|
|
13
|
+
NgrokLabeledTunnelBuilder.prototype._listen = NgrokLabeledTunnelBuilder.prototype.listen;
|
|
14
|
+
|
|
15
|
+
NgrokHttpTunnelBuilder.prototype.listen = ngrokBind;
|
|
16
|
+
NgrokTcpTunnelBuilder.prototype.listen = ngrokBind;
|
|
17
|
+
NgrokTlsTunnelBuilder.prototype.listen = ngrokBind;
|
|
18
|
+
NgrokLabeledTunnelBuilder.prototype.listen = ngrokBind;
|
|
19
|
+
|
|
20
|
+
// Begin listening for new connections on this tunnel,
|
|
21
|
+
// and bind to a local socket so this tunnel can be
|
|
22
|
+
// passed into net.Server.listen().
|
|
23
|
+
async function ngrokBind(bind) {
|
|
24
|
+
const tunnel = await this._listen();
|
|
25
|
+
if (bind !== false) {
|
|
26
|
+
const socket = await randomTcpSocket();
|
|
27
|
+
defineTunnelHandle(tunnel, socket);
|
|
28
|
+
}
|
|
29
|
+
return tunnel;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// add a 'handle' getter to the tunnel so it can be
|
|
33
|
+
// passed into net.Server.listen().
|
|
34
|
+
function defineTunnelHandle(tunnel, socket) {
|
|
35
|
+
// NodeJS net.Server asks passed-in object for 'handle',
|
|
36
|
+
// Return the native TCP object so the pre-existing socket is used.
|
|
37
|
+
Object.defineProperty( tunnel, 'handle', {
|
|
38
|
+
get: function() {
|
|
39
|
+
// turn on forwarding now that it has been requested
|
|
40
|
+
tunnel.forwardTcp('localhost:' + socket.address().port);
|
|
41
|
+
return socket._handle;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// generate a net.Server listening to a random port
|
|
47
|
+
async function randomTcpSocket() {
|
|
48
|
+
return await asyncListen(new net.Server(), {host:'localhost', port:0});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// NodeJS has not promisified 'net': https://github.com/nodejs/node/issues/21482
|
|
52
|
+
function asyncListen(server, options) {
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
const socket = server.listen(options);
|
|
55
|
+
socket.once('listening', () => {
|
|
56
|
+
resolve(socket);
|
|
57
|
+
})
|
|
58
|
+
.once('error', (err) => {
|
|
59
|
+
reject(err);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Make a session using NGROK_AUTHTOKEN from the environment,
|
|
65
|
+
// and then return a listening HTTP tunnel.
|
|
9
66
|
async function defaultTunnel() {
|
|
10
67
|
// set up a default session and tunnel
|
|
11
68
|
var builder = new NgrokSessionBuilder();
|
|
@@ -16,22 +73,12 @@ async function defaultTunnel() {
|
|
|
16
73
|
return tunnel;
|
|
17
74
|
}
|
|
18
75
|
|
|
19
|
-
//
|
|
20
|
-
async function
|
|
21
|
-
|
|
22
|
-
tunnel = await defaultTunnel();
|
|
23
|
-
}
|
|
24
|
-
// use tcp socket with random local port
|
|
25
|
-
const server = new net.Server();
|
|
26
|
-
server.listen(0);
|
|
27
|
-
// forward to this socket
|
|
28
|
-
tunnel.forwardTcp('localhost:' + server.address().port);
|
|
29
|
-
// surface to caller
|
|
30
|
-
server.tunnel = tunnel;
|
|
31
|
-
return server;
|
|
76
|
+
// Get a listenable ngrok tunnel, suitable for passing to net.Server.listen().
|
|
77
|
+
async function listenable() {
|
|
78
|
+
return await defaultTunnel();
|
|
32
79
|
}
|
|
33
80
|
|
|
34
|
-
//
|
|
81
|
+
// Bind a server to a ngrok tunnel, optionally passing in a pre-existing tunnel
|
|
35
82
|
async function ngrokListen(server, tunnel) {
|
|
36
83
|
if (!tunnel) {
|
|
37
84
|
tunnel = await defaultTunnel();
|
|
@@ -40,22 +87,28 @@ async function ngrokListen(server, tunnel) {
|
|
|
40
87
|
// todo: named pipe on windows: https://nodejs.org/api/net.html#ipc-support
|
|
41
88
|
|
|
42
89
|
// attempt unix socket
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
90
|
+
var socket;
|
|
91
|
+
try {
|
|
92
|
+
socket = await ngrokLinkUnix(tunnel, server);
|
|
93
|
+
} catch (err) {
|
|
94
|
+
console.debug("Using TCP socket. " + err);
|
|
95
|
+
// fallback to tcp socket
|
|
96
|
+
socket = await ngrokLinkTcp(tunnel, server);
|
|
97
|
+
}
|
|
98
|
+
registerCleanup(tunnel, socket);
|
|
50
99
|
|
|
51
100
|
server.tunnel = tunnel; // surface to caller
|
|
101
|
+
socket.tunnel = tunnel; // surface to caller
|
|
102
|
+
// return the newly created net.Server, which will be different in the express case
|
|
103
|
+
return socket;
|
|
52
104
|
}
|
|
53
105
|
|
|
54
106
|
async function ngrokLinkTcp(tunnel, server) {
|
|
55
107
|
// random local port
|
|
56
|
-
server
|
|
108
|
+
const socket = await asyncListen(server, {host:'localhost', port:0});
|
|
57
109
|
// forward to socket
|
|
58
|
-
tunnel.forwardTcp('localhost:' +
|
|
110
|
+
tunnel.forwardTcp('localhost:' + socket.address().port);
|
|
111
|
+
return socket;
|
|
59
112
|
}
|
|
60
113
|
|
|
61
114
|
async function ngrokLinkUnix(tunnel, server) {
|
|
@@ -76,7 +129,7 @@ async function ngrokLinkUnix(tunnel, server) {
|
|
|
76
129
|
}
|
|
77
130
|
|
|
78
131
|
// begin listening
|
|
79
|
-
server
|
|
132
|
+
const socket = await asyncListen(server, {path: filename});
|
|
80
133
|
// tighten permissions
|
|
81
134
|
try {
|
|
82
135
|
fs.chmodSync(filename, fs.constants.S_IRWXU);
|
|
@@ -85,7 +138,40 @@ async function ngrokLinkUnix(tunnel, server) {
|
|
|
85
138
|
}
|
|
86
139
|
// forward tunnel
|
|
87
140
|
tunnel.forwardUnix(filename);
|
|
141
|
+
socket.path = filename; // surface to caller
|
|
142
|
+
|
|
143
|
+
return socket;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function registerCleanup(tunnel, socket) {
|
|
147
|
+
process.on('SIGINT', function() {
|
|
148
|
+
if (process.listenerCount('SIGINT') > 1) {
|
|
149
|
+
// user has registered a handler, abort this one
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
// close tunnel
|
|
153
|
+
if (tunnel) {
|
|
154
|
+
tunnel.close().then(()=>{
|
|
155
|
+
console.debug('ngrok closed tunnel: ' + tunnel.id());
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
// close webserver's socket
|
|
159
|
+
if (socket) {
|
|
160
|
+
socket.close(function () {
|
|
161
|
+
console.debug('ngrok closed socket');
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
// unregister any logging callback
|
|
165
|
+
loggingCallback();
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function consoleLog(level) {
|
|
170
|
+
loggingCallback((level, target, message) => {
|
|
171
|
+
console.log(`${level} ${target} - ${message}`);
|
|
172
|
+
}, level);
|
|
88
173
|
}
|
|
89
174
|
|
|
90
|
-
module.exports.
|
|
175
|
+
module.exports.listenable = listenable;
|
|
91
176
|
module.exports.listen = ngrokListen;
|
|
177
|
+
module.exports.consoleLog = consoleLog;
|
package/tsconfig.json
ADDED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
const server = require('http').createServer(
|
|
2
|
-
function(req,res){res.writeHead(200);
|
|
3
|
-
res.write('Hello');
|
|
4
|
-
res.end();
|
|
5
|
-
} );
|
|
6
|
-
|
|
7
|
-
const ngrok = require('@ngrok/ngrok')
|
|
8
|
-
ngrok.getSocket().then((socket) => {
|
|
9
|
-
console.log("tunnel url: " + socket.tunnel.url());
|
|
10
|
-
server.listen(socket);
|
|
11
|
-
server.tunnel = socket.tunnel; // prevent garbage collection
|
|
12
|
-
});
|
|
13
|
-
|