diff options
author | Maximilian Hils <git@maximilianhils.com> | 2015-08-30 15:27:29 +0200 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2015-08-30 15:27:29 +0200 |
commit | a86ec56012136664688fa4a8efcd866b5e3e17a8 (patch) | |
tree | d8aa559db0e3c83a56bc3bac850021f133ad1248 /libmproxy/proxy/modes/socks_proxy.py | |
parent | 421b241ff010ae979cff8df504b6744e4c291aeb (diff) | |
download | mitmproxy-a86ec56012136664688fa4a8efcd866b5e3e17a8.tar.gz mitmproxy-a86ec56012136664688fa4a8efcd866b5e3e17a8.tar.bz2 mitmproxy-a86ec56012136664688fa4a8efcd866b5e3e17a8.zip |
move files around
Diffstat (limited to 'libmproxy/proxy/modes/socks_proxy.py')
-rw-r--r-- | libmproxy/proxy/modes/socks_proxy.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/libmproxy/proxy/modes/socks_proxy.py b/libmproxy/proxy/modes/socks_proxy.py new file mode 100644 index 00000000..ebaf939e --- /dev/null +++ b/libmproxy/proxy/modes/socks_proxy.py @@ -0,0 +1,60 @@ +from __future__ import (absolute_import, print_function, division) + +from netlib import socks +from netlib.tcp import NetLibError + +from ...exceptions import Socks5Exception +from ...protocol import Layer, ServerConnectionMixin + + +class Socks5Proxy(Layer, ServerConnectionMixin): + def __call__(self): + try: + # Parse Client Greeting + client_greet = socks.ClientGreeting.from_file(self.client_conn.rfile, fail_early=True) + client_greet.assert_socks5() + if socks.METHOD.NO_AUTHENTICATION_REQUIRED not in client_greet.methods: + raise socks.SocksError( + socks.METHOD.NO_ACCEPTABLE_METHODS, + "mitmproxy only supports SOCKS without authentication" + ) + + # Send Server Greeting + server_greet = socks.ServerGreeting( + socks.VERSION.SOCKS5, + socks.METHOD.NO_AUTHENTICATION_REQUIRED + ) + server_greet.to_file(self.client_conn.wfile) + self.client_conn.wfile.flush() + + # Parse Connect Request + connect_request = socks.Message.from_file(self.client_conn.rfile) + connect_request.assert_socks5() + if connect_request.msg != socks.CMD.CONNECT: + raise socks.SocksError( + socks.REP.COMMAND_NOT_SUPPORTED, + "mitmproxy only supports SOCKS5 CONNECT." + ) + + # We always connect lazily, but we need to pretend to the client that we connected. + connect_reply = socks.Message( + socks.VERSION.SOCKS5, + socks.REP.SUCCEEDED, + connect_request.atyp, + # dummy value, we don't have an upstream connection yet. + connect_request.addr + ) + connect_reply.to_file(self.client_conn.wfile) + self.client_conn.wfile.flush() + + except (socks.SocksError, NetLibError) as e: + raise Socks5Exception("SOCKS5 mode failure: %s" % repr(e), e) + + self.server_conn.address = connect_request.addr + + layer = self.ctx.next_layer(self) + try: + layer() + finally: + if self.server_conn: + self._disconnect() |