Aqso commited on
Commit
2c2ff35
·
verified ·
1 Parent(s): 310459c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +19 -12
main.py CHANGED
@@ -10,32 +10,39 @@ client = httpx.AsyncClient(timeout=None)
10
 
11
  @app.get("/", response_class=HTMLResponse)
12
  async def root():
13
- return "<h1 style='color:green;text-align:center;'>[ SYSTEM ACTIVE ]</h1>"
14
 
 
15
  @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
16
  async def proxy_http(request: Request, path: str):
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
- req = client.build_request(method=request.method, url=target_url, headers=headers, params=request.query_params, content=await request.body())
23
- resp = await client.send(req, stream=True)
24
- return StreamingResponse(resp.aiter_raw(), status_code=resp.status_code, headers=dict(resp.headers))
 
 
 
25
 
 
26
  @app.websocket("/{path:path}")
27
  async def proxy_ws(websocket: WebSocket, path: str):
28
- # Dapatkan subprotocol (penting untuk ttyd)
29
- protocol = websocket.headers.get("sec-websocket-protocol")
30
- await websocket.accept(subprotocol=protocol)
31
 
32
  target_ws_url = f"ws://127.0.0.1:8080/{path}"
33
 
34
  try:
35
- # KUNCI: Jangan pake extra_headers sama sekali karena ttyd sudah pake flag -a
36
  async with websockets.connect(
37
- target_ws_url,
38
- subprotocols=[protocol] if protocol else None
 
39
  ) as target_ws:
40
 
41
  async def forward(source, destination):
 
10
 
11
  @app.get("/", response_class=HTMLResponse)
12
  async def root():
13
+ return "<body style='background:#000;color:#0f0;text-align:center;padding-top:100px;font-family:monospace;'><h1>[ SYSTEM ACTIVE ]</h1><p>Path: /terminal</p></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 = {k: v for k, v in request.headers.items() if k.lower() not in ["host", "origin"]}
21
+ headers["Host"] = "127.0.0.1:8080"
22
+ headers["Origin"] = "http://127.0.0.1:8080"
23
 
24
+ try:
25
+ req = client.build_request(method=request.method, url=target_url, headers=headers, params=request.query_params, content=await request.body())
26
+ resp = await client.send(req, stream=True)
27
+ return StreamingResponse(resp.aiter_raw(), status_code=resp.status_code, headers=dict(resp.headers))
28
+ except:
29
+ return HTMLResponse("Backend Offline", status_code=503)
30
 
31
+ # PROXY WEBSOCKET (Fix Reconnect & Cant Type)
32
  @app.websocket("/{path:path}")
33
  async def proxy_ws(websocket: WebSocket, path: str):
34
+ # Terima koneksi dengan subprotocol asli dari ttyd
35
+ subproto = websocket.headers.get("sec-websocket-protocol")
36
+ await websocket.accept(subprotocol=subproto)
37
 
38
  target_ws_url = f"ws://127.0.0.1:8080/{path}"
39
 
40
  try:
41
+ # KUNCI: Jangan pake extra_headers agar tidak Exit 1 di log
42
  async with websockets.connect(
43
+ target_ws_url,
44
+ subprotocols=[subproto] if subproto else None,
45
+ ping_interval=None # Agar tidak gampang putus
46
  ) as target_ws:
47
 
48
  async def forward(source, destination):