|
|
|
@ -7,9 +7,10 @@ import logging
|
|
|
|
|
import logging.handlers
|
|
|
|
|
from logging import config
|
|
|
|
|
|
|
|
|
|
CONNECTION_TIMEOUT_SEC = 10
|
|
|
|
|
BUFFER_SIZE = 32 * 1024
|
|
|
|
|
CURRENT_THREADS = 0
|
|
|
|
|
MAX_THREADS = 10
|
|
|
|
|
MAX_THREADS = 50
|
|
|
|
|
BACKLOG = 50
|
|
|
|
|
LOGGING = {
|
|
|
|
|
'version': 1,
|
|
|
|
@ -86,72 +87,111 @@ class Server:
|
|
|
|
|
self.thread_check()
|
|
|
|
|
|
|
|
|
|
CURRENT_THREADS += 1
|
|
|
|
|
thread = Thread(target = connectionHandle, args = (conn, client_addr, ))
|
|
|
|
|
client_connection = ClientConnection(conn, client_addr)
|
|
|
|
|
thread = Thread(target = client_connection.handle_connection)
|
|
|
|
|
CURRENT_THREADS -= 1
|
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
self.sock.close()
|
|
|
|
|
|
|
|
|
|
def is_valid_status_code(status_code:str):
|
|
|
|
|
valid_starts = [str(i) for i in range(5)]
|
|
|
|
|
if status_code.startswith(tuple(valid_starts)):
|
|
|
|
|
class ClientConnection:
|
|
|
|
|
def __init__(self, client_socket, client_address):
|
|
|
|
|
self.client_socket = client_socket
|
|
|
|
|
self.client_addr = client_address[0]
|
|
|
|
|
self.client_port = client_address[1]
|
|
|
|
|
|
|
|
|
|
self.server_socket = None
|
|
|
|
|
|
|
|
|
|
def check_monitorando(self, request):
|
|
|
|
|
if b"monitorando" in request:
|
|
|
|
|
body = open('forbidden.html', 'r').read()
|
|
|
|
|
self.client_socket.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\n")
|
|
|
|
|
self.client_socket.sendall(body.encode())
|
|
|
|
|
self.client_socket.close()
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def connectionHandle(client_socket, client_address):
|
|
|
|
|
request = client_socket.recv(BUFFER_SIZE)
|
|
|
|
|
logger = Logger.instance()
|
|
|
|
|
|
|
|
|
|
if len(request) == 0:
|
|
|
|
|
client_socket.close()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
raw_request = request.decode()
|
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
|
client_socket.close()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if "CONNECT" in raw_request:
|
|
|
|
|
client_socket.sendall(b"HTTP/1.1 200 Connection Established\r\n\r\n")
|
|
|
|
|
request = client_socket.recv(BUFFER_SIZE)
|
|
|
|
|
|
|
|
|
|
request_url = raw_request.split(' ')[1]
|
|
|
|
|
def get_host_port(self, request):
|
|
|
|
|
request_url = request.split(' ')[1]
|
|
|
|
|
request_host = ""
|
|
|
|
|
request_port = 443 if 'https' in request_url else 80
|
|
|
|
|
request_port = 443 if request_url.startswith('https') else 80
|
|
|
|
|
|
|
|
|
|
# Remove protocolo do request
|
|
|
|
|
if request_url.startswith('http'):
|
|
|
|
|
request_host = request_url.split('/')[2]
|
|
|
|
|
request_host = request_url.split('//')[1]
|
|
|
|
|
else:
|
|
|
|
|
request_host = request_url.split('/')[0]
|
|
|
|
|
|
|
|
|
|
# Remove 'www' do request
|
|
|
|
|
if request_host.startswith('www'):
|
|
|
|
|
request_host = request_host[4:]
|
|
|
|
|
request_host = request_host.split('www.')[1]
|
|
|
|
|
|
|
|
|
|
# Remove porta do request e verifica se é um host com porta
|
|
|
|
|
if ':' in request_host:
|
|
|
|
|
request_port = int(request_host.split(':')[1])
|
|
|
|
|
request_host = request_host.split(':')[0]
|
|
|
|
|
|
|
|
|
|
if "monitorando" in request_url.lower():
|
|
|
|
|
body = open('forbidden.html', 'r').read()
|
|
|
|
|
client_socket.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\n")
|
|
|
|
|
client_socket.sendall(body.encode())
|
|
|
|
|
client_socket.close()
|
|
|
|
|
logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - 'Monitorando' - 403 Forbidden")
|
|
|
|
|
return request_host, request_port
|
|
|
|
|
|
|
|
|
|
def handle_connection(self):
|
|
|
|
|
request = self.client_socket.recv(BUFFER_SIZE)
|
|
|
|
|
logger = Logger.instance()
|
|
|
|
|
|
|
|
|
|
# Verifica se o request é vazio, se for, fecha o socket
|
|
|
|
|
if len(request) == 0:
|
|
|
|
|
self.client_socket.close()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
server_socket.connect((request_host, request_port))
|
|
|
|
|
server_socket.send(request)
|
|
|
|
|
try:
|
|
|
|
|
# Tenta decodificar o request, se não conseguir, fecha o socket
|
|
|
|
|
raw_request = request.decode()
|
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
|
self.client_socket.close()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Verifica se é um request CONNECT
|
|
|
|
|
# Se for, retorna 200 Connection Established, e espera o request novamente
|
|
|
|
|
if "CONNECT" in raw_request:
|
|
|
|
|
self.client_socket.sendall(b"HTTP/1.1 200 Connection Established\r\n\r\n")
|
|
|
|
|
request = self.client_socket.recv(BUFFER_SIZE)
|
|
|
|
|
|
|
|
|
|
# Retorna host e porta do request
|
|
|
|
|
request_host, request_port = self.get_host_port(raw_request)
|
|
|
|
|
|
|
|
|
|
# Verifica se 'monitorando' está no request
|
|
|
|
|
if self.check_monitorando(request):
|
|
|
|
|
logger.info(f"REQUEST [{self.client_addr}:{self.client_port}] to [{request_host}:{request_port}] - 'Monitorando' - 403 Forbidden")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Cria socket para o servidor
|
|
|
|
|
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
self.server_socket.connect((request_host, request_port))
|
|
|
|
|
self.server_socket.send(request)
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
Enquanto houver dados para serem lidos, lê do socket do cliente e envia para o servidor
|
|
|
|
|
e lê do socket do servidor e envia para o cliente
|
|
|
|
|
|
|
|
|
|
Se não houver dados para serem lidos, fecha os sockets
|
|
|
|
|
|
|
|
|
|
Exemplo de fluxo:
|
|
|
|
|
proxy -> server
|
|
|
|
|
server -> proxy
|
|
|
|
|
proxy -> client
|
|
|
|
|
proxy -> client
|
|
|
|
|
proxy -> client
|
|
|
|
|
server -> proxy
|
|
|
|
|
'''
|
|
|
|
|
data = None
|
|
|
|
|
while True:
|
|
|
|
|
triple = select.select([client_socket, server_socket], [], [], 10)[0]
|
|
|
|
|
triple = select.select([self.client_socket, self.server_socket], [], [], CONNECTION_TIMEOUT_SEC)[0]
|
|
|
|
|
if not len(triple):
|
|
|
|
|
break
|
|
|
|
|
try:
|
|
|
|
|
if server_socket in triple:
|
|
|
|
|
data = server_socket.recv(BUFFER_SIZE)
|
|
|
|
|
if self.server_socket in triple:
|
|
|
|
|
data = self.server_socket.recv(BUFFER_SIZE)
|
|
|
|
|
if not data:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
@ -159,27 +199,32 @@ def connectionHandle(client_socket, client_address):
|
|
|
|
|
status_code = data.decode().split('\r\n')[0].split(' ')[1:]
|
|
|
|
|
status_code = ' '.join(status_code)
|
|
|
|
|
if is_valid_status_code(status_code):
|
|
|
|
|
logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - {status_code}")
|
|
|
|
|
logger.info(f"REQUEST [{self.client_addr}:{self.client_port}] to [{request_host}:{request_port}] - {status_code}")
|
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
|
logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]")
|
|
|
|
|
logger.info(f"REQUEST [{self.client_addr}:{self.client_port}] to [{request_host}:{request_port}]")
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
client_socket.send(data)
|
|
|
|
|
if client_socket in triple:
|
|
|
|
|
data = client_socket.recv(BUFFER_SIZE)
|
|
|
|
|
self.client_socket.send(data)
|
|
|
|
|
if self.client_socket in triple:
|
|
|
|
|
data = self.client_socket.recv(BUFFER_SIZE)
|
|
|
|
|
if not data:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
server_socket.send(data)
|
|
|
|
|
self.server_socket.send(data)
|
|
|
|
|
except ConnectionAbortedError:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
server_socket.close()
|
|
|
|
|
client_socket.close()
|
|
|
|
|
self.server_socket.close()
|
|
|
|
|
self.client_socket.close()
|
|
|
|
|
|
|
|
|
|
def verify_code_integrity():
|
|
|
|
|
def is_valid_status_code(status_code: str):
|
|
|
|
|
valid_starts = [str(i) for i in range(5)]
|
|
|
|
|
if status_code.startswith(tuple(valid_starts)):
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def check_code():
|
|
|
|
|
import hashlib
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
content = None
|
|
|
|
|
shas256_hash = None
|
|
|
|
@ -199,7 +244,7 @@ def verify_code_integrity():
|
|
|
|
|
print('Arquivo do proxy verificado!')
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
verify_code_integrity()
|
|
|
|
|
check_code()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
ser = Server(host="0.0.0.0", port=8080)
|
|
|
|
|