Update proxy.py with buffer size constant and

localized print statement
This commit is contained in:
Jose Henrique 2023-12-06 17:15:01 -03:00
parent f4f5690ff8
commit 0c9d2d41a4
2 changed files with 14 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import logging
import logging.handlers import logging.handlers
from logging import config from logging import config
BUFFER_SIZE = 32 * 1024
CURRENT_THREADS = 0 CURRENT_THREADS = 0
MAX_THREADS = 10 MAX_THREADS = 10
BACKLOG = 50 BACKLOG = 50
@ -68,7 +69,7 @@ class Server:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind((host, port)) self.sock.bind((host, port))
self.sock.listen(BACKLOG) self.sock.listen(BACKLOG)
print(f"Listening at: http://{host}:{port}") print(f"Proxy executando em: http://{host}:{port}")
def thread_check(self): def thread_check(self):
global CURRENT_THREADS, MAX_THREADS global CURRENT_THREADS, MAX_THREADS
@ -99,7 +100,7 @@ def is_valid_status_code(status_code:str):
return False return False
def connectionHandle(client_socket, client_address): def connectionHandle(client_socket, client_address):
request = client_socket.recv(16 * 1024) request = client_socket.recv(BUFFER_SIZE)
logger = Logger.instance() logger = Logger.instance()
if len(request) == 0: if len(request) == 0:
@ -112,9 +113,9 @@ def connectionHandle(client_socket, client_address):
client_socket.close() client_socket.close()
return return
# if "CONNECT" in raw_request: if "CONNECT" in raw_request:
# client_socket.sendall(b"HTTP/1.1 200 Connection Established\r\n\r\n") client_socket.sendall(b"HTTP/1.1 200 Connection Established\r\n\r\n")
# request = client_socket.recv(16 * 1024) request = client_socket.recv(BUFFER_SIZE)
request_url = raw_request.split(' ')[1] request_url = raw_request.split(' ')[1]
request_host = "" request_host = ""
@ -132,12 +133,12 @@ def connectionHandle(client_socket, client_address):
request_port = int(request_host.split(':')[1]) request_port = int(request_host.split(':')[1])
request_host = request_host.split(':')[0] request_host = request_host.split(':')[0]
if "monitorando" in request_url: if "monitorando" in request_url.lower():
body = open('forbidden.html', 'r').read() body = open('forbidden.html', 'r').read()
client_socket.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\n") client_socket.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\n")
client_socket.sendall(body.encode()) client_socket.sendall(body.encode())
client_socket.close() client_socket.close()
logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - 403 Forbidden") logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - 'Monitorando' - 403 Forbidden")
return return
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -150,7 +151,7 @@ def connectionHandle(client_socket, client_address):
break break
try: try:
if server_socket in triple: if server_socket in triple:
data = server_socket.recv(16 * 1024) data = server_socket.recv(BUFFER_SIZE)
if not data: if not data:
break break
@ -158,14 +159,14 @@ def connectionHandle(client_socket, client_address):
status_code = data.decode().split('\r\n')[0].split(' ')[1:] status_code = data.decode().split('\r\n')[0].split(' ')[1:]
status_code = ' '.join(status_code) status_code = ' '.join(status_code)
if is_valid_status_code(status_code): if is_valid_status_code(status_code):
logger.info(f"PROXY [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - {status_code}") logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - {status_code}")
except UnicodeDecodeError: except UnicodeDecodeError:
logger.info(f"PROXY [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]") logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]")
pass pass
client_socket.send(data) client_socket.send(data)
if client_socket in triple: if client_socket in triple:
data = client_socket.recv(16 * 1024) data = client_socket.recv(BUFFER_SIZE)
if not data: if not data:
break break
@ -198,7 +199,7 @@ def verify_code_integrity():
print('Arquivo do proxy verificado!') print('Arquivo do proxy verificado!')
if __name__ == '__main__': if __name__ == '__main__':
# verify_code_integrity() verify_code_integrity()
try: try:
ser = Server(host="0.0.0.0", port=8080) ser = Server(host="0.0.0.0", port=8080)

View File

@ -1 +1 @@
24d2be62b48be63f31093585f52f7887 dd8f33d81858978c4fec0eb3c2f0acb768028a10d80428417aae91bf86b343d3