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

Add proxy protocol support #352

Open
wants to merge 1 commit into
base: main
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
11 changes: 11 additions & 0 deletions examples/proxy_protocol/client.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# rathole configuration for proxy protocol enabled client
#
# The client configuration is essentially unaffected, since the proxy
# protocol header would be transarently passed to the downstream server.

[client]
remote_addr = "localhost:2333"
default_token = "123"

[client.services.foo1]
local_addr = "127.0.0.1:80"
12 changes: 12 additions & 0 deletions examples/proxy_protocol/server.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# rathole configuration for proxy protocol enabled client
#
# The service configuration has an additional `enable_proxy_protocol` boolean field.
# Not setting this field defaults its value to `false` at runtime.

[server]
bind_addr = "0.0.0.0:2333"
default_token = "123"

[server.services.foo1]
bind_addr = "0.0.0.0:5202"
enable_proxy_protocol = true
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub struct ServerServiceConfig {
pub bind_addr: String,
pub token: Option<MaskedString>,
pub nodelay: Option<bool>,
pub enable_proxy_protocol: Option<bool>,
}

impl ServerServiceConfig {
Expand Down
15 changes: 15 additions & 0 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,18 @@ where
conn.flush().await.with_context(|| "Failed to flush data")?;
Ok(())
}

pub fn generate_proxy_protocol_v1_header(s: &TcpStream) -> Result<String> {
let local_addr = s.local_addr()?;
let remote_addr = s.peer_addr()?;
let proto = if local_addr.is_ipv4() { "TCP4" } else { "TCP6" };
let header = format!(
"PROXY {} {} {} {} {}\r\n",
proto,
remote_addr.ip(),
local_addr.ip(),
remote_addr.port(),
local_addr.port()
);
Ok(header)
}
13 changes: 12 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::{Config, ServerConfig, ServerServiceConfig, ServiceType, TransportType};
use crate::config_watcher::{ConfigChange, ServerServiceChange};
use crate::constants::{listen_backoff, UDP_BUFFER_SIZE};
use crate::helper::{retry_notify_with_deadline, write_and_flush};
use crate::helper::{generate_proxy_protocol_v1_header, retry_notify_with_deadline, write_and_flush};
use crate::multi_map::MultiMap;
use crate::protocol::Hello::{ControlChannelHello, DataChannelHello};
use crate::protocol::{
Expand Down Expand Up @@ -427,11 +427,16 @@ where

let shutdown_rx_clone = shutdown_tx.subscribe();
let bind_addr = service.bind_addr.clone();
let enable_proxy_protocol = service.enable_proxy_protocol.unwrap_or_default();
if enable_proxy_protocol {
debug!("Proxy protocol is enabled");
}
match service.service_type {
ServiceType::Tcp => tokio::spawn(
async move {
if let Err(e) = run_tcp_connection_pool::<T>(
bind_addr,
enable_proxy_protocol,
data_ch_rx,
data_ch_req_tx,
shutdown_rx_clone,
Expand Down Expand Up @@ -625,6 +630,7 @@ fn tcp_listen_and_send(
#[instrument(skip_all)]
async fn run_tcp_connection_pool<T: Transport>(
bind_addr: String,
enable_proxy_protocol: bool,
mut data_ch_rx: mpsc::Receiver<T::Stream>,
data_ch_req_tx: mpsc::UnboundedSender<bool>,
shutdown_rx: broadcast::Receiver<bool>,
Expand All @@ -637,6 +643,11 @@ async fn run_tcp_connection_pool<T: Transport>(
if let Some(mut ch) = data_ch_rx.recv().await {
if write_and_flush(&mut ch, &cmd).await.is_ok() {
tokio::spawn(async move {
if enable_proxy_protocol {
let proxy_proto_header = generate_proxy_protocol_v1_header(&visitor).unwrap();
let _ = ch.write_all(&proxy_proto_header.into_bytes()).await;
let _ = ch.flush().await;
}
let _ = copy_bidirectional(&mut ch, &mut visitor).await;
});
break;
Expand Down
Loading