Spaces:
Paused
Paused
File size: 5,052 Bytes
83c74a5 | 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | 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)
|