aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/net
diff options
context:
space:
mode:
authorSeth Barrios <sethbarrios@google.com>2019-11-18 14:51:25 -0800
committerSeth Barrios <sethbarrios@google.com>2019-11-18 15:26:49 -0800
commit1ada2e26e81916409261ddb476e520e364b1d3be (patch)
tree486165ffd5211addc93fd9ed10ac3d5d20693838 /mitmproxy/net
parentcd660a035f74e00bd94a9b67c240ee44654ce1ce (diff)
downloadmitmproxy-1ada2e26e81916409261ddb476e520e364b1d3be.tar.gz
mitmproxy-1ada2e26e81916409261ddb476e520e364b1d3be.tar.bz2
mitmproxy-1ada2e26e81916409261ddb476e520e364b1d3be.zip
Add support for IPv6-only environments
Previously, the proxy would attempt to bind to an IPv6 + IPv4 enabled socket. On failure, it would try to bind to an IPv4 only socket. If that failed, the proxy would fail to start. This update makes it so that the proxy also tries to bind to an IPv6-only socket, which is necessary in environments where IPv4 is disabled. In short, the proxy will try binding in the following order, only moving to the next step when binding fails: IPv6 + IPv4 -> IPv4 only -> IPv6 only -> proxy fails to start.
Diffstat (limited to 'mitmproxy/net')
-rw-r--r--mitmproxy/net/tcp.py18
1 files changed, 15 insertions, 3 deletions
diff --git a/mitmproxy/net/tcp.py b/mitmproxy/net/tcp.py
index 2496d47c..07cee466 100644
--- a/mitmproxy/net/tcp.py
+++ b/mitmproxy/net/tcp.py
@@ -558,7 +558,7 @@ class TCPServer:
self.socket = None
try:
- # First try to bind an IPv6 socket, with possible IPv4 if the OS supports it.
+ # First try to bind an IPv6 socket, attempting to enable IPv4 support if the OS supports it.
# This allows us to accept connections for ::1 and 127.0.0.1 on the same socket.
# Only works if self.address == ""
self.socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
@@ -572,8 +572,20 @@ class TCPServer:
self.socket = None
if not self.socket:
- # Binding to an IPv6 socket failed, lets fall back to IPv4.
- self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ try:
+ # Binding to an IPv6 + IPv4 socket failed, lets fall back to IPv4 only.
+ self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
+ self.socket.bind(self.address)
+ except socket.error:
+ if self.socket:
+ self.socket.close()
+ self.socket = None
+
+ if not self.socket:
+ # Binding to an IPv4 only socket failed, lets fall back to IPv6 only.
+ self.socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
self.socket.bind(self.address)