diff --git a/README.md b/README.md index 1544b74e..67e96552 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ It downloads the latest version of ZeroNet then starts it automatically. * `python get-pip.py --user pyzmq gevent msgpack-python` * Start with `python zeronet.py` -## Mac +### Mac * Install [brew](http://brew.sh/) * `brew install python` diff --git a/plugins/Stats/StatsPlugin.py b/plugins/Stats/StatsPlugin.py index 8609b26d..9aee507d 100644 --- a/plugins/Stats/StatsPlugin.py +++ b/plugins/Stats/StatsPlugin.py @@ -1,5 +1,6 @@ import re, time, cgi, os from Plugin import PluginManager +from Config import config @PluginManager.registerTo("UiRequest") class UiRequestPlugin(object): @@ -52,6 +53,8 @@ class UiRequestPlugin(object): # Memory try: + yield "Ip external: %s | " % config.ip_external + yield "Port opened: %s | " % main.file_server.port_opened import psutil process = psutil.Process(os.getpid()) mem = process.get_memory_info()[0] / float(2 ** 20) @@ -59,21 +62,23 @@ class UiRequestPlugin(object): yield "Threads: %s | " % len(process.threads()) yield "CPU: usr %.2fs sys %.2fs | " % process.cpu_times() yield "Open files: %s | " % len(process.open_files()) - yield "Sockets: %s" % len(process.connections()) - yield " | Calc size on off
" + yield "Sockets: %s | " % len(process.connections()) + yield "Calc size on off" except Exception, err: pass + yield "
" # Connections yield "Connections (%s):
" % len(main.file_server.connections) - yield "" + yield "
id protocol type ip ping buff
" yield "" for connection in main.file_server.connections: yield self.formatTableRow([ ("%3d", connection.id), ("%s", connection.protocol), ("%s", connection.type), - ("%s", connection.ip), + ("%s:%s", (connection.ip, connection.port)), + ("%s", connection.handshake.get("port_opened")), ("%6.3f", connection.last_ping_delay), ("%s", connection.incomplete_buff_recv), ("since", max(connection.last_send_time, connection.last_recv_time)), @@ -97,7 +102,7 @@ class UiRequestPlugin(object): yield self.formatTableRow([ ("%s", site.address), ("%s", len(site.peers)), - ("%s", len([peer for peer in site.peers.values() if peer.connection and peer.connection.connected])), + ("%s/%s", ( len([peer for peer in site.peers.values() if peer.connection and peer.connection.connected]), len(site.peers) ) ), ("%s", [peer.connection.id for peer in site.peers.values() if peer.connection and peer.connection.connected]), ("%s", len(site.content_manager.contents)), ]) diff --git a/plugins/Zeroname/SiteManagerPlugin.py b/plugins/Zeroname/SiteManagerPlugin.py index 1939e928..e2962549 100644 --- a/plugins/Zeroname/SiteManagerPlugin.py +++ b/plugins/Zeroname/SiteManagerPlugin.py @@ -15,7 +15,7 @@ class SiteManagerPlugin(object): site_zeroname = None def load(self): super(SiteManagerPlugin, self).load() - self.need(self.zeroname_address) # Need ZeroName site + if not self.get(self.zeroname_address): self.need(self.zeroname_address) # Need ZeroName site # Checks if its a valid address def isAddress(self, address): diff --git a/plugins/Zeroname/UiRequestPlugin.py b/plugins/Zeroname/UiRequestPlugin.py index 65a386f1..91eea2a2 100644 --- a/plugins/Zeroname/UiRequestPlugin.py +++ b/plugins/Zeroname/UiRequestPlugin.py @@ -3,10 +3,10 @@ from Plugin import PluginManager @PluginManager.registerTo("UiRequest") class UiRequestPlugin(object): - def __init__(self, server = None): + def __init__(self, *args, **kwargs): from Site import SiteManager self.site_manager = SiteManager.site_manager - super(UiRequestPlugin, self).__init__(server) + super(UiRequestPlugin, self).__init__(*args, **kwargs) # Media request diff --git a/plugins/disabled-Multiuser/MultiuserPlugin.py b/plugins/disabled-Multiuser/MultiuserPlugin.py index ed05dba2..850a0995 100644 --- a/plugins/disabled-Multiuser/MultiuserPlugin.py +++ b/plugins/disabled-Multiuser/MultiuserPlugin.py @@ -4,9 +4,9 @@ from Crypt import CryptBitcoin @PluginManager.registerTo("UiRequest") class UiRequestPlugin(object): - def __init__(self, server = None): + def __init__(self, *args, **kwargs): self.user_manager = sys.modules["User.UserManager"].user_manager - super(UiRequestPlugin, self).__init__(server) + super(UiRequestPlugin, self).__init__(*args, **kwargs) # Create new user and inject user welcome message if necessary diff --git a/src/Config.py b/src/Config.py index af49bc78..c625700c 100644 --- a/src/Config.py +++ b/src/Config.py @@ -87,7 +87,8 @@ class Config(object): parser.add_argument('--fileserver_ip', help='FileServer bind address', default="*", metavar='ip') parser.add_argument('--fileserver_port',help='FileServer bind port', default=15441, type=int, metavar='port') parser.add_argument('--disable_zeromq', help='Disable compatibility with old clients', action='store_true') - + parser.add_argument('--proxy', help='Socks proxy address', metavar='ip:port') + parser.add_argument('--disable_udp', help='Disable UDP connections', action='store_true') parser.add_argument('--ip_external', help='External ip (tested on start if None)', metavar='ip') parser.add_argument('--coffeescript_compiler', help='Coffeescript compiler for developing', default=coffeescript, metavar='executable_path') diff --git a/src/Connection/Connection.py b/src/Connection/Connection.py index 34a6c02e..55d67f7d 100644 --- a/src/Connection/Connection.py +++ b/src/Connection/Connection.py @@ -76,7 +76,7 @@ class Connection: self.log("Connecting...") self.type = "out" self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.sock.connect((self.ip, self.port)) + self.sock.connect((self.ip, int(self.port))) # Detect protocol self.send({"cmd": "handshake", "req_id": 0, "params": self.handshakeInfo()}) gevent.spawn(self.messageLoop) @@ -181,7 +181,8 @@ class Connection: "version": config.version, "protocol": "v2", "peer_id": self.server.peer_id, - "fileserver_port": config.fileserver_port + "fileserver_port": config.fileserver_port, + "port_opened": self.server.port_opened } diff --git a/src/Content/ContentManager.py b/src/Content/ContentManager.py index e5baa739..34157820 100644 --- a/src/Content/ContentManager.py +++ b/src/Content/ContentManager.py @@ -97,6 +97,7 @@ class ContentManager: content = self.contents.get(content_inner_path.strip("/")) if content and "files" in content: # Check if content.json exists back = content["files"].get("/".join(inner_path_parts)) + if not back: return False back["content_inner_path"] = content_inner_path return back else: # No inner path in this dir, lets try the parent dir diff --git a/src/File/FileRequest.py b/src/File/FileRequest.py index 1f982cb7..8d4fdacf 100644 --- a/src/File/FileRequest.py +++ b/src/File/FileRequest.py @@ -102,6 +102,9 @@ class FileRequest: if config.debug_socket: self.log.debug("Sending file %s from position %s to %s" % (file_path, params["location"], back["location"])) self.response(back) if config.debug_socket: self.log.debug("File %s sent" % file_path) + + # Add peer to site if not added before + # site.addPeer(self.connection.ip, self.connection.port) except Exception, err: self.log.debug("GetFile read error: %s" % Debug.formatException(err)) self.response({"error": "File read error: %s" % Debug.formatException(err)}) diff --git a/src/File/FileServer.py b/src/File/FileServer.py index e32ed4e5..b16e1421 100644 --- a/src/File/FileServer.py +++ b/src/File/FileServer.py @@ -74,7 +74,12 @@ class FileServer(ConnectionServer): self.log.info("[BAD :(] Port closed: %s" % message) if port == self.port: self.port_opened = False # Self port, update port_opened status - config.ip_external = False + match = re.match(".*?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", message) # Try find my external ip in message + if match: # Found my ip in message + config.ip_external = match.group(1) + SiteManager.peer_blacklist.append((config.ip_external, self.port)) # Add myself to peer blacklist + else: + config.ip_external = False return {"result": False, "message": message} else: self.log.info("[OK :)] Port open: %s" % message) diff --git a/src/Peer/Peer.py b/src/Peer/Peer.py index 60a453fc..eb7ec3a5 100644 --- a/src/Peer/Peer.py +++ b/src/Peer/Peer.py @@ -27,7 +27,7 @@ class Peer: # Connect to host def connect(self): - if not self.log: self.log = logging.getLogger("Peer:%s:%s %s" % (self.ip, self.port, self.site.address_short)) + if not self.log: self.log = logging.getLogger("Peer:%s:%s %s" % (self.ip, self.port, self.site)) if self.connection: self.log.debug("Getting connection (Closing %s)..." % self.connection) self.connection.close() diff --git a/src/Site/Site.py b/src/Site/Site.py index a2fbd5f2..62cf7391 100644 --- a/src/Site/Site.py +++ b/src/Site/Site.py @@ -1,5 +1,6 @@ -import os, json, logging, hashlib, re, time, string, random +import os, json, logging, hashlib, re, time, string, random, sys, binascii, struct, socket, urllib, urllib2 from lib.subtl.subtl import UdpTrackerClient +from lib import bencode import gevent import util from Config import config @@ -149,7 +150,7 @@ class Site: @util.Noparallel(blocking=False) def download(self, check_size=False): self.log.debug("Start downloading...%s" % self.bad_files) - self.announce() + gevent.spawn(self.announce) if check_size: # Check the size first valid = downloadContent(download_files=False) if not valid: return False # Cant download content.jsons or size is not fits @@ -245,7 +246,7 @@ class Site: self.bad_files[inner_path] = True # Mark as bad file if not self.content_manager.contents.get("content.json"): # No content.json, download it first! self.log.debug("Need content.json first") - self.announce() + gevent.spawn(self.announce) if inner_path != "content.json": # Prevent double download task = self.worker_manager.addTask("content.json", peer) task.get() @@ -256,6 +257,8 @@ class Site: self.log.debug("No info for %s, waiting for all content.json" % inner_path) success = self.downloadContent("content.json", download_files=False) if not success: return False + if not self.content_manager.getFileInfo(inner_path): return False # Still no info for file + task = self.worker_manager.addTask(inner_path, peer, priority=priority) if blocking: @@ -282,43 +285,84 @@ class Site: # Add myself and get other peers from tracker def announce(self, force=False): - if time.time() < self.last_announce+15 and not force: return # No reannouncing within 15 secs + if time.time() < self.last_announce+60 and not force: return # No reannouncing within 60 secs self.last_announce = time.time() errors = [] + address_hash = hashlib.sha1(self.address).hexdigest() + my_peer_id = sys.modules["main"].file_server.peer_id + + # Later, if we have peer exchange + """if sys.modules["main"].file_server.port_opened: + fileserver_port = config.fileserver_port + else: # Port not opened, report port 0 + fileserver_port = 0""" + + fileserver_port = config.fileserver_port + s = time.time() + announced = 0 for protocol, ip, port in SiteManager.TRACKERS: - if protocol == "udp": - # self.log.debug("Announcing to %s://%s:%s..." % (protocol, ip, port)) + if protocol == "udp": # Udp tracker + if config.disable_udp: continue # No udp supported tracker = UdpTrackerClient(ip, port) - tracker.peer_port = config.fileserver_port + tracker.peer_port = fileserver_port try: tracker.connect() tracker.poll_once() - tracker.announce(info_hash=hashlib.sha1(self.address).hexdigest(), num_want=50) + tracker.announce(info_hash=address_hash, num_want=50) back = tracker.poll_once() peers = back["response"]["peers"] except Exception, err: errors.append("%s://%s:%s" % (protocol, ip, port)) continue - - added = 0 - for peer in peers: - if (peer["addr"], peer["port"]) in self.peer_blacklist: # Ignore blacklist (eg. myself) - continue - if self.addPeer(peer["addr"], peer["port"]): added += 1 - if added: - self.worker_manager.onPeers() - self.updateWebsocket(peers_added=added) - self.settings["peers"] = len(peers) - self.saveSettings() - self.log.debug("Found %s peers, new: %s" % (len(peers), added)) - else: - pass # TODO: http tracker support + + else: # Http tracker + params = { + 'info_hash': binascii.a2b_hex(address_hash), + 'peer_id': my_peer_id, 'port': fileserver_port, + 'uploaded': 0, 'downloaded': 0, 'left': 0, 'compact': 1, 'numwant': 30, + 'event': 'started' + } + try: + url = "http://"+ip+"?"+urllib.urlencode(params) + # Load url + opener = urllib2.build_opener() + response = opener.open(url, timeout=10).read() + # Decode peers + peer_data = bencode.decode(response)["peers"] + peer_count = len(peer_data) / 6 + peers = [] + for peer_offset in xrange(peer_count): + off = 6 * peer_offset + peer = peer_data[off:off + 6] + addr, port = struct.unpack('!LH', peer) + peers.append({"addr": socket.inet_ntoa(struct.pack('!L', addr)), "port": port}) + except Exception, err: + self.log.debug("Http tracker %s error: %s" % (url, err)) + errors.append("%s://%s" % (protocol, ip)) + continue + + # Adding peers + added = 0 + for peer in peers: + if not peer["port"]: continue # Dont add peers with port 0 + if (peer["addr"], peer["port"]) in self.peer_blacklist: # Ignore blacklist (eg. myself) + continue + if self.addPeer(peer["addr"], peer["port"]): added += 1 + if added: + self.worker_manager.onPeers() + self.updateWebsocket(peers_added=added) + self.log.debug("Found %s peers, new: %s" % (len(peers), added)) + announced += 1 + # Save peers num + self.settings["peers"] = len(self.peers) + self.saveSettings() + if len(errors) < len(SiteManager.TRACKERS): # Less errors than total tracker nums - self.log.debug("Announced to %s trackers, errors: %s" % (len(SiteManager.TRACKERS), errors)) + self.log.debug("Announced to %s trackers in %.3fs, errors: %s" % (announced, time.time()-s, errors)) else: - self.log.error("Announced to %s trackers, failed" % len(SiteManager.TRACKERS)) + self.log.error("Announced to %s trackers in %.3fs, failed" % (announced, time.time()-s)) # Need open connections diff --git a/src/Site/SiteManager.py b/src/Site/SiteManager.py index 295b744e..1f7d0481 100644 --- a/src/Site/SiteManager.py +++ b/src/Site/SiteManager.py @@ -4,13 +4,17 @@ from Plugin import PluginManager TRACKERS = [ ("udp", "open.demonii.com", 1337), - ("udp", "sugoi.pomf.se", 2710), + #("udp", "sugoi.pomf.se", 2710), #("udp", "tracker.coppersurfer.tk", 80), ("udp", "tracker.leechers-paradise.org", 6969), ("udp", "9.rarbg.com", 2710), - ("udp", "www.eddie4.nl", 6969), - #("udp", "trackr.sytes.net", 80), #Backup trackers + #("udp", "www.eddie4.nl", 6969), + #("udp", "trackr.sytes.net", 80), #("udp", "tracker4.piratux.com", 6969) + ("http", "exodus.desync.com:80/announce", None), + ("http", "announce.torrentsmd.com:6969/announce", None), + #("http", "i.bandito.org/announce", None), + #("http", "tracker.tfile.me/announce", None), ] diff --git a/src/Test/test.py b/src/Test/test.py index 741b3012..7a93da22 100644 --- a/src/Test/test.py +++ b/src/Test/test.py @@ -153,6 +153,8 @@ class TestCase(unittest.TestCase): print "ok" + + diff --git a/src/Ui/UiRequest.py b/src/Ui/UiRequest.py index d140ebcd..d0a0c8c8 100644 --- a/src/Ui/UiRequest.py +++ b/src/Ui/UiRequest.py @@ -16,14 +16,14 @@ status_texts = { @PluginManager.acceptPlugins class UiRequest(object): - def __init__(self, server = None): + def __init__(self, server, get, env, start_response): if server: self.server = server self.log = server.log - self.get = {} # Get parameters - self.env = {} # Enviroment settings + self.get = get # Get parameters + self.env = env # Enviroment settings + self.start_response = start_response # Start response function self.user = None - self.start_response = None # Start response function # Call the request handler function base on path @@ -214,12 +214,19 @@ class UiRequest(object): from Debug import DebugMedia DebugMedia.merge(file_path) if os.path.isfile(file_path): # File exits + #self.sendHeader(content_type=self.getContentType(file_path)) # ?? Get Exception without this return self.actionFile(file_path) else: # File not exits, try to download site = SiteManager.site_manager.need(address, all_file=False) - self.sendHeader(content_type=self.getContentType(file_path)) # ?? Get Exception without this result = site.needFile(match.group("inner_path"), priority=1) # Wait until file downloads - return self.actionFile(file_path) + if result: + #self.sendHeader(content_type=self.getContentType(file_path)) + return self.actionFile(file_path) + else: + self.log.debug("File not found: %s" % match.group("inner_path")) + self.error404(match.group("inner_path")) + #self.sendHeader(404) + #return "Not found" else: # Bad url return self.error404(path) @@ -304,7 +311,7 @@ class UiRequest(object): raise last_error[0], last_error[1], last_error[2] else: self.sendHeader() - yield "No error! :)" + return "No error! :)" # Just raise an error to get console @@ -353,11 +360,11 @@ class UiRequest(object): return "

