Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support proxy_protocol_v2 #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
webpki-roots = "0.26"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
ppp = "2.0"

[profile.release]
lto = true
Expand Down
17 changes: 17 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use monoio::{
net::TcpStream,
};
use serde::Deserialize;
use std::net::SocketAddr;
use ppp::v2;

use crate::{
helper_v2::{
Expand Down Expand Up @@ -314,6 +316,17 @@ impl ShadowTlsServer {
let hmac_sr_s = Hmac::new(&self.password, (&server_random, b"S"));
let mut hmac_sr = Hmac::new(&self.password, (&server_random, &[]));

let client_address = in_stream.peer_addr()?; // Get the client's IP address
let server_address = resolve(&self.target_addr).await?; // Server address to connect to

let header = v2::Builder::with_addresses(
v2::Version::Two | v2::Command::Proxy,
v2::Protocol::Stream,
(client_address, server_address),
)
.build()
.unwrap();

// stage 1.3.2: copy ShadowTLS Client -> Handshake Server until hamc matches
// stage 1.3.3: copy and modify Handshake Server -> ShadowTLS Client until 1.3.2 stops
let (mut c_read, mut c_write) = in_stream.into_split();
Expand Down Expand Up @@ -357,6 +370,10 @@ impl ShadowTlsServer {
// stage 2.3: copy Data Server -> ShadowTLS Client
let mut data_stream = TcpStream::connect_addr(resolve(&self.target_addr).await?).await?;
mod_tcp_conn(&mut data_stream, true, self.nodelay);
// 发送 Proxy Protocol v2 头部到目标服务器
let (res, _) = data_stream.write_all(header).await;
res?; // 检查是否成功发送

let (res, _) = data_stream.write_all(pure_data).await;
res?;
verified_relay(
Expand Down