Skip to content

Instantly share code, notes, and snippets.

@Billcountry
Last active September 29, 2017 03:29
Show Gist options
  • Save Billcountry/0f95d324c566b7bbf5860c14a349e6e2 to your computer and use it in GitHub Desktop.
Save Billcountry/0f95d324c566b7bbf5860c14a349e6e2 to your computer and use it in GitHub Desktop.
An easy way to create personal proxy servers and bouncing your connection off several servers before the connection finally reaches you. Use with caution, am not responsible of your actions.
#!/usr/bin/python
import sys, thread, socket
'''
Under MIT License
To run Simply
(If you make proxy.py executable)
./proxy.py 8090
(Else)
python proxy.py 8090
Where 8090 is the port you want your server to run on
'''
def main():
if len(sys.argv)<2:
port = 16384
else:
port = int(sys.argv[1])
host = '0.0.0.0'
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(100)
except socket.error, (value, message):
if s:
s.close()
sys.exit(1)
while 1:
conn, client_addr = s.accept()
thread.start_new_thread(proxy_thread, (conn, client_addr))
s.close()
def proxy_thread(conn, client_addr):
data_len = 1
request = ""
while data_len:
data = conn.recv(16384)
data_len = len(data)
request += data
if data_len<16384:
break
print request
first_line = request.split('\n')[0] #The first line in a http request contains the URL
url = first_line.split(' ')[1]
# find the webserver and port
http_pos = url.find("://") # find pos of :// (This position allows us to find the begining of the URL)
if http_pos==-1:
temp = url
else:
temp = url[(http_pos+3):] # get the rest of url
port_pos = temp.find(":") # find the port pos (if any)
# find end of web server
webserver_pos = temp.find("/")
if webserver_pos == -1:
webserver_pos = len(temp)
webserver = ""
port = -1
if port_pos==-1 or webserver_pos < port_pos: # default port http=80, https=443
secure = url.find("https://")
if secure>=0 and http_pos>secure:
port = 443
else:
port = 80
webserver = temp[:webserver_pos]
else: # specific port
port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
webserver = temp[:port_pos]
try:
# create a socket to connect to the web server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((webserver, port))
s.send(request) # send request to webserver
while 1:
# receive data from web server
data = s.recv(16384)
if len(data) > 0:
# send to browser
conn.send(data)
else:
break
s.close()
conn.close()
except socket.error, (value, message):
if s:
s.close()
if conn:
conn.close()
sys.exit(1)
if __name__ == '__main__':
main()
#!/usr/bin/python
import sys, thread, socket
'''
Under MIT License
To use the proxy bounce, connect your browser directly to the proxy bounce instead of the proxy,
You can do this as many times as possible as long as the final node is a proxy instead of a proxybounce.
To execute
(If you make proxybounce.py executable)
./proxybounce.py 8080 127.0.0.1 8090
(Else)
python proxybounce.py 8080 127.0.0.1 8090
Where 8080 is the port your bounce is running on and 127.0.0.1 8090 is the IP address or the domain and
the port of the proxy you want to forward the connection to or another proxybounce.
'''
# This one has a simple job, recieve a connection from a proxy server or another proxy bounce and foward the request to the webserver and vise verser
bounce_port = 8090
bounce_ip = "127.0.0.1"
def main():
global bounce_ip, bounce_port
if len(sys.argv)<2:
port = 8080
else:
bounce_ip = sys.argv[2]
bounce_port = int(sys.argv[3])
port = int(sys.argv[1])
host = '0.0.0.0'
try:
print "Listening on: ",
print port,
print " and bouncing to: ",
print bounce_ip,
print " on: ",
print bounce_port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(100)
except socket.error, (value, message):
if s:
s.close()
sys.exit(1)
while 1:
conn, client_addr = s.accept()
thread.start_new_thread(proxy_thread, (conn, client_addr))
s.close()
def proxy_thread(conn, client_addr):
global bounce_ip, bounce_port
data_len = 1
request = ""
while data_len:
data = conn.recv(16384)
data_len = len(data)
request += data
if data_len<16384:
break
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((bounce_ip, bounce_port))
s.send(request)
while 1:
data = s.recv(16384)
if len(data) > 0:
conn.send(data)
else:
break
s.close()
conn.close()
except socket.error, (value, message):
if s:
s.close()
if conn:
conn.close()
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment