Skip to content

Multi-Server Mode - #5393

Merged
mhils merged 2 commits into
mitmproxy:mainfrom
mhils:multiproxy
Jul 21, 2022
Merged

mhils merged 2 commits into
mitmproxy:mainfrom
mhils:multiproxy

Conversation

@mhils

@mhils mhils commented Jun 3, 2022

Copy link
Copy Markdown
Member

This is a very-much-work-in-progress implementation of the multi proxy mode discussed in #5288:

$ mitmdump --mode regular --mode reverse:example.com@8443
HTTP(S) proxy listening at *:8080.
Reverse proxy to example.com listening at *:8443.

Tests are still failing, but the invocation above works. There are a few minor changes on the latest dicussions:

  • @Prinzhorn's question, "what new properties does Flow need so we know where it came from?", to which I foolishly replied "None. If you for some weird reason want different logic here, you can look at flow.client_conn.sockname" was of course spot-on. There are some legitimate cases where we need this (e.g. proxyauth) and Client.proxy_mode is a thing now. 😄
  • After pondering on this a bit, I think it's most sensible if we continue to default to a single HTTP(S) proxy on port 8080. However, we shall have super easy buttons in mitmproxy/mitmweb to quickly start up more services.
  • dns/regular-dns/... wording isn't fully fleshed out yet, we can make final decisions here once we have a raw UDP layer and/or QUIC.
  • @meitinger / @decathorpe: Do mitmproxy/proxy/mode_specs.py and mitmproxy/proxy/mode_servers.py make sense to you? Does that work for WireGuard/QUIC?

TODO:

  • Fix tests.
  • Fail on duplicate ports.
  • Address remaining FIXMEs.
  • Adjust mitmweb.
  • Add migration code for listen_host and listen_port.
  • Docs.

@decathorpe

Copy link
Copy Markdown
Member

This looks remarkably similar to what I looked into doing just earlier today.
So from my (WireGuard server mode) POV this looks good, AFAICT 👍🏼

@mhils mhils mentioned this pull request Jun 5, 2022
6 tasks done
Comment thread mitmproxy/proxy/mode_servers.py Resolved
@meitinger

meitinger commented Jun 6, 2022 •

Copy link
Copy Markdown
Contributor
  • dns/regular-dns/... wording isn't fully fleshed out yet, we can make final decisions here once we have a raw UDP layer and/or QUIC.

If there's only one DnsMode that gets encoded into server_conn.address, it might be beneficial to pick a word that is not a valid host name. (Not a problem if only IP addresses are accepted otherwise, but as far as I can see, the new implementation also accepts host names.)

  • @meitinger / @decathorpe: Do mitmproxy/proxy/mode_specs.py and mitmproxy/proxy/mode_servers.py make sense to you? Does that work for WireGuard/QUIC?

Looks good and should work well :)
To simplify UDP servers, maybe introduce a UdpServerInstance class, that - like TcpServerInstance - also has a make_top_layer and additionally

    @abstractmethod
    def build_connection_id(
        self,
        transport: asyncio.DatagramTransport,
        data: bytes,
        remote_addr: Address,
        local_addr: Address,
    ) -> Optional[str]:
        pass

    @property
    @abstractmethod
    def timeout(self) -> int:
        pass

Because creating a task for new connections or adding packets to an existing one should be the same for most UDP servers. If build_connection_id returns None, the packet is ignored.

EDIT: The preliminary handle_udp_connection method from the upcoming QUIC implementation may be used to inspire UdpServerInstance:
https://github.com/Meitinger/mitmproxy/blob/0e49eebe62dbe3e527ba95934e05ade39fde62d8/mitmproxy/addons/proxyserver.py#L355

@Prinzhorn

Prinzhorn commented Jun 13, 2022 •

Copy link
Copy Markdown
Member

I was thinking what if we deprecate --mode with --listen-port and --listen-host? I don't think re-using --mode with the new semantics makes sense (it's more than just mode), maybe call it --listen / --listener as a new option?

@kckeiks kckeiks mentioned this pull request Jul 18, 2022
2 tasks done

@meitinger meitinger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great, especially having server instance classes (and proxy modes) with your __init_subclass__ method will allow for cleaner code and easier maintenance/extensibility, very cool!

Once #5393 lands I'll update #5435 and introduce a common UdpServerInstance for DnsInstance, QuicInstance and other.

Comment thread mitmproxy/addons/proxyserver.py Resolved
Comment thread mitmproxy/addons/proxyserver.py Resolved
Comment thread mitmproxy/proxy/mode_specs.py Resolved
@mhils
mhils force-pushed the multiproxy branch 2 times, most recently from ce5e20a to ad7bdca Compare July 21, 2022 13:02
@mhils mhils changed the title [wip] Multi-Server Mode Multi-Server Mode Jul 21, 2022
@mhils
mhils enabled auto-merge July 21, 2022 13:23
mhils added 2 commits July 21, 2022 15:24
This commit makes it possible for mitmproxy to spawn multiple
TCP/UDP proxy servers at the same time, see
mitmproxy#5288
@mhils
mhils merged commit c7e40cd into mitmproxy:main Jul 21, 2022
@mhils
mhils deleted the multiproxy branch July 21, 2022 13:36
@meitinger meitinger mentioned this pull request Aug 17, 2022
9 tasks done
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.

4 participants