Upload back_end_proxyserver_159.py
Browse files- back_end_proxyserver_159.py +78 -0
back_end_proxyserver_159.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""back_end_proxyserver.159
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1RfrGjeKUufPga3A-q4wwDAN52Xc09oIc
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import socket
|
| 11 |
+
import threading
|
| 12 |
+
|
| 13 |
+
BUFFER_SIZE = 4096
|
| 14 |
+
LOCAL_HOST = '127.0.0.1'
|
| 15 |
+
LOCAL_PORT = 9999
|
| 16 |
+
|
| 17 |
+
def handle_client(client_socket):
|
| 18 |
+
request = client_socket.recv(BUFFER_SIZE)
|
| 19 |
+
print(f'Received request: {request}')
|
| 20 |
+
|
| 21 |
+
request_line = request.split(b'\n')[0]
|
| 22 |
+
url = request_line.split(b' ')[1]
|
| 23 |
+
|
| 24 |
+
http_pos = url.find(b'://')
|
| 25 |
+
if http_pos == -1:
|
| 26 |
+
temp = url
|
| 27 |
+
else:
|
| 28 |
+
temp = url[(http_pos + 3):]
|
| 29 |
+
|
| 30 |
+
port_pos = temp.find(b':')
|
| 31 |
+
|
| 32 |
+
server_pos = temp.find(b'/')
|
| 33 |
+
if server_pos == -1:
|
| 34 |
+
server_pos = len(temp)
|
| 35 |
+
|
| 36 |
+
server = ""
|
| 37 |
+
port = -1
|
| 38 |
+
if port_pos == -1 or server_pos < port_pos:
|
| 39 |
+
port = 80
|
| 40 |
+
server = temp[:server_port]
|
| 41 |
+
else:
|
| 42 |
+
port = int((temp[(port_pos +1):])[:server_pos - port_pos - 1])
|
| 43 |
+
server = temp[:port_pos]
|
| 44 |
+
|
| 45 |
+
print(f'Connecting to {server.decode("utf-8")} on port {port}')
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
| 49 |
+
proxy_socket.connect((server, port))
|
| 50 |
+
proxy_socket.send(request)
|
| 51 |
+
|
| 52 |
+
while True:
|
| 53 |
+
data = proxy_socket.recv(BUFFER_SIZE)
|
| 54 |
+
if not data:
|
| 55 |
+
break
|
| 56 |
+
client_socket.send(data)
|
| 57 |
+
|
| 58 |
+
proxy_socket.close()
|
| 59 |
+
client_socket.close()
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f'Error: {e}')
|
| 62 |
+
proxy_socket.close()
|
| 63 |
+
client_socket.close()
|
| 64 |
+
|
| 65 |
+
def start_server():
|
| 66 |
+
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
| 67 |
+
server.bind((LOCAL_HOST, LOCAL_PORT))
|
| 68 |
+
server.listen(5)
|
| 69 |
+
print(f'[*] Listening on {LOCAL_HOST}:{LOCAL_PORT}')
|
| 70 |
+
|
| 71 |
+
while True:
|
| 72 |
+
client_socket, addr = server.accept()
|
| 73 |
+
print(f'[*] Accepted connection from {addr[0]}:{addr[1]}')
|
| 74 |
+
client_handler = threading.Thread(target=handle_cient, args=(client_socket,))
|
| 75 |
+
client_handler.start()
|
| 76 |
+
|
| 77 |
+
if __name__ == '__main__':
|
| 78 |
+
start_server()
|