fix: RDP enable auto-health-restore on handshake failure, anti-Huorong firewall rule

This commit is contained in:
hmo
2026-07-22 16:51:52 +08:00
parent d7b2ee0460
commit 8a27d8e847
+70
View File
@@ -241,6 +241,69 @@ def _rdp_tunnel_pid():
pass pass
return None 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(): def _rdp_enable():
import subprocess as _sp, winreg as _wr, time as _t import subprocess as _sp, winreg as _wr, time as _t
try: try:
@@ -254,6 +317,13 @@ def _rdp_enable():
except Exception: except Exception:
pass pass
_rdp_kill_tunnel() _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: try:
# ExitOnForwardFailure=yes: if remote port 8080 is already in use or # ExitOnForwardFailure=yes: if remote port 8080 is already in use or
# sshd rejects the -R forwarding, ssh exits immediately instead of # sshd rejects the -R forwarding, ssh exits immediately instead of