Skip to content

Commit

Permalink
Merge pull request #114 from ngrok/ryno/policy-support
Browse files Browse the repository at this point in the history
Add support for Traffic Policy
  • Loading branch information
bobzilladev authored Feb 1, 2024
2 parents be1247c + 2e325e7 commit bde512c
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ignore artifacts:
*.css
*.json
!__test__/policy.json
*.html
*.md
*.vue
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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.8" }
ngrok = { version = "0.14.0-pre.11" }
parking_lot = "0.12.1"
regex = "1.9.5"
rustls-pemfile = "1.0.1"
Expand Down
19 changes: 19 additions & 0 deletions __test__/connect.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import axiosRetry from "axios-retry";
import * as fs from "fs";
import * as http from "http";
import * as retry from "./retry-config.mjs";
import * as path from "path";

axiosRetry(axios, retry.retryConfig);
const expected = "Hello";
Expand Down Expand Up @@ -208,3 +209,21 @@ test.serial("forward bad domain", async (t) => {
);
t.is("ERR_NGROK_326", error.errorCode, error.message);
});

test("policy", async (t) => {
const policy = 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",
policy: policy,
});
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"]);
});
10 changes: 10 additions & 0 deletions __test__/online.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as fs from "fs";
import * as http from "http";
import * as net from "net";
import * as retry from "./retry-config.mjs";
import * as path from "path";

axiosRetry(axios, retry.retryConfig);
const expected = "Hello";
Expand Down Expand Up @@ -516,3 +517,12 @@ test("listener invalid domain", async (t) => {
);
t.is("ERR_NGROK_326", error.errorCode);
});

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

const [httpServer, session] = await makeHttpAndSession();
const listener = await session.httpEndpoint().policy(policy).listen();
const response = await forwardValidateShutdown(t, httpServer, listener, listener.url());
t.is("bar", response.headers["foo"]);
});
18 changes: 18 additions & 0 deletions __test__/policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"outbound": [
{
"name": "test_out",
"expressions": ["req.Method == 'GET'"],
"actions": [
{
"type": "add-headers",
"config": {
"headers": {
"foo": "bar"
}
}
}
]
}
]
}
5 changes: 5 additions & 0 deletions index.d.ts

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

2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ 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.
pub policy: Option<String>,
/// The port for the listener to forward to.
/// Only used if addr is not defined.
pub port: Option<u32>,
Expand Down
5 changes: 5 additions & 0 deletions src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ macro_rules! config_common {
plumb_vec!($builder, $config, deny_cidr);
plumb!($builder, $config, proxy_proto);
plumb!($builder, $config, forwards_to);

// returns a Result, so we can't use the macro
if let Some(ref v) = $config.policy {
$builder.policy(v.clone())?;
}
};
}

Expand Down
8 changes: 8 additions & 0 deletions src/listener_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ macro_rules! make_listener_builder {
builder.forwards_to(forwards_to);
self
}
#[napi]
pub fn policy(&mut self, policy: String) -> Result<&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}"))),
}
}
}
};

Expand Down

0 comments on commit bde512c

Please sign in to comment.