Update main.py
Browse files
main.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import uvicorn
|
| 2 |
-
from fastapi import FastAPI, Request, WebSocket
|
| 3 |
from fastapi.responses import HTMLResponse, StreamingResponse
|
| 4 |
import httpx
|
| 5 |
import asyncio
|
|
@@ -10,29 +10,57 @@ client = httpx.AsyncClient(timeout=None)
|
|
| 10 |
|
| 11 |
@app.get("/", response_class=HTMLResponse)
|
| 12 |
async def root():
|
| 13 |
-
return "<h1>SYSTEM
|
| 14 |
|
|
|
|
| 15 |
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
|
| 16 |
-
async def
|
| 17 |
if not path or path == "/": return await root()
|
| 18 |
target_url = f"http://127.0.0.1:8080/{path}"
|
| 19 |
headers = dict(request.headers)
|
| 20 |
headers["host"] = "127.0.0.1:8080"
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
resp = await client.send(req, stream=True)
|
| 23 |
return StreamingResponse(resp.aiter_raw(), status_code=resp.status_code, headers=dict(resp.headers))
|
| 24 |
|
|
|
|
| 25 |
@app.websocket("/{path:path}")
|
| 26 |
-
async def
|
| 27 |
await websocket.accept()
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
if __name__ == "__main__":
|
| 38 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import uvicorn
|
| 2 |
+
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
|
| 3 |
from fastapi.responses import HTMLResponse, StreamingResponse
|
| 4 |
import httpx
|
| 5 |
import asyncio
|
|
|
|
| 10 |
|
| 11 |
@app.get("/", response_class=HTMLResponse)
|
| 12 |
async def root():
|
| 13 |
+
return "<body style='background:#000;color:#0f0;text-align:center;'><h1>>> SYSTEM READY <<</h1></body>"
|
| 14 |
|
| 15 |
+
# PROXY HTTP (Aset Terminal)
|
| 16 |
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
|
| 17 |
+
async def proxy_http(request: Request, path: str):
|
| 18 |
if not path or path == "/": return await root()
|
| 19 |
target_url = f"http://127.0.0.1:8080/{path}"
|
| 20 |
headers = dict(request.headers)
|
| 21 |
headers["host"] = "127.0.0.1:8080"
|
| 22 |
+
headers["origin"] = "http://127.0.0.1:8080" # Manipulasi Origin
|
| 23 |
+
|
| 24 |
+
req = client.build_request(method=request.method, url=target_url, headers=headers, params=request.query_params, content=await request.body())
|
| 25 |
resp = await client.send(req, stream=True)
|
| 26 |
return StreamingResponse(resp.aiter_raw(), status_code=resp.status_code, headers=dict(resp.headers))
|
| 27 |
|
| 28 |
+
# PROXY WEBSOCKET (Fix Reconnect Loop)
|
| 29 |
@app.websocket("/{path:path}")
|
| 30 |
+
async def proxy_ws(websocket: WebSocket, path: str):
|
| 31 |
await websocket.accept()
|
| 32 |
+
# PENTING: Target URL terminal lu
|
| 33 |
+
target_ws_url = f"ws://127.0.0.1:8080/{path}"
|
| 34 |
+
|
| 35 |
+
# SUNTIK HEADER: Kita bohongi ttyd biar dia nerima koneksi
|
| 36 |
+
extra_headers = [
|
| 37 |
+
("Origin", "http://127.0.0.1:8080"),
|
| 38 |
+
("Host", "127.0.0.1:8080")
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
async with websockets.connect(target_ws_url, extra_headers=extra_headers) as target_ws:
|
| 43 |
+
async def forward(source, destination):
|
| 44 |
+
try:
|
| 45 |
+
while True:
|
| 46 |
+
# Deteksi tipe data (teks/biner)
|
| 47 |
+
if hasattr(source, 'receive_text'): # Dari Client (FastAPI)
|
| 48 |
+
data = await source.receive_text()
|
| 49 |
+
await destination.send(data)
|
| 50 |
+
else: # Dari Server (ttyd)
|
| 51 |
+
data = await source.recv()
|
| 52 |
+
await destination.send_text(data)
|
| 53 |
+
except: pass
|
| 54 |
+
|
| 55 |
+
# Jalankan pipa dua arah
|
| 56 |
+
await asyncio.gather(
|
| 57 |
+
forward(websocket, target_ws),
|
| 58 |
+
forward(target_ws, websocket)
|
| 59 |
+
)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"WS Error: {e}")
|
| 62 |
+
finally:
|
| 63 |
+
await websocket.close()
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|