@ngrok/ngrok 0.9.0 → 1.0.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 +10 -0
- package/CONTRIBUTING.md +4 -4
- package/README.md +176 -142
- package/index.d.ts +75 -15
- package/index.js +8 -6
- package/package.json +37 -16
- package/.envrc +0 -1
- package/.gitattributes +0 -3
- package/.prettierrc.json +0 -3
- package/.yarnrc.yml +0 -3
- package/Cargo.toml +0 -34
- package/RELEASE.md +0 -26
- package/build.rs +0 -5
- package/docs/.nojekyll +0 -1
- package/docs/assets/highlight.css +0 -92
- package/docs/assets/main.js +0 -58
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1367
- package/docs/classes/HttpListenerBuilder.html +0 -594
- package/docs/classes/LabeledListenerBuilder.html +0 -206
- package/docs/classes/Listener.html +0 -241
- package/docs/classes/Session.html +0 -207
- package/docs/classes/SessionBuilder.html +0 -465
- package/docs/classes/TcpListenerBuilder.html +0 -281
- package/docs/classes/TlsListenerBuilder.html +0 -322
- package/docs/classes/UpdateRequest.html +0 -131
- package/docs/functions/authtoken.html +0 -89
- package/docs/functions/connect.html +0 -89
- package/docs/functions/consoleLog.html +0 -90
- package/docs/functions/disconnect.html +0 -89
- package/docs/functions/getListener.html +0 -89
- package/docs/functions/getListenerByUrl.html +0 -89
- package/docs/functions/kill.html +0 -84
- package/docs/functions/listen.html +0 -92
- package/docs/functions/listenable.html +0 -85
- package/docs/functions/listeners.html +0 -84
- package/docs/functions/loggingCallback.html +0 -108
- package/docs/index.html +0 -292
- package/docs/interfaces/Config.html +0 -734
- package/docs/modules.html +0 -106
- package/examples/domain.crt +0 -18
- package/examples/domain.key +0 -27
- package/examples/express/ngrok-express-quickstart.js +0 -11
- package/examples/express/ngrok-express.js +0 -28
- package/examples/express/package.json +0 -7
- package/examples/fastify/ngrok-fastify.js +0 -26
- package/examples/fastify/package.json +0 -7
- package/examples/hapi/ngrok-hapi.js +0 -35
- package/examples/hapi/package.json +0 -7
- package/examples/koa/ngrok-koa.js +0 -12
- package/examples/koa/package.json +0 -7
- package/examples/nestjs/.eslintrc.js +0 -25
- package/examples/nestjs/.prettierrc +0 -4
- package/examples/nestjs/README.md +0 -73
- package/examples/nestjs/nest-cli.json +0 -8
- package/examples/nestjs/package.json +0 -70
- package/examples/nestjs/src/app.controller.spec.ts +0 -22
- package/examples/nestjs/src/app.controller.ts +0 -12
- package/examples/nestjs/src/app.module.ts +0 -10
- package/examples/nestjs/src/app.service.ts +0 -8
- package/examples/nestjs/src/main.ts +0 -17
- package/examples/nestjs/test/app.e2e-spec.ts +0 -24
- package/examples/nestjs/test/jest-e2e.json +0 -9
- package/examples/nestjs/tsconfig.build.json +0 -4
- package/examples/nestjs/tsconfig.json +0 -21
- package/examples/nextjs/README.md +0 -3
- package/examples/nextjs/next.config.js +0 -12
- package/examples/nextjs/ngrok.config.js +0 -23
- package/examples/nextjs/package.json +0 -6
- package/examples/nextjs/pages/index.js +0 -5
- package/examples/ngrok-connect-full.js +0 -67
- package/examples/ngrok-connect-minimal.js +0 -17
- package/examples/ngrok-http-full.js +0 -81
- package/examples/ngrok-http-minimum.js +0 -18
- package/examples/ngrok-labeled.js +0 -43
- package/examples/ngrok-listen.js +0 -11
- package/examples/ngrok-tcp.js +0 -37
- package/examples/ngrok-tls.js +0 -40
- package/examples/ngrok-typescript.ts +0 -69
- package/examples/ngrok-windows-pipe.js +0 -25
- package/examples/ngrok-winston.js +0 -24
- package/examples/package.json +0 -15
- package/examples/remix/.eslintrc.js +0 -4
- package/examples/remix/README.md +0 -53
- package/examples/remix/app/entry.client.tsx +0 -18
- package/examples/remix/app/entry.server.tsx +0 -101
- package/examples/remix/app/root.tsx +0 -20
- package/examples/remix/app/routes/_index.tsx +0 -30
- package/examples/remix/ngrok.config.js +0 -16
- package/examples/remix/package.json +0 -30
- package/examples/remix/public/favicon.ico +0 -0
- package/examples/remix/remix.config.js +0 -16
- package/examples/remix/remix.env.d.ts +0 -2
- package/examples/remix/tsconfig.json +0 -22
- package/examples/svelte/README.md +0 -38
- package/examples/svelte/jsconfig.json +0 -17
- package/examples/svelte/ngrok.config.cjs +0 -20
- package/examples/svelte/package.json +0 -22
- package/examples/svelte/src/app.d.ts +0 -12
- package/examples/svelte/src/app.html +0 -12
- package/examples/svelte/src/routes/+page.svelte +0 -2
- package/examples/svelte/static/favicon.png +0 -0
- package/examples/svelte/svelte.config.js +0 -14
- package/examples/svelte/vite.config.js +0 -6
- package/examples/vue/.vscode/extensions.json +0 -3
- package/examples/vue/README.md +0 -40
- package/examples/vue/env.d.ts +0 -1
- package/examples/vue/index.html +0 -13
- package/examples/vue/ngrok.config.ts +0 -18
- package/examples/vue/package.json +0 -25
- package/examples/vue/public/favicon.ico +0 -0
- package/examples/vue/src/App.vue +0 -47
- package/examples/vue/src/assets/base.css +0 -74
- package/examples/vue/src/assets/logo.svg +0 -1
- package/examples/vue/src/assets/main.css +0 -35
- package/examples/vue/src/components/HelloWorld.vue +0 -40
- package/examples/vue/src/components/TheWelcome.vue +0 -86
- package/examples/vue/src/components/WelcomeItem.vue +0 -86
- package/examples/vue/src/components/icons/IconCommunity.vue +0 -7
- package/examples/vue/src/components/icons/IconDocumentation.vue +0 -7
- package/examples/vue/src/components/icons/IconEcosystem.vue +0 -7
- package/examples/vue/src/components/icons/IconSupport.vue +0 -7
- package/examples/vue/src/components/icons/IconTooling.vue +0 -19
- package/examples/vue/src/main.ts +0 -6
- package/examples/vue/tsconfig.json +0 -16
- package/examples/vue/tsconfig.node.json +0 -8
- package/examples/vue/vite.config.ts +0 -15
- package/flake.lock +0 -82
- package/flake.nix +0 -111
- package/post-build.js +0 -45
- package/scripts/gc-node-drop.js +0 -49
- package/scripts/gc-rust-drop.js +0 -53
- package/src/config.rs +0 -285
- package/src/connect.rs +0 -342
- package/src/http.rs +0 -244
- package/src/lib.rs +0 -39
- package/src/listener.rs +0 -495
- package/src/listener_builder.rs +0 -204
- package/src/logging.rs +0 -140
- package/src/session.rs +0 -551
- package/src/tcp.rs +0 -18
- package/src/tls.rs +0 -44
- package/trailer.d.ts +0 -15
- package/trailer.js +0 -382
- package/tsconfig.json +0 -8
- package/typedoc.json +0 -3
package/src/listener.rs
DELETED
|
@@ -1,495 +0,0 @@
|
|
|
1
|
-
use core::result::Result as CoreResult;
|
|
2
|
-
use std::{
|
|
3
|
-
collections::HashMap,
|
|
4
|
-
error::Error,
|
|
5
|
-
io,
|
|
6
|
-
sync::Arc,
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
use async_trait::async_trait;
|
|
10
|
-
use futures::prelude::*;
|
|
11
|
-
use lazy_static::lazy_static;
|
|
12
|
-
use napi::bindgen_prelude::*;
|
|
13
|
-
use napi_derive::napi;
|
|
14
|
-
use ngrok::{
|
|
15
|
-
forwarder::Forwarder,
|
|
16
|
-
prelude::*,
|
|
17
|
-
session::ConnectError,
|
|
18
|
-
tunnel::{
|
|
19
|
-
HttpTunnel,
|
|
20
|
-
LabeledTunnel,
|
|
21
|
-
TcpTunnel,
|
|
22
|
-
TlsTunnel,
|
|
23
|
-
},
|
|
24
|
-
Session,
|
|
25
|
-
};
|
|
26
|
-
use regex::Regex;
|
|
27
|
-
use tokio::{
|
|
28
|
-
sync::Mutex,
|
|
29
|
-
task::JoinHandle,
|
|
30
|
-
};
|
|
31
|
-
use tracing::{
|
|
32
|
-
debug,
|
|
33
|
-
info,
|
|
34
|
-
};
|
|
35
|
-
use url::Url;
|
|
36
|
-
|
|
37
|
-
use crate::{
|
|
38
|
-
napi_err,
|
|
39
|
-
napi_ngrok_err,
|
|
40
|
-
};
|
|
41
|
-
|
|
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://";
|
|
45
|
-
|
|
46
|
-
lazy_static! {
|
|
47
|
-
// listener references to be kept until explicit close to prevent nodejs gc from dropping them.
|
|
48
|
-
// the listener wrapper object, and the underlying listener, both have references to the Session
|
|
49
|
-
// so the Session is safe from premature dropping.
|
|
50
|
-
static ref GLOBAL_LISTENERS: Mutex<HashMap<String,Arc<Storage>>> = Mutex::new(HashMap::new());
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/// Stores the listener and session references to be kept until explicit close.
|
|
54
|
-
struct Storage {
|
|
55
|
-
listener: Option<Arc<Mutex<dyn ExtendedListener>>>,
|
|
56
|
-
forwarder: Option<Arc<Mutex<dyn ExtendedForwarder>>>,
|
|
57
|
-
session: Session,
|
|
58
|
-
tun_meta: Arc<ListenerMetadata>,
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
struct ListenerMetadata {
|
|
62
|
-
id: String,
|
|
63
|
-
forwards_to: String,
|
|
64
|
-
metadata: String,
|
|
65
|
-
url: Option<String>,
|
|
66
|
-
proto: Option<String>,
|
|
67
|
-
labels: HashMap<String, String>,
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/// The upstream cannot be turned into an object since it contains generics, so implementing
|
|
71
|
-
/// a proxy trait without generics which can be the dyn type stored in the global map.
|
|
72
|
-
#[async_trait]
|
|
73
|
-
pub trait ExtendedListener: 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>>;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/// An ngrok listener.
|
|
82
|
-
///
|
|
83
|
-
/// @group Listener and Sessions
|
|
84
|
-
#[napi(custom_finalize)]
|
|
85
|
-
#[allow(dead_code)]
|
|
86
|
-
pub struct Listener {
|
|
87
|
-
session: Session,
|
|
88
|
-
tun_meta: Arc<ListenerMetadata>,
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
macro_rules! make_listener_type {
|
|
92
|
-
// the common (non-labeled) branch
|
|
93
|
-
($(#[$outer:meta])* $wrapper:ident, $listener:tt, common) => {
|
|
94
|
-
$(#[$outer])*
|
|
95
|
-
#[allow(dead_code)]
|
|
96
|
-
pub(crate) struct $wrapper {
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
#[napi]
|
|
100
|
-
#[allow(dead_code)]
|
|
101
|
-
impl $wrapper {
|
|
102
|
-
pub(crate) async fn new_listener(session: Session, raw_listener: $listener) -> Listener {
|
|
103
|
-
let id = raw_listener.id().to_string();
|
|
104
|
-
let tun_meta = Arc::new(ListenerMetadata {
|
|
105
|
-
id: id.clone(),
|
|
106
|
-
forwards_to: raw_listener.forwards_to().to_string(),
|
|
107
|
-
metadata: raw_listener.metadata().to_string(),
|
|
108
|
-
url: Some(raw_listener.url().to_string()),
|
|
109
|
-
proto: Some(raw_listener.proto().to_string()),
|
|
110
|
-
labels: HashMap::new(),
|
|
111
|
-
});
|
|
112
|
-
info!("Created listener {id:?} with url {:?}", raw_listener.url());
|
|
113
|
-
// keep a listener reference until an explicit call to close to prevent nodejs gc dropping it
|
|
114
|
-
let storage = Arc::new(Storage {
|
|
115
|
-
listener: Some(Arc::new(Mutex::new(raw_listener))),
|
|
116
|
-
forwarder: None,
|
|
117
|
-
session,
|
|
118
|
-
tun_meta,
|
|
119
|
-
});
|
|
120
|
-
GLOBAL_LISTENERS.lock().await.insert(id, storage.clone());
|
|
121
|
-
// create the user-facing object
|
|
122
|
-
Listener::from_storage(&storage)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
pub(crate) async fn new_forwarder(session: Session, forwarder: Forwarder<$listener>) -> Listener {
|
|
126
|
-
let id = forwarder.id().to_string();
|
|
127
|
-
let tun_meta = Arc::new(ListenerMetadata {
|
|
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 listener {id:?} with url {:?}", forwarder.url());
|
|
136
|
-
// keep a listener reference until an explicit call to close to prevent python gc dropping it
|
|
137
|
-
let storage = Arc::new(Storage {
|
|
138
|
-
listener: None,
|
|
139
|
-
forwarder: Some(Arc::new(Mutex::new(forwarder))),
|
|
140
|
-
session,
|
|
141
|
-
tun_meta,
|
|
142
|
-
});
|
|
143
|
-
GLOBAL_LISTENERS.lock().await.insert(id, storage.clone());
|
|
144
|
-
// create the user-facing object
|
|
145
|
-
Listener::from_storage(&storage)
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
make_listener_type!($wrapper, $listener);
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
// the labeled branch
|
|
153
|
-
($(#[$outer:meta])* $wrapper:ident, $listener:tt, label) => {
|
|
154
|
-
#[allow(dead_code)]
|
|
155
|
-
pub(crate) struct $wrapper {
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
#[napi]
|
|
159
|
-
#[allow(dead_code)]
|
|
160
|
-
impl $wrapper {
|
|
161
|
-
pub(crate) async fn new_listener(session: Session, raw_listener: $listener) -> Listener {
|
|
162
|
-
let id = raw_listener.id().to_string();
|
|
163
|
-
let tun_meta = Arc::new(ListenerMetadata {
|
|
164
|
-
id: id.clone(),
|
|
165
|
-
forwards_to: raw_listener.forwards_to().to_string(),
|
|
166
|
-
metadata: raw_listener.metadata().to_string(),
|
|
167
|
-
url: None,
|
|
168
|
-
proto: None,
|
|
169
|
-
labels: raw_listener.labels().clone(),
|
|
170
|
-
});
|
|
171
|
-
info!("Created listener {id:?} with labels {:?}", tun_meta.labels);
|
|
172
|
-
// keep a listener reference until an explicit call to close to prevent nodejs gc dropping it
|
|
173
|
-
let storage = Arc::new(Storage {
|
|
174
|
-
listener: Some(Arc::new(Mutex::new(raw_listener))),
|
|
175
|
-
forwarder: None,
|
|
176
|
-
session,
|
|
177
|
-
tun_meta,
|
|
178
|
-
});
|
|
179
|
-
GLOBAL_LISTENERS.lock().await.insert(id, storage.clone());
|
|
180
|
-
// create the user-facing object
|
|
181
|
-
Listener::from_storage(&storage)
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
pub(crate) async fn new_forwarder(session: Session, forwarder: Forwarder<$listener>) -> Listener {
|
|
185
|
-
let id = forwarder.id().to_string();
|
|
186
|
-
let tun_meta = Arc::new(ListenerMetadata {
|
|
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 listener {id:?} with labels {:?}", tun_meta.labels);
|
|
195
|
-
// keep a listener reference until an explicit call to close to prevent python gc dropping it
|
|
196
|
-
let storage = Arc::new(Storage {
|
|
197
|
-
listener: None,
|
|
198
|
-
forwarder: Some(Arc::new(Mutex::new(forwarder))),
|
|
199
|
-
session,
|
|
200
|
-
tun_meta,
|
|
201
|
-
});
|
|
202
|
-
GLOBAL_LISTENERS.lock().await.insert(id, storage.clone());
|
|
203
|
-
// create the user-facing object
|
|
204
|
-
Listener::from_storage(&storage)
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
make_listener_type!($wrapper, $listener);
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
// all listeners get these
|
|
212
|
-
($wrapper:ident, $listener:tt) => {
|
|
213
|
-
#[async_trait]
|
|
214
|
-
impl ExtendedListener for $listener {
|
|
215
|
-
#[allow(deprecated)]
|
|
216
|
-
async fn fwd(&mut self, url: Url) -> CoreResult<(), io::Error> {
|
|
217
|
-
ngrok::prelude::TunnelExt::forward(self, url).await
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
impl ExtendedForwarder for Forwarder<$listener> {
|
|
222
|
-
fn get_join(&mut self) -> &mut JoinHandle<CoreResult<(), io::Error>> {
|
|
223
|
-
self.join()
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
#[napi]
|
|
230
|
-
#[allow(dead_code)]
|
|
231
|
-
impl Listener {
|
|
232
|
-
/// Create Listener from Storage
|
|
233
|
-
fn from_storage(storage: &Arc<Storage>) -> Listener {
|
|
234
|
-
// create the user-facing object
|
|
235
|
-
Listener {
|
|
236
|
-
session: storage.session.clone(),
|
|
237
|
-
tun_meta: storage.tun_meta.clone(),
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/// The URL that this listener backs.
|
|
242
|
-
#[napi]
|
|
243
|
-
pub fn url(&self) -> Option<String> {
|
|
244
|
-
self.tun_meta.url.clone()
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/// The protocol of the endpoint that this listener backs.
|
|
248
|
-
#[napi]
|
|
249
|
-
pub fn proto(&self) -> Option<String> {
|
|
250
|
-
self.tun_meta.proto.clone()
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/// The labels this listener was started with.
|
|
254
|
-
#[napi]
|
|
255
|
-
pub fn labels(&self) -> HashMap<String, String> {
|
|
256
|
-
self.tun_meta.labels.clone()
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/// Returns a listener's unique ID.
|
|
260
|
-
#[napi]
|
|
261
|
-
pub fn id(&self) -> String {
|
|
262
|
-
self.tun_meta.id.clone()
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/// Returns a human-readable string presented in the ngrok dashboard
|
|
266
|
-
/// and the API. Use the [HttpListenerBuilder::forwards_to],
|
|
267
|
-
/// [TcpListenerBuilder::forwards_to], etc. to set this value
|
|
268
|
-
/// explicitly.
|
|
269
|
-
#[napi]
|
|
270
|
-
pub fn forwards_to(&self) -> String {
|
|
271
|
-
self.tun_meta.forwards_to.clone()
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/// Returns the arbitrary metadata string for this listener.
|
|
275
|
-
#[napi]
|
|
276
|
-
pub fn metadata(&self) -> String {
|
|
277
|
-
self.tun_meta.metadata.clone()
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/// Forward incoming listener 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
|
|
283
|
-
#[napi]
|
|
284
|
-
pub async fn forward(&self, addr: String) -> Result<()> {
|
|
285
|
-
forward(&self.tun_meta.id, addr).await
|
|
286
|
-
}
|
|
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("Listener is not joinable"))
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/// Close the listener.
|
|
308
|
-
///
|
|
309
|
-
/// This is an RPC call that must be `.await`ed.
|
|
310
|
-
/// It is equivalent to calling `Session::close_listener` with this
|
|
311
|
-
/// listener's ID.
|
|
312
|
-
#[napi]
|
|
313
|
-
pub async fn close(&self) -> Result<()> {
|
|
314
|
-
debug!("Listener closing, id: {}", self.tun_meta.id);
|
|
315
|
-
|
|
316
|
-
// we may not be able to lock our reference to the listener due to the forward_* calls which
|
|
317
|
-
// continuously accept-loop while the listener is active, so calling close on the Session.
|
|
318
|
-
let res = self
|
|
319
|
-
.session
|
|
320
|
-
.close_tunnel(self.tun_meta.id.clone())
|
|
321
|
-
.await
|
|
322
|
-
.map_err(|e| napi_ngrok_err("error closing listener", &e));
|
|
323
|
-
|
|
324
|
-
// drop our internal reference to the listener after awaiting close
|
|
325
|
-
GLOBAL_LISTENERS.lock().await.remove(&self.tun_meta.id);
|
|
326
|
-
|
|
327
|
-
res
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
#[allow(unused_mut)]
|
|
332
|
-
impl ObjectFinalize for Listener {
|
|
333
|
-
fn finalize(mut self, _env: Env) -> Result<()> {
|
|
334
|
-
debug!("Listener finalize, id: {}", self.tun_meta.id);
|
|
335
|
-
Ok(())
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
make_listener_type! {
|
|
340
|
-
/// An ngrok listener backing an HTTP endpoint.
|
|
341
|
-
///
|
|
342
|
-
/// @group Listeners
|
|
343
|
-
HttpListener, HttpTunnel, common
|
|
344
|
-
}
|
|
345
|
-
make_listener_type! {
|
|
346
|
-
/// An ngrok listener backing a TCP endpoint.
|
|
347
|
-
///
|
|
348
|
-
/// @group Listeners
|
|
349
|
-
TcpListener, TcpTunnel, common
|
|
350
|
-
}
|
|
351
|
-
make_listener_type! {
|
|
352
|
-
/// An ngrok listener bcking a TLS endpoint.
|
|
353
|
-
///
|
|
354
|
-
/// @group Listeners
|
|
355
|
-
TlsListener, TlsTunnel, common
|
|
356
|
-
}
|
|
357
|
-
make_listener_type! {
|
|
358
|
-
/// A labeled ngrok listener.
|
|
359
|
-
///
|
|
360
|
-
/// @group Listeners
|
|
361
|
-
LabeledListener, LabeledTunnel, label
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
pub async fn forward(id: &String, mut addr: String) -> Result<()> {
|
|
365
|
-
let tun_option = &get_storage_by_id(id).await?.listener;
|
|
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}")))?;
|
|
381
|
-
|
|
382
|
-
info!("Listener {id:?} forwarding to {:?}", url.to_string());
|
|
383
|
-
let res = tun.lock().await.fwd(url).await;
|
|
384
|
-
|
|
385
|
-
debug!("forward returning");
|
|
386
|
-
canceled_is_ok(res)
|
|
387
|
-
} else {
|
|
388
|
-
Err(napi_err("listener is not forwardable"))
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
fn canceled_is_ok(input: CoreResult<(), io::Error>) -> Result<()> {
|
|
393
|
-
match input {
|
|
394
|
-
Ok(_) => Ok(()),
|
|
395
|
-
Err(e) => {
|
|
396
|
-
if let Some(source) = e
|
|
397
|
-
.source()
|
|
398
|
-
.and_then(|s| s.downcast_ref::<Arc<ConnectError>>())
|
|
399
|
-
{
|
|
400
|
-
if let ConnectError::Canceled = **source {
|
|
401
|
-
debug!("Reconnect was canceled, session is closing, returning Ok");
|
|
402
|
-
return Ok(());
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
Err(napi_err(format!("error forwarding: {e:?}")))
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
async fn get_storage_by_id(id: &String) -> Result<Arc<Storage>> {
|
|
412
|
-
// we must clone the Arc before any locking so there is a local reference
|
|
413
|
-
// to the mutex to unlock if the listener wrapper struct is dropped.
|
|
414
|
-
Ok(GLOBAL_LISTENERS
|
|
415
|
-
.lock()
|
|
416
|
-
.await
|
|
417
|
-
.get(id)
|
|
418
|
-
.ok_or(napi_err("Listener is no longer running"))?
|
|
419
|
-
.clone()) // required clone
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/// Delete any reference to the listener id
|
|
423
|
-
pub(crate) async fn remove_global_listener(id: &String) {
|
|
424
|
-
GLOBAL_LISTENERS.lock().await.remove(id);
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
/// Close a listener with the given url, or all listeners if no url is defined.
|
|
428
|
-
pub(crate) async fn close_url(url: Option<String>) -> Result<()> {
|
|
429
|
-
let mut close_ids: Vec<String> = vec![];
|
|
430
|
-
let listeners = GLOBAL_LISTENERS.lock().await;
|
|
431
|
-
for (id, storage) in listeners.iter() {
|
|
432
|
-
debug!("listener: {}", id);
|
|
433
|
-
if url.as_ref().is_none() || url == storage.tun_meta.url {
|
|
434
|
-
debug!("closing listener: {}", id);
|
|
435
|
-
storage
|
|
436
|
-
.session
|
|
437
|
-
.close_tunnel(id)
|
|
438
|
-
.await
|
|
439
|
-
.map_err(|e| napi_ngrok_err("error closing listener", &e))?;
|
|
440
|
-
close_ids.push(id.clone());
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
drop(listeners); // unlock GLOBAL_LISTENERS
|
|
444
|
-
|
|
445
|
-
// remove references entirely
|
|
446
|
-
for id in close_ids {
|
|
447
|
-
remove_global_listener(&id).await;
|
|
448
|
-
}
|
|
449
|
-
Ok(())
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
/// Make a list of all listeners by iterating over the global listener map and creating an Listener from each.
|
|
453
|
-
pub(crate) async fn search_listeners(
|
|
454
|
-
session_id: Option<String>,
|
|
455
|
-
url: Option<String>,
|
|
456
|
-
) -> Vec<Listener> {
|
|
457
|
-
let mut listeners: Vec<Listener> = vec![];
|
|
458
|
-
for (_, storage) in GLOBAL_LISTENERS.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 Listener from the storage
|
|
470
|
-
listeners.push(Listener::from_storage(storage));
|
|
471
|
-
}
|
|
472
|
-
listeners
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
/// Retrieve a list of non-closed listeners, in no particular order.
|
|
476
|
-
#[napi]
|
|
477
|
-
pub async fn listeners() -> Vec<Listener> {
|
|
478
|
-
search_listeners(None, None).await
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
/// Retrieve listener using the id
|
|
482
|
-
#[napi]
|
|
483
|
-
pub async fn get_listener(id: String) -> Option<Listener> {
|
|
484
|
-
GLOBAL_LISTENERS
|
|
485
|
-
.lock()
|
|
486
|
-
.await
|
|
487
|
-
.get(&id)
|
|
488
|
-
.map(Listener::from_storage)
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
/// Retrieve listener using the url
|
|
492
|
-
#[napi]
|
|
493
|
-
pub async fn get_listener_by_url(url: String) -> Option<Listener> {
|
|
494
|
-
search_listeners(None, Some(url)).await.into_iter().next()
|
|
495
|
-
}
|
package/src/listener_builder.rs
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
use std::{
|
|
2
|
-
str::FromStr,
|
|
3
|
-
sync::Arc,
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
use napi::bindgen_prelude::*;
|
|
7
|
-
use napi_derive::napi;
|
|
8
|
-
use ngrok::{
|
|
9
|
-
config::{
|
|
10
|
-
HttpTunnelBuilder,
|
|
11
|
-
LabeledTunnelBuilder,
|
|
12
|
-
ProxyProto,
|
|
13
|
-
TcpTunnelBuilder,
|
|
14
|
-
TlsTunnelBuilder,
|
|
15
|
-
},
|
|
16
|
-
prelude::*,
|
|
17
|
-
Session,
|
|
18
|
-
};
|
|
19
|
-
use parking_lot::Mutex;
|
|
20
|
-
use tracing::debug;
|
|
21
|
-
use url::Url;
|
|
22
|
-
|
|
23
|
-
use crate::{
|
|
24
|
-
listener::{
|
|
25
|
-
HttpListener,
|
|
26
|
-
LabeledListener,
|
|
27
|
-
Listener,
|
|
28
|
-
TcpListener,
|
|
29
|
-
TlsListener,
|
|
30
|
-
},
|
|
31
|
-
napi_err,
|
|
32
|
-
napi_ngrok_err,
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
macro_rules! make_listener_builder {
|
|
36
|
-
($(#[$outer:meta])* $wrapper:ident, $builder:tt, $listener:tt, $mode:tt) => {
|
|
37
|
-
$(#[$outer])*
|
|
38
|
-
#[napi(custom_finalize)]
|
|
39
|
-
#[allow(dead_code)]
|
|
40
|
-
pub(crate) struct $wrapper {
|
|
41
|
-
session: Arc<Mutex<Session>>,
|
|
42
|
-
pub(crate) listener_builder: Arc<Mutex<$builder>>,
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
#[napi]
|
|
46
|
-
#[allow(dead_code)]
|
|
47
|
-
impl $wrapper {
|
|
48
|
-
pub(crate) fn new(session: Session, raw_listener_builder: $builder) -> Self {
|
|
49
|
-
$wrapper {
|
|
50
|
-
session: Arc::new(Mutex::new(session)),
|
|
51
|
-
listener_builder: Arc::new(Mutex::new(raw_listener_builder)),
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/// Listener-specific opaque metadata. Viewable via the API.
|
|
56
|
-
#[napi]
|
|
57
|
-
pub fn metadata(&mut self, metadata: String) -> &Self {
|
|
58
|
-
let mut builder = self.listener_builder.lock();
|
|
59
|
-
builder.metadata(metadata);
|
|
60
|
-
self
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/// Begin listening for new connections on this listener.
|
|
64
|
-
#[napi]
|
|
65
|
-
pub async fn listen(&self, _bind: Option<bool>) -> Result<Listener> {
|
|
66
|
-
let session = self.session.lock().clone();
|
|
67
|
-
let tun = self.listener_builder.lock().clone();
|
|
68
|
-
let result = tun
|
|
69
|
-
.listen()
|
|
70
|
-
.await
|
|
71
|
-
.map_err(|e| napi_ngrok_err("failed to start listener", &e));
|
|
72
|
-
|
|
73
|
-
// create the wrapping listener object via its async new()
|
|
74
|
-
match result {
|
|
75
|
-
Ok(raw_tun) => Ok($listener::new_listener(session, raw_tun).await),
|
|
76
|
-
Err(val) => Err(val),
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/// Begin listening for new connections on this listener and forwarding them to the given url.
|
|
81
|
-
#[napi]
|
|
82
|
-
pub async fn listen_and_forward(&self, to_url: String) -> Result<Listener> {
|
|
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.listener_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 listener", &e));
|
|
91
|
-
|
|
92
|
-
// create the wrapping listener object via its async new()
|
|
93
|
-
match result {
|
|
94
|
-
Ok(raw_fwd) => Ok($listener::new_forwarder(session, raw_fwd).await),
|
|
95
|
-
Err(val) => Err(val),
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/// Begin listening for new connections on this listener 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<Listener> {
|
|
102
|
-
Err(napi_err(format!("listen_and_serve implemented in wrapper, {server}")))
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
impl ObjectFinalize for $wrapper {
|
|
107
|
-
fn finalize(self, mut _env: Env) -> Result<()> {
|
|
108
|
-
debug!("{} finalize", stringify!($wrapper));
|
|
109
|
-
Ok(())
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
make_listener_builder!($mode, $wrapper);
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
(common, $wrapper:ty) => {
|
|
117
|
-
#[napi]
|
|
118
|
-
#[allow(dead_code)]
|
|
119
|
-
impl $wrapper {
|
|
120
|
-
/// Restriction placed on the origin of incoming connections to the edge to only allow these CIDR ranges.
|
|
121
|
-
/// Call multiple times to add additional CIDR ranges.
|
|
122
|
-
/// See [IP restrictions] in the ngrok docs for additional details.
|
|
123
|
-
///
|
|
124
|
-
/// [IP restrictions]: https://ngrok.com/docs/cloud-edge/modules/ip-restrictions/
|
|
125
|
-
#[napi]
|
|
126
|
-
pub fn allow_cidr(&mut self, cidr: String) -> &Self {
|
|
127
|
-
let mut builder = self.listener_builder.lock();
|
|
128
|
-
builder.allow_cidr(cidr);
|
|
129
|
-
self
|
|
130
|
-
}
|
|
131
|
-
/// Restriction placed on the origin of incoming connections to the edge to deny these CIDR ranges.
|
|
132
|
-
/// Call multiple times to add additional CIDR ranges.
|
|
133
|
-
/// See [IP restrictions] in the ngrok docs for additional details.
|
|
134
|
-
///
|
|
135
|
-
/// [IP restrictions]: https://ngrok.com/docs/cloud-edge/modules/ip-restrictions/
|
|
136
|
-
#[napi]
|
|
137
|
-
pub fn deny_cidr(&mut self, cidr: String) -> &Self {
|
|
138
|
-
let mut builder = self.listener_builder.lock();
|
|
139
|
-
builder.deny_cidr(cidr);
|
|
140
|
-
self
|
|
141
|
-
}
|
|
142
|
-
/// The version of PROXY protocol to use with this listener "1", "2", or "" if not using.
|
|
143
|
-
#[napi]
|
|
144
|
-
pub fn proxy_proto(&mut self, proxy_proto: String) -> &Self {
|
|
145
|
-
let mut builder = self.listener_builder.lock();
|
|
146
|
-
builder.proxy_proto(
|
|
147
|
-
ProxyProto::from_str(proxy_proto.as_str())
|
|
148
|
-
.unwrap_or_else(|_| panic!("Unknown proxy protocol: {:?}", proxy_proto)),
|
|
149
|
-
);
|
|
150
|
-
self
|
|
151
|
-
}
|
|
152
|
-
/// Listener backend metadata. Viewable via the dashboard and API, but has no
|
|
153
|
-
/// bearing on listener behavior.
|
|
154
|
-
#[napi]
|
|
155
|
-
pub fn forwards_to(&mut self, forwards_to: String) -> &Self {
|
|
156
|
-
let mut builder = self.listener_builder.lock();
|
|
157
|
-
builder.forwards_to(forwards_to);
|
|
158
|
-
self
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
(label, $wrapper:ty) => {
|
|
164
|
-
#[napi]
|
|
165
|
-
#[allow(dead_code)]
|
|
166
|
-
impl $wrapper {
|
|
167
|
-
/// Add a label, value pair for this listener.
|
|
168
|
-
/// See [Using Labels] in the ngrok docs for additional details.
|
|
169
|
-
///
|
|
170
|
-
/// [Using Labels]: https://ngrok.com/docs/guides/using-labels-within-ngrok/
|
|
171
|
-
#[napi]
|
|
172
|
-
pub fn label(&mut self, label: String, value: String) -> &Self {
|
|
173
|
-
let mut builder = self.listener_builder.lock();
|
|
174
|
-
builder.label(label, value);
|
|
175
|
-
self
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
make_listener_builder! {
|
|
182
|
-
/// An ngrok listener backing an HTTP endpoint.
|
|
183
|
-
///
|
|
184
|
-
/// @group Listener Builders
|
|
185
|
-
HttpListenerBuilder, HttpTunnelBuilder, HttpListener, common
|
|
186
|
-
}
|
|
187
|
-
make_listener_builder! {
|
|
188
|
-
/// An ngrok listener backing a TCP endpoint.
|
|
189
|
-
///
|
|
190
|
-
/// @group Listener Builders
|
|
191
|
-
TcpListenerBuilder, TcpTunnelBuilder, TcpListener, common
|
|
192
|
-
}
|
|
193
|
-
make_listener_builder! {
|
|
194
|
-
/// An ngrok listener backing a TLS endpoint.
|
|
195
|
-
///
|
|
196
|
-
/// @group Listener Builders
|
|
197
|
-
TlsListenerBuilder, TlsTunnelBuilder, TlsListener, common
|
|
198
|
-
}
|
|
199
|
-
make_listener_builder! {
|
|
200
|
-
/// A labeled ngrok listener.
|
|
201
|
-
///
|
|
202
|
-
/// @group Listener Builders
|
|
203
|
-
LabeledListenerBuilder, LabeledTunnelBuilder, LabeledListener, label
|
|
204
|
-
}
|