Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,136 +3,116 @@ import os
|
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 7 |
-
# ู
ุญุฑู ุงุชุตุงู
|
| 8 |
-
#
|
| 9 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 10 |
-
import os, time, threading, socket,
|
| 11 |
-
|
| 12 |
CURRENT_SERVER = None
|
| 13 |
-
PORT = None
|
| 14 |
CONNECTED = threading.Event()
|
| 15 |
-
RELAY_SIO = None
|
| 16 |
|
| 17 |
-
|
| 18 |
-
def _load_candidates():
|
| 19 |
import peer_discovery as pd
|
| 20 |
importlib.reload(pd)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
servers = list(getattr(pd, "CENTRAL_REGISTRY_SERVERS", []))
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 31 |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
| 32 |
try:
|
| 33 |
-
s.bind((host, int(
|
| 34 |
return True
|
| 35 |
except OSError:
|
| 36 |
return False
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
"""
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
info = {
|
| 44 |
-
"node_id": os.getenv("NODE_ID", socket.gethostname()),
|
| 45 |
-
"ip": get_ip(),
|
| 46 |
-
"port": int(port),
|
| 47 |
-
}
|
| 48 |
-
r = requests.post(f"{server}/register", json=info, timeout=6)
|
| 49 |
-
r.raise_for_status() # ูุฑู
ู ุงุณุชุซูุงุก ูู ูุดู
|
| 50 |
-
return True
|
| 51 |
-
|
| 52 |
-
def _start_relay_client(server):
|
| 53 |
-
"""
|
| 54 |
-
ูุถุน Relay: ููุงุฉ Socket.IO ุฏุงุฆู
ุฉ ู
ุน ุงูุณูุฑูุฑ (ุนูู 443 ุบุงูุจูุง).
|
| 55 |
-
ูุง ุชุญุชุงุฌ ุฃู ู
ููุฐ inbound. ุฅุฐุง ุงูุณูุฑูุฑ ูุฏุนู
relayุ ููุนุชุจุฑ ุงุชุตุงู ุฎุงุฑุฌู ูุงุฌุญ.
|
| 56 |
-
"""
|
| 57 |
-
global RELAY_SIO
|
| 58 |
-
import socketio # python-socketio client
|
| 59 |
-
sio = socketio.Client(reconnection=True, reconnection_attempts=0)
|
| 60 |
-
|
| 61 |
-
node_id = os.getenv("NODE_ID", socket.gethostname())
|
| 62 |
-
|
| 63 |
-
@sio.event
|
| 64 |
-
def connect():
|
| 65 |
-
print(f"๐ Relay connected to {server}")
|
| 66 |
-
sio.emit("register_node", {"node_id": node_id})
|
| 67 |
-
# ุงุนุชุจุฑ ุงูุงุชุตุงู ูุงุฌุญ ุฎุงุฑุฌููุง
|
| 68 |
-
# ููุจูู PORT ูุงุณุชุฎุฏุงู
LAN/RPC ู
ุญูู (ูู ุงุญุชุฌุชู). ุชุณุชุทูุน ุถุจุทู ูุงุญููุง.
|
| 69 |
-
|
| 70 |
-
@sio.event
|
| 71 |
-
def disconnect():
|
| 72 |
-
print("โ ๏ธ Relay disconnected; will auto-reconnectโฆ")
|
| 73 |
-
|
| 74 |
-
# ุฃู
ุซูุฉ ุฃุญุฏุงุซ (ุนุฏูู ุนูู ู
ุฒุงุฌู):
|
| 75 |
-
@sio.on("ping_node")
|
| 76 |
-
def _on_ping(msg):
|
| 77 |
-
sio.emit("pong_node", {"node_id": node_id, "t": time.time()})
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
def _connect_until_success():
|
| 89 |
-
global
|
| 90 |
backoff = 1
|
| 91 |
while True:
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
except Exception as e:
|
| 107 |
-
print(f"โ ๏ธ DIRECT failed on {s} / {p}: {e}")
|
| 108 |
-
|
| 109 |
-
# 2) ุฅุฐุง ูุดูุช ุงูู
ุจุงุดุฑุฉ ุนูู ุงููู โ ุฌุฑูุจ RELAY ููู ุณูุฑูุฑ ุจุงูุชุฑุชูุจ
|
| 110 |
-
for s in servers:
|
| 111 |
-
ok = _start_relay_client(s)
|
| 112 |
-
if ok:
|
| 113 |
-
MODE, CURRENT_SERVER = "relay", s
|
| 114 |
-
# PORT ุงุฎุชูุงุฑู ููุง (ูู LAN ููุท). ูู ุนูุฏู RPC ู
ุญููู ุดุบููู ุนูู ุฃู ู
ููุฐ ู
ุชุงุญ.
|
| 115 |
-
if ports:
|
| 116 |
-
# ุงุฎุชุฑ ุฃูู ู
ููุฐ ู
ุชุงุญ ู
ุญูููุง ููุชุดุบูู ุงูุฏุงุฎูู
|
| 117 |
-
for p in ports:
|
| 118 |
-
if _is_port_free(p):
|
| 119 |
-
PORT = int(p)
|
| 120 |
-
break
|
| 121 |
-
print(f"โ
RELAY connected via {s} (no inbound port needed)")
|
| 122 |
CONNECTED.set()
|
|
|
|
| 123 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
# ู
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
time.sleep(backoff)
|
| 127 |
-
backoff = min(backoff * 2,
|
| 128 |
|
| 129 |
def start_connect_loop():
|
| 130 |
threading.Thread(target=_connect_until_success, daemon=True).start()
|
| 131 |
|
| 132 |
-
# ุดุบูู ุญููุฉ ุงูุงุชุตุงู ู
ุจูุฑูุง:
|
| 133 |
-
start_connect_loop()
|
| 134 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 135 |
-
|
| 136 |
# ู
ุณุงุฑ ุงุฎุชุจุงุฑ ุณุฑูุน
|
| 137 |
@app.get("/")
|
| 138 |
def hello():
|
|
|
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 7 |
+
# ู
ุญุฑู ุงุชุตุงู: ุณูุฑูุฑ ุนุดูุงุฆู ู
ู peer_discovery + ู
ูุงูุฐ 0001..9999 ุจุงูุชุณูุณู
|
| 8 |
+
# ูุชููู ููุท ุนูุฏ ุฃูู ุชุณุฌูู ูุงุฌุญ
|
| 9 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 10 |
+
import os, time, threading, socket, requests, importlib, random, sys
|
| 11 |
+
|
| 12 |
CURRENT_SERVER = None
|
| 13 |
+
PORT = None
|
| 14 |
CONNECTED = threading.Event()
|
|
|
|
| 15 |
|
| 16 |
+
def _load_peer_discovery():
|
|
|
|
| 17 |
import peer_discovery as pd
|
| 18 |
importlib.reload(pd)
|
| 19 |
+
return pd
|
| 20 |
+
|
| 21 |
+
def _pick_random_server(pd):
|
| 22 |
+
# ุฅู ูุงู ูุฏูู pd.random_server() ุงุณุชุฎุฏู
ูุ ูุฅูุง ุงุฎุชุฑ ุนุดูุงุฆูุงู ู
ู ุงููุงุฆู
ุฉ
|
| 23 |
+
if hasattr(pd, "random_server"):
|
| 24 |
+
return pd.random_server()
|
| 25 |
servers = list(getattr(pd, "CENTRAL_REGISTRY_SERVERS", []))
|
| 26 |
+
if not servers:
|
| 27 |
+
raise RuntimeError("ูุง ุชูุฌุฏ ุณูุฑูุฑุงุช ูู peer_discovery.CENTRAL_REGISTRY_SERVERS")
|
| 28 |
+
random.shuffle(servers)
|
| 29 |
+
return servers[0]
|
| 30 |
+
|
| 31 |
+
def _get_local_ip(pd):
|
| 32 |
+
if hasattr(pd, "get_local_ip"):
|
| 33 |
+
return pd.get_local_ip()
|
| 34 |
+
# ุงุญุชูุงุท
|
| 35 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 36 |
+
try:
|
| 37 |
+
s.connect(("8.8.8.8", 80))
|
| 38 |
+
return s.getsockname()[0]
|
| 39 |
+
except Exception:
|
| 40 |
+
return "127.0.0.1"
|
| 41 |
+
finally:
|
| 42 |
+
s.close()
|
| 43 |
+
|
| 44 |
+
def _lan_port(pd):
|
| 45 |
+
return int(getattr(pd, "LAN_PORT", os.getenv("LAN_PORT", 7520)))
|
| 46 |
+
|
| 47 |
+
def _can_bind_low_ports():
|
| 48 |
+
# ุนูู ููููุณ: ุชุญุชุงุฌ root ููู
ูุงูุฐ <1024
|
| 49 |
+
if os.name != "nt":
|
| 50 |
+
try:
|
| 51 |
+
return os.geteuid() == 0 # type: ignore[attr-defined]
|
| 52 |
+
except Exception:
|
| 53 |
+
return False
|
| 54 |
+
return True # ูููุฏูุฒ ุนุงุฏุฉ ูุณู
ุญ
|
| 55 |
+
|
| 56 |
+
def _is_port_free(port, host="0.0.0.0"):
|
| 57 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 58 |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
| 59 |
try:
|
| 60 |
+
s.bind((host, int(port)))
|
| 61 |
return True
|
| 62 |
except OSError:
|
| 63 |
return False
|
| 64 |
|
| 65 |
+
def _try_register(server, port, node_id, ip):
|
| 66 |
+
payload = {"node_id": node_id, "ip": ip, "port": int(port)}
|
| 67 |
+
r = requests.post(f"{server}/register", json=payload, timeout=6)
|
| 68 |
+
r.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
def _sequential_ports(pd):
|
| 71 |
+
"""ููููุฏ 0001..9999 ู
ุน ู
ุฑุงุนุงุฉ ุงูู LAN_PORT ูุชุฎุทู <1024 ุฅุฐุง ุบูุฑ ู
ุณู
ูุญ."""
|
| 72 |
+
lan_p = _lan_port(pd)
|
| 73 |
+
start = 1 if _can_bind_low_ports() else 1025
|
| 74 |
+
for p in range(start, 10000):
|
| 75 |
+
if p == lan_p:
|
| 76 |
+
continue
|
| 77 |
+
yield p
|
| 78 |
|
| 79 |
def _connect_until_success():
|
| 80 |
+
global CURRENT_SERVER, PORT
|
| 81 |
backoff = 1
|
| 82 |
while True:
|
| 83 |
+
pd = _load_peer_discovery()
|
| 84 |
+
server = _pick_random_server(pd)
|
| 85 |
+
node_id = os.getenv("NODE_ID", socket.gethostname())
|
| 86 |
+
ip = _get_local_ip(pd)
|
| 87 |
+
print(f"๐ Trying server: {server}")
|
| 88 |
+
|
| 89 |
+
any_attempt = False
|
| 90 |
+
for p in _sequential_ports(pd):
|
| 91 |
+
any_attempt = True
|
| 92 |
+
if not _is_port_free(p):
|
| 93 |
+
continue
|
| 94 |
+
try:
|
| 95 |
+
_try_register(server, p, node_id, ip)
|
| 96 |
+
CURRENT_SERVER, PORT = server, int(p)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
CONNECTED.set()
|
| 98 |
+
print(f"โ
Connected to {server} with port {PORT:04d}")
|
| 99 |
return
|
| 100 |
+
except Exception as e:
|
| 101 |
+
# ุงุทุจุน ุนูู ูุชุฑุงุช ูุชุฌูุจ ุงูุถูุถุงุก
|
| 102 |
+
if p % 1000 == 0:
|
| 103 |
+
print(f"โฆstill scanning (last error on {server}:{p:04d}) -> {e}")
|
| 104 |
|
| 105 |
+
# ูู
ููุฌุญ ุฃู ู
ููุฐ ู
ุน ูุฐุง ุงูุณูุฑูุฑ โ ุฌุฑูุจ ุณูุฑูุฑ ุฌุฏูุฏ
|
| 106 |
+
if not any_attempt:
|
| 107 |
+
print("โ ๏ธ ูู
ุชูุฌุฑู ู
ุญุงููุงุช ู
ูุงูุฐ (ุชุญูู ู
ู ุตูุงุญูุงุช ุงูู
ูุงูุฐ ุงูู
ูุฎูุถุฉ).")
|
| 108 |
+
|
| 109 |
+
print(f"โป No success on {server}. Picking a new serverโฆ")
|
| 110 |
time.sleep(backoff)
|
| 111 |
+
backoff = min(backoff * 2, 10) # backoff ุจุณูุท ูุชุฎููู ุงูุถุบุท
|
| 112 |
|
| 113 |
def start_connect_loop():
|
| 114 |
threading.Thread(target=_connect_until_success, daemon=True).start()
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# ู
ุณุงุฑ ุงุฎุชุจุงุฑ ุณุฑูุน
|
| 117 |
@app.get("/")
|
| 118 |
def hello():
|