Skip to content

Commit

Permalink
plumb TrafficPolicy, remap Policy
Browse files Browse the repository at this point in the history
  • Loading branch information
TheConcierge committed Jul 24, 2024
1 parent 708edd0 commit d07ae9f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
18 changes: 15 additions & 3 deletions ngrok-java-native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,11 @@ impl<'local> NativeSessionRsImpl<'local> {
bldr.proxy_proto(ProxyProto::from(jeb.get_proxy_proto_version(self.env)));

if let Some(policy) = jeb.get_policy(self.env).of_string(self.env) {
bldr.policy(policy.as_str()).map_err(io_exc)?;
bldr.traffic_policy(policy.as_str());
}

if let Some(traffic_policy) = jeb.get_traffic_policy(self.env).of_string(self.env) {
bldr.traffic_policy(traffic_policy);
}

// from TcpBuilder
Expand Down Expand Up @@ -514,7 +518,11 @@ impl<'local> NativeSessionRsImpl<'local> {
bldr.proxy_proto(ProxyProto::from(jeb.get_proxy_proto_version(self.env)));

if let Some(policy) = jeb.get_policy(self.env).of_string(self.env) {
bldr.policy(policy.as_str()).map_err(io_exc)?;
bldr.traffic_policy(policy.as_str());
}

if let Some(traffic_policy) = jeb.get_traffic_policy(self.env).of_string(self.env) {
bldr.traffic_policy(traffic_policy);
}

// from TlsBuilder
Expand Down Expand Up @@ -580,7 +588,11 @@ impl<'local> NativeSessionRsImpl<'local> {
bldr.proxy_proto(ProxyProto::from(jeb.get_proxy_proto_version(self.env)));

if let Some(policy) = jeb.get_policy(self.env).of_string(self.env) {
bldr.policy(policy.as_str()).map_err(io_exc)?;
bldr.traffic_policy(policy.as_str());
}

if let Some(traffic_policy) = jeb.get_traffic_policy(self.env).of_string(self.env) {
bldr.traffic_policy(traffic_policy);
}

// from HttpBuilder
Expand Down
12 changes: 12 additions & 0 deletions ngrok-java-native/src/test/java/com/ngrok/DataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ public void testPolicy() throws Exception {
Runtime.getLogger().log("info", "session", listener.getUrl());
}
}

@Test
public void testTrafficPolicy() throws Exception {
final ClassLoader classLoader = getClass().getClassLoader();
final String trafficPolicy = new String(classLoader.getResourceAsStream("policy.json").readAllBytes());

try (var session = Session.withAuthtokenFromEnv().connect();
var listener = session.httpEndpoint().metadata("java-endpoint").trafficPolicy(trafficPolicy).listen()) {
assertEquals("java-endpoint", listener.getMetadata());
Runtime.getLogger().log("info", "session", listener.getUrl());
}
}
}
30 changes: 25 additions & 5 deletions ngrok-java/src/main/java/com/ngrok/EndpointBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class EndpointBuilder<T extends EndpointBuilder<T>> extends Meta
private final List<String> allowCIDR = new ArrayList<>();
private final List<String> denyCIDR = new ArrayList<>();
private ProxyProto proxyProto = ProxyProto.None;
private Optional<String> policy = Optional.empty();
private Optional<String> trafficPolicy = Optional.empty();

/**
* Adds a CIDR to the list of allowed CIDRs for this endpoint.
Expand Down Expand Up @@ -60,13 +60,24 @@ public T proxyProto(ProxyProto proxyProto) {
}

/**
* Sets the policy for this endpoint.
* DEPRECATED: use trafficPolicy instead.
*
* @param policy the policy for the builder
* @return An instance the builder represented by type T
*/
public T policy(final String policy) {
this.policy = Optional.ofNullable(policy);
this.trafficPolicy = Optional.ofNullable(policy);
return (T) this;
}

/**
* Sets the policy for this endpoint.
*
* @param trafficPolicy the policy for the builder
* @return An instance the builder represented by type T
*/
public T trafficPolicy(final String trafficPolicy) {
this.trafficPolicy = Optional.ofNullable(trafficPolicy);
return (T) this;
}

Expand Down Expand Up @@ -107,11 +118,20 @@ public long getProxyProtoVersion() {
}

/**
* Returns the policy for this endpoint.
* DEPRECATED: use getTrafficPolicy instead.
*
* @return the currently set policy
*/
public Optional<String> getPolicy() {
return this.policy;
return this.trafficPolicy;
}

/**
* Returns the policy for this endpoint.
*
* @return the currently set policy
*/
public Optional<String> getTrafficPolicy() {
return this.trafficPolicy;
}
}

0 comments on commit d07ae9f

Please sign in to comment.