@ngrok/ngrok 0.0.7 → 0.0.9
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 +20 -0
- package/CONTRIBUTING.md +4 -4
- package/Cargo.toml +4 -2
- package/README.md +26 -1
- 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 +3 -2
- 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 +29 -8
- package/examples/ngrok-windows-pipe.js +17 -0
- package/examples/ngrok-winston.js +24 -0
- package/index.d.ts +115 -31
- package/index.js +121 -33
- package/package.json +20 -16
- package/scripts/gc-node-drop.js +6 -2
- package/scripts/gc-rust-drop.js +8 -4
- package/src/http.rs +64 -42
- package/src/lib.rs +1 -0
- package/src/logging.rs +134 -0
- package/src/session.rs +75 -85
- package/src/tcp.rs +2 -1
- package/src/tls.rs +6 -4
- package/src/tunnel.rs +99 -51
- package/src/tunnel_builder.rs +27 -15
- package/trailer.d.ts +9 -2
- package/trailer.js +119 -31
- package/examples/ngrok-get-socket.js +0 -12
package/src/tunnel.rs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
use std::{
|
|
2
2
|
collections::HashMap,
|
|
3
|
+
io,
|
|
3
4
|
sync::Arc,
|
|
4
5
|
};
|
|
5
6
|
|
|
7
|
+
use async_trait::async_trait;
|
|
6
8
|
use lazy_static::lazy_static;
|
|
7
9
|
use napi::bindgen_prelude::*;
|
|
8
10
|
use napi_derive::napi;
|
|
@@ -19,7 +21,10 @@ use ngrok::{
|
|
|
19
21
|
Session,
|
|
20
22
|
};
|
|
21
23
|
use tokio::sync::Mutex;
|
|
22
|
-
use tracing::
|
|
24
|
+
use tracing::{
|
|
25
|
+
debug,
|
|
26
|
+
info,
|
|
27
|
+
};
|
|
23
28
|
|
|
24
29
|
use crate::napi_err;
|
|
25
30
|
|
|
@@ -27,7 +32,15 @@ lazy_static! {
|
|
|
27
32
|
// tunnel references to be kept until explicit close to prevent nodejs gc from dropping them.
|
|
28
33
|
// the tunnel wrapper object, and the underlying tunnel, both have references to the Session
|
|
29
34
|
// so the Session is safe from premature dropping.
|
|
30
|
-
static ref GLOBAL_TUNNELS: Mutex<HashMap<String,Arc<Mutex<dyn
|
|
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
|
+
async fn fwd_pipe(&mut self, addr: String) -> core::result::Result<(), io::Error>;
|
|
31
44
|
}
|
|
32
45
|
|
|
33
46
|
macro_rules! make_tunnel_type {
|
|
@@ -39,10 +52,11 @@ macro_rules! make_tunnel_type {
|
|
|
39
52
|
#[allow(dead_code)]
|
|
40
53
|
pub(crate) struct $wrapper {
|
|
41
54
|
id: String,
|
|
55
|
+
forwards_to: String,
|
|
56
|
+
metadata: String,
|
|
42
57
|
url: String,
|
|
43
58
|
proto: String,
|
|
44
59
|
session: Session,
|
|
45
|
-
inner: Option<Arc<Mutex<$tunnel>>>,
|
|
46
60
|
}
|
|
47
61
|
|
|
48
62
|
#[napi]
|
|
@@ -50,17 +64,20 @@ macro_rules! make_tunnel_type {
|
|
|
50
64
|
impl $wrapper {
|
|
51
65
|
pub(crate) async fn new(session: Session, raw_tunnel: $tunnel) -> Self {
|
|
52
66
|
let id = raw_tunnel.id().to_string();
|
|
67
|
+
let forwards_to = raw_tunnel.forwards_to().to_string();
|
|
68
|
+
let metadata = raw_tunnel.metadata().to_string();
|
|
53
69
|
let url = raw_tunnel.url().to_string();
|
|
54
70
|
let proto = raw_tunnel.proto().to_string();
|
|
55
|
-
|
|
71
|
+
info!("Created tunnel {id:?} with url {url:?}");
|
|
56
72
|
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
57
|
-
GLOBAL_TUNNELS.lock().await.insert(id.clone(),
|
|
73
|
+
GLOBAL_TUNNELS.lock().await.insert(id.clone(), Arc::new(Mutex::new(raw_tunnel)));
|
|
58
74
|
$wrapper {
|
|
59
75
|
id,
|
|
76
|
+
forwards_to,
|
|
77
|
+
metadata,
|
|
60
78
|
url,
|
|
61
79
|
proto,
|
|
62
80
|
session,
|
|
63
|
-
inner: Some(arc),
|
|
64
81
|
}
|
|
65
82
|
}
|
|
66
83
|
|
|
@@ -87,9 +104,10 @@ macro_rules! make_tunnel_type {
|
|
|
87
104
|
#[allow(dead_code)]
|
|
88
105
|
pub(crate) struct $wrapper {
|
|
89
106
|
id: String,
|
|
107
|
+
forwards_to: String,
|
|
108
|
+
metadata: String,
|
|
90
109
|
labels: HashMap<String,String>,
|
|
91
110
|
session: Session,
|
|
92
|
-
inner: Option<Arc<Mutex<$tunnel>>>,
|
|
93
111
|
}
|
|
94
112
|
|
|
95
113
|
#[napi]
|
|
@@ -97,15 +115,18 @@ macro_rules! make_tunnel_type {
|
|
|
97
115
|
impl $wrapper {
|
|
98
116
|
pub(crate) async fn new(session: Session, raw_tunnel: $tunnel) -> Self {
|
|
99
117
|
let id = raw_tunnel.id().to_string();
|
|
118
|
+
let forwards_to = raw_tunnel.forwards_to().to_string();
|
|
119
|
+
let metadata = raw_tunnel.metadata().to_string();
|
|
100
120
|
let labels = raw_tunnel.labels().clone();
|
|
101
|
-
|
|
121
|
+
info!("Created tunnel {id:?} with labels {labels:?}");
|
|
102
122
|
// keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
|
|
103
|
-
GLOBAL_TUNNELS.lock().await.insert(id.clone(),
|
|
123
|
+
GLOBAL_TUNNELS.lock().await.insert(id.clone(), Arc::new(Mutex::new(raw_tunnel)));
|
|
104
124
|
$wrapper {
|
|
105
125
|
id,
|
|
126
|
+
forwards_to,
|
|
127
|
+
metadata,
|
|
106
128
|
labels,
|
|
107
129
|
session,
|
|
108
|
-
inner: Some(arc),
|
|
109
130
|
}
|
|
110
131
|
}
|
|
111
132
|
|
|
@@ -121,85 +142,112 @@ macro_rules! make_tunnel_type {
|
|
|
121
142
|
|
|
122
143
|
// all tunnels get these
|
|
123
144
|
($wrapper:ident, $tunnel:tt) => {
|
|
145
|
+
#[async_trait]
|
|
146
|
+
impl ExtendedTunnel for $tunnel {
|
|
147
|
+
async fn fwd_tcp(&mut self, addr: String) -> core::result::Result<(), io::Error> {
|
|
148
|
+
self.forward_tcp(addr).await
|
|
149
|
+
}
|
|
150
|
+
async fn fwd_pipe(&mut self, addr: String) -> core::result::Result<(), io::Error> {
|
|
151
|
+
self.forward_pipe(addr).await
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
124
155
|
#[napi]
|
|
125
156
|
#[allow(dead_code)]
|
|
126
157
|
impl $wrapper {
|
|
127
|
-
///
|
|
158
|
+
/// Returns a tunnel's unique ID.
|
|
128
159
|
#[napi]
|
|
129
160
|
pub fn id(&self) -> String {
|
|
130
161
|
self.id.clone()
|
|
131
162
|
}
|
|
132
163
|
|
|
164
|
+
/// Returns a human-readable string presented in the ngrok dashboard
|
|
165
|
+
/// and the Tunnels API. Use the [HttpTunnelBuilder::forwards_to],
|
|
166
|
+
/// [TcpTunnelBuilder::forwards_to], etc. to set this value
|
|
167
|
+
/// explicitly.
|
|
168
|
+
#[napi]
|
|
169
|
+
pub fn forwards_to(&self) -> String {
|
|
170
|
+
self.forwards_to.clone()
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// Returns the arbitrary metadata string for this tunnel.
|
|
174
|
+
#[napi]
|
|
175
|
+
pub fn metadata(&self) -> String {
|
|
176
|
+
self.metadata.clone()
|
|
177
|
+
}
|
|
178
|
+
|
|
133
179
|
/// Forward incoming tunnel connections to the provided TCP address.
|
|
134
180
|
#[napi]
|
|
135
181
|
pub async fn forward_tcp(&self, addr: String) -> Result<()> {
|
|
182
|
+
info!("Tunnel {:?} TCP forwarding to {addr:?}", &self.id);
|
|
136
183
|
// we must clone the Arc before locking so we have a local reference
|
|
137
184
|
// to the mutex to unlock if this struct is dropped.
|
|
138
|
-
let
|
|
185
|
+
let arc = GLOBAL_TUNNELS.lock().await
|
|
186
|
+
.get(&self.id)
|
|
139
187
|
.ok_or(napi_err("Tunnel is no longer running"))?
|
|
140
|
-
.clone() // required clone
|
|
188
|
+
.clone(); // required clone
|
|
189
|
+
|
|
190
|
+
// doing this as a seperate statement so the GLOBAL_TUNNELS lock is dropped
|
|
191
|
+
let res = arc
|
|
141
192
|
.lock()
|
|
142
193
|
.await
|
|
143
|
-
.
|
|
194
|
+
.fwd_tcp(addr)
|
|
144
195
|
.await
|
|
145
196
|
.map_err(|e| napi_err(format!("cannot forward tcp: {e:?}")));
|
|
146
197
|
debug!("forward_tcp returning");
|
|
147
198
|
res
|
|
148
199
|
}
|
|
149
200
|
|
|
150
|
-
/// Forward incoming tunnel connections to the provided
|
|
201
|
+
/// Forward incoming tunnel connections to the provided file socket path.
|
|
202
|
+
/// On Linux/Darwin addr can be a unix domain socket path, e.g. "/tmp/ngrok.sock"
|
|
203
|
+
/// On Windows addr can be a named pipe, e.e. "\\.\pipe\an_ngrok_pipe"
|
|
151
204
|
#[napi]
|
|
152
|
-
pub async fn
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
Err(napi_err(format!("forward_unix not supported on windows")))
|
|
170
|
-
}
|
|
205
|
+
pub async fn forward_pipe(&self, addr: String) -> Result<()> {
|
|
206
|
+
// we must clone the Arc before locking so we have a local reference
|
|
207
|
+
// to the mutex to unlock if this struct is dropped.
|
|
208
|
+
let arc = GLOBAL_TUNNELS.lock().await
|
|
209
|
+
.get(&self.id)
|
|
210
|
+
.ok_or(napi_err("Tunnel is no longer running"))?
|
|
211
|
+
.clone(); // required clone
|
|
212
|
+
|
|
213
|
+
// doing this as a seperate statement so the GLOBAL_TUNNELS lock is dropped
|
|
214
|
+
let res = arc
|
|
215
|
+
.lock()
|
|
216
|
+
.await
|
|
217
|
+
.fwd_pipe(addr)
|
|
218
|
+
.await
|
|
219
|
+
.map_err(|e| napi_err(format!("cannot forward pipe: {e:?}")));
|
|
220
|
+
debug!("forward_pipe returning");
|
|
221
|
+
res
|
|
171
222
|
}
|
|
172
223
|
|
|
173
224
|
/// Close the tunnel.
|
|
225
|
+
///
|
|
226
|
+
/// This is an RPC call that must be `.await`ed.
|
|
227
|
+
/// It is equivalent to calling `Session::close_tunnel` with this
|
|
228
|
+
/// tunnel's ID.
|
|
174
229
|
#[napi]
|
|
175
230
|
pub async fn close(&self) -> Result<()> {
|
|
176
231
|
debug!("{} closing, id: {}", stringify!($wrapper), self.id);
|
|
177
|
-
// drop our internal reference to the tunnel, giving drop control of the tunnel back to the nodejs.
|
|
178
|
-
GLOBAL_TUNNELS.lock().await.remove(&self.id);
|
|
179
232
|
|
|
180
233
|
// we may not be able to lock our reference to the tunnel due to the forward_* calls which
|
|
181
234
|
// continuously accept-loop while the tunnel is active, so calling close on the Session.
|
|
182
|
-
self.session.close_tunnel(self.id.clone())
|
|
235
|
+
let res = self.session.close_tunnel(self.id.clone())
|
|
183
236
|
.await
|
|
184
|
-
.map_err(|e| napi_err(format!("error closing tunnel: {e:?}")))
|
|
237
|
+
.map_err(|e| napi_err(format!("error closing tunnel: {e:?}")));
|
|
238
|
+
|
|
239
|
+
// drop our internal reference to the tunnel after awaiting close
|
|
240
|
+
GLOBAL_TUNNELS.lock().await.remove(&self.id);
|
|
241
|
+
|
|
242
|
+
res
|
|
185
243
|
}
|
|
186
244
|
}
|
|
187
245
|
|
|
246
|
+
#[allow(unused_mut)]
|
|
188
247
|
impl ObjectFinalize for $wrapper {
|
|
189
|
-
fn finalize(mut self,
|
|
248
|
+
fn finalize(mut self, _env: Env) -> Result<()> {
|
|
190
249
|
debug!("{} finalize, id: {}", stringify!($wrapper), self.id);
|
|
191
|
-
|
|
192
|
-
let inner = self.inner.take();
|
|
193
|
-
|
|
194
|
-
// send inner Tunnel into a tokio spawn to be dropped, as dropping spawns a tokio task but
|
|
195
|
-
// there is no tokio context here.
|
|
196
|
-
env.spawn_future(
|
|
197
|
-
async move {
|
|
198
|
-
debug!("{} dropping tunnel reference, id: {}", stringify!($wrapper), self.id);
|
|
199
|
-
drop(inner);
|
|
200
|
-
Ok(())
|
|
201
|
-
})
|
|
202
|
-
.map(|_| ())
|
|
250
|
+
Ok(())
|
|
203
251
|
}
|
|
204
252
|
}
|
|
205
253
|
};
|
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;
|
|
@@ -13,6 +16,7 @@ use ngrok::{
|
|
|
13
16
|
prelude::*,
|
|
14
17
|
Session,
|
|
15
18
|
};
|
|
19
|
+
use parking_lot::Mutex;
|
|
16
20
|
use tracing::debug;
|
|
17
21
|
|
|
18
22
|
use crate::{
|
|
@@ -31,8 +35,8 @@ macro_rules! make_tunnel_builder {
|
|
|
31
35
|
#[napi(custom_finalize)]
|
|
32
36
|
#[allow(dead_code)]
|
|
33
37
|
pub(crate) struct $wrapper {
|
|
34
|
-
session: Session
|
|
35
|
-
pub(crate) tunnel_builder:
|
|
38
|
+
session: Arc<Mutex<Session>>,
|
|
39
|
+
pub(crate) tunnel_builder: Arc<Mutex<$builder>>,
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
#[napi]
|
|
@@ -40,29 +44,32 @@ macro_rules! make_tunnel_builder {
|
|
|
40
44
|
impl $wrapper {
|
|
41
45
|
pub(crate) fn new(session: Session, raw_tunnel_builder: $builder) -> Self {
|
|
42
46
|
$wrapper {
|
|
43
|
-
session,
|
|
44
|
-
tunnel_builder: raw_tunnel_builder,
|
|
47
|
+
session: Arc::new(Mutex::new(session)),
|
|
48
|
+
tunnel_builder: Arc::new(Mutex::new(raw_tunnel_builder)),
|
|
45
49
|
}
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
/// Tunnel-specific opaque metadata. Viewable via the API.
|
|
49
53
|
#[napi]
|
|
50
54
|
pub fn metadata(&mut self, metadata: String) -> &Self {
|
|
51
|
-
|
|
55
|
+
let mut builder = self.tunnel_builder.lock();
|
|
56
|
+
*builder = builder.clone().metadata(metadata);
|
|
52
57
|
self
|
|
53
58
|
}
|
|
54
59
|
|
|
55
60
|
/// Begin listening for new connections on this tunnel.
|
|
56
61
|
#[napi]
|
|
57
|
-
pub async fn listen(&self) -> Result<$tunnel> {
|
|
58
|
-
let
|
|
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
|
|
59
66
|
.listen()
|
|
60
67
|
.await
|
|
61
68
|
.map_err(|e| napi_err(format!("failed to start tunnel: {e:?}")));
|
|
62
69
|
|
|
63
70
|
// create the wrapping tunnel object via its async new()
|
|
64
71
|
match result {
|
|
65
|
-
Ok(raw_tun) => Ok($tunnel::new(
|
|
72
|
+
Ok(raw_tun) => Ok($tunnel::new(session, raw_tun).await),
|
|
66
73
|
Err(val) => Err(val),
|
|
67
74
|
}
|
|
68
75
|
}
|
|
@@ -86,20 +93,23 @@ macro_rules! make_tunnel_builder {
|
|
|
86
93
|
/// Call multiple times to add additional CIDR ranges.
|
|
87
94
|
#[napi]
|
|
88
95
|
pub fn allow_cidr(&mut self, cidr: String) -> &Self {
|
|
89
|
-
|
|
96
|
+
let mut builder = self.tunnel_builder.lock();
|
|
97
|
+
*builder = builder.clone().allow_cidr(cidr);
|
|
90
98
|
self
|
|
91
99
|
}
|
|
92
100
|
/// Restriction placed on the origin of incoming connections to the edge to deny these CIDR ranges.
|
|
93
101
|
/// Call multiple times to add additional CIDR ranges.
|
|
94
102
|
#[napi]
|
|
95
103
|
pub fn deny_cidr(&mut self, cidr: String) -> &Self {
|
|
96
|
-
|
|
104
|
+
let mut builder = self.tunnel_builder.lock();
|
|
105
|
+
*builder = builder.clone().deny_cidr(cidr);
|
|
97
106
|
self
|
|
98
107
|
}
|
|
99
|
-
/// 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.
|
|
100
109
|
#[napi]
|
|
101
110
|
pub fn proxy_proto(&mut self, proxy_proto: String) -> &Self {
|
|
102
|
-
|
|
111
|
+
let mut builder = self.tunnel_builder.lock();
|
|
112
|
+
*builder = builder.clone().proxy_proto(
|
|
103
113
|
ProxyProto::from_str(proxy_proto.as_str())
|
|
104
114
|
.unwrap_or_else(|_| panic!("Unknown proxy protocol: {:?}", proxy_proto)),
|
|
105
115
|
);
|
|
@@ -109,7 +119,8 @@ macro_rules! make_tunnel_builder {
|
|
|
109
119
|
/// bearing on tunnel behavior.
|
|
110
120
|
#[napi]
|
|
111
121
|
pub fn forwards_to(&mut self, forwards_to: String) -> &Self {
|
|
112
|
-
|
|
122
|
+
let mut builder = self.tunnel_builder.lock();
|
|
123
|
+
*builder = builder.clone().forwards_to(forwards_to);
|
|
113
124
|
self
|
|
114
125
|
}
|
|
115
126
|
}
|
|
@@ -122,7 +133,8 @@ macro_rules! make_tunnel_builder {
|
|
|
122
133
|
/// Add a label, value pair for this tunnel.
|
|
123
134
|
#[napi]
|
|
124
135
|
pub fn label(&mut self, label: String, value: String) -> &Self {
|
|
125
|
-
|
|
136
|
+
let mut builder = self.tunnel_builder.lock();
|
|
137
|
+
*builder = builder.clone().label(label, value);
|
|
126
138
|
self
|
|
127
139
|
}
|
|
128
140
|
}
|
package/trailer.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
export function
|
|
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,61 +6,114 @@ const net = require('net');
|
|
|
6
6
|
const fs = require('fs');
|
|
7
7
|
const os = require('os');
|
|
8
8
|
|
|
9
|
-
|
|
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.
|
|
66
|
+
async function defaultTunnel(bind) {
|
|
10
67
|
// set up a default session and tunnel
|
|
11
68
|
var builder = new NgrokSessionBuilder();
|
|
12
69
|
builder.authtokenFromEnv();
|
|
13
70
|
var session = await builder.connect();
|
|
14
|
-
var tunnel = await session.httpEndpoint().listen();
|
|
71
|
+
var tunnel = await session.httpEndpoint().listen(bind);
|
|
15
72
|
tunnel.session = session; // surface to caller
|
|
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
|
-
await 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
|
+
// turn off automatic bind
|
|
85
|
+
tunnel = await defaultTunnel(false);
|
|
38
86
|
}
|
|
39
|
-
// todo: abstract socket on linux: https://stackoverflow.com/a/60014174
|
|
40
|
-
// todo: named pipe on windows: https://nodejs.org/api/net.html#ipc-support
|
|
41
87
|
|
|
42
|
-
// attempt
|
|
88
|
+
// attempt pipe socket
|
|
43
89
|
try {
|
|
44
|
-
await
|
|
90
|
+
socket = await ngrokLinkPipe(tunnel, server);
|
|
45
91
|
} catch (err) {
|
|
46
92
|
console.debug("Using TCP socket. " + err);
|
|
47
93
|
// fallback to tcp socket
|
|
48
|
-
await ngrokLinkTcp(tunnel, server);
|
|
94
|
+
socket = await ngrokLinkTcp(tunnel, server);
|
|
49
95
|
}
|
|
96
|
+
registerCleanup(tunnel, socket);
|
|
50
97
|
|
|
51
98
|
server.tunnel = tunnel; // surface to caller
|
|
52
|
-
|
|
99
|
+
socket.tunnel = tunnel; // surface to caller
|
|
100
|
+
// return the newly created net.Server, which will be different in the express case
|
|
101
|
+
return socket;
|
|
53
102
|
}
|
|
54
103
|
|
|
55
104
|
async function ngrokLinkTcp(tunnel, server) {
|
|
56
105
|
// random local port
|
|
57
|
-
await server
|
|
106
|
+
const socket = await asyncListen(server, {host:'localhost', port:0});
|
|
58
107
|
// forward to socket
|
|
59
|
-
tunnel.forwardTcp('localhost:' +
|
|
108
|
+
tunnel.forwardTcp('localhost:' + socket.address().port);
|
|
109
|
+
return socket;
|
|
60
110
|
}
|
|
61
111
|
|
|
62
|
-
async function
|
|
63
|
-
|
|
112
|
+
async function ngrokLinkPipe(tunnel, server) {
|
|
113
|
+
var proposed = "tun-" + tunnel.id() + ".sock";
|
|
114
|
+
if (platform == 'win32') {
|
|
115
|
+
proposed = '\\\\.\\pipe\\' + proposed;
|
|
116
|
+
}
|
|
64
117
|
var filename;
|
|
65
118
|
try {
|
|
66
119
|
fs.accessSync(process.cwd(), fs.constants.W_OK);
|
|
@@ -77,16 +130,51 @@ async function ngrokLinkUnix(tunnel, server) {
|
|
|
77
130
|
}
|
|
78
131
|
|
|
79
132
|
// begin listening
|
|
80
|
-
await server
|
|
133
|
+
const socket = await asyncListen(server, {path: filename});
|
|
81
134
|
// tighten permissions
|
|
82
135
|
try {
|
|
83
|
-
|
|
136
|
+
if (platform != 'win32') {
|
|
137
|
+
fs.chmodSync(filename, fs.constants.S_IRWXU);
|
|
138
|
+
}
|
|
84
139
|
} catch (err) {
|
|
85
140
|
console.debug("Cannot change permissions of file: " + filename);
|
|
86
141
|
}
|
|
87
142
|
// forward tunnel
|
|
88
|
-
tunnel.
|
|
143
|
+
tunnel.forwardPipe(filename);
|
|
144
|
+
socket.path = filename; // surface to caller
|
|
145
|
+
|
|
146
|
+
return socket;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function registerCleanup(tunnel, socket) {
|
|
150
|
+
process.on('SIGINT', function() {
|
|
151
|
+
if (process.listenerCount('SIGINT') > 1) {
|
|
152
|
+
// user has registered a handler, abort this one
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
// close tunnel
|
|
156
|
+
if (tunnel) {
|
|
157
|
+
tunnel.close().then(()=>{
|
|
158
|
+
console.debug('ngrok closed tunnel: ' + tunnel.id());
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
// close webserver's socket
|
|
162
|
+
if (socket) {
|
|
163
|
+
socket.close(function () {
|
|
164
|
+
console.debug('ngrok closed socket');
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
// unregister any logging callback
|
|
168
|
+
loggingCallback();
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function consoleLog(level) {
|
|
173
|
+
loggingCallback((level, target, message) => {
|
|
174
|
+
console.log(`${level} ${target} - ${message}`);
|
|
175
|
+
}, level);
|
|
89
176
|
}
|
|
90
177
|
|
|
91
|
-
module.exports.
|
|
178
|
+
module.exports.listenable = listenable;
|
|
92
179
|
module.exports.listen = ngrokListen;
|
|
180
|
+
module.exports.consoleLog = consoleLog;
|
|
@@ -1,12 +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
|
-
});
|
|
12
|
-
|