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

correctly bind to both ipv4 and ipv6 when [::] is set as listen endpoint #1193

Merged
merged 6 commits into from
Dec 27, 2024
Merged
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
9 changes: 6 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ serde_json = "1.0"
russh = { version = "0.49.0" }
russh-keys = { version = "0.49.0" }
tracing = "0.1"
futures = "0.3"
tokio-stream = { version = "0.1.17", features = ["net"] }

[profile.release]
lto = true
Expand Down
19 changes: 11 additions & 8 deletions tests/test_ssh_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def test_direct_tcpip(
"-v",
*common_args,
"-L",
f"{local_port}:neverssl.com:80",
f"{local_port}:github.com:443",
"-N",
password="123",
)
Expand All @@ -178,8 +178,8 @@ def test_direct_tcpip(

s = requests.Session()
retries = requests.adapters.Retry(total=5, backoff_factor=1)
s.mount("http://", requests.adapters.HTTPAdapter(max_retries=retries))
response = s.get(f"http://localhost:{local_port}", timeout=timeout)
s.mount("https://", requests.adapters.HTTPAdapter(max_retries=retries))
response = s.get(f"https://localhost:{local_port}", timeout=timeout, verify=False)
assert response.status_code == 200
ssh_client.kill()

Expand All @@ -193,32 +193,35 @@ def test_tcpip_forward(
user, ssh_target = setup_user_and_target(
processes, shared_wg, wg_c_ed25519_pubkey
)
fw_port = alloc_port()
pf_client = processes.start_ssh_client(
f"{user.username}:{ssh_target.name}@localhost",
"-p",
str(shared_wg.ssh_port),
"-v",
*common_args,
"-R",
"1234:neverssl.com:80",
f"{fw_port}:www.google.com:443",
"-N",
password="123",
)
time.sleep(5)
# time.sleep(5)
ssh_client = processes.start_ssh_client(
f"{user.username}:{ssh_target.name}@localhost",
"-p",
str(shared_wg.ssh_port),
"-v",
*common_args,
"curl",
"-v",
"http://localhost:1234",
"-vk",
"--http1.1",
"-H", "Host: www.google.com",
f"https://localhost:{fw_port}",
password="123",
)
output = ssh_client.communicate(timeout=timeout)[0]
assert ssh_client.returncode == 0
assert b"<html>" in output
assert b"</html>" in output
pf_client.kill()

def test_shell(
Expand Down
2 changes: 1 addition & 1 deletion warpgate-admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ anyhow = { version = "1.0", features = ["std"] }
async-trait = "0.1"
bytes.workspace = true
chrono = { version = "0.4", default-features = false }
futures = "0.3"
futures.workspace = true
hex = "0.4"
mime_guess = { version = "2.0", default-features = false }
poem = { version = "3.1", features = [
Expand Down
3 changes: 2 additions & 1 deletion warpgate-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ chrono = { version = "0.4", default-features = false, features = ["serde"] }
data-encoding.workspace = true
delegate = "0.6"
humantime-serde = "1.1"
futures = "0.3"
futures.workspace = true
once_cell = "1.17"
password-hash = "0.4"
poem = { version = "3.1", features = ["rustls"] }
Expand Down Expand Up @@ -47,3 +47,4 @@ rustls = "0.23"
rustls-pemfile = "1.0"
webpki = "0.22"
aho-corasick = "1.1.3"
tokio-stream.workspace = true
8 changes: 4 additions & 4 deletions warpgate-common/src/config/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ pub(crate) fn _default_database_url() -> Secret<String> {

#[inline]
pub(crate) fn _default_http_listen() -> ListenEndpoint {
ListenEndpoint(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 8888))
ListenEndpoint::from(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 8888))
}

#[inline]
pub(crate) fn _default_mysql_listen() -> ListenEndpoint {
ListenEndpoint(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 33306))
ListenEndpoint::from(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 33306))
}

#[inline]
pub(crate) fn _default_postgres_listen() -> ListenEndpoint {
ListenEndpoint(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 55432))
ListenEndpoint::from(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 55432))
}

