|
@@ -0,0 +1,89 @@
|
|
|
|
|
+"""
|
|
|
|
|
+使用 Cloudflare Tunnel 替代 ngrok
|
|
|
|
|
+"""
|
|
|
|
|
+import subprocess
|
|
|
|
|
+import time
|
|
|
|
|
+import json
|
|
|
|
|
+import requests
|
|
|
|
|
+
|
|
|
|
|
+def setup_cloudflare_tunnel(port=8101):
|
|
|
|
|
+ """设置 Cloudflare Tunnel"""
|
|
|
|
|
+
|
|
|
|
|
+ print("🔄 Setting up Cloudflare Tunnel...")
|
|
|
|
|
+
|
|
|
|
|
+ # 1. 下载 cloudflared
|
|
|
|
|
+ download_cmd = [
|
|
|
|
|
+ 'wget', '-O', '/tmp/cloudflared',
|
|
|
|
|
+ 'https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64'
|
|
|
|
|
+ ]
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ subprocess.run(download_cmd, check=True, env={
|
|
|
|
|
+ 'https_proxy': 'http://172.16.40.16:7280',
|
|
|
|
|
+ 'http_proxy': 'http://172.16.40.16:7280'
|
|
|
|
|
+ })
|
|
|
|
|
+ subprocess.run(['chmod', '+x', '/tmp/cloudflared'], check=True)
|
|
|
|
|
+ print("✅ Cloudflared downloaded")
|
|
|
|
|
+ except subprocess.CalledProcessError as e:
|
|
|
|
|
+ print(f"❌ Download failed: {e}")
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+ # 2. 启动临时隧道
|
|
|
|
|
+ tunnel_cmd = [
|
|
|
|
|
+ '/tmp/cloudflared', 'tunnel',
|
|
|
|
|
+ '--url', f'http://localhost:{port}',
|
|
|
|
|
+ '--no-autoupdate'
|
|
|
|
|
+ ]
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ print(f"🚀 Starting tunnel for port {port}...")
|
|
|
|
|
+ process = subprocess.Popen(
|
|
|
|
|
+ tunnel_cmd,
|
|
|
|
|
+ stdout=subprocess.PIPE,
|
|
|
|
|
+ stderr=subprocess.PIPE,
|
|
|
|
|
+ text=True,
|
|
|
|
|
+ env={
|
|
|
|
|
+ 'https_proxy': 'http://172.16.40.16:7280',
|
|
|
|
|
+ 'http_proxy': 'http://172.16.40.16:7280'
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ # 等待隧道启动并获取 URL
|
|
|
|
|
+ for _ in range(30): # 等待最多30秒
|
|
|
|
|
+ if process.poll() is not None:
|
|
|
|
|
+ stdout, stderr = process.communicate()
|
|
|
|
|
+ print(f"❌ Process failed: {stderr}")
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+ time.sleep(1)
|
|
|
|
|
+
|
|
|
|
|
+ # 尝试从输出中提取 URL
|
|
|
|
|
+ try:
|
|
|
|
|
+ # 读取一些输出
|
|
|
|
|
+ output = process.stdout.readline()
|
|
|
|
|
+ if 'trycloudflare.com' in output:
|
|
|
|
|
+ url = output.split()[-1]
|
|
|
|
|
+ print(f"✅ Tunnel URL: {url}")
|
|
|
|
|
+ return url, process
|
|
|
|
|
+ except:
|
|
|
|
|
+ continue
|
|
|
|
|
+
|
|
|
|
|
+ print("⏰ Timeout waiting for tunnel URL")
|
|
|
|
|
+ process.terminate()
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(f"❌ Failed to start tunnel: {e}")
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ result = setup_cloudflare_tunnel(8101)
|
|
|
|
|
+ if result:
|
|
|
|
|
+ url, process = result
|
|
|
|
|
+ print(f"🌐 Your service is now accessible at: {url}")
|
|
|
|
|
+ print("Press Ctrl+C to stop the tunnel")
|
|
|
|
|
+ try:
|
|
|
|
|
+ process.wait()
|
|
|
|
|
+ except KeyboardInterrupt:
|
|
|
|
|
+ print("\n🛑 Stopping tunnel...")
|
|
|
|
|
+ process.terminate()
|