Skip to content

Conversation

@errorxyz
Copy link
Member

@errorxyz errorxyz commented Mar 5, 2025

Refs mitmproxy/mitmproxy#6671
Fixes bug where wireguard server binding over IPv6 and IPv4 is handled incorrectly when no host is specified.

@errorxyz errorxyz changed the title Require host to be specified for WireGuardConf and set IPV6_V6ONLY off Require host to be specified for WireGuardConf and set IPV6_V6ONLY Mar 5, 2025
Copy link
Member

@mhils mhils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this! Would it make sense to merge both implementations into a common function with an explanatory docstring?

Copy link
Member

@decathorpe decathorpe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, reading the PR title / commit message first and the actual code changes second I was very confused what this PR was actually doing 😅

As far as I can tell, you're

  1. dropping the unintended fallback to an IPv6 socket if binding to an IPv4 socket fails
  2. requiring the "host" argument not to be empty, and
  3. setting IPv6 only mode if the address is an IPv6 address.

The changes 1. and 3. look good to me, but I'm not sure I like losing the default behaviour of "if no host is specified, fall back to 0.0.0.0, which is a good default IMO.

Maybe falling back to 0.0.0.0 would be ok if the "host" argument is empty? That should match the current behaviour in all cases except the pathological one where binding to 0.0.0.0 failed but binding to ::1 works for some strange reason.

@mhils
Copy link
Member

mhils commented Mar 6, 2025

I think there's an argument to be made that we should just mirror what std::net::UdpListener does, which also requires "host" to not be empty. What do you think? We could also just take std::net::ToSocketAddrs and be done with it. We don't need special empty string logic. :)

@decathorpe
Copy link
Member

Ok, if the UDP mode already does it this way, it makes sense to have consistent behaviour.

@errorxyz
Copy link
Member Author

errorxyz commented Mar 6, 2025

FWIW, reading the PR title / commit message first and the actual code changes second I was very confused what this PR was actually doing 😅

Totally agreed 😅 I was drafting this in the middle of the night and I guess I just got too tired by the time I submitted the PR. Will make it clearer in the coming days :)

@errorxyz errorxyz changed the title Require host to be specified for WireGuardConf and set IPV6_V6ONLY Wireguard: Bind server to specified address only Mar 16, 2025
@errorxyz errorxyz requested review from decathorpe and mhils March 16, 2025 17:11
Copy link
Member

@mhils mhils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great already, thank you!

}

/// Creates a UDP socket bound to the specified address, restricted to either IPv4 or IPv6 only.
pub fn create_udp_socket(host: &str, port: u16) -> Result<tokio::net::UdpSocket> {
Copy link
Member

@mhils mhils Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn create_udp_socket(host: &str, port: u16) -> Result<tokio::net::UdpSocket> {
pub fn create_and_bind_udp_socket<A: ToSocketAddrs>(addr: A) -> Result<tokio::net::UdpSocket> {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to make this pub(crate) instead of pub? Probably doesn't make sense to have a helper function like that in the public API. But given that everything and the kitchen sink in the mitmproxy crate is already pub, not sure if that makes a difference 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The crate is not public, so we can't break any downstream users. But I agree, we can make it pub(crate). 😛

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've assumed addr.to_socket_addrs() returns an iterator containing a single address only.

@errorxyz errorxyz requested a review from mhils March 23, 2025 16:42
Copy link
Member

@mhils mhils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the diff I actually didn't like my own suggestion of using ToSocketAddrs. Here's an alternative proposal to go with just SocketAddr. What do you think?

@errorxyz
Copy link
Member Author

errorxyz commented Mar 29, 2025

Looking at the diff I actually didn't like my own suggestion of using ToSocketAddrs. Here's an alternative proposal to go with just SocketAddr. What do you think?

I prefer passing the host as a string and converting it to IpAddr on the Rust side, as it feels more natural. But I don't have a strong preference.

@errorxyz errorxyz requested a review from mhils March 31, 2025 09:40
@mhils mhils merged commit 11bf9ab into mitmproxy:main Mar 31, 2025
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants