File size: 1,854 Bytes
4ba4388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import subprocess
import time
import sys
import os

def run_background():
    print("🚀 正在后台启动服务...")
    
    # 1. 启动服务器 (输出重定向到 server.log)
    server_log = open("server.log", "w")
    server_process = subprocess.Popen(
        [sys.executable, "server.py"],
        stdout=server_log,
        stderr=subprocess.STDOUT
    )
    print(f"✅ 服务器已启动 (PID: {server_process.pid})")
    
    # 2. 等待服务器就绪
    print("⏳ 等待服务器启动 (约10秒)...")
    time.sleep(10)
    
    # 3. 启动 Cloudflare 隧道
    if not os.path.exists("./cloudflared"):
        print("❌ 未找到 cloudflared,请先运行下载命令")
        return

    print("🌐 正在建立公网连接...")
    tunnel_process = subprocess.Popen(
        ["./cloudflared", "tunnel", "--url", "http://localhost:8000", "--no-autoupdate"],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True
    )
    
    # 4. 读取并打印访问链接
    print("🔍 正在获取访问地址...")
    found_url = False
    while True:
        line = tunnel_process.stdout.readline()
        if not line:
            break
        
        # 打印隧道日志以便调试
        # print(f"[Tunnel] {line.strip()}")
        
        if "trycloudflare.com" in line:
            import re
            url_match = re.search(r"https?://[\w\.-]+trycloudflare\.com", line)
            if url_match:
                print("\n" + "="*50)
                print("🎉 成功!请访问以下地址:")
                print(f"👉 {url_match.group(0)}")
                print("="*50 + "\n")
                found_url = True
                break
    
    if not found_url:
        print("⚠️ 未能获取到公网地址,请检查 server.log")

if __name__ == "__main__":
    run_background()