Files
kaoyan/studio/pool_service.py
T

279 lines
14 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Read Windows pool state without assuming elevated processes are unrelated."""
import base64
import ctypes
import json
import os
import shlex
import shutil
import subprocess
import tempfile
import threading
from ctypes import wintypes
from pathlib import Path
from urllib.parse import urlparse
import requests
PROBE = r"""
$ErrorActionPreference = 'Stop'
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
$result = @{}
foreach ($name in @('server', 'schedule', 'redis')) {
$pidPath = Join-Path $env:KAOYAN_POOL_PROJECT ".runtime\$name.pid"
$record = @{ pid = $null; exists = $false; invalid = $false }
if (Test-Path -LiteralPath $pidPath) {
$serviceId = 0
if (-not [int]::TryParse((Get-Content -LiteralPath $pidPath -Raw).Trim(), [ref]$serviceId) -or $serviceId -le 0) {
$record.invalid = $true
} else {
$record.pid = $serviceId
$process = Get-CimInstance Win32_Process -Filter "ProcessId=$serviceId"
if ($process) {
$record.exists = $true
$record.executable = $process.ExecutablePath
$record.command = $process.CommandLine
}
}
}
$result[$name] = $record
}
$result | ConvertTo-Json -Depth 4 -Compress
"""
RUN_SCRIPT = r"""
$ErrorActionPreference = 'Stop'
$utf8 = New-Object System.Text.UTF8Encoding($false)
[Console]::OutputEncoding = $utf8
$OutputEncoding = $utf8
try {
$modernArguments = $PSVersionTable.PSVersion -ge [version]'7.3'
if ($modernArguments) { $PSNativeCommandArgumentPassing = 'Standard' }
if (-not $modernArguments -and (Get-Content -LiteralPath $env:KAOYAN_POOL_SCRIPT -Raw -Encoding UTF8) -match '-c\s+\$\w+') {
throw '此代理池脚本需要 PowerShell 7.3 或更高版本,以正确传递 Python 命令中的引号。安装后重新启动工作台。'
}
& $env:KAOYAN_POOL_SCRIPT $env:KAOYAN_POOL_ACTION
if ($LASTEXITCODE -and $LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
} catch {
[Console]::Error.WriteLine($_.Exception.Message)
exit 1
}
"""
def _powershell():
# Prefer stable installed locations so the desktop shortcut works outside Codex.
candidates = []
if os.environ.get("ProgramFiles"):
candidates.append(Path(os.environ["ProgramFiles"]) / "PowerShell/7/pwsh.exe")
if os.environ.get("LOCALAPPDATA"):
candidates.append(Path(os.environ["LOCALAPPDATA"]) / "Microsoft/WindowsApps/pwsh.exe")
for candidate in candidates:
if candidate.is_file():
return str(candidate)
return shutil.which("pwsh.exe") or "powershell.exe"
def _native_process(pid):
"""Limited query access can identify elevation even when CIM has no command."""
if os.name != "nt":
return {}
kernel = ctypes.WinDLL("kernel32", use_last_error=True)
security = ctypes.WinDLL("advapi32", use_last_error=True)
kernel.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
kernel.OpenProcess.restype = wintypes.HANDLE
kernel.CloseHandle.argtypes = [wintypes.HANDLE]
kernel.QueryFullProcessImageNameW.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.LPWSTR, ctypes.POINTER(wintypes.DWORD)]
security.OpenProcessToken.argtypes = [wintypes.HANDLE, wintypes.DWORD, ctypes.POINTER(wintypes.HANDLE)]
security.GetTokenInformation.argtypes = [wintypes.HANDLE, ctypes.c_int, wintypes.LPVOID, wintypes.DWORD, ctypes.POINTER(wintypes.DWORD)]
handle = kernel.OpenProcess(0x1000, False, pid) # PROCESS_QUERY_LIMITED_INFORMATION
if not handle:
return {}
result = {}
try:
terminate_handle = kernel.OpenProcess(0x0001, False, pid)
result["can_terminate"] = bool(terminate_handle)
if terminate_handle:
kernel.CloseHandle(terminate_handle)
image = ctypes.create_unicode_buffer(32768)
length = wintypes.DWORD(len(image))
if kernel.QueryFullProcessImageNameW(handle, 0, image, ctypes.byref(length)):
result["executable"] = image.value
token = wintypes.HANDLE()
if security.OpenProcessToken(handle, 8, ctypes.byref(token)):
try:
elevation, size = wintypes.DWORD(), wintypes.DWORD()
if security.GetTokenInformation(token, 20, ctypes.byref(elevation), ctypes.sizeof(elevation), ctypes.byref(size)):
result["elevated"] = bool(elevation.value)
finally:
kernel.CloseHandle(token)
finally:
kernel.CloseHandle(handle)
return result
def _path(value):
return os.path.normcase(os.path.abspath(str(value))) if value else ""
def _pool_entry(arguments, project, service):
index = 1 # argv[0] is the Python executable.
while index < len(arguments):
option = arguments[index]
if option in ("-c", "-m", "-"):
return False
if option == "--":
index += 1
break
if option in ("-X", "-W"):
index += 2
elif option in ("-u", "-B", "-E", "-I", "-O", "-OO", "-s", "-S", "-v", "-b", "-bb") or option.startswith(("-X", "-W")):
index += 1
else:
break
return index + 1 < len(arguments) and _path(arguments[index]) == _path(project / "proxyPool.py") and arguments[index + 1] == service
class PoolService:
def __init__(self, settings):
self.settings = settings
self.session = requests.Session()
self.session.trust_env = False
self.lock = threading.Lock()
def _processes(self, project):
environment = {**os.environ, "KAOYAN_POOL_PROJECT": str(project)}
encoded = base64.b64encode(PROBE.encode("utf-16-le")).decode("ascii")
result = subprocess.run(
[_powershell(), "-NoProfile", "-NonInteractive", "-EncodedCommand", encoded],
env=environment, capture_output=True, timeout=12,
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
)
if result.returncode:
raise ValueError("无法读取本机代理池进程状态,请检查 Windows 进程查询权限")
return json.loads(result.stdout.decode("utf-8-sig"))
def status(self, config=None):
if config is None:
config = self.settings.read()
project = Path(config["pool_project"])
counts, api_ready = {}, False
try:
response = self.session.get(config["pool_url"] + "/count/", timeout=2)
response.raise_for_status()
counts = response.json()
api_ready = isinstance(counts, dict) and type(counts.get("count")) is int
except (requests.RequestException, ValueError):
pass
if not api_ready:
counts = {}
probe_error = ""
try:
facts = self._processes(project)
except (OSError, subprocess.TimeoutExpired, ValueError) as exc:
facts = {}
probe_error = "本机进程状态查询超时" if isinstance(exc, subprocess.TimeoutExpired) else "无法读取本机进程状态,请检查 Windows 权限"
items = []
for name, label in (("server", "代理 API"), ("schedule", "抓取与检测"), ("redis", "Redis")):
fact = facts.get(name, {})
pid = fact.get("pid")
item = {"name": name, "label": label, "pid": pid, "state": "stopped", "verified": False, "elevated": False, "detail": "未运行"}
if probe_error:
item.update(state="unknown", detail=probe_error)
elif fact.get("invalid"):
item.update(state="invalid", detail="PID 记录无效,请核实代理池 .runtime 目录")
elif fact.get("exists"):
native = _native_process(pid)
executable = fact.get("executable") or native.get("executable")
command = fact.get("command")
item["elevated"] = bool(native.get("elevated"))
if command:
arguments = [arg.strip('"') for arg in shlex.split(command, posix=False)]
if name == "redis":
matched = Path(executable or "").name.lower() == "redis-server.exe" and len(arguments) > 1 and _path(arguments[1]) == _path(project / ".runtime/redis.conf")
else:
matched = Path(executable or "").name.lower() in ("python.exe", "pythonw.exe") and _pool_entry(arguments, project, name)
if matched:
if native.get("can_terminate") is False:
item.update(state="restricted", verified=True, detail="管理员权限运行" if item["elevated"] else "运行中,无停止权限")
else:
item.update(state="running", verified=True, detail="运行中")
else:
item.update(state="conflict", detail="PID 与本项目不符,需要核实;该进程不会被操作")
elif name != "redis" and _path(executable) == _path(project / ".venv/Scripts/python.exe"):
item.update(state="restricted", detail="管理员权限运行" if item["elevated"] else "运行中,权限不足以读取进程详情")
else:
item.update(state="unknown", detail="权限不足,无法确认进程身份")
elif name == "redis" and api_ready and urlparse(config["pool_url"]).hostname in ("127.0.0.1", "localhost", "::1"):
item.update(state="external", pid=None, detail="API 已连接现有 Redis;独立服务")
elif not fact.get("exists"):
item["pid"] = None
items.append(item)
blocked = [item for item in items if item["state"] in ("restricted", "unknown", "invalid", "conflict")]
elevated = [item for item in blocked if item["elevated"]]
warning = ""
admin_command = ""
if elevated:
warning = "、".join(item["label"] for item in elevated) + "以管理员权限运行。当前工作台可以使用代理,但没有停止这些进程的权限。如需停止,请在管理员 PowerShell 中执行下方命令。"
admin_command = "& '" + str(project / "manage.ps1").replace("'", "''") + "' stop"
elif blocked:
warning = ";".join(item["label"] + ":" + item["detail"] for item in blocked)
return {"api_ready": api_ready, "counts": counts, "items": items, "can_stop": not blocked and any(item["verified"] for item in items), "warning": warning, "admin_command": admin_command}
@staticmethod
def _summary(status):
api = "正常响应" if status["api_ready"] else "未响应"
lines = [f"代理池 API:{api}"]
for item in status["items"]:
lines.append(item["label"] + ":" + item["detail"] + (f'(PID {item["pid"]})' if item["pid"] else ""))
if status["warning"]:
lines.append(status["warning"])
return "\n".join(lines)
def manage(self, action):
if action not in ("start", "stop", "status"):
raise ValueError("代理池操作无效")
with self.lock:
config = self.settings.read()
status = self.status(config)
result = {"ok": True, "action": action, "changed": False, "status": status, "message": "服务状态已更新", "output": self._summary(status)}
if action == "status":
return result
blocked = any(item["state"] in ("restricted", "unknown", "invalid", "conflict") for item in status["items"])
if action == "start" and status["api_ready"] and all(item["state"] in ("running", "restricted") for item in status["items"][:2]):
result["message"] = "代理池已在运行,已复用现有服务"
return result
if blocked:
result.update(ok=False, message=status["warning"])
return result
if action == "start" and status["api_ready"] and status["items"][0]["state"] == "stopped":
result.update(ok=False, message="API 地址已有服务响应,但缺少可验证的本项目 PID 记录,请先核实服务来源")
return result
script = Path(config["pool_project"]) / "manage.ps1"
if not script.is_file():
raise ValueError("代理池目录没有 manage.ps1,请检查配置;也可手动启动原代理池")
environment = {**os.environ, "KAOYAN_POOL_SCRIPT": str(script), "KAOYAN_POOL_ACTION": action}
result["changed"] = True
try:
# Start-Process can leave pipe handles inherited by background workers.
# A file lets us wait for the management shell itself to exit.
with tempfile.TemporaryFile(dir=self.settings.path.parent) as console:
completed = subprocess.run(
[_powershell(), "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", RUN_SCRIPT],
env=environment, stdout=console, stderr=subprocess.STDOUT, timeout=55,
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
)
console.seek(0)
output = console.read().decode("utf-8-sig", errors="replace")[-5000:]
except subprocess.TimeoutExpired:
result.update(ok=False, status=self.status(config), message="代理池操作超时,请检查服务状态和代理池日志后重试")
return result
except OSError:
result.update(ok=False, changed=False, message="无法运行服务管理命令,请检查本机环境和目录权限")
return result
ok = completed.returncode == 0
result.update(ok=ok, changed=True, status=self.status(config), output=output, message=("代理池启动完成" if action == "start" else "代理池已停止") if ok else (output.strip() or "代理池操作失败,请检查代理池日志"))
return result