mirror of
https://github.com/Justuser3310/jetwork.git
synced 2025-02-07 17:34:39 +00:00
83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
import http.server
|
|
import socketserver
|
|
import urllib.request
|
|
import logging
|
|
from os.path import exists
|
|
|
|
from db import *
|
|
base_url = read()['base_url']
|
|
|
|
# Логирование
|
|
from icecream import ic
|
|
ic.disable() # Выключить отладку
|
|
|
|
class Proxy(http.server.SimpleHTTPRequestHandler):
|
|
# Отключение всех сообщений (чтобы не забивать вывод)
|
|
def log_message(self, format, *args):
|
|
pass
|
|
def do_GET(self):
|
|
ic(f"Request for: {self.path}")
|
|
|
|
# 'js-check.jet/favicon.ico' -> '127.0.0.1:8000/favicon.ico'
|
|
target = self.path
|
|
domain = target[7:]
|
|
domain = domain[:domain.find('/')]
|
|
|
|
if not exists(f'cached/{domain}'):
|
|
self.send_error(404, f"Site not found")
|
|
return 404
|
|
|
|
# Если статичный
|
|
if read(f'cached/{domain}/config.json')['type'] == 'static':
|
|
target = f'{base_url}/{target[7:]}' # http://127.0.0.1:8000 / js-check.jet
|
|
# Если динамический
|
|
elif read(f'cached/{domain}/config.json')['type'] == 'dynamic':
|
|
port = read(f'cached/{domain}/config.json')['port']
|
|
target = f'http://bore.del.pw:{port}/{target.replace(f"http://{domain}/", "")}'
|
|
|
|
ic(f"Modded request: {target}")
|
|
|
|
# Направление запроса по адресу
|
|
try:
|
|
with urllib.request.urlopen(target) as response:
|
|
self.send_response(response.getcode())
|
|
self.send_header("Content-type", response.headers.get_content_type())
|
|
self.end_headers()
|
|
self.wfile.write(response.read())
|
|
except Exception as e:
|
|
self.send_error(500, f"Error: {str(e)}")
|
|
|
|
def do_POST(self):
|
|
ic(f"Request for: {self.path}")
|
|
|
|
if not exists(f'cached/{domain}'):
|
|
self.send_error(404, f"Site not found")
|
|
return 404
|
|
|
|
if read(f'cached/{domain}/config.json')['type'] == 'static':
|
|
target = f'{base_url}/{target[7:]}' # http://127.0.0.1:8000 / js-check.jet
|
|
elif read(f'cached/{domain}/config.json')['type'] == 'dynamic':
|
|
port = read(f'cached/{domain}/config.json')['port']
|
|
target = f'http://bore.del.pw:{port}/{target.replace(f"http://{domain}/", "")}'
|
|
|
|
ic(f"Modded request: {target}")
|
|
|
|
# Направление запроса по адресу
|
|
try:
|
|
content_length = int(self.headers['Content-Length'])
|
|
post_data = self.rfile.read(content_length)
|
|
req = urllib.request.Request(self.path, data=post_data, method='POST')
|
|
with urllib.request.urlopen(req) as response:
|
|
self.send_response(response.getcode())
|
|
self.send_header("Content-type", response.headers.get_content_type())
|
|
self.end_headers()
|
|
self.wfile.write(response.read())
|
|
except Exception as e:
|
|
self.send_error(500, f"Error: {str(e)}")
|
|
|
|
def web_proxy():
|
|
PORT = 8080
|
|
with socketserver.TCPServer(("", PORT), Proxy) as httpd:
|
|
print(f"HTTP proxy on port {PORT}")
|
|
httpd.serve_forever()
|