2019-12-07 23:59:24 +06:00
|
|
|
use std::net::ToSocketAddrs;
|
|
|
|
|
2022-02-18 02:44:02 +00:00
|
|
|
use actix_web::{error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
2022-02-02 01:51:58 +00:00
|
|
|
use awc::Client;
|
|
|
|
use clap::StructOpt;
|
2019-08-19 21:42:31 +03:00
|
|
|
use url::Url;
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn forward(
|
2019-08-19 21:42:31 +03:00
|
|
|
req: HttpRequest,
|
2021-11-12 21:30:48 +08:00
|
|
|
payload: web::Payload,
|
2019-08-19 21:42:31 +03:00
|
|
|
url: web::Data<Url>,
|
2019-03-26 23:33:13 -07:00
|
|
|
client: web::Data<Client>,
|
2019-12-07 23:59:24 +06:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2019-08-19 21:42:31 +03:00
|
|
|
let mut new_url = url.get_ref().clone();
|
|
|
|
new_url.set_path(req.uri().path());
|
|
|
|
new_url.set_query(req.uri().query());
|
|
|
|
|
2022-02-02 01:51:58 +00:00
|
|
|
// TODO: This forwarded implementation is incomplete as it only handles the unofficial
|
2019-11-15 19:47:18 +01:00
|
|
|
// X-Forwarded-For header but not the official Forwarded one.
|
2019-08-19 21:42:31 +03:00
|
|
|
let forwarded_req = client
|
|
|
|
.request_from(new_url.as_str(), req.head())
|
|
|
|
.no_decompress();
|
2022-02-02 02:02:47 +00:00
|
|
|
let forwarded_req = match req.head().peer_addr {
|
2022-02-18 02:44:02 +00:00
|
|
|
Some(addr) => forwarded_req.insert_header(("x-forwarded-for", format!("{}", addr.ip()))),
|
2022-02-02 02:02:47 +00:00
|
|
|
None => forwarded_req,
|
2019-08-19 21:42:31 +03:00
|
|
|
};
|
|
|
|
|
2022-02-02 02:02:47 +00:00
|
|
|
let res = forwarded_req
|
|
|
|
.send_stream(payload)
|
|
|
|
.await
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
2019-12-07 23:59:24 +06:00
|
|
|
|
|
|
|
let mut client_resp = HttpResponse::build(res.status());
|
|
|
|
// Remove `Connection` as per
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection#Directives
|
2022-02-18 02:44:02 +00:00
|
|
|
for (header_name, header_value) in res.headers().iter().filter(|(h, _)| *h != "connection") {
|
2022-02-02 01:51:58 +00:00
|
|
|
client_resp.insert_header((header_name.clone(), header_value.clone()));
|
2019-12-07 23:59:24 +06:00
|
|
|
}
|
|
|
|
|
2021-11-12 21:30:48 +08:00
|
|
|
Ok(client_resp.streaming(res))
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
2022-02-02 01:51:58 +00:00
|
|
|
#[derive(clap::Parser, Debug)]
|
|
|
|
struct CliArguments {
|
|
|
|
listen_addr: String,
|
|
|
|
listen_port: u16,
|
|
|
|
forward_addr: String,
|
|
|
|
forward_port: u16,
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:49:45 +01:00
|
|
|
#[actix_web::main]
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2022-02-02 02:02:47 +00:00
|
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
|
2022-02-02 01:51:58 +00:00
|
|
|
let args = CliArguments::parse();
|
2022-02-02 02:02:47 +00:00
|
|
|
|
|
|
|
let forward_socket_addr = (args.forward_addr, args.forward_port)
|
|
|
|
.to_socket_addrs()?
|
|
|
|
.next()
|
|
|
|
.expect("given forwarding address was not valid");
|
|
|
|
|
|
|
|
let forward_url = format!("http://{}", forward_socket_addr);
|
|
|
|
let forward_url = Url::parse(&forward_url).unwrap();
|
|
|
|
|
|
|
|
log::info!(
|
2022-02-06 08:19:35 +00:00
|
|
|
"starting HTTP server at http://{}:{}",
|
2022-02-02 02:02:47 +00:00
|
|
|
&args.listen_addr,
|
|
|
|
args.listen_port
|
|
|
|
);
|
|
|
|
|
|
|
|
log::info!("forwarding to {forward_url}");
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2019-08-19 21:42:31 +03:00
|
|
|
HttpServer::new(move || {
|
2018-05-07 18:55:18 -07:00
|
|
|
App::new()
|
2022-02-02 02:02:47 +00:00
|
|
|
.app_data(web::Data::new(Client::default()))
|
2022-02-02 01:51:58 +00:00
|
|
|
.app_data(web::Data::new(forward_url.clone()))
|
2019-03-26 23:33:13 -07:00
|
|
|
.wrap(middleware::Logger::default())
|
2022-02-02 02:02:47 +00:00
|
|
|
.default_service(web::to(forward))
|
2019-03-09 18:03:09 -08:00
|
|
|
})
|
2022-02-02 01:51:58 +00:00
|
|
|
.bind((args.listen_addr, args.listen_port))?
|
2022-02-02 02:02:47 +00:00
|
|
|
.workers(2)
|
2019-12-25 20:48:33 +04:00
|
|
|
.run()
|
2019-12-07 23:59:24 +06:00
|
|
|
.await
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|