MCPcopy Create free account
hub / github.com/WavesMan/Disable-automatic-Windows-update / FirewallService

Class FirewallService

python/core/services.py:371–449  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

369
370
371class FirewallService:
372 # NOTE: 使用 netsh 控制所有防火墙配置文件开关,提高跨版本兼容性
373
374 def _batch(self, cmds: List[str]) -> Tuple[bool, List[str]]:
375 errors: List[str] = []
376 for cmd in cmds:
377 step_start = time.perf_counter()
378 log_event(logging.INFO, "firewall_command_started", "执行命令", action="firewall_command", status="started", cmd=cmd)
379 ok, out, err = run_cmd(cmd)
380 duration_ms = int((time.perf_counter() - step_start) * 1000)
381 if ok:
382 log_event(logging.INFO, "firewall_command_finished", "执行成功", action="firewall_command", status="ok", cmd=cmd, duration_ms=duration_ms)
383 else:
384 msg = err or out or "执行失败"
385 log_event(
386 logging.ERROR,
387 "firewall_command_finished",
388 f"执行失败: {msg}",
389 action="firewall_command",
390 status="failed",
391 cmd=cmd,
392 duration_ms=duration_ms,
393 error_type="CommandFailed",
394 error_message=msg,
395 )
396 errors.append(msg)
397 return (len(errors) == 0), errors
398
399 def disable(self) -> Result:
400 start = time.perf_counter()
401 log_event(logging.INFO, "firewall_disable_started", "开始停用防火墙", action="disable_firewall", status="started")
402 cmds = [
403 "netsh advfirewall set allprofiles state off",
404 "netsh advfirewall set domainprofile state off",
405 "netsh advfirewall set privateprofile state off",
406 "netsh advfirewall set publicprofile state off",
407 ]
408 ok, errors = self._batch(cmds)
409 duration_ms = int((time.perf_counter() - start) * 1000)
410 if ok:
411 log_event(logging.INFO, "firewall_disable_finished", "停用防火墙成功", action="disable_firewall", status="ok", duration_ms=duration_ms)
412 return Result(True, data={"status": "firewall_disabled"})
413 log_event(
414 logging.ERROR,
415 "firewall_disable_finished",
416 "停用防火墙失败",
417 action="disable_firewall",
418 status="failed",
419 duration_ms=duration_ms,
420 error_type="FIREWALL_DISABLE_FAILED",
421 error_message="; ".join(errors),
422 )
423 return Result(False, error={"code": "FIREWALL_DISABLE_FAILED", "message": "防火墙停用失败", "details": errors})
424
425 def enable(self) -> Result:
426 start = time.perf_counter()
427 log_event(logging.INFO, "firewall_enable_started", "开始恢复防火墙", action="enable_firewall", status="started")
428 cmds = [

Callers 1

make_servicesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected