fix x-forwarded-for bug

This commit is contained in:
Jun Kurihara 2022-07-07 21:47:05 +09:00
commit 98fce0e4df
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
3 changed files with 23 additions and 5 deletions

View file

@ -1,13 +1,29 @@
#!/bin/sh #!/bin/sh
# echo "----------------------------"
# echo "Benchmark on rpxy"
# ab -c 16 -n 10000 http://127.0.0.1:8080/index.html
# echo "----------------------------"
# echo "Benchmark on nginx"
# ab -c 16 -n 10000 http://127.0.0.1:8090/index.html
# echo "----------------------------"
# echo "Benchmark on caddy"
# ab -c 16 -n 10000 http://127.0.0.1:8100/index.html
echo "----------------------------" echo "----------------------------"
echo "Benchmark on rpxy" echo "Benchmark on rpxy"
ab -c 100 -n 10000 http://127.0.0.1:8080/index.html # TODO: localhost = 127.0.0.1を解決できるように決めておかんとだめそう #wrk -t8 -c100 -d30s http://127.0.0.1:8080/index.html
rewrk -c 256 -t 8 -d 15s -h http://127.0.0.1:8080 --pct
echo "----------------------------" echo "----------------------------"
echo "Benchmark on nginx" echo "Benchmark on nginx"
ab -c 100 -n 10000 http://127.0.0.1:8090/index.html # wrk -t8 -c100 -d30s http://127.0.0.1:8090/index.html
rewrk -c 256 -t 8 -d 15s -h http://127.0.0.1:8090 --pct
echo "----------------------------" echo "----------------------------"
echo "Benchmark on caddy" echo "Benchmark on caddy"
ab -c 100 -n 10000 http://127.0.0.1:8100/index.html # wrk -t8 -c100 -d30s http://127.0.0.1:8100/index.html
rewrk -c 256 -t 8 -d 15s -h http://127.0.0.1:8100 --pct

View file

@ -16,7 +16,7 @@ reverse_proxy = [
# Array for load balancing # Array for load balancing
{ upstream = [ { upstream = [
{ location = 'backend-nginx', tls = false, upstream_options = [ { location = 'backend-nginx', tls = false, upstream_options = [
"override_host", # "override_host",
] }, ] },
] }, ] },
# { upstream = [{ location = '192.168.100.100', tls = false }] }, # { upstream = [{ location = '192.168.100.100', tls = false }] },

View file

@ -212,6 +212,7 @@ fn generate_request_forwarded<B: core::fmt::Debug>(
remove_hop_header(headers); remove_hop_header(headers);
// X-Forwarded-For // X-Forwarded-For
add_forwarding_header(headers, client_addr)?; add_forwarding_header(headers, client_addr)?;
println!("{:?}", headers);
// Add te: trailer if te_trailer // Add te: trailer if te_trailer
if te_trailers { if te_trailers {
@ -285,7 +286,7 @@ fn add_forwarding_header(headers: &mut HeaderMap, client_addr: SocketAddr) -> Re
hyper::header::Entry::Vacant(entry) => { hyper::header::Entry::Vacant(entry) => {
entry.insert(client_ip.to_string().parse()?); entry.insert(client_ip.to_string().parse()?);
} }
hyper::header::Entry::Occupied(entry) => { hyper::header::Entry::Occupied(mut entry) => {
let client_ip_str = client_ip.to_string(); let client_ip_str = client_ip.to_string();
let mut addr = String::with_capacity(entry.get().as_bytes().len() + 2 + client_ip_str.len()); let mut addr = String::with_capacity(entry.get().as_bytes().len() + 2 + client_ip_str.len());
@ -293,6 +294,7 @@ fn add_forwarding_header(headers: &mut HeaderMap, client_addr: SocketAddr) -> Re
addr.push(','); addr.push(',');
addr.push(' '); addr.push(' ');
addr.push_str(&client_ip_str); addr.push_str(&client_ip_str);
entry.insert(addr.to_owned().parse()?);
} }
} }
Ok(()) Ok(())