Server error

%s" % cgi.escape(message) - # - Reload for eaiser developing - - def reload(self): - import imp, sys - global UiWebsocket - UiWebsocket = imp.load_source("UiWebsocket", "src/Ui/UiWebsocket.py").UiWebsocket - #reload(sys.modules["User.UserManager"]) - #UserManager.reloadModule() - #self.user = UserManager.user_manager.getCurrent() +# - Reload for eaiser developing - +#def reload(): + #import imp, sys + #global UiWebsocket + #UiWebsocket = imp.load_source("UiWebsocket", "src/Ui/UiWebsocket.py").UiWebsocket + #reload(sys.modules["User.UserManager"]) + #UserManager.reloadModule() + #self.user = UserManager.user_manager.getCurrent() diff --git a/src/Ui/UiServer.py b/src/Ui/UiServer.py index a8bd1f1d..a16f3490 100644 --- a/src/Ui/UiServer.py +++ b/src/Ui/UiServer.py @@ -45,33 +45,34 @@ class UiServer: self.sites = SiteManager.site_manager.list() self.log = logging.getLogger(__name__) - self.ui_request = UiRequest(self) # Handle WSGI request def handleRequest(self, env, start_response): path = env["PATH_INFO"] - self.ui_request.env = env - self.ui_request.start_response = start_response if env.get("QUERY_STRING"): - self.ui_request.get = dict(cgi.parse_qsl(env['QUERY_STRING'])) + get = dict(cgi.parse_qsl(env['QUERY_STRING'])) else: - self.ui_request.get = {} + get = {} + ui_request = UiRequest(self, get, env, start_response) if config.debug: # Let the exception catched by werkezung - return self.ui_request.route(path) + return ui_request.route(path) else: # Catch and display the error try: - return self.ui_request.route(path) + return ui_request.route(path) except Exception, err: logging.debug("UiRequest error: %s" % Debug.formatException(err)) - return self.ui_request.error500("Err: %s" % Debug.formatException(err)) + return ui_request.error500("Err: %s" % Debug.formatException(err)) + # Reload the UiRequest class to prevent restarts in debug mode def reload(self): + global UiRequest import imp, sys reload(sys.modules["User.UserManager"]) - self.ui_request = imp.load_source("UiRequest", "src/Ui/UiRequest.py").UiRequest(self) - self.ui_request.reload() + reload(sys.modules["Ui.UiWebsocket"]) + UiRequest = imp.load_source("UiRequest", "src/Ui/UiRequest.py").UiRequest + #UiRequest.reload() # Bind and run the server diff --git a/src/Ui/UiWebsocket.py b/src/Ui/UiWebsocket.py index fd5927be..b27b10ee 100644 --- a/src/Ui/UiWebsocket.py +++ b/src/Ui/UiWebsocket.py @@ -24,9 +24,9 @@ class UiWebsocket(object): def start(self): ws = self.ws if self.site.address == config.homepage and not self.site.page_requested: # Add open fileserver port message or closed port error to homepage at first request after start - if config.ip_external: + if sys.modules["main"].file_server.port_opened == True: self.site.notifications.append(["done", "Congratulation, your port "+str(config.fileserver_port)+" is opened.
You are full member of ZeroNet network!", 10000]) - elif config.ip_external == False: + elif sys.modules["main"].file_server.port_opened == False: self.site.notifications.append(["error", "Your network connection is restricted. Please, open "+str(config.fileserver_port)+" port
on your router to become full member of ZeroNet network.", 0]) self.site.page_requested = True # Dont add connection notification anymore @@ -41,7 +41,6 @@ class UiWebsocket(object): except Exception, err: if err.message != 'Connection is already closed': if config.debug: # Allow websocket errors to appear on /Debug - import sys sys.modules["main"].DebugHook.handleError() self.log.error("WebSocket error: %s" % Debug.formatException(err)) return "Bye." @@ -213,7 +212,7 @@ class UiWebsocket(object): def formatServerInfo(self): return { - "ip_external": bool(config.ip_external), + "ip_external": bool(sys.modules["main"].file_server.port_opened), "platform": sys.platform, "fileserver_ip": config.fileserver_ip, "fileserver_port": config.fileserver_port, @@ -401,7 +400,6 @@ class UiWebsocket(object): def actionServerUpdate(self, to): - import sys self.cmd("updating") sys.modules["main"].update_after_shutdown = True sys.modules["main"].file_server.stop() diff --git a/src/lib/PySocks/LICENSE b/src/lib/PySocks/LICENSE new file mode 100644 index 00000000..04b6b1f3 --- /dev/null +++ b/src/lib/PySocks/LICENSE @@ -0,0 +1,22 @@ +Copyright 2006 Dan-Haim. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. +3. Neither the name of Dan Haim nor the names of his contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY DAN HAIM "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL DAN HAIM OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMANGE. diff --git a/src/lib/PySocks/README.md b/src/lib/PySocks/README.md new file mode 100644 index 00000000..65419240 --- /dev/null +++ b/src/lib/PySocks/README.md @@ -0,0 +1,299 @@ +PySocks +======= + +Updated version of SocksiPy. Many old bugs fixed, and overall code cleanup. + +Acts as a drop-in replacement to the socket module. + +---------------- + +Features +======== + +* Fully supports Python 2.6 - 3.4 + +* UDP support + +* SocksiPyHandler, courtesy e000, was also added as an example of how this module can be used with urllib2. See example code in sockshandler.py. `pip install` and `setup.py install` will automatically install the `sockshandler` module. + +* Bugs in the original SocksiPy were fixed, including two that could lead to infinite hanging when communicating with bad proxy servers. + +* urllib3, which powers the requests module, is working on integrating SOCKS proxy support based on this branch + +* `SOCKS5`, `SOCKS4`, and `HTTP` are now aliases for `PROXY_TYPE_SOCKS5`, `PROXY_TYPE_SOCKS4`, and `PROXY_TYPE_HTTP` + +* Tests added + +* Various style and performance improvements; codebase simplified + +* Actively maintained + +Installation +============ + + pip install PySocks + +Or download the tarball / `git clone` and... + + python setup.py install + +These will install both the `socks` and `sockshandler` modules. + +Alternatively, include just `socks.py` in your project. + +-------------------------------------------- + +*Warning:* PySocks/SocksiPy only supports HTTP proxies that use CONNECT tunneling. Certain HTTP proxies may not work with this library. If you wish to use HTTP proxies (and not SOCKS proxies), it is recommended that you rely on your HTTP client's native proxy support (`proxies` dict for `requests`, or `urllib2.ProxyHandler` for `urllib2`) instead. + +-------------------------------------------- + +Usage +===== + +## Example ## + + import socks + + s = socks.socksocket() + + s.set_proxy(socks.SOCKS5, "localhost") # SOCKS4 and SOCKS5 use port 1080 by default + # Or + s.set_proxy(socks.SOCKS4, "localhost", 4444) + # Or + s.set_proxy(socks.HTTP, "5.5.5.5", 8888) + + # Can be treated identical to a regular socket object + s.connect(("www.test.com", 80)) + s.sendall("GET / ...") + print s.recv(4096) + + +To monkeypatch the entire standard library with a single default proxy: + + import socket + import socks + import urllib2 + + socks.set_default_proxy(socks.SOCKS5, "localhost") + socket.socket = socks.socksocket + + urllib2.urlopen("http://...") # All requests will pass through the SOCKS proxy + +Note that monkeypatching may not work for all standard modules or for all third party modules, and generally isn't recommended. + +-------------------------------------------- + +Original SocksiPy README attached below, amended to reflect API changes. + +-------------------------------------------- + +SocksiPy - version 1.5.0 + +A Python SOCKS module. + +(C) 2006 Dan-Haim. All rights reserved. + +See LICENSE file for details. + + +*WHAT IS A SOCKS PROXY?* + +A SOCKS proxy is a proxy server at the TCP level. In other words, it acts as +a tunnel, relaying all traffic going through it without modifying it. +SOCKS proxies can be used to relay traffic using any network protocol that +uses TCP. + +*WHAT IS SOCKSIPY?* + +This Python module allows you to create TCP connections through a SOCKS +proxy without any special effort. +It also supports relaying UDP packets with a SOCKS5 proxy. + +*PROXY COMPATIBILITY* + +SocksiPy is compatible with three different types of proxies: + +1. SOCKS Version 4 (SOCKS4), including the SOCKS4a extension. +2. SOCKS Version 5 (SOCKS5). +3. HTTP Proxies which support tunneling using the CONNECT method. + +*SYSTEM REQUIREMENTS* + +Being written in Python, SocksiPy can run on any platform that has a Python +interpreter and TCP/IP support. +This module has been tested with Python 2.3 and should work with greater versions +just as well. + + +INSTALLATION +------------- + +Simply copy the file "socks.py" to your Python's `lib/site-packages` directory, +and you're ready to go. [Editor's note: it is better to use `python setup.py install` for PySocks] + + +USAGE +------ + +First load the socks module with the command: + + >>> import socks + >>> + +The socks module provides a class called `socksocket`, which is the base to all of the module's functionality. + +The `socksocket` object has the same initialization parameters as the normal socket +object to ensure maximal compatibility, however it should be noted that `socksocket` will only function with family being `AF_INET` and +type being either `SOCK_STREAM` or `SOCK_DGRAM`. +Generally, it is best to initialize the `socksocket` object with no parameters + + >>> s = socks.socksocket() + >>> + +The `socksocket` object has an interface which is very similiar to socket's (in fact +the `socksocket` class is derived from socket) with a few extra methods. +To select the proxy server you would like to use, use the `set_proxy` method, whose +syntax is: + + set_proxy(proxy_type, addr[, port[, rdns[, username[, password]]]]) + +Explanation of the parameters: + +`proxy_type` - The type of the proxy server. This can be one of three possible +choices: `PROXY_TYPE_SOCKS4`, `PROXY_TYPE_SOCKS5` and `PROXY_TYPE_HTTP` for SOCKS4, +SOCKS5 and HTTP servers respectively. `SOCKS4`, `SOCKS5`, and `HTTP` are all aliases, respectively. + +`addr` - The IP address or DNS name of the proxy server. + +`port` - The port of the proxy server. Defaults to 1080 for socks and 8080 for http. + +`rdns` - This is a boolean flag than modifies the behavior regarding DNS resolving. +If it is set to True, DNS resolving will be preformed remotely, on the server. +If it is set to False, DNS resolving will be preformed locally. Please note that +setting this to True with SOCKS4 servers actually use an extension to the protocol, +called SOCKS4a, which may not be supported on all servers (SOCKS5 and http servers +always support DNS). The default is True. + +`username` - For SOCKS5 servers, this allows simple username / password authentication +with the server. For SOCKS4 servers, this parameter will be sent as the userid. +This parameter is ignored if an HTTP server is being used. If it is not provided, +authentication will not be used (servers may accept unauthenticated requests). + +`password` - This parameter is valid only for SOCKS5 servers and specifies the +respective password for the username provided. + +Example of usage: + + >>> s.set_proxy(socks.SOCKS5, "socks.example.com") # uses default port 1080 + >>> s.set_proxy(socks.SOCKS4, "socks.test.com", 1081) + +After the set_proxy method has been called, simply call the connect method with the +traditional parameters to establish a connection through the proxy: + + >>> s.connect(("www.sourceforge.net", 80)) + >>> + +Connection will take a bit longer to allow negotiation with the proxy server. +Please note that calling connect without calling `set_proxy` earlier will connect +without a proxy (just like a regular socket). + +Errors: Any errors in the connection process will trigger exceptions. The exception +may either be generated by the underlying socket layer or may be custom module +exceptions, whose details follow: + +class `ProxyError` - This is a base exception class. It is not raised directly but +rather all other exception classes raised by this module are derived from it. +This allows an easy way to catch all proxy-related errors. It descends from `IOError`. + +All `ProxyError` exceptions have an attribute `socket_err`, which will contain either a +caught `socket.error` exception, or `None` if there wasn't any. + +class `GeneralProxyError` - When thrown, it indicates a problem which does not fall +into another category. + +* `Sent invalid data` - This error means that unexpected data has been received from +the server. The most common reason is that the server specified as the proxy is +not really a SOCKS4/SOCKS5/HTTP proxy, or maybe the proxy type specified is wrong. + +* `Connection closed unexpectedly` - The proxy server unexpectedly closed the connection. +This may indicate that the proxy server is experiencing network or software problems. + +* `Bad proxy type` - This will be raised if the type of the proxy supplied to the +set_proxy function was not one of `SOCKS4`/`SOCKS5`/`HTTP`. + +* `Bad input` - This will be raised if the `connect()` method is called with bad input +parameters. + +class `SOCKS5AuthError` - This indicates that the connection through a SOCKS5 server +failed due to an authentication problem. + +* `Authentication is required` - This will happen if you use a SOCKS5 server which +requires authentication without providing a username / password at all. + +* `All offered authentication methods were rejected` - This will happen if the proxy +requires a special authentication method which is not supported by this module. + +* `Unknown username or invalid password` - Self descriptive. + +class `SOCKS5Error` - This will be raised for SOCKS5 errors which are not related to +authentication. +The parameter is a tuple containing a code, as given by the server, +and a description of the +error. The possible errors, according to the RFC, are: + +* `0x01` - General SOCKS server failure - If for any reason the proxy server is unable to +fulfill your request (internal server error). +* `0x02` - connection not allowed by ruleset - If the address you're trying to connect to +is blacklisted on the server or requires authentication. +* `0x03` - Network unreachable - The target could not be contacted. A router on the network +had replied with a destination net unreachable error. +* `0x04` - Host unreachable - The target could not be contacted. A router on the network +had replied with a destination host unreachable error. +* `0x05` - Connection refused - The target server has actively refused the connection +(the requested port is closed). +* `0x06` - TTL expired - The TTL value of the SYN packet from the proxy to the target server +has expired. This usually means that there are network problems causing the packet +to be caught in a router-to-router "ping-pong". +* `0x07` - Command not supported - For instance if the server does not support UDP. +* `0x08` - Address type not supported - The client has provided an invalid address type. +When using this module, this error should not occur. + +class `SOCKS4Error` - This will be raised for SOCKS4 errors. The parameter is a tuple +containing a code and a description of the error, as given by the server. The +possible error, according to the specification are: + +* `0x5B` - Request rejected or failed - Will be raised in the event of an failure for any +reason other then the two mentioned next. +* `0x5C` - request rejected because SOCKS server cannot connect to identd on the client - +The Socks server had tried an ident lookup on your computer and has failed. In this +case you should run an identd server and/or configure your firewall to allow incoming +connections to local port 113 from the remote server. +* `0x5D` - request rejected because the client program and identd report different user-ids - +The Socks server had performed an ident lookup on your computer and has received a +different userid than the one you have provided. Change your userid (through the +username parameter of the set_proxy method) to match and try again. + +class `HTTPError` - This will be raised for HTTP errors. The message will contain +the HTTP status code and provided error message. + +After establishing the connection, the object behaves like a standard socket. +Methods like `makefile()` and `settimeout()` should behave just like regular sockets. +Call the `close()` method to close the connection. + +In addition to the `socksocket` class, an additional function worth mentioning is the +`set_default_proxy` function. The parameters are the same as the `set_proxy` method. +This function will set default proxy settings for newly created `socksocket` objects, +in which the proxy settings haven't been changed via the `set_proxy` method. +This is quite useful if you wish to force 3rd party modules to use a SOCKS proxy, +by overriding the socket object. +For example: + + >>> socks.set_default_proxy(socks.SOCKS5, "socks.example.com") + >>> socket.socket = socks.socksocket + >>> urllib.urlopen("http://www.sourceforge.net/") + + +PROBLEMS +--------- + +Please open a GitHub issue at https://github.com/Anorov/PySocks diff --git a/src/lib/PySocks/__init__.py b/src/lib/PySocks/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/lib/PySocks/setup.py b/src/lib/PySocks/setup.py new file mode 100644 index 00000000..9db0f3d2 --- /dev/null +++ b/src/lib/PySocks/setup.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python +from distutils.core import setup + +VERSION = "1.5.3" + +setup( + name = "PySocks", + version = VERSION, + description = "A Python SOCKS client module. See https://github.com/Anorov/PySocks for more information.", + url = "https://github.com/Anorov/PySocks", + license = "BSD", + author = "Anorov", + author_email = "anorov.vorona@gmail.com", + keywords = ["socks", "proxy"], + py_modules=["socks", "sockshandler"] +) + diff --git a/src/lib/PySocks/socks.py b/src/lib/PySocks/socks.py new file mode 100644 index 00000000..ad1e9780 --- /dev/null +++ b/src/lib/PySocks/socks.py @@ -0,0 +1,699 @@ +""" +SocksiPy - Python SOCKS module. +Version 1.5.3 + +Copyright 2006 Dan-Haim. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. +3. Neither the name of Dan Haim nor the names of his contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY DAN HAIM "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL DAN HAIM OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMANGE. + + +This module provides a standard socket-like interface for Python +for tunneling connections through SOCKS proxies. + +=============================================================================== + +Minor modifications made by Christopher Gilbert (http://motomastyle.com/) +for use in PyLoris (http://pyloris.sourceforge.net/) + +Minor modifications made by Mario Vilas (http://breakingcode.wordpress.com/) +mainly to merge bug fixes found in Sourceforge + +Modifications made by Anorov (https://github.com/Anorov) +-Forked and renamed to PySocks +-Fixed issue with HTTP proxy failure checking (same bug that was in the old ___recvall() method) +-Included SocksiPyHandler (sockshandler.py), to be used as a urllib2 handler, + courtesy of e000 (https://github.com/e000): https://gist.github.com/869791#file_socksipyhandler.py +-Re-styled code to make it readable + -Aliased PROXY_TYPE_SOCKS5 -> SOCKS5 etc. + -Improved exception handling and output + -Removed irritating use of sequence indexes, replaced with tuple unpacked variables + -Fixed up Python 3 bytestring handling - chr(0x03).encode() -> b"\x03" + -Other general fixes +-Added clarification that the HTTP proxy connection method only supports CONNECT-style tunneling HTTP proxies +-Various small bug fixes +""" + +__version__ = "1.5.3" + +import socket +import struct +from errno import EOPNOTSUPP, EINVAL, EAGAIN +from io import BytesIO +from os import SEEK_CUR +from collections import Callable + +PROXY_TYPE_SOCKS4 = SOCKS4 = 1 +PROXY_TYPE_SOCKS5 = SOCKS5 = 2 +PROXY_TYPE_HTTP = HTTP = 3 + +PROXY_TYPES = {"SOCKS4": SOCKS4, "SOCKS5": SOCKS5, "HTTP": HTTP} +PRINTABLE_PROXY_TYPES = dict(zip(PROXY_TYPES.values(), PROXY_TYPES.keys())) + +_orgsocket = _orig_socket = socket.socket + +class ProxyError(IOError): + """ + socket_err contains original socket.error exception. + """ + def __init__(self, msg, socket_err=None): + self.msg = msg + self.socket_err = socket_err + + if socket_err: + self.msg += ": {0}".format(socket_err) + + def __str__(self): + return self.msg + +class GeneralProxyError(ProxyError): pass +class ProxyConnectionError(ProxyError): pass +class SOCKS5AuthError(ProxyError): pass +class SOCKS5Error(ProxyError): pass +class SOCKS4Error(ProxyError): pass +class HTTPError(ProxyError): pass + +SOCKS4_ERRORS = { 0x5B: "Request rejected or failed", + 0x5C: "Request rejected because SOCKS server cannot connect to identd on the client", + 0x5D: "Request rejected because the client program and identd report different user-ids" + } + +SOCKS5_ERRORS = { 0x01: "General SOCKS server failure", + 0x02: "Connection not allowed by ruleset", + 0x03: "Network unreachable", + 0x04: "Host unreachable", + 0x05: "Connection refused", + 0x06: "TTL expired", + 0x07: "Command not supported, or protocol error", + 0x08: "Address type not supported" + } + +DEFAULT_PORTS = { SOCKS4: 1080, + SOCKS5: 1080, + HTTP: 8080 + } + +def set_default_proxy(proxy_type=None, addr=None, port=None, rdns=True, username=None, password=None): + """ + set_default_proxy(proxy_type, addr[, port[, rdns[, username, password]]]) + + Sets a default proxy which all further socksocket objects will use, + unless explicitly changed. All parameters are as for socket.set_proxy(). + """ + socksocket.default_proxy = (proxy_type, addr, port, rdns, + username.encode() if username else None, + password.encode() if password else None) + +setdefaultproxy = set_default_proxy + +def get_default_proxy(): + """ + Returns the default proxy, set by set_default_proxy. + """ + return socksocket.default_proxy + +getdefaultproxy = get_default_proxy + +def wrap_module(module): + """ + Attempts to replace a module's socket library with a SOCKS socket. Must set + a default proxy using set_default_proxy(...) first. + This will only work on modules that import socket directly into the namespace; + most of the Python Standard Library falls into this category. + """ + if socksocket.default_proxy: + module.socket.socket = socksocket + else: + raise GeneralProxyError("No default proxy specified") + +wrapmodule = wrap_module + +def create_connection(dest_pair, proxy_type=None, proxy_addr=None, + proxy_port=None, proxy_username=None, + proxy_password=None, timeout=None, + source_address=None): + """create_connection(dest_pair, *[, timeout], **proxy_args) -> socket object + + Like socket.create_connection(), but connects to proxy + before returning the socket object. + + dest_pair - 2-tuple of (IP/hostname, port). + **proxy_args - Same args passed to socksocket.set_proxy() if present. + timeout - Optional socket timeout value, in seconds. + source_address - tuple (host, port) for the socket to bind to as its source + address before connecting (only for compatibility) + """ + sock = socksocket() + if isinstance(timeout, (int, float)): + sock.settimeout(timeout) + if proxy_type is not None: + sock.set_proxy(proxy_type, proxy_addr, proxy_port, + proxy_username, proxy_password) + sock.connect(dest_pair) + return sock + +class _BaseSocket(socket.socket): + """Allows Python 2's "delegated" methods such as send() to be overridden + """ + def __init__(self, *pos, **kw): + _orig_socket.__init__(self, *pos, **kw) + + self._savedmethods = dict() + for name in self._savenames: + self._savedmethods[name] = getattr(self, name) + delattr(self, name) # Allows normal overriding mechanism to work + + _savenames = list() + +def _makemethod(name): + return lambda self, *pos, **kw: self._savedmethods[name](*pos, **kw) +for name in ("sendto", "send", "recvfrom", "recv"): + method = getattr(_BaseSocket, name, None) + + # Determine if the method is not defined the usual way + # as a function in the class. + # Python 2 uses __slots__, so there are descriptors for each method, + # but they are not functions. + if not isinstance(method, Callable): + _BaseSocket._savenames.append(name) + setattr(_BaseSocket, name, _makemethod(name)) + +class socksocket(_BaseSocket): + """socksocket([family[, type[, proto]]]) -> socket object + + Open a SOCKS enabled socket. The parameters are the same as + those of the standard socket init. In order for SOCKS to work, + you must specify family=AF_INET and proto=0. + The "type" argument must be either SOCK_STREAM or SOCK_DGRAM. + """ + + default_proxy = None + + def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, _sock=None): + if type not in (socket.SOCK_STREAM, socket.SOCK_DGRAM): + msg = "Socket type must be stream or datagram, not {!r}" + raise ValueError(msg.format(type)) + + _BaseSocket.__init__(self, family, type, proto, _sock) + self._proxyconn = None # TCP connection to keep UDP relay alive + + if self.default_proxy: + self.proxy = self.default_proxy + else: + self.proxy = (None, None, None, None, None, None) + self.proxy_sockname = None + self.proxy_peername = None + + def _readall(self, file, count): + """ + Receive EXACTLY the number of bytes requested from the file object. + Blocks until the required number of bytes have been received. + """ + data = b"" + while len(data) < count: + d = file.read(count - len(data)) + if not d: + raise GeneralProxyError("Connection closed unexpectedly") + data += d + return data + + def set_proxy(self, proxy_type=None, addr=None, port=None, rdns=True, username=None, password=None): + """set_proxy(proxy_type, addr[, port[, rdns[, username[, password]]]]) + Sets the proxy to be used. + + proxy_type - The type of the proxy to be used. Three types + are supported: PROXY_TYPE_SOCKS4 (including socks4a), + PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP + addr - The address of the server (IP or DNS). + port - The port of the server. Defaults to 1080 for SOCKS + servers and 8080 for HTTP proxy servers. + rdns - Should DNS queries be performed on the remote side + (rather than the local side). The default is True. + Note: This has no effect with SOCKS4 servers. + username - Username to authenticate with to the server. + The default is no authentication. + password - Password to authenticate with to the server. + Only relevant when username is also provided. + """ + self.proxy = (proxy_type, addr, port, rdns, + username.encode() if username else None, + password.encode() if password else None) + + setproxy = set_proxy + + def bind(self, *pos, **kw): + """ + Implements proxy connection for UDP sockets, + which happens during the bind() phase. + """ + proxy_type, proxy_addr, proxy_port, rdns, username, password = self.proxy + if not proxy_type or self.type != socket.SOCK_DGRAM: + return _orig_socket.bind(self, *pos, **kw) + + if self._proxyconn: + raise socket.error(EINVAL, "Socket already bound to an address") + if proxy_type != SOCKS5: + msg = "UDP only supported by SOCKS5 proxy type" + raise socket.error(EOPNOTSUPP, msg) + _BaseSocket.bind(self, *pos, **kw) + + # Need to specify actual local port because + # some relays drop packets if a port of zero is specified. + # Avoid specifying host address in case of NAT though. + _, port = self.getsockname() + dst = ("0", port) + + self._proxyconn = _orig_socket() + proxy = self._proxy_addr() + self._proxyconn.connect(proxy) + + UDP_ASSOCIATE = b"\x03" + _, relay = self._SOCKS5_request(self._proxyconn, UDP_ASSOCIATE, dst) + + # The relay is most likely on the same host as the SOCKS proxy, + # but some proxies return a private IP address (10.x.y.z) + host, _ = proxy + _, port = relay + _BaseSocket.connect(self, (host, port)) + self.proxy_sockname = ("0.0.0.0", 0) # Unknown + + def sendto(self, bytes, *args, **kwargs): + if self.type != socket.SOCK_DGRAM: + return _BaseSocket.sendto(self, bytes, *args, **kwargs) + if not self._proxyconn: + self.bind(("", 0)) + + address = args[-1] + flags = args[:-1] + + header = BytesIO() + RSV = b"\x00\x00" + header.write(RSV) + STANDALONE = b"\x00" + header.write(STANDALONE) + self._write_SOCKS5_address(address, header) + + sent = _BaseSocket.send(self, header.getvalue() + bytes, *flags, **kwargs) + return sent - header.tell() + + def send(self, bytes, flags=0, **kwargs): + if self.type == socket.SOCK_DGRAM: + return self.sendto(bytes, flags, self.proxy_peername, **kwargs) + else: + return _BaseSocket.send(self, bytes, flags, **kwargs) + + def recvfrom(self, bufsize, flags=0): + if self.type != socket.SOCK_DGRAM: + return _BaseSocket.recvfrom(self, bufsize, flags) + if not self._proxyconn: + self.bind(("", 0)) + + buf = BytesIO(_BaseSocket.recv(self, bufsize, flags)) + buf.seek(+2, SEEK_CUR) + frag = buf.read(1) + if ord(frag): + raise NotImplementedError("Received UDP packet fragment") + fromhost, fromport = self._read_SOCKS5_address(buf) + + if self.proxy_peername: + peerhost, peerport = self.proxy_peername + if fromhost != peerhost or peerport not in (0, fromport): + raise socket.error(EAGAIN, "Packet filtered") + + return (buf.read(), (fromhost, fromport)) + + def recv(self, *pos, **kw): + bytes, _ = self.recvfrom(*pos, **kw) + return bytes + + def close(self): + if self._proxyconn: + self._proxyconn.close() + return _BaseSocket.close(self) + + def get_proxy_sockname(self): + """ + Returns the bound IP address and port number at the proxy. + """ + return self.proxy_sockname + + getproxysockname = get_proxy_sockname + + def get_proxy_peername(self): + """ + Returns the IP and port number of the proxy. + """ + return _BaseSocket.getpeername(self) + + getproxypeername = get_proxy_peername + + def get_peername(self): + """ + Returns the IP address and port number of the destination + machine (note: get_proxy_peername returns the proxy) + """ + return self.proxy_peername + + getpeername = get_peername + + def _negotiate_SOCKS5(self, *dest_addr): + """ + Negotiates a stream connection through a SOCKS5 server. + """ + CONNECT = b"\x01" + self.proxy_peername, self.proxy_sockname = self._SOCKS5_request(self, + CONNECT, dest_addr) + + def _SOCKS5_request(self, conn, cmd, dst): + """ + Send SOCKS5 request with given command (CMD field) and + address (DST field). Returns resolved DST address that was used. + """ + proxy_type, addr, port, rdns, username, password = self.proxy + + writer = conn.makefile("wb") + reader = conn.makefile("rb", 0) # buffering=0 renamed in Python 3 + try: + # First we'll send the authentication packages we support. + if username and password: + # The username/password details were supplied to the + # set_proxy method so we support the USERNAME/PASSWORD + # authentication (in addition to the standard none). + writer.write(b"\x05\x02\x00\x02") + else: + # No username/password were entered, therefore we + # only support connections with no authentication. + writer.write(b"\x05\x01\x00") + + # We'll receive the server's response to determine which + # method was selected + writer.flush() + chosen_auth = self._readall(reader, 2) + + if chosen_auth[0:1] != b"\x05": + # Note: string[i:i+1] is used because indexing of a bytestring + # via bytestring[i] yields an integer in Python 3 + raise GeneralProxyError("SOCKS5 proxy server sent invalid data") + + # Check the chosen authentication method + + if chosen_auth[1:2] == b"\x02": + # Okay, we need to perform a basic username/password + # authentication. + writer.write(b"\x01" + chr(len(username)).encode() + + username + + chr(len(password)).encode() + + password) + writer.flush() + auth_status = self._readall(reader, 2) + if auth_status[0:1] != b"\x01": + # Bad response + raise GeneralProxyError("SOCKS5 proxy server sent invalid data") + if auth_status[1:2] != b"\x00": + # Authentication failed + raise SOCKS5AuthError("SOCKS5 authentication failed") + + # Otherwise, authentication succeeded + + # No authentication is required if 0x00 + elif chosen_auth[1:2] != b"\x00": + # Reaching here is always bad + if chosen_auth[1:2] == b"\xFF": + raise SOCKS5AuthError("All offered SOCKS5 authentication methods were rejected") + else: + raise GeneralProxyError("SOCKS5 proxy server sent invalid data") + + # Now we can request the actual connection + writer.write(b"\x05" + cmd + b"\x00") + resolved = self._write_SOCKS5_address(dst, writer) + writer.flush() + + # Get the response + resp = self._readall(reader, 3) + if resp[0:1] != b"\x05": + raise GeneralProxyError("SOCKS5 proxy server sent invalid data") + + status = ord(resp[1:2]) + if status != 0x00: + # Connection failed: server returned an error + error = SOCKS5_ERRORS.get(status, "Unknown error") + raise SOCKS5Error("{0:#04x}: {1}".format(status, error)) + + # Get the bound address/port + bnd = self._read_SOCKS5_address(reader) + return (resolved, bnd) + finally: + reader.close() + writer.close() + + def _write_SOCKS5_address(self, addr, file): + """ + Return the host and port packed for the SOCKS5 protocol, + and the resolved address as a tuple object. + """ + host, port = addr + proxy_type, _, _, rdns, username, password = self.proxy + + # If the given destination address is an IP address, we'll + # use the IPv4 address request even if remote resolving was specified. + try: + addr_bytes = socket.inet_aton(host) + file.write(b"\x01" + addr_bytes) + host = socket.inet_ntoa(addr_bytes) + except socket.error: + # Well it's not an IP number, so it's probably a DNS name. + if rdns: + # Resolve remotely + host_bytes = host.encode('idna') + file.write(b"\x03" + chr(len(host_bytes)).encode() + host_bytes) + else: + # Resolve locally + addr_bytes = socket.inet_aton(socket.gethostbyname(host)) + file.write(b"\x01" + addr_bytes) + host = socket.inet_ntoa(addr_bytes) + + file.write(struct.pack(">H", port)) + return host, port + + def _read_SOCKS5_address(self, file): + atyp = self._readall(file, 1) + if atyp == b"\x01": + addr = socket.inet_ntoa(self._readall(file, 4)) + elif atyp == b"\x03": + length = self._readall(file, 1) + addr = self._readall(file, ord(length)) + else: + raise GeneralProxyError("SOCKS5 proxy server sent invalid data") + + port = struct.unpack(">H", self._readall(file, 2))[0] + return addr, port + + def _negotiate_SOCKS4(self, dest_addr, dest_port): + """ + Negotiates a connection through a SOCKS4 server. + """ + proxy_type, addr, port, rdns, username, password = self.proxy + + writer = self.makefile("wb") + reader = self.makefile("rb", 0) # buffering=0 renamed in Python 3 + try: + # Check if the destination address provided is an IP address + remote_resolve = False + try: + addr_bytes = socket.inet_aton(dest_addr) + except socket.error: + # It's a DNS name. Check where it should be resolved. + if rdns: + addr_bytes = b"\x00\x00\x00\x01" + remote_resolve = True + else: + addr_bytes = socket.inet_aton(socket.gethostbyname(dest_addr)) + + # Construct the request packet + writer.write(struct.pack(">BBH", 0x04, 0x01, dest_port)) + writer.write(addr_bytes) + + # The username parameter is considered userid for SOCKS4 + if username: + writer.write(username) + writer.write(b"\x00") + + # DNS name if remote resolving is required + # NOTE: This is actually an extension to the SOCKS4 protocol + # called SOCKS4A and may not be supported in all cases. + if remote_resolve: + writer.write(dest_addr.encode('idna') + b"\x00") + writer.flush() + + # Get the response from the server + resp = self._readall(reader, 8) + if resp[0:1] != b"\x00": + # Bad data + raise GeneralProxyError("SOCKS4 proxy server sent invalid data") + + status = ord(resp[1:2]) + if status != 0x5A: + # Connection failed: server returned an error + error = SOCKS4_ERRORS.get(status, "Unknown error") + raise SOCKS4Error("{0:#04x}: {1}".format(status, error)) + + # Get the bound address/port + self.proxy_sockname = (socket.inet_ntoa(resp[4:]), struct.unpack(">H", resp[2:4])[0]) + if remote_resolve: + self.proxy_peername = socket.inet_ntoa(addr_bytes), dest_port + else: + self.proxy_peername = dest_addr, dest_port + finally: + reader.close() + writer.close() + + def _negotiate_HTTP(self, dest_addr, dest_port): + """ + Negotiates a connection through an HTTP server. + NOTE: This currently only supports HTTP CONNECT-style proxies. + """ + proxy_type, addr, port, rdns, username, password = self.proxy + + # If we need to resolve locally, we do this now + addr = dest_addr if rdns else socket.gethostbyname(dest_addr) + + self.sendall(b"CONNECT " + addr.encode('idna') + b":" + str(dest_port).encode() + + b" HTTP/1.1\r\n" + b"Host: " + dest_addr.encode('idna') + b"\r\n\r\n") + + # We just need the first line to check if the connection was successful + fobj = self.makefile() + status_line = fobj.readline() + fobj.close() + + if not status_line: + raise GeneralProxyError("Connection closed unexpectedly") + + try: + proto, status_code, status_msg = status_line.split(" ", 2) + except ValueError: + raise GeneralProxyError("HTTP proxy server sent invalid response") + + if not proto.startswith("HTTP/"): + raise GeneralProxyError("Proxy server does not appear to be an HTTP proxy") + + try: + status_code = int(status_code) + except ValueError: + raise HTTPError("HTTP proxy server did not return a valid HTTP status") + + if status_code != 200: + error = "{0}: {1}".format(status_code, status_msg) + if status_code in (400, 403, 405): + # It's likely that the HTTP proxy server does not support the CONNECT tunneling method + error += ("\n[*] Note: The HTTP proxy server may not be supported by PySocks" + " (must be a CONNECT tunnel proxy)") + raise HTTPError(error) + + self.proxy_sockname = (b"0.0.0.0", 0) + self.proxy_peername = addr, dest_port + + _proxy_negotiators = { + SOCKS4: _negotiate_SOCKS4, + SOCKS5: _negotiate_SOCKS5, + HTTP: _negotiate_HTTP + } + + + def connect(self, dest_pair): + """ + Connects to the specified destination through a proxy. + Uses the same API as socket's connect(). + To select the proxy server, use set_proxy(). + + dest_pair - 2-tuple of (IP/hostname, port). + """ + dest_addr, dest_port = dest_pair + + if self.type == socket.SOCK_DGRAM: + if not self._proxyconn: + self.bind(("", 0)) + dest_addr = socket.gethostbyname(dest_addr) + + # If the host address is INADDR_ANY or similar, reset the peer + # address so that packets are received from any peer + if dest_addr == "0.0.0.0" and not dest_port: + self.proxy_peername = None + else: + self.proxy_peername = (dest_addr, dest_port) + return + + proxy_type, proxy_addr, proxy_port, rdns, username, password = self.proxy + + # Do a minimal input check first + if (not isinstance(dest_pair, (list, tuple)) + or len(dest_pair) != 2 + or not dest_addr + or not isinstance(dest_port, int)): + raise GeneralProxyError("Invalid destination-connection (host, port) pair") + + + if proxy_type is None: + # Treat like regular socket object + self.proxy_peername = dest_pair + _BaseSocket.connect(self, (dest_addr, dest_port)) + return + + proxy_addr = self._proxy_addr() + + try: + # Initial connection to proxy server + _BaseSocket.connect(self, proxy_addr) + + except socket.error as error: + # Error while connecting to proxy + self.close() + proxy_addr, proxy_port = proxy_addr + proxy_server = "{0}:{1}".format(proxy_addr, proxy_port) + printable_type = PRINTABLE_PROXY_TYPES[proxy_type] + + msg = "Error connecting to {0} proxy {1}".format(printable_type, + proxy_server) + raise ProxyConnectionError(msg, error) + + else: + # Connected to proxy server, now negotiate + try: + # Calls negotiate_{SOCKS4, SOCKS5, HTTP} + negotiate = self._proxy_negotiators[proxy_type] + negotiate(self, dest_addr, dest_port) + except socket.error as error: + # Wrap socket errors + self.close() + raise GeneralProxyError("Socket error", error) + except ProxyError: + # Protocol error while negotiating with proxy + self.close() + raise + + def _proxy_addr(self): + """ + Return proxy address to connect to as tuple object + """ + proxy_type, proxy_addr, proxy_port, rdns, username, password = self.proxy + proxy_port = proxy_port or DEFAULT_PORTS.get(proxy_type) + if not proxy_port: + raise GeneralProxyError("Invalid proxy type") + return proxy_addr, proxy_port diff --git a/src/lib/PySocks/sockshandler.py b/src/lib/PySocks/sockshandler.py new file mode 100644 index 00000000..26c83439 --- /dev/null +++ b/src/lib/PySocks/sockshandler.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +""" +SocksiPy + urllib2 handler + +version: 0.3 +author: e + +This module provides a Handler which you can use with urllib2 to allow it to tunnel your connection through a socks.sockssocket socket, with out monkey patching the original socket... +""" +import ssl + +try: + import urllib2 + import httplib +except ImportError: # Python 3 + import urllib.request as urllib2 + import http.client as httplib + +import socks # $ pip install PySocks + +def merge_dict(a, b): + d = a.copy() + d.update(b) + return d + +class SocksiPyConnection(httplib.HTTPConnection): + def __init__(self, proxytype, proxyaddr, proxyport=None, rdns=True, username=None, password=None, *args, **kwargs): + self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password) + httplib.HTTPConnection.__init__(self, *args, **kwargs) + + def connect(self): + self.sock = socks.socksocket() + self.sock.setproxy(*self.proxyargs) + if type(self.timeout) in (int, float): + self.sock.settimeout(self.timeout) + self.sock.connect((self.host, self.port)) + +class SocksiPyConnectionS(httplib.HTTPSConnection): + def __init__(self, proxytype, proxyaddr, proxyport=None, rdns=True, username=None, password=None, *args, **kwargs): + self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password) + httplib.HTTPSConnection.__init__(self, *args, **kwargs) + + def connect(self): + sock = socks.socksocket() + sock.setproxy(*self.proxyargs) + if type(self.timeout) in (int, float): + sock.settimeout(self.timeout) + sock.connect((self.host, self.port)) + self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file) + +class SocksiPyHandler(urllib2.HTTPHandler, urllib2.HTTPSHandler): + def __init__(self, *args, **kwargs): + self.args = args + self.kw = kwargs + urllib2.HTTPHandler.__init__(self) + + def http_open(self, req): + def build(host, port=None, timeout=0, **kwargs): + kw = merge_dict(self.kw, kwargs) + conn = SocksiPyConnection(*self.args, host=host, port=port, timeout=timeout, **kw) + return conn + return self.do_open(build, req) + + def https_open(self, req): + def build(host, port=None, timeout=0, **kwargs): + kw = merge_dict(self.kw, kwargs) + conn = SocksiPyConnectionS(*self.args, host=host, port=port, timeout=timeout, **kw) + return conn + return self.do_open(build, req) + +if __name__ == "__main__": + import sys + try: + port = int(sys.argv[1]) + except (ValueError, IndexError): + port = 9050 + opener = urllib2.build_opener(SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, "localhost", port)) + print("HTTP: " + opener.open("http://httpbin.org/ip").read().decode()) + print("HTTPS: " + opener.open("https://httpbin.org/ip").read().decode()) diff --git a/src/lib/PySocks/test/README b/src/lib/PySocks/test/README new file mode 100644 index 00000000..e08608ef --- /dev/null +++ b/src/lib/PySocks/test/README @@ -0,0 +1,5 @@ +Very rudimentary tests for Python 2 and Python 3. + +Requirements: tornado, twisted (available through pip) + +./test.sh diff --git a/src/lib/PySocks/test/httpproxy.py b/src/lib/PySocks/test/httpproxy.py new file mode 100644 index 00000000..df0ad031 --- /dev/null +++ b/src/lib/PySocks/test/httpproxy.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python +# +# Simple asynchronous HTTP proxy with tunnelling (CONNECT). +# +# GET/POST proxying based on +# http://groups.google.com/group/python-tornado/msg/7bea08e7a049cf26 +# +# Copyright (C) 2012 Senko Rasic +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +import sys +import socket + +import tornado.httpserver +import tornado.ioloop +import tornado.iostream +import tornado.web +import tornado.httpclient + +__all__ = ['ProxyHandler', 'run_proxy'] + + +class ProxyHandler(tornado.web.RequestHandler): + SUPPORTED_METHODS = ['GET', 'POST', 'CONNECT'] + + @tornado.web.asynchronous + def get(self): + + def handle_response(response): + if response.error and not isinstance(response.error, + tornado.httpclient.HTTPError): + self.set_status(500) + self.write('Internal server error:\n' + str(response.error)) + self.finish() + else: + self.set_status(response.code) + for header in ('Date', 'Cache-Control', 'Server', + 'Content-Type', 'Location'): + v = response.headers.get(header) + if v: + self.set_header(header, v) + if response.body: + self.write(response.body) + self.finish() + + req = tornado.httpclient.HTTPRequest(url=self.request.uri, + method=self.request.method, body=self.request.body, + headers=self.request.headers, follow_redirects=False, + allow_nonstandard_methods=True) + + client = tornado.httpclient.AsyncHTTPClient() + try: + client.fetch(req, handle_response) + except tornado.httpclient.HTTPError as e: + if hasattr(e, 'response') and e.response: + self.handle_response(e.response) + else: + self.set_status(500) + self.write('Internal server error:\n' + str(e)) + self.finish() + + @tornado.web.asynchronous + def post(self): + return self.get() + + @tornado.web.asynchronous + def connect(self): + host, port = self.request.uri.split(':') + client = self.request.connection.stream + + def read_from_client(data): + upstream.write(data) + + def read_from_upstream(data): + client.write(data) + + def client_close(data=None): + if upstream.closed(): + return + if data: + upstream.write(data) + upstream.close() + + def upstream_close(data=None): + if client.closed(): + return + if data: + client.write(data) + client.close() + + def start_tunnel(): + client.read_until_close(client_close, read_from_client) + upstream.read_until_close(upstream_close, read_from_upstream) + client.write(b'HTTP/1.0 200 Connection established\r\n\r\n') + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) + upstream = tornado.iostream.IOStream(s) + upstream.connect((host, int(port)), start_tunnel) + + +def run_proxy(port=8080, start_ioloop=True): + """ + Run proxy on the specified port. If start_ioloop is True (default), + the tornado IOLoop will be started immediately. + """ + app = tornado.web.Application([ + (r'.*', ProxyHandler), + ]) + app.listen(port, address="127.0.0.1") + ioloop = tornado.ioloop.IOLoop.instance() + if start_ioloop: + ioloop.start() + +if __name__ == '__main__': + port = 8081 + if len(sys.argv) > 1: + port = int(sys.argv[1]) + + print ("Running HTTP proxy server") + run_proxy(port) diff --git a/src/lib/PySocks/test/mocks b/src/lib/PySocks/test/mocks new file mode 100644 index 00000000..5299a3f4 Binary files /dev/null and b/src/lib/PySocks/test/mocks differ diff --git a/src/lib/PySocks/test/mocks.conf b/src/lib/PySocks/test/mocks.conf new file mode 100644 index 00000000..ab5ef590 --- /dev/null +++ b/src/lib/PySocks/test/mocks.conf @@ -0,0 +1,104 @@ +################################################# +# # +# Sample configuration file for MOCKS 0.0.2 # +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # +# # +# I recommend reading the examples in this file # +# and then extending it to suite your needs. # +# # +################################################# + + + +######################### +# +# General daemon config +# ~~~~~~~~~~~~~~~~~~~~~ +# +######################### + +PORT = 1081 # Port MOCKS is to listen to +MOCKS_ADDR = 127.0.0.1 # IP adress MOCKS is to bind to +LOG_FILE = mocks.log # MOCKS log file +PID_FILE = mocks.pid # File holding MOCKS's process ID +BUFFER_SIZE = 65536 # Traffic buffer size in bytes +BACKLOG = 5 # Backlog for listen() +NEGOTIATION_TIMEOUT = 5 +CONNECTION_IDLE_TIMEOUT = 300 +BIND_TIMEOUT = 30 +SHUTDOWN_TIMEOUT = 3 +MAX_CONNECTIONS = 50 + + + +########################################################################## +# +# Client filter config +# ~~~~~~~~~~~~~~~~~~~~ +# +# Client filtering means sorting out which clients are allowed +# connection and which are not. This is basically done like this: +# MOCKS has a default behaviour regarding filtering client +# connections. This behaviour is called the 'policy' and can either +# be to ALLOW or to DENY the connection. After setting the policy +# you can specify a list of exceptions. The action MOCKS takes +# for a client matching any of these exceptions is the opposite +# of the policy (that is, if the policy is set to ALLOW the exceptions +# are denied and if the policy is set to DENY the exceptions are allowed). +# An exception is specified in the form ip_address/mask, where mask +# is optional and is an integer ranging from 0 to 32 identifying the +# number of common heading bits that ip_address and the client's IP +# address must have in order to yield a match. If mask is missing, +# 32 will be assumed. For instance, 192.168.1.0/24 will match any IP +# ranging from 192.168.1.1 to 192.168.1.255. +# +# Let's take two examples, one for each type of policy. Let's say we +# only want to allow IPs 10.12.0.0 through 10.12.255.255, 172.23.2.5 and +# 192.168.52.26 to use MOCKS. What we have to to is this: +# +# FILTER_POLICY = DENY +# FILTER_EXCEPTION = 10.12.0.0/16 +# FILTER_EXCEPTION = 172.23.2.5 # implied /32 +# FILTER_EXCEPTION = 192.168.52.26 # implied /32 +# +# Now, let's say this is a public proxy server, but for some reason +# you don't want to let any IP ranging from 192.168.1.1 to 192.168.1.255 +# and neither 10.2.5.13 to connect to it: +# +# FILTER_POLICY = ALLOW +# FILTER_EXCEPTION = 192.168.1.0/24 +# FILTER_EXCEPTION = 10.2.5.13 +# +########################################################################### + +FILTER_POLICY = ALLOW + + + +############################################################################# +# +# Upstream proxy config +# ~~~~~~~~~~~~~~~~~~~~~ +# +# You can choose to further relay traffic through another proxy server. +# MOCKS supports upstream HTTP CONNECT, SOCKS4 and SOCKS5 proxies. You +# must specify the proxy type (one of HTTPCONNECT, SOCKS4 or SOCKS5), the +# proxy address and the proxy port. Optionally you can specify an user +# name and a password used to authenicate to the upstream proxy. This is +# pretty straight forward, so let's just take an example. Let's say you +# want to use the HTTP CONNECT server at httpconnectproxy.com, on port 3128, +# using the username 'foo' and the password 'bar'. You do it like this: +# +# UP_PROXY_TYPE = HTTPCONNECT +# UP_PROXY_ADDR = httpconnectproxy.com +# UP_PROXY_PORT = 3128 +# UP_PROXY_USER = foo # These two can be missing if you +# UP_PROXY_PASSWD = bar # are not required to authenticate +# +############################################################################# + +# UP_PROXY_TYPE = HTTPCONNECT +# UP_PROXY_ADDR = 192.168.1.12 +# UP_PROXY_PORT = 3128 + + diff --git a/src/lib/PySocks/test/socks4server.py b/src/lib/PySocks/test/socks4server.py new file mode 100644 index 00000000..05a54b93 --- /dev/null +++ b/src/lib/PySocks/test/socks4server.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +from twisted.internet import reactor +from twisted.protocols.socks import SOCKSv4Factory + +def run_proxy(): + reactor.listenTCP(1080, SOCKSv4Factory("/dev/null"), interface="127.0.0.1") + try: + reactor.run() + except (KeyboardInterrupt, SystemExit): + reactor.stop() + +if __name__ == "__main__": + print "Running SOCKS4 proxy server" + run_proxy() diff --git a/src/lib/PySocks/test/sockstest.py b/src/lib/PySocks/test/sockstest.py new file mode 100644 index 00000000..526cb3fc --- /dev/null +++ b/src/lib/PySocks/test/sockstest.py @@ -0,0 +1,174 @@ +import sys +sys.path.append("..") +import socks +import socket + +PY3K = sys.version_info[0] == 3 + +if PY3K: + import urllib.request as urllib2 +else: + import sockshandler + import urllib2 + +def raw_HTTP_request(): + req = "GET /ip HTTP/1.1\r\n" + req += "Host: ifconfig.me\r\n" + req += "User-Agent: Mozilla\r\n" + req += "Accept: text/html\r\n" + req += "\r\n" + return req.encode() + +def socket_HTTP_test(): + s = socks.socksocket() + s.set_proxy(socks.HTTP, "127.0.0.1", 8081) + s.connect(("ifconfig.me", 80)) + s.sendall(raw_HTTP_request()) + status = s.recv(2048).splitlines()[0] + assert status.startswith(b"HTTP/1.1 200") + +def socket_SOCKS4_test(): + s = socks.socksocket() + s.set_proxy(socks.SOCKS4, "127.0.0.1", 1080) + s.connect(("ifconfig.me", 80)) + s.sendall(raw_HTTP_request()) + status = s.recv(2048).splitlines()[0] + assert status.startswith(b"HTTP/1.1 200") + +def socket_SOCKS5_test(): + s = socks.socksocket() + s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081) + s.connect(("ifconfig.me", 80)) + s.sendall(raw_HTTP_request()) + status = s.recv(2048).splitlines()[0] + assert status.startswith(b"HTTP/1.1 200") + +def SOCKS5_connect_timeout_test(): + s = socks.socksocket() + s.settimeout(0.0001) + s.set_proxy(socks.SOCKS5, "8.8.8.8", 80) + try: + s.connect(("ifconfig.me", 80)) + except socks.ProxyConnectionError as e: + assert str(e.socket_err) == "timed out" + +def SOCKS5_timeout_test(): + s = socks.socksocket() + s.settimeout(0.0001) + s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081) + try: + s.connect(("ifconfig.me", 4444)) + except socks.GeneralProxyError as e: + assert str(e.socket_err) == "timed out" + + +def socket_SOCKS5_auth_test(): + # TODO: add support for this test. Will need a better SOCKS5 server. + s = socks.socksocket() + s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081, username="a", password="b") + s.connect(("ifconfig.me", 80)) + s.sendall(raw_HTTP_request()) + status = s.recv(2048).splitlines()[0] + assert status.startswith(b"HTTP/1.1 200") + +def socket_HTTP_IP_test(): + s = socks.socksocket() + s.set_proxy(socks.HTTP, "127.0.0.1", 8081) + s.connect(("133.242.129.236", 80)) + s.sendall(raw_HTTP_request()) + status = s.recv(2048).splitlines()[0] + assert status.startswith(b"HTTP/1.1 200") + +def socket_SOCKS4_IP_test(): + s = socks.socksocket() + s.set_proxy(socks.SOCKS4, "127.0.0.1", 1080) + s.connect(("133.242.129.236", 80)) + s.sendall(raw_HTTP_request()) + status = s.recv(2048).splitlines()[0] + assert status.startswith(b"HTTP/1.1 200") + +def socket_SOCKS5_IP_test(): + s = socks.socksocket() + s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081) + s.connect(("133.242.129.236", 80)) + s.sendall(raw_HTTP_request()) + status = s.recv(2048).splitlines()[0] + assert status.startswith(b"HTTP/1.1 200") + +def urllib2_HTTP_test(): + socks.set_default_proxy(socks.HTTP, "127.0.0.1", 8081) + socks.wrap_module(urllib2) + status = urllib2.urlopen("http://ifconfig.me/ip").getcode() + assert status == 200 + +def urllib2_SOCKS5_test(): + socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 1081) + socks.wrap_module(urllib2) + status = urllib2.urlopen("http://ifconfig.me/ip").getcode() + assert status == 200 + +def urllib2_handler_HTTP_test(): + opener = urllib2.build_opener(sockshandler.SocksiPyHandler(socks.HTTP, "127.0.0.1", 8081)) + status = opener.open("http://ifconfig.me/ip").getcode() + assert status == 200 + +def urllib2_handler_SOCKS5_test(): + opener = urllib2.build_opener(sockshandler.SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 1081)) + status = opener.open("http://ifconfig.me/ip").getcode() + assert status == 200 + +def global_override_HTTP_test(): + socks.set_default_proxy(socks.HTTP, "127.0.0.1", 8081) + good = socket.socket + socket.socket = socks.socksocket + status = urllib2.urlopen("http://ifconfig.me/ip").getcode() + socket.socket = good + assert status == 200 + +def global_override_SOCKS5_test(): + default_proxy = (socks.SOCKS5, "127.0.0.1", 1081) + socks.set_default_proxy(*default_proxy) + good = socket.socket + socket.socket = socks.socksocket + status = urllib2.urlopen("http://ifconfig.me/ip").getcode() + socket.socket = good + assert status == 200 + assert socks.get_default_proxy()[1].decode() == default_proxy[1] + + +def main(): + print("Running tests...") + socket_HTTP_test() + print("1/12") + socket_SOCKS4_test() + print("2/12") + socket_SOCKS5_test() + print("3/12") + if not PY3K: + urllib2_handler_HTTP_test() + print("3.33/12") + urllib2_handler_SOCKS5_test() + print("3.66/12") + socket_HTTP_IP_test() + print("4/12") + socket_SOCKS4_IP_test() + print("5/12") + socket_SOCKS5_IP_test() + print("6/12") + SOCKS5_connect_timeout_test() + print("7/12") + SOCKS5_timeout_test() + print("8/12") + urllib2_HTTP_test() + print("9/12") + urllib2_SOCKS5_test() + print("10/12") + global_override_HTTP_test() + print("11/12") + global_override_SOCKS5_test() + print("12/12") + print("All tests ran successfully") + + +if __name__ == "__main__": + main() diff --git a/src/lib/PySocks/test/test.sh b/src/lib/PySocks/test/test.sh new file mode 100644 index 00000000..18479b9f --- /dev/null +++ b/src/lib/PySocks/test/test.sh @@ -0,0 +1,25 @@ +#!/bin/bash +shopt -s expand_aliases +type python2 >/dev/null 2>&1 || alias python2='python' + +echo "Starting proxy servers..." +python2 socks4server.py > /dev/null & +python2 httpproxy.py > /dev/null & +./mocks start + +sleep 2 +echo "Python 2.6 tests" +python2.6 sockstest.py +exit + +sleep 2 +echo "Python 2.7 tests" +python2.7 sockstest.py + +sleep 2 +echo "Python 3.x tests" +python3 sockstest.py + +pkill python2 > /dev/null +./mocks shutdown +echo "Finished tests" diff --git a/src/lib/bencode/__init__.py b/src/lib/bencode/__init__.py new file mode 100644 index 00000000..c484c651 --- /dev/null +++ b/src/lib/bencode/__init__.py @@ -0,0 +1,7 @@ +try: + string_type = basestring +except NameError: + string_type = str + +from .encode import encode +from .decode import decode diff --git a/src/lib/bencode/decode.py b/src/lib/bencode/decode.py new file mode 100644 index 00000000..65362490 --- /dev/null +++ b/src/lib/bencode/decode.py @@ -0,0 +1,141 @@ +import itertools +import collections + +from . import string_type + +try: + range = xrange +except NameError: + pass + +def decode(data): + ''' + Bdecodes data into Python built-in types. + ''' + + return consume(LookaheadIterator(data)) + +class LookaheadIterator(collections.Iterator): + ''' + An iterator that lets you peek at the next item. + ''' + + def __init__(self, iterator): + self.iterator, self.next_iterator = itertools.tee(iter(iterator)) + + # Be one step ahead + self._advance() + + def _advance(self): + self.next_item = next(self.next_iterator, None) + + def __next__(self): + self._advance() + + return next(self.iterator) + + # Python 2 compatibility + next = __next__ + +def consume(stream): + item = stream.next_item + + if item is None: + raise ValueError('Encoding empty data is undefined') + elif item == 'i': + return consume_int(stream) + elif item == 'l': + return consume_list(stream) + elif item == 'd': + return consume_dict(stream) + elif item is not None and item[0].isdigit(): + return consume_str(stream) + else: + raise ValueError('Invalid bencode object type: ', item) + +def consume_number(stream): + result = '' + + while True: + chunk = stream.next_item + + if not chunk.isdigit(): + return result + elif result.startswith('0'): + raise ValueError('Invalid number') + + next(stream) + result += chunk + +def consume_int(stream): + if next(stream) != 'i': + raise ValueError() + + negative = stream.next_item == '-' + + if negative: + next(stream) + + result = int(consume_number(stream)) + + if negative: + result *= -1 + + if result == 0: + raise ValueError('Negative zero is not allowed') + + if next(stream) != 'e': + raise ValueError('Unterminated integer') + + return result + +def consume_str(stream): + length = int(consume_number(stream)) + + if next(stream) != ':': + raise ValueError('Malformed string') + + result = '' + + for i in range(length): + try: + result += next(stream) + except StopIteration: + raise ValueError('Invalid string length') + + return result + +def consume_list(stream): + if next(stream) != 'l': + raise ValueError() + + l = [] + + while stream.next_item != 'e': + l.append(consume(stream)) + + if next(stream) != 'e': + raise ValueError('Unterminated list') + + return l + +def consume_dict(stream): + if next(stream) != 'd': + raise ValueError() + + d = {} + + while stream.next_item != 'e': + key = consume(stream) + + if not isinstance(key, string_type): + raise ValueError('Dictionary keys must be strings') + + value = consume(stream) + + d[key] = value + + if next(stream) != 'e': + raise ValueError('Unterminated dictionary') + + return d diff --git a/src/lib/bencode/encode.py b/src/lib/bencode/encode.py new file mode 100644 index 00000000..be98f994 --- /dev/null +++ b/src/lib/bencode/encode.py @@ -0,0 +1,22 @@ +from . import string_type + +def encode(obj): + ''' + Bencodes the object. The object must be an instance of: str, int, list, or dict. + ''' + + if isinstance(obj, string_type): + return '{0}:{1}'.format(len(obj), obj) + elif isinstance(obj, int): + return 'i{0}e'.format(obj) + elif isinstance(obj, list): + values = ''.join([encode(o) for o in obj]) + + return 'l{0}e'.format(values) + elif isinstance(obj, dict): + items = sorted(obj.items()) + values = ''.join([encode(str(key)) + encode(value) for key, value in items]) + + return 'd{0}e'.format(values) + else: + raise TypeError('Unsupported type: {0}. Must be one of: str, int, list, dict.'.format(type(obj))) diff --git a/src/main.py b/src/main.py index 0acfbfb5..e68548b7 100644 --- a/src/main.py +++ b/src/main.py @@ -10,7 +10,6 @@ if not os.path.isfile("data/users.json"): open("data/users.json", "w").write("{} # Load config from Config import config - # Setup logging import logging if config.action == "main": @@ -44,11 +43,20 @@ else: import gevent import time - # Log current config logging.debug("Config: %s" % config) +# Socks Proxy monkey patch +if config.proxy: + from util import SocksProxy + import urllib2 + logging.info("Patching sockets to socks proxy: %s" % config.proxy) + config.disable_zeromq = True # ZeroMQ doesnt support proxy + config.fileserver_ip = '127.0.0.1' # Do not accept connections anywhere but localhost + SocksProxy.monkeyPath(*config.proxy.split(":")) + + # Load plugins from Plugin import PluginManager PluginManager.plugin_manager.loadPlugins() @@ -199,7 +207,7 @@ def sitePublish(address, peer_ip=None, peer_port=15441, inner_path="content.json logging.info("Gathering peers from tracker") site.announce() # Gather peers site.publish(20, inner_path) # Push to 20 peers - time.sleep(1) + time.sleep(3) logging.info("Serving files...") gevent.joinall([file_server_thread]) logging.info("Done.") @@ -226,7 +234,7 @@ def peerPing(peer_ip, peer_port): file_server = ConnectionServer("127.0.0.1", 1234) from Peer import Peer - logging.info("Pinging 5 times peer: %s:%s..." % (peer_ip, peer_port)) + logging.info("Pinging 5 times peer: %s:%s..." % (peer_ip, int(peer_port))) peer = Peer(peer_ip, peer_port) for i in range(5): s = time.time() diff --git a/src/util/SocksProxy.py b/src/util/SocksProxy.py new file mode 100644 index 00000000..d8b0447d --- /dev/null +++ b/src/util/SocksProxy.py @@ -0,0 +1,22 @@ +from lib.PySocks import socks +import socket + + +def create_connection(address, timeout=None, source_address=None): + sock = socks.socksocket() + sock.connect(address) + return sock + + +# Dns queries using the proxy +def getaddrinfo(*args): + return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))] + + +def monkeyPath(proxy_ip, proxy_port): + print proxy_ip, proxy_port + socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy_ip, int(proxy_port)) + socket.socket = socks.socksocket + socket.create_connection = create_connection + socket.getaddrinfo = getaddrinfo + diff --git a/src/util/UpnpPunch.py b/src/util/UpnpPunch.py index 2f86b689..a514659d 100644 --- a/src/util/UpnpPunch.py +++ b/src/util/UpnpPunch.py @@ -1,8 +1,5 @@ import gevent from gevent import socket -from gevent import monkey - -monkey.patch_socket() import re, urllib2, httplib, logging from urlparse import urlparse @@ -231,5 +228,8 @@ def open_port(port=15441, desc="UpnpPunch"): return False if __name__ == "__main__": + from gevent import monkey + monkey.patch_socket() + logging.getLogger().setLevel(logging.DEBUG) print open_port(15441, "ZeroNet")
id protocol type ip open ping buffidle open delay sent received last sent waiting version peerid