This commit is contained in:
Jose Henrique 2023-12-06 17:22:04 -03:00
parent 0c9d2d41a4
commit 12f4176fca
2 changed files with 84 additions and 85 deletions

View File

@ -9,7 +9,7 @@ from logging import config
BUFFER_SIZE = 32 * 1024 BUFFER_SIZE = 32 * 1024
CURRENT_THREADS = 0 CURRENT_THREADS = 0
MAX_THREADS = 10 MAX_THREADS = 50
BACKLOG = 50 BACKLOG = 50
LOGGING = { LOGGING = {
'version': 1, 'version': 1,
@ -86,100 +86,99 @@ class Server:
self.thread_check() self.thread_check()
CURRENT_THREADS += 1 CURRENT_THREADS += 1
thread = Thread(target = connectionHandle, args = (conn, client_addr, )) thread = Thread(target = self.handle_connection, args = (conn, client_addr, ))
CURRENT_THREADS -= 1 CURRENT_THREADS -= 1
thread.start() thread.start()
def __del__(self): def __del__(self):
self.sock.close() self.sock.close()
def is_valid_status_code(status_code:str): def handle_connection(self, 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]
request_host = ""
request_port = 443 if 'https' in request_url else 80
if request_url.startswith('http'):
request_host = request_url.split('/')[2]
else:
request_host = request_url.split('/')[0]
if request_host.startswith('www'):
request_host = request_host[4:]
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
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.connect((request_host, request_port))
server_socket.send(request)
while True:
triple = select.select([client_socket, server_socket], [], [], 10)[0]
if not len(triple):
break
try:
if server_socket in triple:
data = server_socket.recv(BUFFER_SIZE)
if not data:
break
try:
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}")
except UnicodeDecodeError:
logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]")
pass
client_socket.send(data)
if client_socket in triple:
data = client_socket.recv(BUFFER_SIZE)
if not data:
break
server_socket.send(data)
except ConnectionAbortedError:
break
server_socket.close()
client_socket.close()
def is_valid_status_code(status_code: str):
valid_starts = [str(i) for i in range(5)] valid_starts = [str(i) for i in range(5)]
if status_code.startswith(tuple(valid_starts)): if status_code.startswith(tuple(valid_starts)):
return True return True
return False return False
def connectionHandle(client_socket, client_address): def check_code():
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]
request_host = ""
request_port = 443 if 'https' in request_url else 80
if request_url.startswith('http'):
request_host = request_url.split('/')[2]
else:
request_host = request_url.split('/')[0]
if request_host.startswith('www'):
request_host = request_host[4:]
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
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.connect((request_host, request_port))
server_socket.send(request)
while True:
triple = select.select([client_socket, server_socket], [], [], 10)[0]
if not len(triple):
break
try:
if server_socket in triple:
data = server_socket.recv(BUFFER_SIZE)
if not data:
break
try:
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}")
except UnicodeDecodeError:
logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]")
pass
client_socket.send(data)
if client_socket in triple:
data = client_socket.recv(BUFFER_SIZE)
if not data:
break
server_socket.send(data)
except ConnectionAbortedError:
break
server_socket.close()
client_socket.close()
def verify_code_integrity():
import hashlib import hashlib
import sys
content = None content = None
shas256_hash = None shas256_hash = None
@ -199,7 +198,7 @@ def verify_code_integrity():
print('Arquivo do proxy verificado!') print('Arquivo do proxy verificado!')
if __name__ == '__main__': if __name__ == '__main__':
verify_code_integrity() check_code()
try: try:
ser = Server(host="0.0.0.0", port=8080) ser = Server(host="0.0.0.0", port=8080)

View File

@ -1 +1 @@
dd8f33d81858978c4fec0eb3c2f0acb768028a10d80428417aae91bf86b343d3 02c9fe27bfc80f2a71e6a7b618c379fcd60b18c4e8cc646d081b1eebb7682bba