#717·trojan

Reproducing client request issues in Python

Author: TridaysCreated Jun 13, 2025Updated Jun 13, 2025
Labelsbug

English: I am using Python to reproduce a client request and want to connect to a node through socks to send the request. However, I encountered an issue where requesting HTTP is normal, but requesting HTTPS does return the following message. I kindly ask for your guidance

Chinese: 我使用python复现客户端请求,想通过socks连接节点发送请求,但是我遇到了一个问题,请求HTTP是正常的,但是请求HTTPS确返回如下信息,还请各位大佬指点

import socket
import ssl
import hashlib
from enum import Enum

class TrojanAddressType(Enum):
    IPV4 = 0x01
    DOMAIN = 0x03
    IPV6 = 0x04

def build_trojan_header(password, host, port):
    """构造Trojan协议头"""
    # SHA224哈希 (56字节十六进制)
    hash_hex = hashlib.sha224(password.encode()).hexdigest().encode()
    
    # 请求结构
    header = bytearray()
    header.extend(hash_hex)      # 密码哈希
    header.extend(b"\r\n")       # CRLF
    
    # SOCKS5样式请求
    header.append(0x01)          # CMD: CONNECT
    header.append(0x03)          # ATYP: 域名
    header.append(len(host))     # 域名长度
    header.extend(host.encode()) # 域名
    header.extend(port.to_bytes(2, 'big'))  # 端口
    header.extend(b"\r\n")       # CRLF
    
    return bytes(header)

def test_trojan_https():
    # 配置参数
    server = "sunflower.zhenji.me"
    port = 443
    password = "7411074112www"
    sni = "sunflower.zhenji.me"
    
    # 目标HTTPS网站
    target_host = "httpbin.org"
    target_port = 443
    
    try:
        # 1. 建立TCP连接
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(15)
        sock.connect((server, port))

        # 2. 建立TLS连接
        context = ssl.create_default_context()
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE
        ssl_sock = context.wrap_socket(sock, server_hostname=sni)
        
        # 3. 构造Trojan协议头
        trojan_header = build_trojan_header(password, target_host, target_port)
        
        # 4. 构造HTTPS请求 (注意: 这是被封装的实际请求)
        http_request = (
            f"GET /get HTTP/1.1\r\n"
            f"Host: {target_host}\r\n"
            f"Connection: close\r\n\r\n"
        ).encode()
        
        # 5. 组合发送 (Trojan头 + 实际请求)
        ssl_sock.sendall(trojan_header + http_request)
        
        # 6. 接收响应
        response = ssl_sock.recv(4096)
        print("响应结果:", response.decode())

    except Exception as e:
        print(f"连接失败: {str(e)}")
    finally:
        ssl_sock.close() if 'ssl_sock' in locals() else None

if __name__ == "__main__":
    test_trojan_https()

——————————————————————————————————————————————————HTTP PS C:\home\WorkSpace\Code\VSCode> & C:/home/app/Python/Python313/python.exe c:/home/WorkSpace/Code/VSCode/qoc2/test/a.py 响应结果: HTTP/1.1 200 OK Date: Fri, 13 Jun 2025 12:36:19 GMT Content-Type: application/json Content-Length: 197 Connection: close Server: gunicorn/19.9.0 Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true

{ "args": {}, "headers": { "Host": "httpbin.org", "X-Amzn-Trace-Id": "Root=1-684c1b42-122c0b312387a2cb1e389c94" }, "origin": "43.134.68.32", "url": "http://httpbin.org/get" } ——————————————————————————————————————————————————HTTPS PS C:\home\WorkSpace\Code\VSCode> & C:/home/app/Python/Python313/python.exe c:/home/WorkSpace/Code/VSCode/qoc2/test/a.py 响应结果: HTTP/1.1 400 Bad Request Server: awselb/2.0 Date: Fri, 13 Jun 2025 12:36:27 GMT Content-Type: text/html Content-Length: 220 Connection: close

400 The plain HTTP request was sent to HTTPS port

400 Bad Request

The plain HTTP request was sent to HTTPS port