Skip to content

Listen on a unix socket in reverse-proxy mode #7072

@axelkar

Description

@axelkar

Problem Description

I'm trying to debug a HTTP application on a server and need mitmproxy to listen on a HTTPS port. I need features from Nginx so I made nginx proxy_pass to a Unix socket. I don't want to make mitmproxy accessible outside of Nginx.

Proposal

The ability to listen on a unix socket in server modes should be added.

I addition, a feature to get the real IP from X-Real-IP or X-Forwarded-For would be needed!
This is needed for reverse-proxying mitmproxy's reverse proxy without Unix sockets in between too.

The architecture for ProxyMode would need to be changed slightly to accept anything not fitting with to a host string and a port number:

custom_listen_host: str | None
"""A custom listen host, if specified in the spec."""
custom_listen_port: int | None
"""A custom listen port, if specified in the spec."""

AsyncioServerInstance._start and .listen would also need to be changed to be able to listen on a Unix socket:

async def listen(
self, host: str, port: int
) -> list[asyncio.Server | mitmproxy_rs.UdpServer]:
if self.mode.transport_protocol not in ("tcp", "udp", "both"):
raise AssertionError(self.mode.transport_protocol)

Alternatives

I could use socat, but it's error-prone and I still would need the feature to get the real IP.

Additional context

import socket

server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind("/tmp/socket_test.sock")

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featureNew features / enhancements

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions