diff --git a/trabalho5/forbidden.html b/trabalho5/forbidden.html new file mode 100644 index 0000000..b92657d --- /dev/null +++ b/trabalho5/forbidden.html @@ -0,0 +1,57 @@ + + + + Forbidden + + + +
+

403 Forbidden

+

Access to the requested resource is forbidden.

+

Contact support.

+
+ + diff --git a/trabalho5/proxy.py b/trabalho5/proxy.py index db0fb1e..ec17bb6 100644 --- a/trabalho5/proxy.py +++ b/trabalho5/proxy.py @@ -41,7 +41,6 @@ LOGGING = { } config.dictConfig(LOGGING) - class Logger: _instance = None def __init__(self): @@ -51,7 +50,7 @@ class Logger: self.logger = logging.getLogger('PythonProxy') self.logger.info('Initiating Proxy logger!') - def log(self, message:str): + def info(self, message:str): self.logger.info(message) def critical(self, message:str): @@ -113,9 +112,9 @@ def connectionHandle(client_socket, client_address): 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(16 * 1024) + # if "CONNECT" in raw_request: + # client_socket.sendall(b"HTTP/1.1 200 Connection Established\r\n\r\n") + # request = client_socket.recv(16 * 1024) request_url = raw_request.split(' ')[1] request_host = "" @@ -134,11 +133,11 @@ def connectionHandle(client_socket, client_address): request_host = request_host.split(':')[0] if "monitorando" in request_url: - body = "403 Acesso não autorizado! " + 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.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - 403") + logger.info(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - 403 Forbidden") return server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -146,7 +145,7 @@ def connectionHandle(client_socket, client_address): server_socket.send(request) while True: - triple = select.select([client_socket, server_socket], [], [], 20)[0] + triple = select.select([client_socket, server_socket], [], [], 10)[0] if not len(triple): break try: @@ -159,9 +158,9 @@ 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.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - {status_code}") + logger.info(f"PROXY [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - {status_code}") except UnicodeDecodeError: - logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]") + logger.info(f"PROXY [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]") pass client_socket.send(data) @@ -182,28 +181,28 @@ def verify_code_integrity(): import sys content = None - md5_hash = None + shas256_hash = None with open(sys.argv[0], 'rb') as f: content = f.read() - with open('proxy.py.md5', 'r') as f: - md5_hash = f.read() + with open('proxy.sha256sum', 'r') as f: + shas256_hash = f.read() - hash = hashlib.md5(content).hexdigest() + hash = hashlib.sha256(content).hexdigest() - if hash != md5_hash: - print('ERROR: proxy.py has been tampered with!') + if hash != shas256_hash: + print('ERRO: Código do proxy está diferente!') exit(1) - print('[Code Integrity] proxy.py is verified!') + print('Arquivo do proxy verificado!') if __name__ == '__main__': - verify_code_integrity() + # verify_code_integrity() try: ser = Server(host="0.0.0.0", port=8080) ser.start() except KeyboardInterrupt: - print("Shutting down...") + print("Desligando proxy...") exit(0) diff --git a/trabalho5/proxy.py.md5 b/trabalho5/proxy.py.md5 deleted file mode 100644 index 8609951..0000000 --- a/trabalho5/proxy.py.md5 +++ /dev/null @@ -1 +0,0 @@ -bd693dcd1e1a0f178fcd0cf0aa50efaf \ No newline at end of file diff --git a/trabalho5/proxy.sha256sum b/trabalho5/proxy.sha256sum new file mode 100644 index 0000000..d71a27e --- /dev/null +++ b/trabalho5/proxy.sha256sum @@ -0,0 +1 @@ +24d2be62b48be63f31093585f52f7887 \ No newline at end of file