-
Notifications
You must be signed in to change notification settings - Fork 20
/
axum.rs
128 lines (118 loc) · 3.45 KB
/
axum.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::net::SocketAddr;
use axum::{
extract::ConnectInfo,
routing::get,
Router,
};
use ngrok::{
prelude::*,
tunnel::HttpTunnel,
};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// build our application with a single route
let app = Router::new().route(
"/",
get(
|ConnectInfo(remote_addr): ConnectInfo<SocketAddr>| async move {
format!("Hello, {remote_addr:?}!\r\n")
},
),
);
// run it with hyper on localhost:8000
// axum::Server::bind(&"0.0.0.0:8000".parse().unwrap())
// Or with an ngrok tunnel
axum::Server::builder(start_tunnel().await?)
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
.await
.unwrap();
Ok(())
}
// const CA_CERT: &[u8] = include_bytes!("ca.crt");
async fn start_tunnel() -> anyhow::Result<HttpTunnel> {
let tun = ngrok::Session::builder()
.authtoken_from_env()
.connect()
.await?
.http_endpoint()
// .allow_cidr("0.0.0.0/0")
// .basic_auth("ngrok", "online1line")
// .circuit_breaker(0.5)
// .compression()
// .deny_cidr("10.1.1.1/32")
// .verify_upstream_tls(false)
// .domain("<somedomain>.ngrok.io")
// .forwards_to("example rust")
// .mutual_tlsca(CA_CERT.into())
// .oauth(
// OauthOptions::new("google")
// .allow_email("<user>@<domain>")
// .allow_domain("<domain>")
// .scope("<scope>"),
// )
// .oidc(
// OidcOptions::new("<url>", "<id>", "<secret>")
// .allow_email("<user>@<domain>")
// .allow_domain("<domain>")
// .scope("<scope>"),
// )
// .traffic_policy(POLICY_JSON)
// .proxy_proto(ProxyProto::None)
// .remove_request_header("X-Req-Nope")
// .remove_response_header("X-Res-Nope")
// .request_header("X-Req-Yup", "true")
// .response_header("X-Res-Yup", "true")
// .scheme(ngrok::Scheme::HTTPS)
// .websocket_tcp_conversion()
// .webhook_verification("twilio", "asdf"),
.metadata("example tunnel metadata from rust")
.listen()
.await?;
println!("Tunnel started on URL: {:?}", tun.url());
Ok(tun)
}
#[allow(dead_code)]
const POLICY_JSON: &str = r###"{
"inbound":[
{
"name":"deny_put",
"expressions":["req.Method == 'PUT'"],
"actions":[{"Type":"deny"}]
}],
"outbound":[
{
"name":"change success response",
"expressions":["res.StatusCode == '200'"],
"actions":[{
"type":"custom-response",
"config":{
"status_code":201,
"content": "Custom 200 response.",
"headers": {
"content_type": "text/html"
}
}
}]
}]
}"###;
#[allow(dead_code)]
const POLICY_YAML: &str = r###"
---
inbound:
- name: "deny_put"
expressions:
- "req.Method == 'PUT'"
actions:
- type: "deny"
outbound:
- name: "change success response"
expressions:
- "res.StatusCode == '200'"
actions:
- type: "custom-response"
config:
status_code: 201
content: "Custom 200 response."
headers:
content_type: "text/html"
"###;