|
| 1 | +import ssl |
| 2 | +import string |
| 3 | +import random |
| 4 | +import secrets |
| 5 | +from util.instant_cert_ctx import InstantCertServerSideCtx |
| 6 | +from util.proxy_protocols import struct_pure_tcp_trojan |
| 7 | +from util.tls_parser import parse_tls_client_hello |
| 8 | +from util.timer import Timer |
| 9 | +from transports.wrapped_transport import WrappedTransport |
| 10 | +from transports.tcp_stream_transport import TcpStreamTransport |
| 11 | +from tcp_relays.final_relays.bypass_relay import BypassRelay |
| 12 | +from tcp_relays.final_relays.one_repeater import OneRepeater |
| 13 | +from tcp_relays.final_relays.domain_fronter import DomainFronter |
| 14 | +from initialize import issuer_private_key_pass, issuer_private_key_path, issuer_cert_path, \ |
| 15 | + main_gateway_host, \ |
| 16 | + main_gateway_port, main_gateway_trojan_hashed_password, bypass_gateway_host, bypass_gateway_port, \ |
| 17 | + bypass_gateway_trojan_hashed_password, local_connect_timeout, \ |
| 18 | + loopback_connect_host, listen_port, upgrade_incoming_timeout, upgrade_incoming_handshake_timeout, \ |
| 19 | + upgrade_incoming_shutdown_timeout, others_connect_upgrade_timeout, others_connect_upgrade_handshake_timeout, \ |
| 20 | + others_connect_upgrade_shutdown_timeout, incoming_write_timeout, outgoing_write_timeout, \ |
| 21 | + private_domain_front_trojan_hashed_password, fake_sni, fake_sni_tld, pure_fake_sni_length_min, \ |
| 22 | + pure_fake_sni_length_max, incoming_wait_for_cancel_timeout, instant_certificate_temp_file_path |
| 23 | + |
| 24 | + |
| 25 | +class Controller: |
| 26 | + icc = InstantCertServerSideCtx(issuer_cert_path, issuer_private_key_path, issuer_private_key_pass, |
| 27 | + instant_certificate_temp_file_path) |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def get_fake_sni(): |
| 31 | + if fake_sni != "random": |
| 32 | + return fake_sni |
| 33 | + n = random.randint(pure_fake_sni_length_min, pure_fake_sni_length_max) |
| 34 | + return (''.join(secrets.choice(string.ascii_lowercase + string.digits) for _ in range(n))) + fake_sni_tld |
| 35 | + |
| 36 | + @classmethod |
| 37 | + async def on_repeater_domain_front_request(cls, incoming_tp: WrappedTransport[TcpStreamTransport], |
| 38 | + incoming_parsed_tj, incoming_payload: bytes): |
| 39 | + |
| 40 | + try: |
| 41 | + parsed_tls = parse_tls_client_hello(incoming_payload) |
| 42 | + except Exception: |
| 43 | + gateway_trojan = struct_pure_tcp_trojan(bypass_gateway_trojan_hashed_password, |
| 44 | + incoming_parsed_tj["remote_address_type"], |
| 45 | + incoming_parsed_tj["remote_address"], |
| 46 | + incoming_parsed_tj["remote_port"]) |
| 47 | + bypass_relay = BypassRelay(incoming_tp, None, bypass_gateway_host, bypass_gateway_port, |
| 48 | + gateway_trojan, 3, incoming_payload, |
| 49 | + None, None, |
| 50 | + list(), bytearray(), bytearray(), |
| 51 | + None, None, Timer(), local_connect_timeout, incoming_write_timeout, |
| 52 | + outgoing_write_timeout) |
| 53 | + print("Non-TLS Request Bypassed", incoming_parsed_tj["remote_address_type"], incoming_parsed_tj["remote_address"], |
| 54 | + incoming_parsed_tj["remote_port"], incoming_payload) |
| 55 | + await bypass_relay.main_loop(incoming_tp) |
| 56 | + incoming_tp.sync_close() |
| 57 | + return |
| 58 | + else: |
| 59 | + gateway_trojan = struct_pure_tcp_trojan(private_domain_front_trojan_hashed_password, |
| 60 | + incoming_parsed_tj["remote_address_type"], |
| 61 | + incoming_parsed_tj["remote_address"], |
| 62 | + incoming_parsed_tj["remote_port"]) |
| 63 | + one_repeater_relay = OneRepeater(incoming_tp, None, loopback_connect_host, listen_port, |
| 64 | + gateway_trojan, -1, incoming_payload, |
| 65 | + None, None, |
| 66 | + list(), bytearray(), bytearray(), |
| 67 | + None, None, Timer(), local_connect_timeout, incoming_write_timeout, |
| 68 | + outgoing_write_timeout) |
| 69 | + await one_repeater_relay.main_loop(incoming_tp) |
| 70 | + incoming_tp.sync_close() |
| 71 | + return |
| 72 | + |
| 73 | + @classmethod |
| 74 | + async def on_private_domain_front_request(cls, incoming_tp: WrappedTransport[TcpStreamTransport], |
| 75 | + incoming_parsed_tj, incoming_payload: bytes): |
| 76 | + |
| 77 | + try: |
| 78 | + parsed_tls = parse_tls_client_hello(incoming_payload) |
| 79 | + except Exception: |
| 80 | + print("!!!CRITICAL WARNING!!! non-tls in private.", incoming_parsed_tj, incoming_payload) |
| 81 | + incoming_tp.sync_close() |
| 82 | + return |
| 83 | + gateway_trojan = struct_pure_tcp_trojan(main_gateway_trojan_hashed_password, |
| 84 | + incoming_parsed_tj["remote_address_type"], |
| 85 | + incoming_parsed_tj["remote_address"], |
| 86 | + incoming_parsed_tj["remote_port"]) |
| 87 | + upgrade_incoming_magic = {"in_alpn": parsed_tls["alpn"], "in_sni": parsed_tls["sni"], "icc": cls.icc, |
| 88 | + "chosen_alpn": -1, "wait_for_cancel_timeout": incoming_wait_for_cancel_timeout, |
| 89 | + "remote_address_type": incoming_parsed_tj["remote_address_type"], |
| 90 | + "remote_address": incoming_parsed_tj["remote_address"], |
| 91 | + "repeat_command": incoming_payload, "timeout": upgrade_incoming_timeout, |
| 92 | + "sslcontext": -1, "kwargs": {"server_hostname": None, |
| 93 | + "ssl_handshake_timeout": upgrade_incoming_handshake_timeout, |
| 94 | + "ssl_shutdown_timeout": upgrade_incoming_shutdown_timeout}} |
| 95 | + upgrade_outgoing_magic = {"sslcontext": -1, "check_hostname": False, "verify_mode": ssl.CERT_NONE, |
| 96 | + "cadata": None, |
| 97 | + "out_alpn": parsed_tls["alpn"], "timeout": others_connect_upgrade_timeout, |
| 98 | + "kwargs": {"server_hostname": cls.get_fake_sni(), |
| 99 | + "ssl_handshake_timeout": others_connect_upgrade_handshake_timeout, |
| 100 | + "ssl_shutdown_timeout": others_connect_upgrade_shutdown_timeout}} |
| 101 | + domain_front_relay = DomainFronter(incoming_tp, None, main_gateway_host, main_gateway_port, |
| 102 | + gateway_trojan, -1, None, |
| 103 | + upgrade_incoming_magic, upgrade_outgoing_magic, |
| 104 | + list(), bytearray(), bytearray(), |
| 105 | + None, None, Timer(), local_connect_timeout, incoming_write_timeout, |
| 106 | + outgoing_write_timeout) |
| 107 | + |
| 108 | + await domain_front_relay.main_loop(incoming_tp) |
| 109 | + incoming_tp.sync_close() |
| 110 | + return |
0 commit comments