#[inline]
Expand All @@ -75,7 +75,7 @@ pub(crate) fn _default_empty_vec<T>() -> Vec<T> {
}

pub(crate) fn _default_ssh_listen() -> ListenEndpoint {
ListenEndpoint(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 2222))
ListenEndpoint::from(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 2222))
}

pub(crate) fn _default_ssh_keys_path() -> String {
Expand Down
2 changes: 2 additions & 0 deletions warpgate-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum WarpgateError {
Sso(#[from] SsoError),
#[error(transparent)]
RusshKeys(#[from] russh_keys::Error),
#[error("I/O: {0}")]
Io(#[from] std::io::Error),

#[error("Session end")]
SessionEnd,
Expand Down
83 changes: 76 additions & 7 deletions warpgate-common/src/types/listen_endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,85 @@
use std::fmt::Debug;
use std::net::{SocketAddr, ToSocketAddrs};
use std::ops::Deref;
use std::io::ErrorKind;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs};

use futures::stream::{iter, FuturesUnordered};
use futures::{Stream, StreamExt, TryStreamExt};
use poem::listener::Listener;
use serde::{Deserialize, Serialize};
use tokio::net::{TcpListener, TcpStream};
use tokio_stream::wrappers::TcpListenerStream;

use crate::WarpgateError;

#[derive(Clone)]
pub struct ListenEndpoint(pub SocketAddr);
pub struct ListenEndpoint(SocketAddr);

impl ListenEndpoint {
pub fn addresses_to_listen_on(&self) -> Result<Vec<SocketAddr>, WarpgateError> {
// For [::], explicitly return both addresses so that we are not affected
// by the state of the ipv6only sysctl.
if self.0.ip() == Ipv6Addr::UNSPECIFIED {
let addr6 = SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), self.0.port());
let addr4 = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), self.0.port());
let listener6 = std::net::TcpListener::bind(addr6)?;
let listener4 = std::net::TcpListener::bind(addr4);
let result = match listener4 {
Ok(_) => vec![addr4, addr6],
Err(e) if e.kind() == ErrorKind::AddrInUse => vec![addr6],
Err(e) => return Err(WarpgateError::Io(e)),
};
drop(listener6);
Ok(result)
} else {
Ok(vec![self.0])
}
}

pub async fn tcp_listeners(&self) -> Result<Vec<TcpListener>, WarpgateError> {
Ok(self
.addresses_to_listen_on()?
.into_iter()
.map(TcpListener::bind)
.collect::<FuturesUnordered<_>>()
.try_collect()
.await?)
}

pub async fn poem_listener(&self) -> Result<poem::listener::BoxListener, WarpgateError> {
let addrs = self.addresses_to_listen_on()?;
#[allow(clippy::unwrap_used)] // length known >=1
let (first, rest) = addrs.split_first().unwrap();
let mut listener: poem::listener::BoxListener =
poem::listener::TcpListener::bind(first.to_string()).boxed();
for addr in rest {
listener = listener
.combine(poem::listener::TcpListener::bind(addr.to_string()))
.boxed();
}

Ok(listener)
}

pub async fn tcp_accept_stream(
&self,
) -> Result<impl Stream<Item = std::io::Result<TcpStream>>, WarpgateError> {
Ok(iter(
self.tcp_listeners()
.await?
.into_iter()
.map(TcpListenerStream::new),
)
.flatten_unordered(None))
}

pub fn port(&self) -> u16 {
self.0.port()
}
}

