sandbox: self-healing tunnel (tunnel_loop), forced tool description, fallback to tool output; e2e via live bot returns 2**64
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
# tunnel_loop.py — держит reverse-туннель home:48971 -> VPS:48979 бесконечно.
|
||||
# ssh при обрыве выходит (ExitOnForwardFailure), цикл поднимает заново через 2с.
|
||||
# Запуск: python tunnel_loop.py (супервизор спавнит отсоединённо)
|
||||
import os, subprocess, sys, time
|
||||
|
||||
VPS = "root@2.27.98.196"
|
||||
FWD = "48979:127.0.0.1:48971"
|
||||
CMD = r"C:\Windows\System32\cmd.exe"
|
||||
HOME_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
LOOPLOG = os.path.join(HOME_DIR, "tunnel_loop.log")
|
||||
OPTS = ("-o StrictHostKeyChecking=no -o BatchMode=yes -o ServerAliveInterval=15 "
|
||||
"-o ServerAliveCountMax=2 -o ExitOnForwardFailure=yes -o ConnectTimeout=10")
|
||||
|
||||
if "USERPROFILE" not in os.environ:
|
||||
os.environ["USERPROFILE"] = r"C:\Users\User"
|
||||
os.environ["HOMEDRIVE"] = "C:"
|
||||
os.environ["HOMEPATH"] = r"\Users\User"
|
||||
os.environ["HOME"] = r"C:\Users\User"
|
||||
|
||||
def log(m):
|
||||
with open(LOOPLOG, "a", encoding="utf-8") as f:
|
||||
f.write("%s %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), m))
|
||||
|
||||
def main():
|
||||
log("loop start pid=%d" % os.getpid())
|
||||
fails = 0
|
||||
while True:
|
||||
t0 = time.time()
|
||||
r = subprocess.run([CMD, "/c", "ssh %s -N -R %s %s" % (OPTS, FWD, VPS)],
|
||||
cwd=HOME_DIR, stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
dur = time.time() - t0
|
||||
if dur < 20:
|
||||
fails += 1
|
||||
else:
|
||||
fails = 0
|
||||
log("ssh exit rc=%s after %.0fs (fails=%d)" % (r.returncode, dur, fails))
|
||||
time.sleep(2 if fails < 5 else 15)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user