1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00

clean up http-proxy

This commit is contained in:
Rob Ede 2022-02-02 02:02:47 +00:00
parent d94a8ead36
commit a99c4ca546
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 89 additions and 81 deletions

104
Cargo.lock generated
View File

@ -106,8 +106,6 @@ dependencies = [
"futures-util",
"http",
"log",
"openssl",
"tokio-openssl 0.4.0",
"trust-dns-proto 0.19.7",
"trust-dns-resolver 0.19.7",
]
@ -216,7 +214,6 @@ dependencies = [
"actix-rt 1.1.1",
"actix-service 1.0.6",
"actix-threadpool",
"actix-tls 2.0.0",
"actix-utils 2.0.0",
"base64 0.13.0",
"bitflags",
@ -566,8 +563,6 @@ dependencies = [
"actix-service 1.0.6",
"actix-utils 2.0.0",
"futures-util",
"openssl",
"tokio-openssl 0.4.0",
]
[[package]]
@ -585,7 +580,7 @@ dependencies = [
"log",
"openssl",
"pin-project-lite 0.2.8",
"tokio-openssl 0.6.3",
"tokio-openssl",
"tokio-rustls 0.23.2",
"tokio-util 0.6.9",
"webpki-roots 0.22.2",
@ -666,7 +661,6 @@ dependencies = [
"fxhash",
"log",
"mime",
"openssl",
"pin-project 1.0.10",
"regex",
"serde 1.0.136",
@ -942,15 +936,6 @@ dependencies = [
"yansi-term",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "anyhow"
version = "1.0.53"
@ -1272,7 +1257,6 @@ dependencies = [
"futures-core",
"log",
"mime",
"openssl",
"percent-encoding",
"rand 0.7.3",
"serde 1.0.136",
@ -1708,17 +1692,32 @@ dependencies = [
[[package]]
name = "clap"
version = "2.34.0"
version = "3.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
checksum = "b63edc3f163b3c71ec8aa23f9bd6070f77edbf3d1d198b164afa90ff00e4ec62"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim 0.8.0",
"clap_derive",
"indexmap",
"lazy_static",
"os_str_bytes",
"strsim",
"termcolor",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "clap_derive"
version = "3.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a1132dc3944b31c20dd8b906b3a9f0a5d0243e092d59171414969657ac6aa85"
dependencies = [
"heck 0.4.0",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@ -2066,7 +2065,7 @@ dependencies = [
"ident_case",
"proc-macro2",
"quote",
"strsim 0.10.0",
"strsim",
"syn",
]
@ -2080,7 +2079,7 @@ dependencies = [
"ident_case",
"proc-macro2",
"quote",
"strsim 0.10.0",
"strsim",
"syn",
]
@ -2324,7 +2323,7 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c5f0096a91d210159eceb2ff5e1c4da18388a170e1e3ce948aac9c8fdbbf595"
dependencies = [
"heck",
"heck 0.3.3",
"proc-macro2",
"quote",
"syn",
@ -2807,6 +2806,12 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hello-world"
version = "1.0.0"
@ -2935,8 +2940,11 @@ dependencies = [
name = "http-proxy"
version = "1.0.0"
dependencies = [
"actix-web 3.3.3",
"actix-web 4.0.0-rc.1",
"awc 3.0.0-beta.20",
"clap",
"env_logger 0.9.0",
"log",
"url",
]
@ -3743,7 +3751,7 @@ dependencies = [
"sha2 0.9.9",
"socket2 0.4.4",
"stringprep",
"strsim 0.10.0",
"strsim",
"take_mut",
"thiserror",
"tokio 1.16.1",
@ -4096,6 +4104,15 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
dependencies = [
"memchr",
]
[[package]]
name = "parking_lot"
version = "0.11.2"
@ -5601,7 +5618,7 @@ checksum = "584866c833511b1a152e87a7ee20dee2739746f60c858b3c5209150bc4b466f5"
dependencies = [
"dotenv",
"either",
"heck",
"heck 0.3.3",
"once_cell",
"proc-macro2",
"quote",
@ -5762,12 +5779,6 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "strsim"
version = "0.10.0"
@ -5924,12 +5935,9 @@ dependencies = [
[[package]]
name = "textwrap"
version = "0.11.0"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
[[package]]
name = "thiserror"
@ -6122,16 +6130,6 @@ dependencies = [
"tokio 1.16.1",
]
[[package]]
name = "tokio-openssl"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c4b08c5f4208e699ede3df2520aca2e82401b2de33f45e96696a074480be594"
dependencies = [
"openssl",
"tokio 0.2.25",
]
[[package]]
name = "tokio-openssl"
version = "0.6.3"
@ -6760,12 +6758,6 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.1.5"

View File

@ -4,7 +4,10 @@ version = "1.0.0"
edition = "2021"
[dependencies]
actix-web = { version = "4.0.0-beta.21", features = ["openssl"] }
actix-web = { version = "4.0.0-rc.1", features = ["openssl"] }
awc = "3.0.0-beta.19"
clap = { version = "3.0.13", features = ["derive"] }
url = "2.0"
clap = { version = "3", features = ["derive"] }
env_logger = "0.9"
log = "0.4"
url = "2.2"

View File

@ -1,4 +1,4 @@
## HTTP Full proxy example
## HTTP naïve proxy example
This is a relatively simple HTTP proxy, forwarding HTTP requests to another HTTP server, including
request body, headers, and streaming uploads.
@ -6,5 +6,7 @@ request body, headers, and streaming uploads.
To start:
``` shell
cargo run <listen addr> <listen port> <forward addr> <forward port>
cargo run -- <listen addr> <listen port> <forward addr> <forward port>
# example:
cargo run -- 127.0.0.1 3333 127.0.0.1 8080
```

View File

@ -1,7 +1,7 @@
use std::net::ToSocketAddrs;
use actix_web::{
http::StatusCode, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
};
use awc::Client;
use clap::StructOpt;
@ -22,15 +22,17 @@ async fn forward(
let forwarded_req = client
.request_from(new_url.as_str(), req.head())
.no_decompress();
let forwarded_req = if let Some(addr) = req.head().peer_addr {
forwarded_req.insert_header(("x-forwarded-for", format!("{}", addr.ip())))
} else {
forwarded_req
let forwarded_req = match req.head().peer_addr {
Some(addr) => {
forwarded_req.insert_header(("x-forwarded-for", format!("{}", addr.ip())))
}
None => forwarded_req,
};
let res = forwarded_req.send_stream(payload).await.map_err(|e| {
actix_web::error::InternalError::new(e, StatusCode::INTERNAL_SERVER_ERROR)
})?;
let res = forwarded_req
.send_stream(payload)
.await
.map_err(error::ErrorInternalServerError)?;
let mut client_resp = HttpResponse::build(res.status());
// Remove `Connection` as per
@ -54,26 +56,35 @@ struct CliArguments {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let args = CliArguments::parse();
let forward_url = Url::parse(&format!(
"http://{}",
(args.forward_addr, args.forward_port)
.to_socket_addrs()
.unwrap()
.next()
.unwrap()
))
.unwrap();
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!(
"starting HTTP serer at http://{}:{}",
&args.listen_addr,
args.listen_port
);
log::info!("forwarding to {forward_url}");
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(Client::new()))
.app_data(web::Data::new(Client::default()))
.app_data(web::Data::new(forward_url.clone()))
.wrap(middleware::Logger::default())
.default_service(web::route().to(forward))
.default_service(web::to(forward))
})
.bind((args.listen_addr, args.listen_port))?
.system_exit()
.workers(2)
.run()
.await
}