Skip to content

Commit

Permalink
Merge pull request #143 from ngrok/mo/bindings
Browse files Browse the repository at this point in the history
add bindings to ngrok-rust
  • Loading branch information
Megalonia authored Jul 1, 2024
2 parents 9300e91 + 101563f commit 1d80c8b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
11 changes: 10 additions & 1 deletion ngrok/src/config/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ struct HttpOptions {
pub(crate) webhook_verification: Option<WebhookVerification>,
// Flitering placed on the origin of incoming connections to the edge.
pub(crate) user_agent_filter: UaFilter,
pub(crate) bindings: Vec<String>,
}

impl HttpOptions {
Expand Down Expand Up @@ -151,6 +152,7 @@ impl TunnelConfig for HttpOptions {
token: Default::default(),
ip_policy_ref: Default::default(),
metadata: self.common_opts.metadata.clone().unwrap_or_default(),
bindings: self.bindings.clone(),
}
}
fn proto(&self) -> String {
Expand Down Expand Up @@ -251,6 +253,11 @@ impl HttpTunnelBuilder {
self.options.common_opts.metadata = Some(metadata.into());
self
}
/// Sets the ingress configuration for this endpoint
pub fn binding(&mut self, binding: impl Into<String>) -> &mut Self {
self.options.bindings.push(binding.into());
self
}
/// Sets the ForwardsTo string for this tunnel. This can be viewed via the
/// API or dashboard.
///
Expand Down Expand Up @@ -455,7 +462,7 @@ impl HttpTunnelBuilder {
mod test {
use super::*;
use crate::config::policies::test::POLICY_JSON;

const BINDING: &str = "public";
const METADATA: &str = "testmeta";
const TEST_FORWARD: &str = "testforward";
const TEST_FORWARD_PROTO: &str = "http2";
Expand All @@ -482,6 +489,7 @@ mod test {
.deny_cidr(DENY_CIDR)
.proxy_proto(ProxyProto::V2)
.metadata(METADATA)
.binding(BINDING)
.scheme(Scheme::from_str("hTtPs").unwrap())
.domain(DOMAIN)
.mutual_tlsca(CA_CERT.into())
Expand Down Expand Up @@ -526,6 +534,7 @@ mod test {
let extra = tunnel_cfg.extra();
assert_eq!(String::default(), *extra.token);
assert_eq!(METADATA, extra.metadata);
assert_eq!(Vec::from([BINDING]), extra.bindings);
assert_eq!(String::default(), extra.ip_policy_ref);

assert_eq!("https", tunnel_cfg.proto());
Expand Down
1 change: 1 addition & 0 deletions ngrok/src/config/labeled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl TunnelConfig for LabeledOptions {
token: Default::default(),
ip_policy_ref: Default::default(),
metadata: self.common_opts.metadata.clone().unwrap_or_default(),
bindings: Vec::new(),
}
}
fn proto(&self) -> String {
Expand Down
11 changes: 10 additions & 1 deletion ngrok/src/config/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::{
struct TcpOptions {
pub(crate) common_opts: CommonOpts,
pub(crate) remote_addr: Option<String>,
pub(crate) bindings: Vec<String>,
}

impl TunnelConfig for TcpOptions {
Expand All @@ -46,6 +47,7 @@ impl TunnelConfig for TcpOptions {
token: Default::default(),
ip_policy_ref: Default::default(),
metadata: self.common_opts.metadata.clone().unwrap_or_default(),
bindings: self.bindings.clone(),
}
}
fn proto(&self) -> String {
Expand Down Expand Up @@ -116,6 +118,11 @@ impl TcpTunnelBuilder {
self.options.common_opts.metadata = Some(metadata.into());
self
}
/// Sets the ingress configuration for this endpoint
pub fn binding(&mut self, binding: impl Into<String>) -> &mut Self {
self.options.bindings.push(binding.into());
self
}
/// Sets the ForwardsTo string for this tunnel. This can be viewed via the
/// API or dashboard.
///
Expand Down Expand Up @@ -164,7 +171,7 @@ impl TcpTunnelBuilder {
mod test {
use super::*;
use crate::config::policies::test::POLICY_JSON;

const BINDING: &str = "public";
const METADATA: &str = "testmeta";
const TEST_FORWARD: &str = "testforward";
const REMOTE_ADDR: &str = "4.tcp.ngrok.io:1337";
Expand All @@ -184,6 +191,7 @@ mod test {
.deny_cidr(DENY_CIDR)
.proxy_proto(ProxyProto::V2)
.metadata(METADATA)
.binding(BINDING)
.remote_addr(REMOTE_ADDR)
.forwards_to(TEST_FORWARD)
.policy(POLICY_JSON)
Expand All @@ -201,6 +209,7 @@ mod test {
let extra = tunnel_cfg.extra();
assert_eq!(String::default(), *extra.token);
assert_eq!(METADATA, extra.metadata);
assert_eq!(Vec::from([BINDING]), extra.bindings);
assert_eq!(String::default(), extra.ip_policy_ref);

assert_eq!("tcp", tunnel_cfg.proto());
Expand Down
10 changes: 10 additions & 0 deletions ngrok/src/config/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct TlsOptions {
pub(crate) mutual_tlsca: Vec<bytes::Bytes>,
pub(crate) key_pem: Option<bytes::Bytes>,
pub(crate) cert_pem: Option<bytes::Bytes>,
pub(crate) bindings: Vec<String>,
}

impl TunnelConfig for TlsOptions {
Expand All @@ -61,6 +62,7 @@ impl TunnelConfig for TlsOptions {
token: Default::default(),
ip_policy_ref: Default::default(),
metadata: self.common_opts.metadata.clone().unwrap_or_default(),
bindings: self.bindings.clone(),
}
}
fn proto(&self) -> String {
Expand Down Expand Up @@ -135,6 +137,11 @@ impl TlsTunnelBuilder {
self.options.common_opts.metadata = Some(metadata.into());
self
}
/// Sets the ingress configuration for this endpoint
pub fn binding(&mut self, binding: impl Into<String>) -> &mut Self {
self.options.bindings.push(binding.into());
self
}
/// Sets the ForwardsTo string for this tunnel. This can be viewed via the
/// API or dashboard.
///
Expand Down Expand Up @@ -205,6 +212,7 @@ mod test {
use super::*;
use crate::config::policies::test::POLICY_JSON;

const BINDING: &str = "public";
const METADATA: &str = "testmeta";
const TEST_FORWARD: &str = "testforward";
const ALLOW_CIDR: &str = "0.0.0.0/0";
Expand All @@ -228,6 +236,7 @@ mod test {
.deny_cidr(DENY_CIDR)
.proxy_proto(ProxyProto::V2)
.metadata(METADATA)
.binding(BINDING)
.domain(DOMAIN)
.mutual_tlsca(CA_CERT.into())
.mutual_tlsca(CA_CERT2.into())
Expand All @@ -248,6 +257,7 @@ mod test {
let extra = tunnel_cfg.extra();
assert_eq!(String::default(), *extra.token);
assert_eq!(METADATA, extra.metadata);
assert_eq!(Vec::from([BINDING]), extra.bindings);
assert_eq!(String::default(), extra.ip_policy_ref);

assert_eq!("tls", tunnel_cfg.proto());
Expand Down
1 change: 1 addition & 0 deletions ngrok/src/internals/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ pub struct BindExtra {
#[serde(rename = "IPPolicyRef")]
pub ip_policy_ref: String,
pub metadata: String,
pub bindings: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions ngrok/src/online_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async fn tunnel() -> Result<(), Error> {
let tun = setup_session()
.await?
.http_endpoint()
.binding("public")
.metadata("Hello, world!")
.forwards_to("some application")
.listen()
Expand Down

0 comments on commit 1d80c8b

Please sign in to comment.