-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proxy protocol support, bump to 0.15.24
- Loading branch information
1 parent
9a46da9
commit ebbe973
Showing
13 changed files
with
176 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::net::{Ipv4Addr, Ipv6Addr}; | ||
|
||
use tokio::io::{AsyncWrite, AsyncWriteExt}; | ||
|
||
pub enum ProxyProtocolHeader { | ||
Tcp4 { | ||
client_ip: Ipv4Addr, | ||
proxy_ip: Ipv4Addr, | ||
client_port: u16, | ||
proxy_port: u16, | ||
}, | ||
Tcp6 { | ||
client_ip: Ipv6Addr, | ||
proxy_ip: Ipv6Addr, | ||
client_port: u16, | ||
proxy_port: u16, | ||
}, | ||
} | ||
|
||
impl std::fmt::Display for ProxyProtocolHeader { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::Tcp4 { client_ip, proxy_ip, client_port, proxy_port } => { | ||
write!(f, "PROXY TCP4 {client_ip} {proxy_ip} {client_port} {proxy_port}\r\n") | ||
} | ||
Self::Tcp6 { client_ip, proxy_ip, client_port, proxy_port } => { | ||
write!(f, "PROXY TCP6 {client_ip} {proxy_ip} {client_port} {proxy_port}\r\n") | ||
} | ||
} | ||
} | ||
} | ||
|
||
const PROXY_PROTOCOL_V2_HEADER: &'static [u8] = &[ | ||
0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, | ||
/* version 2 + proxy connection byte */ 0x21 | ||
]; | ||
|
||
impl ProxyProtocolHeader { | ||
pub async fn write_v1<W: AsyncWrite + Unpin>(&self, out: &mut W) -> Result<(), std::io::Error> { | ||
out.write_all(self.to_string().as_bytes()).await | ||
} | ||
|
||
pub async fn write_v2<W: AsyncWrite + Unpin>(&self, out: &mut W) -> Result<(), std::io::Error> { | ||
out.write_all(PROXY_PROTOCOL_V2_HEADER).await?; | ||
|
||
match self { | ||
Self::Tcp4 { client_ip, proxy_ip, client_port, proxy_port } => { | ||
out.write_all(&[ /* TCP4: AF_INET + STREAM */ 0x11 ]).await?; | ||
out.write_all(/* length */ &12u16.to_be_bytes()).await?; | ||
out.write_all(&client_ip.octets()).await?; | ||
out.write_all(&proxy_ip.octets()).await?; | ||
out.write_all(&client_port.to_be_bytes()).await?; | ||
out.write_all(&proxy_port.to_be_bytes()).await?; | ||
} | ||
Self::Tcp6 { client_ip, proxy_ip, client_port, proxy_port } => { | ||
out.write_all(&[ /* TCP6: AF_INET6 + STREAM */ 0x21 ]).await?; | ||
out.write_all(/* length */ &36u16.to_be_bytes()).await?; | ||
out.write_all(&client_ip.octets()).await?; | ||
out.write_all(&proxy_ip.octets()).await?; | ||
out.write_all(&client_port.to_be_bytes()).await?; | ||
out.write_all(&proxy_port.to_be_bytes()).await?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters