public inbox for gentoo-portage-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Michał Górny" <mgorny@gentoo.org>
To: gentoo-portage-dev@lists.gentoo.org
Cc: "Michał Górny" <mgorny@gentoo.org>
Subject: [gentoo-portage-dev] [PATCH] SOCKSv5: report bound socket name
Date: Sun, 25 Jan 2015 13:19:37 +0100	[thread overview]
Message-ID: <1422188377-27703-1-git-send-email-mgorny@gentoo.org> (raw)

Report bound socket name as requested by the protocol. Supports both
IPv4 and IPv6 sockets.
---
 bin/socks5-server.py | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/bin/socks5-server.py b/bin/socks5-server.py
index 4795dcc..78a6751 100644
--- a/bin/socks5-server.py
+++ b/bin/socks5-server.py
@@ -11,6 +11,7 @@ import sys
 
 
 class ProxyConnection(asyncore.dispatcher_with_send):
+	_family = None
 	_proxy_conn = None
 
 	def __init__(self, host, port, proxy_conn):
@@ -18,6 +19,7 @@ class ProxyConnection(asyncore.dispatcher_with_send):
 		asyncore.dispatcher_with_send.__init__(self)
 		# TODO: how to support IPv6? ugly fail-then-reinit?
 		self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
+		self._family = socket.AF_INET
 		self.connect((host, port))
 
 	def handle_read(self):
@@ -25,7 +27,7 @@ class ProxyConnection(asyncore.dispatcher_with_send):
 		self._proxy_conn.send(buf)
 
 	def handle_connect(self):
-		self._proxy_conn.send_connected()
+		self._proxy_conn.send_connected(self._family, self.getsockname())
 
 	def handle_close(self):
 		self._proxy_conn.close()
@@ -126,9 +128,20 @@ class ProxyHandler(asyncore.dispatcher_with_send):
 		if self._my_conn is not None:
 			self._my_conn.close()
 
-	def send_connected(self):
-		repl = struct.pack('!BBBBLH', 0x05, 0x00, 0x00, 0x01,
-				0x00000000, 0x0000)
+	def send_connected(self, family, addr):
+		if family == socket.AF_INET:
+			host, port = addr
+			bin_host = socket.inet_aton(host)
+
+			repl = struct.pack('!BBBB4sH', 0x05, 0x00, 0x00, 0x01,
+					bin_host, port)
+		elif family == socket.AF_INET6:
+			host, port = addr
+			bin_host = socket.inet_pton(family, host)
+
+			repl = struct.pack('!BBBB16sH', 0x05, 0x00, 0x00, 0x04,
+					bin_host, port)
+
 		self.send(repl)
 		self._my_state = 3
 
-- 
2.2.2



             reply	other threads:[~2015-01-25 12:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-25 12:19 Michał Górny [this message]
2015-01-25 14:01 ` [gentoo-portage-dev] [PATCH] SOCKSv5: report bound socket name Michał Górny

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1422188377-27703-1-git-send-email-mgorny@gentoo.org \
    --to=mgorny@gentoo.org \
    --cc=gentoo-portage-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox