Skip to content

Commit 68d1195

Browse files
committed
fix query param forwarding
1 parent 18129ae commit 68d1195

File tree

7 files changed

+47
-8
lines changed

7 files changed

+47
-8
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/target
22
tunnelto_server
33
!tunnelto_server/
4-
.env
4+
.env
5+
tunnelto_proxy/

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
members = [
44
"tunnelto_lib",
55
"tunnelto",
6-
"tunnelto_server"
6+
"tunnelto_server",
7+
]
8+
9+
exclude = [
10+
"tunnelto_proxy"
711
]

tunnelto/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "tunnelto"
33
description = "expose your local web server to the internet with a public url"
4-
version = "0.1.11"
4+
version = "0.1.12"
55
authors = ["Alex Grinman <[email protected]>"]
66
edition = "2018"
77
license = "MIT"

tunnelto/src/introspect/mod.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use futures::{Stream, StreamExt};
1010
use bytes::Buf;
1111
use uuid::Uuid;
1212
use http_body::Body;
13+
use std::convert::Infallible;
1314

1415
type HttpClient = hyper::Client<hyper::client::HttpConnector>;
1516

@@ -19,6 +20,7 @@ pub struct Request {
1920
status: u16,
2021
is_replay: bool,
2122
path: String,
23+
query: Option<String>,
2224
method: Method,
2325
headers: HashMap<String, Vec<String>>,
2426
body_data: Vec<u8>,
@@ -28,6 +30,16 @@ pub struct Request {
2830
completed: chrono::NaiveDateTime,
2931
}
3032

33+
impl Request {
34+
pub fn path_and_query(&self) -> String {
35+
if let Some(query) = self.query.as_ref() {
36+
format!("{}?{}", self.path, query)
37+
} else {
38+
self.path.clone()
39+
}
40+
}
41+
}
42+
3143
impl Request {
3244
pub fn elapsed(&self) -> String {
3345
let duration = self.completed - self.started;
@@ -72,6 +84,7 @@ pub fn start_introspection_server(config: Config) -> IntrospectionAddrs {
7284
.and(warp::any().map(move || local_addr.clone()))
7385
.and(warp::method())
7486
.and(warp::path::full())
87+
.and(opt_raw_query())
7588
.and(warp::header::headers_cloned())
7689
.and(warp::body::stream())
7790
.and(get_client())
@@ -124,6 +137,7 @@ pub fn start_introspection_server(config: Config) -> IntrospectionAddrs {
124137
async fn forward(local_addr: String,
125138
method: Method,
126139
path: FullPath,
140+
query: Option<String>,
127141
headers: HeaderMap,
128142
mut body: impl Stream<Item = Result<impl Buf, warp::Error>> + Send + Sync + Unpin + 'static,
129143
client: HttpClient) -> Result<Box<dyn warp::Reply>, warp::reject::Rejection>
@@ -147,7 +161,14 @@ async fn forward(local_addr: String,
147161
collected.extend_from_slice(chunk.as_ref())
148162
}
149163

150-
let url = format!("http://{}{}", local_addr, path.as_str());
164+
let query_str = if let Some(query) = query.as_ref() {
165+
format!("?{}", query)
166+
} else {
167+
String::new()
168+
};
169+
170+
let url = format!("http://{}{}{}", local_addr, path.as_str(), query_str);
171+
log::debug!("forwarding to: {}", &url);
151172

152173
let mut request = hyper::Request::builder()
153174
.method(method.clone())
@@ -196,6 +217,7 @@ async fn forward(local_addr: String,
196217
id: Uuid::new_v4().to_string(),
197218
status: parts.status.as_u16(),
198219
path: path.as_str().to_owned(),
220+
query,
199221
method,
200222
headers: request_headers,
201223
body_data: collected,
@@ -294,7 +316,13 @@ async fn replay_request(rid: String, client: HttpClient, addr: SocketAddr) -> Re
294316
None => return Err(warp::reject::not_found())
295317
};
296318

297-
let url = format!("http://localhost:{}{}", addr.port(), &request.path);
319+
let query_str = if let Some(query) = request.query.as_ref() {
320+
format!("?{}", query)
321+
} else {
322+
String::new()
323+
};
324+
325+
let url = format!("http://localhost:{}{}{}", addr.port(), &request.path, query_str);
298326

299327
let mut new_request = hyper::Request::builder()
300328
.method(request.method)
@@ -339,4 +367,10 @@ impl <T> warp::reply::Reply for Page<T> where T:askama::Template + Send + 'stati
339367
"text/html",
340368
).body(res.into()).unwrap()
341369
}
370+
}
371+
372+
fn opt_raw_query() -> impl Filter<Extract = (Option<String>,), Error = Infallible> + Copy {
373+
warp::filters::query::raw().map(|q| Some(q))
374+
.or(warp::any().map(|| None))
375+
.unify()
342376
}

tunnelto/templates/detail.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<span class="has-text-weight-bold">{{request.method}}</span>
4848
</td>
4949
<td>
50-
<span class="is-family-code">{{request.path}}</span>
50+
<span class="is-family-code">{{request.path_and_query()}}</span>
5151
</td>
5252
<td class="is-narrow">
5353
<span class="">{{request.body_data.len()/1024}} KB</span>

tunnelto/templates/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<span class="has-text-weight-bold">{{r.method}}</span>
5151
</td>
5252
<td>
53-
<span class="is-family-code">{{r.path}}</span>
53+
<span class="is-family-code">{{r.path_and_query()}}</span>
5454
</td>
5555
<td class="is-narrow">
5656
<span class="">{{r.body_data.len()/1024}} KB</span>

0 commit comments

Comments
 (0)