# pyChainedProxy #
This is a modified version of socksipy module, which supports chaining of
proxy servers and various modes of TLS/SSL encryption.
https://pypi.org/project/pyChainedProxy/
Chaining of Proxy
===========================
Client <--> Proxy(1) <--> Proxy(2) <--> ... <--> Proxy(n) <--> Web Server
Proxies can be Http, Socks4, Socks5, etc.
## Install ##
```
pip install pyChainedProxy
```
## Usage ##
```
import socket
import pyChainedProxy as socks #import pyChainedProxy
# Enable debugging
def DEBUG(msg):
print (msg)
socks.DEBUG = DEBUG
print ("Check IP w/o proxyfying: ",urlfetch.get('http://ip-api.com/json').content)
# Configure a default chain
chain = [
'socks5://localhost:9050/', # First hop is Tor,
'http://user1:pass@example.com/' # ...and then auth to an HTTP proxy
]
socks.setdefaultproxy() # Clear the default chain
#adding hops with proxies
for hop in chain:
socks.adddefaultproxy(*socks.parseproxy(hop))
#wrap a single module
#socks.wrapmodule(urlfetch)
# Configure alternate routes (No proxy for localhost)
socks.setproxy('localhost', socks.PROXY_TYPE_NONE)
socks.setproxy('127.0.0.1', socks.PROXY_TYPE_NONE)
# This would have set proxies using the standard environment variables:
#socks.usesystemdefaults()
# Monkey Patching whole socket class (everything will be proxified)
rawsocket = socket.socket
socket.socket = socks.socksocket
# Everything will be proxied!
print ("Check IP After proxyfying: ",urlfetch.get('http://ip-api.com/json').content)
```
## Example ##
You can edit and use `test.py` according to your config.
-------------------------------------------------------------------------------
# Original README #
<pre>
SocksiPy version 1.00
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.
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.
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 SOCK_STREAM.
Generally, it is best to initialize the socksocket object with no parameters
>>> s = socks.socksocket()
>>>
The socksocket object has an interface which is very similar 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 setproxy method, whose
syntax is:
setproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
Explaination of the parameters:
proxytype - 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.
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 unauthentication requests).
password - This parameter is valid only for Socks5 servers and specifies the
respective password for the username provided.
Example of usage:
>>> s.setproxy(socks.PROXY_TYPE_SOCKS5,"socks.example.com")
>>>
After the setproxy 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 setproxy 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.
class GeneralProxyError - When thrown, it indicates a problem which does not fall
into another category. The parameter is a tuple containing an error code and a
description of the error, from the following list:
1 - 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.
4 - bad proxy type - This will be raised if the type of the proxy supplied to the
setproxy function was not PROXY_TYPE_SOCKS4/PROXY_TYPE_SOCKS5/PROXY_TYPE_HTTP.
5 - 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. The parameter is a tuple containing a
code and a description message according to the following list:
1 - authentication is required - This will happen if you use a Socks5 server which
requires authentication without providing a username / password at all.
2 - all offered authentication methods were rejected - This will happen if the proxy
requires a special authentication method which is not supported by this module.
3 - 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 and a description of the
error, as given by the server. The possible errors, according to the RFC are:
1 - General SOCKS server failure - If for any reason the proxy server is unable to
fulfill your request (internal server error).
2 - connection not allowed by ruleset - If the address you're trying to connect to
is blacklisted on the server or requires authentication.
3 - Network unreachable - The target could not be contacted. A router on the network
had replied with a destination net unreachable error.
4 - Host unreachable - The target could not be contacted. A router on the network
had replied with a destination host unreachable error.
5 - Connection refused - The target server has actively refused the connection
(the requested port is closed).
6 - 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".
7 - Command not supported - The client has issued an invalid command. When using this
module, this error should not occur.
8 - 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:
1 - Request rejected or failed - Will be raised in the event of an failure for any
reason other then the two mentioned next.
2 - 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.
3 - 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 setproxy method) to match and try again.
class HTTPError - This will be raised for HTTP errors. The parameter is a tuple
containing the HTTP status code and the description of the server.
After establishing the connection, the object behaves like a standard socket.
Call the close method to close the connection.
In addition to the socksocket class, an additional function worth mentioning is the
setdefaultproxy function. The parameters are the same as the setproxy method.
This function will set default proxy settings for newly created socksocket objects,
in which the proxy settings haven't been changed via the setproxy 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.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,"socks.example.com")
>>> socket.socket = socks.socksocket
>>> urllib.urlopen("http://www.sourceforge.net/")
</pre>
Raw data
{
"_id": null,
"home_page": "https://github.com/nighthawkk/pyChainedProxy",
"name": "pyChainedProxy",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Aman Kumar",
"author_email": "akdapunk@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/77/62/dc2e3b0be14605c602c7c4a5859286ac2ecdfef0fd73abcc24bff0bcd382/pychainedproxy-1.3.tar.gz",
"platform": null,
"description": "# pyChainedProxy #\r\n\r\nThis is a modified version of socksipy module, which supports chaining of\r\nproxy servers and various modes of TLS/SSL encryption.\r\n\r\nhttps://pypi.org/project/pyChainedProxy/\r\n\r\nChaining of Proxy \r\n===========================\r\nClient <--> Proxy(1) <--> Proxy(2) <--> ... <--> Proxy(n) <--> Web Server\r\n\r\nProxies can be Http, Socks4, Socks5, etc. \r\n\r\n## Install ##\r\n\r\n```\r\npip install pyChainedProxy\r\n```\r\n## Usage ##\r\n\r\n```\r\nimport socket\r\nimport pyChainedProxy as socks #import pyChainedProxy\r\n\r\n# Enable debugging\r\ndef DEBUG(msg):\r\n print (msg)\r\n\r\nsocks.DEBUG = DEBUG\r\n\r\n\r\nprint (\"Check IP w/o proxyfying: \",urlfetch.get('http://ip-api.com/json').content) \r\n\r\n# Configure a default chain\r\nchain = [\r\n 'socks5://localhost:9050/', # First hop is Tor,\r\n 'http://user1:pass@example.com/' # ...and then auth to an HTTP proxy\r\n]\r\nsocks.setdefaultproxy() # Clear the default chain\r\n#adding hops with proxies\r\nfor hop in chain:\r\n socks.adddefaultproxy(*socks.parseproxy(hop))\r\n\r\n#wrap a single module \r\n#socks.wrapmodule(urlfetch) \r\n\r\n# Configure alternate routes (No proxy for localhost)\r\nsocks.setproxy('localhost', socks.PROXY_TYPE_NONE)\r\nsocks.setproxy('127.0.0.1', socks.PROXY_TYPE_NONE)\r\n\r\n# This would have set proxies using the standard environment variables:\r\n#socks.usesystemdefaults()\r\n\r\n\r\n# Monkey Patching whole socket class (everything will be proxified)\r\nrawsocket = socket.socket\r\nsocket.socket = socks.socksocket\r\n# Everything will be proxied!\r\n\r\nprint (\"Check IP After proxyfying: \",urlfetch.get('http://ip-api.com/json').content)\r\n```\r\n## Example ##\r\nYou can edit and use `test.py` according to your config.\r\n\r\n-------------------------------------------------------------------------------\r\n# Original README #\r\n\r\n<pre>\r\nSocksiPy version 1.00\r\nA Python SOCKS module.\r\n(C) 2006 Dan-Haim. All rights reserved.\r\nSee LICENSE file for details.\r\n\r\n\r\nWHAT IS A SOCKS PROXY?\r\nA SOCKS proxy is a proxy server at the TCP level. In other words, it acts as\r\na tunnel, relaying all traffic going through it without modifying it.\r\nSOCKS proxies can be used to relay traffic using any network protocol that\r\nuses TCP.\r\n\r\nWHAT IS SOCKSIPY?\r\nThis Python module allows you to create TCP connections through a SOCKS\r\nproxy without any special effort.\r\n\r\nPROXY COMPATIBILITY\r\nSocksiPy is compatible with three different types of proxies:\r\n1. SOCKS Version 4 (Socks4), including the Socks4a extension.\r\n2. SOCKS Version 5 (Socks5).\r\n3. HTTP Proxies which support tunneling using the CONNECT method.\r\n\r\nSYSTEM REQUIREMENTS\r\nBeing written in Python, SocksiPy can run on any platform that has a Python\r\ninterpreter and TCP/IP support.\r\nThis module has been tested with Python 2.3 and should work with greater versions\r\njust as well.\r\n\r\n\r\nINSTALLATION\r\n-------------\r\n\r\nSimply copy the file \"socks.py\" to your Python's lib/site-packages directory,\r\nand you're ready to go.\r\n\r\n\r\nUSAGE\r\n------\r\n\r\nFirst load the socks module with the command:\r\n\r\n>>> import socks\r\n>>>\r\n\r\nThe socks module provides a class called \"socksocket\", which is the base to\r\nall of the module's functionality.\r\nThe socksocket object has the same initialization parameters as the normal socket\r\nobject to ensure maximal compatibility, however it should be noted that socksocket\r\nwill only function with family being AF_INET and type being SOCK_STREAM.\r\nGenerally, it is best to initialize the socksocket object with no parameters\r\n\r\n>>> s = socks.socksocket()\r\n>>>\r\n\r\nThe socksocket object has an interface which is very similar to socket's (in fact\r\nthe socksocket class is derived from socket) with a few extra methods.\r\nTo select the proxy server you would like to use, use the setproxy method, whose\r\nsyntax is:\r\n\r\nsetproxy(proxytype, addr[, port[, rdns[, username[, password]]]])\r\n\r\nExplaination of the parameters:\r\n\r\nproxytype - The type of the proxy server. This can be one of three possible\r\nchoices: PROXY_TYPE_SOCKS4, PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP for Socks4,\r\nSocks5 and HTTP servers respectively.\r\n\r\naddr - The IP address or DNS name of the proxy server.\r\n\r\nport - The port of the proxy server. Defaults to 1080 for socks and 8080 for http.\r\n\r\nrdns - This is a boolean flag than modifies the behavior regarding DNS resolving.\r\nIf it is set to True, DNS resolving will be preformed remotely, on the server.\r\nIf it is set to False, DNS resolving will be preformed locally. Please note that\r\nsetting this to True with Socks4 servers actually use an extension to the protocol,\r\ncalled Socks4a, which may not be supported on all servers (Socks5 and http servers\r\nalways support DNS). The default is True.\r\n\r\nusername - For Socks5 servers, this allows simple username / password authentication\r\nwith the server. For Socks4 servers, this parameter will be sent as the userid.\r\nThis parameter is ignored if an HTTP server is being used. If it is not provided,\r\nauthentication will not be used (servers may accept unauthentication requests).\r\n\r\npassword - This parameter is valid only for Socks5 servers and specifies the\r\nrespective password for the username provided.\r\n\r\nExample of usage:\r\n\r\n>>> s.setproxy(socks.PROXY_TYPE_SOCKS5,\"socks.example.com\")\r\n>>>\r\n\r\nAfter the setproxy method has been called, simply call the connect method with the\r\ntraditional parameters to establish a connection through the proxy:\r\n\r\n>>> s.connect((\"www.sourceforge.net\",80))\r\n>>>\r\n\r\nConnection will take a bit longer to allow negotiation with the proxy server.\r\nPlease note that calling connect without calling setproxy earlier will connect\r\nwithout a proxy (just like a regular socket).\r\n\r\nErrors: Any errors in the connection process will trigger exceptions. The exception\r\nmay either be generated by the underlying socket layer or may be custom module\r\nexceptions, whose details follow:\r\n\r\nclass ProxyError - This is a base exception class. It is not raised directly but\r\nrather all other exception classes raised by this module are derived from it.\r\nThis allows an easy way to catch all proxy-related errors.\r\n\r\nclass GeneralProxyError - When thrown, it indicates a problem which does not fall\r\ninto another category. The parameter is a tuple containing an error code and a\r\ndescription of the error, from the following list:\r\n1 - invalid data - This error means that unexpected data has been received from\r\nthe server. The most common reason is that the server specified as the proxy is\r\nnot really a Socks4/Socks5/HTTP proxy, or maybe the proxy type specified is wrong.\r\n4 - bad proxy type - This will be raised if the type of the proxy supplied to the\r\nsetproxy function was not PROXY_TYPE_SOCKS4/PROXY_TYPE_SOCKS5/PROXY_TYPE_HTTP.\r\n5 - bad input - This will be raised if the connect method is called with bad input\r\nparameters.\r\n\r\nclass Socks5AuthError - This indicates that the connection through a Socks5 server\r\nfailed due to an authentication problem. The parameter is a tuple containing a\r\ncode and a description message according to the following list:\r\n\r\n1 - authentication is required - This will happen if you use a Socks5 server which\r\nrequires authentication without providing a username / password at all.\r\n2 - all offered authentication methods were rejected - This will happen if the proxy\r\nrequires a special authentication method which is not supported by this module.\r\n3 - unknown username or invalid password - Self descriptive.\r\n\r\nclass Socks5Error - This will be raised for Socks5 errors which are not related to\r\nauthentication. The parameter is a tuple containing a code and a description of the\r\nerror, as given by the server. The possible errors, according to the RFC are:\r\n\r\n1 - General SOCKS server failure - If for any reason the proxy server is unable to\r\nfulfill your request (internal server error).\r\n2 - connection not allowed by ruleset - If the address you're trying to connect to\r\nis blacklisted on the server or requires authentication.\r\n3 - Network unreachable - The target could not be contacted. A router on the network\r\nhad replied with a destination net unreachable error.\r\n4 - Host unreachable - The target could not be contacted. A router on the network\r\nhad replied with a destination host unreachable error.\r\n5 - Connection refused - The target server has actively refused the connection\r\n(the requested port is closed).\r\n6 - TTL expired - The TTL value of the SYN packet from the proxy to the target server\r\nhas expired. This usually means that there are network problems causing the packet\r\nto be caught in a router-to-router \"ping-pong\".\r\n7 - Command not supported - The client has issued an invalid command. When using this\r\nmodule, this error should not occur.\r\n8 - Address type not supported - The client has provided an invalid address type.\r\nWhen using this module, this error should not occur.\r\n\r\nclass Socks4Error - This will be raised for Socks4 errors. The parameter is a tuple\r\ncontaining a code and a description of the error, as given by the server. The\r\npossible error, according to the specification are:\r\n\r\n1 - Request rejected or failed - Will be raised in the event of an failure for any\r\nreason other then the two mentioned next.\r\n2 - request rejected because SOCKS server cannot connect to identd on the client -\r\nThe Socks server had tried an ident lookup on your computer and has failed. In this\r\ncase you should run an identd server and/or configure your firewall to allow incoming\r\nconnections to local port 113 from the remote server.\r\n3 - request rejected because the client program and identd report different user-ids -\r\nThe Socks server had performed an ident lookup on your computer and has received a\r\ndifferent userid than the one you have provided. Change your userid (through the\r\nusername parameter of the setproxy method) to match and try again.\r\n\r\nclass HTTPError - This will be raised for HTTP errors. The parameter is a tuple\r\ncontaining the HTTP status code and the description of the server.\r\n\r\n\r\nAfter establishing the connection, the object behaves like a standard socket.\r\nCall the close method to close the connection.\r\n\r\nIn addition to the socksocket class, an additional function worth mentioning is the\r\nsetdefaultproxy function. The parameters are the same as the setproxy method.\r\nThis function will set default proxy settings for newly created socksocket objects,\r\nin which the proxy settings haven't been changed via the setproxy method.\r\nThis is quite useful if you wish to force 3rd party modules to use a socks proxy,\r\nby overriding the socket object.\r\nFor example:\r\n\r\n>>> socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,\"socks.example.com\")\r\n>>> socket.socket = socks.socksocket\r\n>>> urllib.urlopen(\"http://www.sourceforge.net/\")\r\n\r\n\r\n</pre>\r\n",
"bugtrack_url": null,
"license": null,
"summary": "A python module for Chaining of Proxies.",
"version": "1.3",
"project_urls": {
"Homepage": "https://github.com/nighthawkk/pyChainedProxy"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "108f1f6d581915d3b21e35be4e46e47688161ab28398ca7e58daba514e570d45",
"md5": "76f79877f9d055ddab5a30b39a7b6999",
"sha256": "6f2e1d3267f11033fd9b083def7199af70b5b0648b1db3bada1d27da4236f7ea"
},
"downloads": -1,
"filename": "pyChainedProxy-1.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "76f79877f9d055ddab5a30b39a7b6999",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 16823,
"upload_time": "2024-08-14T08:39:50",
"upload_time_iso_8601": "2024-08-14T08:39:50.379476Z",
"url": "https://files.pythonhosted.org/packages/10/8f/1f6d581915d3b21e35be4e46e47688161ab28398ca7e58daba514e570d45/pyChainedProxy-1.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7762dc2e3b0be14605c602c7c4a5859286ac2ecdfef0fd73abcc24bff0bcd382",
"md5": "a6d6ae55b5b400ac09b67cc888b0a9fe",
"sha256": "080f013f6d535ed77a9cc9812cbbcf0286195dd484b8d1ee28663adeb978148d"
},
"downloads": -1,
"filename": "pychainedproxy-1.3.tar.gz",
"has_sig": false,
"md5_digest": "a6d6ae55b5b400ac09b67cc888b0a9fe",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 19889,
"upload_time": "2024-08-14T08:39:52",
"upload_time_iso_8601": "2024-08-14T08:39:52.064513Z",
"url": "https://files.pythonhosted.org/packages/77/62/dc2e3b0be14605c602c7c4a5859286ac2ecdfef0fd73abcc24bff0bcd382/pychainedproxy-1.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-14 08:39:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "nighthawkk",
"github_project": "pyChainedProxy",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pychainedproxy"
}