impl Deref for ListenEndpoint {
type Target = SocketAddr;
fn deref(&self) -> &Self::Target {
&self.0
impl From<SocketAddr> for ListenEndpoint {
fn from(addr: SocketAddr) -> Self {
Self(addr)
}
}

Expand Down
2 changes: 1 addition & 1 deletion warpgate-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bytes.workspace = true
chrono = { version = "0.4", default-features = false, features = ["serde"] }
data-encoding.workspace = true
humantime-serde = "1.1"
futures = "0.3"
futures.workspace = true
once_cell = "1.17"
packet = "0.1"
password-hash = "0.4"
Expand Down
5 changes: 2 additions & 3 deletions warpgate-core/src/protocols/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
mod handle;
use std::net::SocketAddr;

use anyhow::Result;
use async_trait::async_trait;
pub use handle::{SessionHandle, WarpgateServerHandle};
use warpgate_common::Target;
use warpgate_common::{ListenEndpoint, Target};

#[derive(Debug, thiserror::Error)]
pub enum TargetTestError {
Expand All @@ -22,6 +21,6 @@ pub enum TargetTestError {

#[async_trait]
pub trait ProtocolServer {
async fn run(self, address: SocketAddr) -> Result<()>;
async fn run(self, address: ListenEndpoint) -> Result<()>;
async fn test_target(&self, target: Target) -> Result<(), TargetTestError>;
}
2 changes: 1 addition & 1 deletion warpgate-protocol-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ chrono = { version = "0.4", default-features = false, features = ["serde"] }
cookie = "0.17"
data-encoding.workspace = true
delegate = "0.6"
futures = "0.3"
futures.workspace = true
http = "1.0"
once_cell = "1.17"
poem = { version = "3.1", features = [
Expand Down
12 changes: 7 additions & 5 deletions warpgate-protocol-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod session;
mod session_handle;

use std::fmt::Debug;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

Expand All @@ -21,7 +20,7 @@ pub use common::{SsoLoginState, PROTOCOL_NAME};
use http::HeaderValue;
use logging::{get_client_ip, log_request_error, log_request_result, span_for_request};
use poem::endpoint::{EmbeddedFileEndpoint, EmbeddedFilesEndpoint};
use poem::listener::{Listener, RustlsConfig, TcpListener};
use poem::listener::{Listener, RustlsConfig};
use poem::middleware::SetHeader;
use poem::session::{CookieConfig, MemoryStorage, ServerSession, Session};
use poem::web::Data;
Expand All @@ -31,7 +30,8 @@ use tokio::sync::Mutex;
use tracing::*;
use warpgate_admin::admin_api_app;
use warpgate_common::{
Target, TargetOptions, TlsCertificateAndPrivateKey, TlsCertificateBundle, TlsPrivateKey,
ListenEndpoint, Target, TargetOptions, TlsCertificateAndPrivateKey, TlsCertificateBundle,
TlsPrivateKey,
};
use warpgate_core::{ProtocolServer, Services, TargetTestError};
use warpgate_web::Assets;
Expand Down Expand Up @@ -59,7 +59,7 @@ fn make_session_storage() -> SharedSessionStorage {

#[async_trait]
impl ProtocolServer for HTTPProtocolServer {
async fn run(self, address: SocketAddr) -> Result<()> {
async fn run(self, address: ListenEndpoint) -> Result<()> {
let admin_api_app = admin_api_app(&self.services).into_endpoint();
let api_service = OpenApiService::new(
crate::api::get(),
Expand Down Expand Up @@ -205,7 +205,9 @@ impl ProtocolServer for HTTPProtocolServer {

info!(?address, "Listening");
Server::new(
TcpListener::bind(address)
address
.poem_listener()
.await?
.rustls(RustlsConfig::new().fallback(certificate_and_key.into())),
)
.run(app)
Expand Down
1 change: 1 addition & 0 deletions warpgate-protocol-mysql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ warpgate-db-entities = { version = "*", path = "../warpgate-db-entities" }
warpgate-database-protocols = { version = "*", path = "../warpgate-database-protocols" }
anyhow = { version = "1.0", features = ["std"] }
async-trait = "0.1"
futures.workspace = true
tokio = { version = "1.20", features = ["tracing", "signal"] }
tracing.workspace = true
uuid = { version = "1.3", features = ["v4"] }
Expand Down
Loading
Loading