Spaces:
Paused
Paused
Download app.py from proletcraft/server: direct link, hf CLI and curl.
- Browser
- Download file 5.05 kB
-
https://huggingface.co/spaces/proletcraft/server/resolve/main/app.py
- Command line
-
hf download hf://spaces/proletcraft/server/app.py
-
curl -L -o app.py https://huggingface.co/spaces/proletcraft/server/resolve/main/app.py
5.05 kB
| import asyncio | |
| import struct | |
| import uuid | |
| import os | |
| from aiohttp import web | |
| ALLOWED_UUIDS = { | |
| "91a77ee1-1918-66d1-398a-7ef36ab6f605", # leader | |
| "51741641-3ea7-4aba-a78b-9241b29e453a", # Artur | |
| "62a1f3e4-1205-48b0-b9b9-553a6ff50056", # Dima | |
| "3d1cb43d-6b7b-462e-8c72-2696aa491459", # RinatA | |
| "3315f393-8f7b-4983-8515-3b0326fa8084", # Tolyas | |
| "223b5f2f-4c17-4182-9aec-1acc28939da2" # Misha | |
| } | |
| index_html = "" | |
| if os.path.exists("index.html"): | |
| with open("index.html", "r", encoding="utf-8") as f: | |
| index_html = f.read() | |
| async def handle_http_or_ws(request): | |
| if request.headers.get("Upgrade", "").lower() == "websocket": | |
| ws = web.WebSocketResponse() | |
| await ws.prepare(request) | |
| return await handle_vless_ws(ws) | |
| return web.Response(text=index_html, content_type="text/html", headers={ | |
| "X-Powered-By": "ProletCraft HuggingFace Node" | |
| }) | |
| async def handle_vless_ws(ws): | |
| remote_reader = None | |
| remote_writer = None | |
| first_packet = True | |
| try: | |
| async for msg in ws: | |
| if msg.type == web.WSMsgType.BINARY: | |
| data = msg.data | |
| if first_packet: | |
| first_packet = False | |
| if len(data) < 24: | |
| await ws.close(code=1002, message=b"Short chunk") | |
| return ws | |
| user_uuid = str(uuid.UUID(bytes=data[1:17])) | |
| if user_uuid not in ALLOWED_UUIDS: | |
| await ws.close(code=1008, message=b"Unauthorized") | |
| return ws | |
| addon_len = data[17] | |
| offset = 18 + addon_len | |
| cmd = data[offset] # 1: TCP | |
| offset += 1 | |
| port = struct.unpack('>H', data[offset:offset+2])[0] | |
| offset += 2 | |
| addr_type = data[offset] | |
| offset += 1 | |
| if addr_type == 1: | |
| addr = ".".join(str(b) for b in data[offset:offset+4]) | |
| offset += 4 | |
| elif addr_type == 2: | |
| domain_len = data[offset] | |
| offset += 1 | |
| addr = data[offset:offset+domain_len].decode('utf-8') | |
| offset += domain_len | |
| elif addr_type == 3: | |
| addr = ":".join(f"{data[offset+i]:02x}{data[offset+i+1]:02x}" for i in range(0, 16, 2)) | |
| offset += 16 | |
| else: | |
| await ws.close(code=1002, message=b"Bad address type") | |
| return ws | |
| # Connect to destination | |
| try: | |
| remote_reader, remote_writer = await asyncio.wait_for( | |
| asyncio.open_connection(addr, port), timeout=10 | |
| ) | |
| except Exception as e: | |
| await ws.close(code=1006, message=str(e).encode('utf-8')) | |
| return ws | |
| # Send VLESS response: version 0, addons length 0 | |
| await ws.send_bytes(b'\x00\x00') | |
| # Send initial payload if present | |
| payload = data[offset:] | |
| if payload: | |
| remote_writer.write(payload) | |
| await remote_writer.drain() | |
| # Start background reader task from remote to websocket | |
| async def remote_to_ws(): | |
| try: | |
| while True: | |
| chunk = await remote_reader.read(16384) | |
| if not chunk: | |
| break | |
| await ws.send_bytes(chunk) | |
| except Exception: | |
| pass | |
| finally: | |
| if not ws.closed: | |
| await ws.close() | |
| asyncio.create_task(remote_to_ws()) | |
| else: | |
| if remote_writer: | |
| remote_writer.write(data) | |
| await remote_writer.drain() | |
| elif msg.type in (web.WSMsgType.CLOSE, web.WSMsgType.ERROR): | |
| break | |
| except Exception: | |
| pass | |
| finally: | |
| if remote_writer: | |
| try: | |
| remote_writer.close() | |
| await remote_writer.wait_closed() | |
| except Exception: | |
| pass | |
| if not ws.closed: | |
| await ws.close() | |
| return ws | |
| app = web.Application() | |
| app.router.add_get('/', handle_http_or_ws) | |
| app.router.add_get('/{tail:.*}', handle_http_or_ws) | |
| if __name__ == '__main__': | |
| port = int(os.environ.get('PORT', 7860)) | |
| print(f"Starting ProletCraft VLESS server on port {port}...") | |
| web.run_app(app, host='0.0.0.0', port=port) | |