commit 1d6c8b543e311cd3b219693723df5b0f5b411023
Author: David Fifield <david(a)bamsoftware.com>
Date: Sun Sep 23 10:09:08 2012 -0700
Add a format_sockaddr for Python sockaddr tuples.
As opposed to our own internal (host, port) tuples. They are the same in
IPv4, but Python IPv6 sockaddr tuples contain an additional two
elements: flowinfo and scopeid.
---
flashproxy-client | 48 ++++++++++++++++++++++++++++--------------------
1 files changed, 28 insertions(+), 20 deletions(-)
diff --git a/flashproxy-client b/flashproxy-client
index 226be6d..293c26c 100755
--- a/flashproxy-client
+++ b/flashproxy-client
@@ -144,6 +144,20 @@ def format_addr(addr):
def safe_format_addr(addr):
return safe_str(format_addr(addr))
+def format_sockaddr(sockaddr):
+ host, port = socket.getnameinfo(sockaddr, socket.NI_NUMERICHOST | socket.NI_NUMERICSERV)
+ port = int(port)
+ return format_addr((host, port))
+
+def safe_format_sockaddr(sockaddr):
+ return safe_str(format_sockaddr(sockaddr))
+
+def safe_format_peername(s):
+ try:
+ return safe_format_sockaddr(s.getpeername())
+ except socket.error, e:
+ return "<unconnected>"
+
def apply_mask_numpy(payload, mask_key):
if len(payload) == 0:
@@ -428,12 +442,6 @@ def listen_socket(addr):
s.setblocking(0)
return s
-def format_peername(s):
- try:
- return safe_format_addr(s.getpeername())
- except socket.error, e:
- return "<unconnected>"
-
# How long to wait for a WebSocket request on the remote socket. It is limited
# to avoid Slowloris-like attacks.
WEBSOCKET_REQUEST_TIMEOUT = 2.0
@@ -618,8 +626,8 @@ def handle_socks_request(fd):
return True
def report_pending():
- log(u"locals (%d): %s" % (len(locals), [format_peername(x) for x in locals]))
- log(u"remotes (%d): %s" % (len(remotes), [format_peername(x) for x in remotes]))
+ log(u"locals (%d): %s" % (len(locals), [safe_format_peername(x) for x in locals]))
+ log(u"remotes (%d): %s" % (len(remotes), [safe_format_peername(x) for x in remotes]))
def register():
if not options.register:
@@ -651,7 +659,7 @@ def proxy_chunk_local_to_remote(local, remote, data = None):
remote.close()
return False
if not data:
- log(u"EOF from local %s." % format_peername(local))
+ log(u"EOF from local %s." % safe_format_peername(local))
local.close()
remote.close()
return False
@@ -673,7 +681,7 @@ def proxy_chunk_remote_to_local(remote, local, data = None):
local.close()
return False
if not data:
- log(u"EOF from remote %s." % format_peername(remote))
+ log(u"EOF from remote %s." % safe_format_peername(remote))
remote.close()
local.close()
return False
@@ -690,7 +698,7 @@ def proxy_chunk_remote_to_local(remote, local, data = None):
if data is None:
break
elif not data:
- log(u"WebSocket close from remote %s." % format_peername(remote))
+ log(u"WebSocket close from remote %s." % safe_format_peername(remote))
remote.close()
local.close()
return False
@@ -714,14 +722,14 @@ def receive_unlinked(fd, label):
fd.close()
return False
if not data:
- log(u"EOF from unlinked %s %s with %d bytes buffered." % (label, format_peername(fd), len(fd.buf)))
+ log(u"EOF from unlinked %s %s with %d bytes buffered." % (label, safe_format_peername(fd), len(fd.buf)))
fd.close()
return False
else:
- log(u"Data from unlinked %s %s (%d bytes)." % (label, format_peername(fd), len(data)))
+ log(u"Data from unlinked %s %s (%d bytes)." % (label, safe_format_peername(fd), len(data)))
fd.buf += data
if len(fd.buf) >= UNCONNECTED_BUFFER_LIMIT:
- log(u"Refusing to buffer more than %d bytes from %s %s." % (UNCONNECTED_BUFFER_LIMIT, label, format_peername(fd)))
+ log(u"Refusing to buffer more than %d bytes from %s %s." % (UNCONNECTED_BUFFER_LIMIT, label, safe_format_peername(fd)))
fd.close()
return False
return True
@@ -732,7 +740,7 @@ def match_proxies():
local = unlinked_locals.pop(0)
remote_addr, remote_port = remote.getpeername()
local_addr, local_port = local.getpeername()
- log(u"Linking %s and %s." % (format_peername(local), format_peername(remote)))
+ log(u"Linking %s and %s." % (safe_format_peername(local), safe_format_peername(remote)))
remote.partner = local
local.partner = remote
if remote.buf:
@@ -791,15 +799,15 @@ def main():
for fd in rset:
if fd == remote_s:
remote_c, addr = fd.accept()
- log(u"Remote connection from %s." % safe_format_addr(addr))
+ log(u"Remote connection from %s." % safe_format_sockaddr(addr))
websocket_pending.append(TimeoutSocket(remote_c))
elif fd == local_s:
local_c, addr = fd.accept()
- log(u"Local connection from %s." % safe_format_addr(addr))
+ log(u"Local connection from %s." % safe_format_sockaddr(addr))
socks_pending.append(local_c)
register()
elif fd in websocket_pending:
- log(u"Data from WebSocket-pending %s." % safe_format_addr(addr))
+ log(u"Data from WebSocket-pending %s." % safe_format_sockaddr(addr))
protocols = handle_websocket_request(fd)
if protocols is not None:
wrapped = RemoteSocket(fd, protocols)
@@ -811,7 +819,7 @@ def main():
websocket_pending.remove(fd)
report_pending()
elif fd in socks_pending:
- log(u"SOCKS request from %s." % safe_format_addr(addr))
+ log(u"SOCKS request from %s." % safe_format_sockaddr(addr))
if handle_socks_request(fd):
wrapped = LocalSocket(fd)
locals.append(wrapped)
@@ -849,7 +857,7 @@ def main():
pending = websocket_pending[0]
if pending.age() < WEBSOCKET_REQUEST_TIMEOUT:
break
- log(u"Expired remote connection from %s." % format_peername(pending))
+ log(u"Expired remote connection from %s." % safe_format_peername(pending))
pending.close()
websocket_pending.pop(0)
report_pending()