Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,19 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
"""
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
# ------------------------------------------------
|
21 |
-
async def process_request(path, request_headers):
|
22 |
-
"""
|
23 |
-
process_request 用于处理非 WebSocket 升级请求。
|
24 |
-
如果请求头中没有 Upgrade:websocket,则认为是普通 HTTP 请求,
|
25 |
-
返回状态码200及简单文本“网站运行中”。
|
26 |
-
如果是 WebSocket 升级请求,则返回 None 由后续 WebSocket 处理。
|
27 |
-
"""
|
28 |
-
# 检查是否为 WebSocket 升级请求(Upgrade 头部存在且值为 websocket)
|
29 |
-
if request_headers.get("Upgrade", "").lower() == "websocket":
|
30 |
-
return None # 表示继续执行 WebSocket 握手
|
31 |
-
# 构造 HTTP 响应内容
|
32 |
-
body = "网站运行中".encode("utf-8")
|
33 |
-
headers = [
|
34 |
-
("Content-Type", "text/plain; charset=utf-8"),
|
35 |
-
("Content-Length", str(len(body)))
|
36 |
-
]
|
37 |
-
# 返回 HTTP 响应:状态码、头部、响应体
|
38 |
-
return 200, headers, body
|
39 |
-
|
40 |
-
# ------------------------------------------------
|
41 |
-
# 处理 WebSocket 连接的函数(原代码逻辑)
|
42 |
-
# ------------------------------------------------
|
43 |
-
async def handle_ws(websocket: websockets.WebSocketServerProtocol, path: str):
|
44 |
-
try:
|
45 |
-
# --- 1. 等待客户端发送连接请求信息 ---
|
46 |
-
msg = await websocket.recv()
|
47 |
-
req = json.loads(msg)
|
48 |
-
if req.get("cmd") != "connect":
|
49 |
-
await websocket.send(json.dumps({"cmd": "connect", "status": "error", "error": "invalid command"}))
|
50 |
-
return
|
51 |
-
|
52 |
-
dest_addr = req.get("address")
|
53 |
-
dest_port = req.get("port")
|
54 |
-
print(f"收到连接请求:目标 {dest_addr}:{dest_port}")
|
55 |
-
|
56 |
-
# --- 2. 尝试建立到目标的TCP连接 ---
|
57 |
-
try:
|
58 |
-
reader, writer = await asyncio.open_connection(dest_addr, dest_port)
|
59 |
-
except Exception as e:
|
60 |
-
error_msg = f"连接目标失败:{e}"
|
61 |
-
print(error_msg)
|
62 |
-
await websocket.send(json.dumps({"cmd": "connect", "status": "error", "error": str(e)}))
|
63 |
-
return
|
64 |
-
|
65 |
-
# --- 3. 回复客户端连接成功 ---
|
66 |
-
await websocket.send(json.dumps({"cmd": "connect", "status": "ok"}))
|
67 |
-
|
68 |
-
# --- 4. 双向转发数据(WebSocket <--> 目标TCP连接) ---
|
69 |
-
async def ws_to_remote():
|
70 |
-
try:
|
71 |
-
async for message in websocket:
|
72 |
-
# 如果收到的是文本消息,则转为二进制数据发送给目标
|
73 |
-
if isinstance(message, str):
|
74 |
-
data = message.encode()
|
75 |
-
else:
|
76 |
-
data = message
|
77 |
-
writer.write(data)
|
78 |
-
await writer.drain()
|
79 |
-
except Exception as e:
|
80 |
-
print("ws_to_remote 出现异常:", e)
|
81 |
-
writer.close()
|
82 |
-
|
83 |
-
async def remote_to_ws():
|
84 |
-
try:
|
85 |
-
while True:
|
86 |
-
data = await reader.read(1024)
|
87 |
-
if not data:
|
88 |
-
break
|
89 |
-
await websocket.send(data)
|
90 |
-
except Exception as e:
|
91 |
-
print("remote_to_ws 出现异常:", e)
|
92 |
-
await websocket.close()
|
93 |
-
|
94 |
-
# 同时运行两个数据转发任务,实现全双工通信
|
95 |
-
await asyncio.gather(ws_to_remote(), remote_to_ws())
|
96 |
-
|
97 |
-
except Exception as e:
|
98 |
-
print("handle_ws 出现异常:", e)
|
99 |
-
|
100 |
-
# ------------------------------------------------
|
101 |
-
# 主函数,启动服务器
|
102 |
-
# ------------------------------------------------
|
103 |
-
async def main():
|
104 |
-
# 使用 websockets.serve 启动服务器,绑定 process_request 参数处理 HTTP 请求
|
105 |
-
ws_server = await websockets.serve(
|
106 |
-
handle_ws, # WebSocket 连接处理函数
|
107 |
-
"0.0.0.0", # 监听所有网卡
|
108 |
-
7860, # 监听端口8765(可自行修改)
|
109 |
-
process_request=process_request # 用于处理非 WebSocket 请求
|
110 |
-
)
|
111 |
-
print("服务器已启动,监听端口8765,支持 HTTP 和 WebSocket 请求")
|
112 |
-
await ws_server.wait_closed()
|
113 |
-
|
114 |
-
if __name__ == '__main__':
|
115 |
-
asyncio.run(main())
|
|
|
1 |
+
# app.py
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
|
6 |
+
if __name__ == "__main__":
|
7 |
+
# 第一步:给可执行文件赋予执行权限
|
8 |
+
os.system("chmod +x socks5_http")
|
9 |
+
|
10 |
+
# 第二步:以指定端口7860启动 .NET 7 自包含应用
|
11 |
+
# 如果想兼容Hugging Face自动映射端口,可改为:
|
12 |
+
# port = os.environ.get("PORT", "7860")
|
13 |
+
# p = subprocess.Popen(["./socks5_http", "--urls", f"http://0.0.0.0:{port}"])
|
14 |
+
p = subprocess.Popen(["./socks5_http", "--urls", "http://0.0.0.0:7860"])
|
15 |
+
|
16 |
+
# 第三步:阻塞当前脚本,让进程保持运行状态
|
17 |
+
# 如果脚本结束,Hugging Face容器会退出
|
18 |
+
while True:
|
19 |
+
time.sleep(60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|