Commit 9c35952c authored by Ondřej Nový's avatar Ondřej Nový

Import python-socksipy_1.5.6.orig.tar.gz

parent 68697041
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.
Metadata-Version: 1.0
Name: PySocks
Version: 1.5.6
Summary: A Python SOCKS client module. See https://github.com/Anorov/PySocks for more information.
Home-page: https://github.com/Anorov/PySocks
Author: Anorov
Author-email: anorov.vorona@gmail.com
License: BSD
Description: UNKNOWN
Keywords: socks,proxy
Platform: UNKNOWN
This diff is collapsed.
#!/usr/bin/env python
from distutils.core import setup
VERSION = "1.5.0"
VERSION = "1.5.6"
setup(
name = "PySocks",
version = VERSION,
description = "A Python SOCKS module",
description = "A Python SOCKS client module. See https://github.com/Anorov/PySocks for more information.",
url = "https://github.com/Anorov/PySocks",
download_url = "https://github.com/Anorov/PySocks/tarball/1.5.0",
license = "BSD",
author = "Anorov",
author_email = "anorov.vorona@gmail.com",
keywords = ["socks", "proxy"],
py_modules=["socks"]
py_modules=["socks", "sockshandler"]
)
This diff is collapsed.
#!/usr/bin/env python
"""
SocksiPy + urllib2 handler
version: 0.2
version: 0.3
author: e<e@tr0ll.in>
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 urllib2
import httplib
import socks
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):
......@@ -22,18 +35,45 @@ class SocksiPyConnection(httplib.HTTPConnection):
self.sock.settimeout(self.timeout)
self.sock.connect((self.host, self.port))
class SocksiPyHandler(urllib2.HTTPHandler):
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, strict=None, timeout=0):
conn = SocksiPyConnection(*self.args, host=host, port=port, strict=strict, timeout=timeout, **self.kw)
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__":
opener = urllib2.build_opener(SocksiPyHandler(socks.PROXY_TYPE_SOCKS4, "localhost", 9050))
print opener.open("http://whatismyip.org/").read()
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())
Very rudimentary tests for Python 2 and Python 3.
Requirements: tornado, twisted (available through pip)
./test.sh
#!/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 <senko.rasic@dobarkod.hr>
#
# 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)
#################################################
# #
# 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
#!/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()
import sys
sys.path.append("..")
import socks
import socket
PY3K = sys.version_info.major == 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()
#!/bin/bash
echo "Starting proxy servers..."
python socks4server.py > /dev/null 2>&1 &
python httpproxy.py > /dev/null 2>&1 &
./mocks start >/dev/null 2>&1 &
sleep 2
echo "Python 2.x tests"
python sockstest.py
sleep 2
echo "Python 3.x tests"
python3 sockstest.py
pkill python > /dev/null 2>&1
./mocks shutdown >/dev/null 2>&1 &
echo "Finished tests"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment