2015-10-04 18:56:26 +02:00
|
|
|
|
#!/usr/bin/env python2.7
|
2013-12-10 21:19:37 +01:00
|
|
|
|
# -*- coding: cp1252 -*-
|
|
|
|
|
# <PythonProxy.py>
|
|
|
|
|
#
|
|
|
|
|
#Copyright (c) <2009> <F<>bio Domingues - fnds3000 in gmail.com>
|
|
|
|
|
#
|
|
|
|
|
#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.
|
|
|
|
|
|
|
|
|
|
"""\
|
|
|
|
|
Copyright (c) <2009> <F<>bio Domingues - fnds3000 in gmail.com> <MIT Licence>
|
|
|
|
|
|
|
|
|
|
**************************************
|
|
|
|
|
*** Python Proxy - A Fast HTTP proxy ***
|
|
|
|
|
**************************************
|
|
|
|
|
|
|
|
|
|
Neste momento este proxy <20> um Elie Proxy.
|
|
|
|
|
|
|
|
|
|
Suporta os m<>todos HTTP:
|
|
|
|
|
- OPTIONS;
|
|
|
|
|
- GET;
|
|
|
|
|
- HEAD;
|
|
|
|
|
- POST;
|
|
|
|
|
- PUT;
|
|
|
|
|
- DELETE;
|
|
|
|
|
- TRACE;
|
|
|
|
|
- CONENCT.
|
|
|
|
|
|
|
|
|
|
Suporta:
|
|
|
|
|
- Conex<65>es dos cliente em IPv4 ou IPv6;
|
|
|
|
|
- Conex<65>es ao alvo em IPv4 e IPv6;
|
|
|
|
|
- Conex<65>es todo o tipo de transmiss<73>o de dados TCP (CONNECT tunneling),
|
|
|
|
|
p.e. liga<67><61>es SSL, como <20> o caso do HTTPS.
|
|
|
|
|
|
|
|
|
|
A fazer:
|
|
|
|
|
- Verificar se o input vindo do cliente est<73> correcto;
|
|
|
|
|
- Enviar os devidos HTTP erros se n<>o, ou simplesmente quebrar a liga<67><61>o;
|
|
|
|
|
- Criar um gestor de erros;
|
|
|
|
|
- Criar ficheiro log de erros;
|
|
|
|
|
- Colocar excep<65><70>es nos s<>tios onde <20> previs<69>vel a ocorr<72>ncia de erros,
|
|
|
|
|
p.e.sockets e ficheiros;
|
|
|
|
|
- Rever tudo e melhorar a estrutura do programar e colocar nomes adequados nas
|
|
|
|
|
vari<72>veis e m<>todos;
|
|
|
|
|
- Comentar o programa decentemente;
|
|
|
|
|
- Doc Strings.
|
|
|
|
|
|
|
|
|
|
Funcionalidades futuras:
|
|
|
|
|
- Adiconar a funcionalidade de proxy an<61>nimo e transparente;
|
|
|
|
|
- Suportar FTP?.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(!) Aten<65><6E>o o que se segue s<> tem efeito em conex<65>es n<>o CONNECT, para estas o
|
|
|
|
|
proxy <20> sempre Elite.
|
|
|
|
|
|
|
|
|
|
Qual a diferen<65>a entre um proxy Elite, An<41>nimo e Transparente?
|
|
|
|
|
- Um proxy elite <20> totalmente an<61>nimo, o servidor que o recebe n<>o consegue ter
|
|
|
|
|
conhecimento da exist<73>ncia do proxy e n<>o recebe o endere<72>o IP do cliente;
|
|
|
|
|
- Quando <20> usado um proxy an<61>nimo o servidor sabe que o cliente est<73> a usar um
|
|
|
|
|
proxy mas n<>o sabe o endere<72>o IP do cliente;
|
|
|
|
|
<20> enviado o cabe<62>alho HTTP "Proxy-agent".
|
|
|
|
|
- Um proxy transparente fornece ao servidor o IP do cliente e um informa<6D><61>o que
|
|
|
|
|
se est<73> a usar um proxy.
|
|
|
|
|
S<>o enviados os cabe<62>alhos HTTP "Proxy-agent" e "HTTP_X_FORWARDED_FOR".
|
|
|
|
|
|
|
|
|
|
"""
|
2016-03-10 17:15:34 +01:00
|
|
|
|
from __future__ import print_function
|
2016-03-11 09:36:04 +01:00
|
|
|
|
from __future__ import division
|
2013-12-10 21:19:37 +01:00
|
|
|
|
|
2016-03-10 18:28:02 +01:00
|
|
|
|
import socket, six.moves._thread, select
|
2013-12-10 21:19:37 +01:00
|
|
|
|
PORT = 8085
|
|
|
|
|
|
|
|
|
|
__version__ = '0.1.0 Draft 1'
|
|
|
|
|
BUFLEN = 8192
|
|
|
|
|
VERSION = 'Python Proxy/'+__version__
|
|
|
|
|
HTTPVER = 'HTTP/1.1'
|
|
|
|
|
|
2016-03-11 08:53:44 +01:00
|
|
|
|
class ConnectionHandler(object):
|
2013-12-10 21:19:37 +01:00
|
|
|
|
def __init__(self, connection, address, timeout):
|
|
|
|
|
self.client = connection
|
|
|
|
|
self.client_buffer = ''
|
|
|
|
|
self.timeout = timeout
|
|
|
|
|
self.method, self.path, self.protocol = self.get_base_header()
|
|
|
|
|
if self.method=='CONNECT':
|
|
|
|
|
self.method_CONNECT()
|
|
|
|
|
elif self.method in ('OPTIONS', 'GET', 'HEAD', 'POST', 'PUT',
|
|
|
|
|
'DELETE', 'TRACE'):
|
|
|
|
|
self.method_others()
|
|
|
|
|
self.client.close()
|
|
|
|
|
self.target.close()
|
|
|
|
|
|
|
|
|
|
def get_base_header(self):
|
2016-03-10 14:52:28 +01:00
|
|
|
|
while True:
|
2013-12-10 21:19:37 +01:00
|
|
|
|
self.client_buffer += self.client.recv(BUFLEN)
|
|
|
|
|
end = self.client_buffer.find('\n')
|
|
|
|
|
if end!=-1:
|
|
|
|
|
break
|
|
|
|
|
data = (self.client_buffer[:end+1]).split()
|
|
|
|
|
self.client_buffer = self.client_buffer[end+1:]
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
def method_CONNECT(self):
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print('self.path', self.path)
|
2013-12-10 21:19:37 +01:00
|
|
|
|
self._connect_target(self.path)
|
|
|
|
|
self.client.send(HTTPVER+' 200 Connection established\n'+
|
|
|
|
|
'Proxy-agent: %s\n\n'%VERSION)
|
|
|
|
|
self.client_buffer = ''
|
|
|
|
|
self._read_write()
|
|
|
|
|
|
|
|
|
|
def method_others(self):
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print()
|
|
|
|
|
print('=====================================')
|
|
|
|
|
print('method', self.method)
|
|
|
|
|
print('protocol', self.protocol)
|
|
|
|
|
print('self.path', self.path)
|
|
|
|
|
print()
|
2013-12-10 21:19:37 +01:00
|
|
|
|
|
|
|
|
|
horking = False
|
|
|
|
|
if horking:
|
|
|
|
|
if self.path.endswith('.js'):
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print('HORKING JS!!!!!!!')
|
|
|
|
|
print()
|
2013-12-10 21:19:37 +01:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if self.path.endswith('.png'):
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print('HORKING PNG!!!!!!!')
|
|
|
|
|
print()
|
2013-12-10 21:19:37 +01:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if self.path.endswith('.css'):
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print('HORKING CSS!!!!!!!')
|
|
|
|
|
print()
|
2013-12-10 21:19:37 +01:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self._connect_target()
|
2015-12-01 17:11:16 +01:00
|
|
|
|
payload = '%s %s %s\n' % (self.method, self.path, self.protocol)
|
2013-12-10 21:19:37 +01:00
|
|
|
|
buf = payload
|
|
|
|
|
try:
|
|
|
|
|
headers, rest = self.client_buffer.split('\r\n\r\n')
|
|
|
|
|
except:
|
|
|
|
|
headers = self.client_buffer
|
|
|
|
|
rest = None
|
|
|
|
|
|
|
|
|
|
for line in headers.split('\n'):
|
|
|
|
|
line = line.strip()
|
|
|
|
|
if not line:
|
|
|
|
|
continue
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print(repr(line))
|
2013-12-10 21:19:37 +01:00
|
|
|
|
field, value = line.split(':', 1)
|
|
|
|
|
if line.startswith('Host:'):
|
|
|
|
|
line = line.replace(str(PORT), '9991')
|
|
|
|
|
if field in ['Connection']:
|
|
|
|
|
line = 'Connection: close'
|
|
|
|
|
if field in ['If-Modified-Since']:
|
|
|
|
|
continue
|
|
|
|
|
buf += line + '\n'
|
|
|
|
|
buf += '\n'
|
|
|
|
|
buf = buf.replace('\n', '\r\n')
|
|
|
|
|
self.target.send(buf)
|
|
|
|
|
if rest:
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print('REST')
|
|
|
|
|
print(repr(self.client_buffer))
|
|
|
|
|
print(repr(buf))
|
|
|
|
|
print(repr(rest))
|
2013-12-10 21:19:37 +01:00
|
|
|
|
self.target.send(rest)
|
|
|
|
|
|
|
|
|
|
self.client_buffer = ''
|
|
|
|
|
self._read_write()
|
|
|
|
|
|
|
|
|
|
def _connect_target(self):
|
|
|
|
|
host = '127.0.0.1'
|
|
|
|
|
port = 9991
|
|
|
|
|
(soc_family, _, _, _, address) = socket.getaddrinfo(host, port)[0]
|
|
|
|
|
self.target = socket.socket(soc_family)
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print('Connecting...', host, port)
|
2013-12-10 21:19:37 +01:00
|
|
|
|
self.target.connect(address)
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print('Connected')
|
2013-12-10 21:19:37 +01:00
|
|
|
|
|
|
|
|
|
def _read_write(self):
|
2016-03-11 09:36:04 +01:00
|
|
|
|
time_out_max = self.timeout//3
|
2013-12-10 21:19:37 +01:00
|
|
|
|
socs = [self.client, self.target]
|
|
|
|
|
count = 0
|
2016-03-10 14:52:28 +01:00
|
|
|
|
while True:
|
2013-12-10 21:19:37 +01:00
|
|
|
|
count += 1
|
|
|
|
|
(recv, _, error) = select.select(socs, [], socs, 3)
|
|
|
|
|
if error:
|
|
|
|
|
break
|
|
|
|
|
if recv:
|
|
|
|
|
for in_ in recv:
|
|
|
|
|
data = in_.recv(BUFLEN)
|
|
|
|
|
if in_ is self.client:
|
|
|
|
|
out = self.target
|
|
|
|
|
else:
|
|
|
|
|
if data:
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print()
|
|
|
|
|
print('OUT')
|
|
|
|
|
print('path =', self.path)
|
|
|
|
|
print(len(data))
|
|
|
|
|
print(repr(data))
|
|
|
|
|
print()
|
2013-12-10 21:19:37 +01:00
|
|
|
|
out = self.client
|
|
|
|
|
# super hacky and fragile
|
|
|
|
|
data = data.replace('9991', str(PORT))
|
|
|
|
|
|
|
|
|
|
if data:
|
|
|
|
|
if '400 Bad Request' in data:
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print('DONE!')
|
2013-12-10 21:19:37 +01:00
|
|
|
|
import sys; sys.exit(1)
|
|
|
|
|
out.send(data)
|
|
|
|
|
count = 0
|
|
|
|
|
if count == time_out_max:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
def start_server(host='localhost', port=PORT, IPv6=False, timeout=60,
|
|
|
|
|
handler=ConnectionHandler):
|
|
|
|
|
if IPv6==True:
|
|
|
|
|
soc_type=socket.AF_INET6
|
|
|
|
|
else:
|
|
|
|
|
soc_type=socket.AF_INET
|
|
|
|
|
soc = socket.socket(soc_type)
|
|
|
|
|
soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
|
soc.bind((host, port))
|
2016-03-10 17:15:34 +01:00
|
|
|
|
print("Serving on %s:%d." % (host, port)) #debug
|
2013-12-10 21:19:37 +01:00
|
|
|
|
soc.listen(0)
|
2016-03-10 14:52:28 +01:00
|
|
|
|
while True:
|
2016-03-10 18:28:02 +01:00
|
|
|
|
six.moves._thread.start_new_thread(handler, soc.accept()+(timeout,))
|
2013-12-10 21:19:37 +01:00
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
start_server()
|