Skip to content

Commit 8fe39cf

Browse files
committed
Redirect http to https
1 parent c47a0cc commit 8fe39cf

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/main.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ mod id_generator;
66
use std::sync::{Arc, Mutex};
77
use std::collections::HashMap;
88
use warp::{self, Filter};
9-
use warp::http::{Response};
9+
use warp::http::{Response, Uri};
10+
use warp::path::FullPath;
1011
use hoster_manager::HosterManager;
1112
use futures::{Future, Stream};
1213
use futures::sync::{mpsc};
@@ -22,6 +23,11 @@ type HosterManagers = Arc<Mutex<HashMap<String, HosterManager>>>;
2223
fn main() {
2324
let matches = App::new("fibridge proxy")
2425
.about("Share local files via HTTP streaming")
26+
.arg(Arg::with_name("host")
27+
.short("h")
28+
.long("host")
29+
.value_name("HOST")
30+
.takes_value(true))
2531
.arg(Arg::with_name("ip")
2632
.short("i")
2733
.long("ip-address")
@@ -44,6 +50,10 @@ fn main() {
4450
.long("cert")
4551
.value_name("TLS_CERT")
4652
.takes_value(true))
53+
.arg(Arg::with_name("secure-port")
54+
.long("secure-port")
55+
.value_name("SECURE_PORT")
56+
.takes_value(true))
4757
.get_matches();
4858

4959
let port = matches.value_of("port").unwrap_or("9001");
@@ -161,13 +171,37 @@ fn main() {
161171
let key = matches.value_of("key");
162172
let cert = matches.value_of("cert");
163173

174+
164175
if key.is_some() && cert.is_some() {
165-
let server_future = warp::serve(routes)
176+
177+
let secure_port = matches.value_of("secure-port").unwrap_or("9002");
178+
179+
// if host was specified, use that value, otherwise use the ip
180+
let host = matches.value_of("host").unwrap_or(ip);
181+
let secure_redirect_link = format!("{}:{}", host, secure_port);
182+
183+
// redirect http to https
184+
let http_routes = warp::path::full().map(move |path: FullPath| {
185+
let uri = Uri::builder()
186+
.scheme("https")
187+
.authority(secure_redirect_link.as_str())
188+
.path_and_query(path.as_str())
189+
.build()
190+
.expect("parse uri");
191+
warp::redirect(uri)
192+
});
193+
let http_server_future = warp::serve(http_routes)
194+
.bind(addr.parse::<SocketAddrV4>()
195+
.expect("parse address"));
196+
197+
let secure_addr = format!("{}:{}", ip, secure_port);
198+
let https_server_future = warp::serve(routes)
166199
.tls(cert.unwrap(), key.unwrap())
167-
.bind(addr.parse::<SocketAddrV4>().expect("parse address"));
200+
.bind(secure_addr.parse::<SocketAddrV4>().expect("parse address"));
168201
rt::run(rt::lazy(|| {
169202
rt::spawn(done_stream);
170-
rt::spawn(server_future);
203+
rt::spawn(http_server_future);
204+
rt::spawn(https_server_future);
171205
Ok(())
172206
}));
173207
}

0 commit comments

Comments
 (0)