Skip to content

Commit

Permalink
Merge pull request #147 from ngrok/ngrok/ryan/traffic-polilcy
Browse files Browse the repository at this point in the history
add `traffic_policy`, remap `policy`->`traffic_policy`
  • Loading branch information
TheConcierge authored Jul 30, 2024
2 parents 0f7c921 + 8741ac6 commit cd0d368
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 38 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mio = { version = "=0.8.6" }
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.1", default-features = false, features = ["napi4", "tokio_rt"] }
napi-derive = "2.12.1"
ngrok = { version = "=0.14.0-pre.13" }
ngrok = { version = "=0.14.0-pre.14" }
parking_lot = "0.12.1"
regex = "1.9.5"
rustls = "0.22.2"
Expand All @@ -32,3 +32,6 @@ napi-build = "2.0.1"

[profile.release]
lto = true

[package.metadata.cargo-udeps.ignore]
normal = ["mio"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ const listener = await ngrok.forward({
oidc_allow_domains: ["<domain>"],
oidc_allow_emails: ["<email>"],
oidc_scopes: ["<scope>"],
policy: "<policy_json>",
traffic_policy: "<policy_json>",
request_header_remove: ["X-Req-Nope"],
response_header_remove: ["X-Res-Nope"],
request_header_add: ["X-Req-Yup:true"],
Expand Down
18 changes: 18 additions & 0 deletions __test__/connect.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,21 @@ test("policy", async (t) => {
const response = await validateShutdown(t, httpServer, url);
t.is("bar", response.headers["foo"]);
});

test("traffic policy", async (t) => {
const trafficPolicy = fs.readFileSync(path.resolve("__test__", "policy.json"), "utf8");

const httpServer = await makeHttp();
const listener = await ngrok.forward({
addr: httpServer.listenTo,
authtoken: process.env["NGROK_AUTHTOKEN"],
proto: "http",
trafficPolicy: trafficPolicy,
});
const url = listener.url();

t.truthy(url);
t.truthy(url.startsWith("https://"), url);
const response = await validateShutdown(t, httpServer, url);
t.is("bar", response.headers["foo"]);
});
9 changes: 9 additions & 0 deletions __test__/online.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,12 @@ test("policy", async (t) => {
const response = await forwardValidateShutdown(t, httpServer, listener, listener.url());
t.is("bar", response.headers["foo"]);
});

test("traffic policy", async (t) => {
const trafficPolicy = fs.readFileSync(path.resolve("__test__", "policy.json"), "utf8");

const [httpServer, session] = await makeHttpAndSession();
const listener = await session.httpEndpoint().trafficPolicy(trafficPolicy).listen();
const response = await forwardValidateShutdown(t, httpServer, listener, listener.url());
t.is("bar", response.headers["foo"]);
});
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"rust-src"
"rustc"
"rustfmt"
"rust-analyzer"
];
node-toolchain = with pkgs; [
nodejs
Expand Down
40 changes: 15 additions & 25 deletions index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub struct Config {
/// 'closed' - connection is lost, 'connected' - reconnected
#[napi(ts_type = "(status: string) => void")]
pub on_status_change: Option<bool>,
/// The Traffic Policy to use for this endpoint.
/// DEPRECATED: use TrafficPolicy instead.
pub policy: Option<String>,
/// The port for the listener to forward to.
/// Only used if addr is not defined.
Expand Down Expand Up @@ -301,6 +301,8 @@ pub struct Config {
/// Unused, will warn and be ignored
#[napi(js_name = "terminate_at")]
pub terminate_at: Option<String>,
/// The Traffic Policy to use for this endpoint.
pub traffic_policy: Option<String>,
/// Whether to disable certificate verification for this listener
#[napi(js_name = "verify_upstream_tls")]
pub verify_upstream_tls: Option<bool>,
Expand Down
8 changes: 3 additions & 5 deletions src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ macro_rules! config_common {
plumb!($builder, $config, proxy_proto);
plumb!($builder, $config, forwards_to);
plumb!($builder, $config, verify_upstream_tls);

// returns a Result, so we can't use the macro
if let Some(ref v) = $config.policy {
$builder.policy(v.clone())?;
}
plumb!($builder, $config, traffic_policy);
// policy is in the process of being deprecated. for now, we just remap it to traffic_policy
plumb!($builder, $config, traffic_policy, policy);
};
}

Expand Down
14 changes: 9 additions & 5 deletions src/listener_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,16 @@ macro_rules! make_listener_builder {
self
}
#[napi]
pub fn policy(&mut self, policy: String) -> Result<&Self> {
pub fn policy(&mut self, policy: String) -> &Self {
let mut builder = self.listener_builder.lock();
match builder.policy(policy.as_str()) {
Ok(_) => Ok(self),
Err(e) => Err(napi_err(format!("Error parsing policy, {e}"))),
}
builder.traffic_policy(policy);
self
}
#[napi]
pub fn traffic_policy(&mut self, traffic_policy: String) -> &Self {
let mut builder = self.listener_builder.lock();
builder.traffic_policy(traffic_policy);
self
}
}
};
Expand Down

0 comments on commit cd0d368

Please sign in to comment.