fix: handle elevated proxy pool services and Windows output
This commit is contained in:
@@ -62,6 +62,12 @@
|
|||||||
|
|
||||||
服务默认仅监听 `127.0.0.1`,拒绝非本机 Host 和跨来源写入。代理池设置页使用现有 `manage.ps1` 控制本机服务;工作台停止脚本只结束自己管理且进程身份匹配的服务,不停止代理池。
|
服务默认仅监听 `127.0.0.1`,拒绝非本机 Host 和跨来源写入。代理池设置页使用现有 `manage.ps1` 控制本机服务;工作台停止脚本只结束自己管理且进程身份匹配的服务,不停止代理池。
|
||||||
|
|
||||||
|
代理池页分别显示 API、抓取与检测、Redis 的状态。重复启动会复用已运行的服务;检查状态不改变当前代理或隔离列表。工作台优先使用已安装的 PowerShell 7.3+ 运行代理池脚本,并统一输出为 UTF-8。后台启动的输出通过文件收集,避免等待服务关闭才能返回。原代理池文件保持不变。
|
||||||
|
|
||||||
|
如果现有 API 和调度器由管理员启动,普通权限工作台仍可使用代理、查询状态和复用服务,页面会显示「管理员权限运行」及停止命令,并禁用普通权限下的停止按钮。如需改为网页直接管理:在管理员 PowerShell 中执行页面显示的停止命令,然后回到普通权限工作台点击「启动」。外部 Redis 服务会继续保留。PID 无效或进程入口不匹配时,工作台显示具体原因,禁止操作该进程。
|
||||||
|
|
||||||
|
Python 命令引号的版本兼容性参见 [PowerShell 官方参数解析说明](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7.5#passing-arguments-that-contain-quote-characters)。
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
# 后端行为测试
|
# 后端行为测试
|
||||||
.\.venv\Scripts\python.exe -m pytest -q
|
.\.venv\Scripts\python.exe -m pytest -q
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
2026-10-11,Windows、本地独立虚拟环境,Python 3.12.13 / Node 24.16。
|
2026-10-11,Windows、本地独立虚拟环境,Python 3.12.13 / Node 24.16。
|
||||||
|
|
||||||
- 后端:`python -m pytest -q`,35 passed。Starlette TestClient 提示一条 httpx 迁移 warning,测试退出 0,生产服务不使用 TestClient。
|
- 后端:`python -m pytest -q`,49 passed。Starlette TestClient 提示一条 httpx 迁移 warning,测试退出 0,生产服务不使用 TestClient。
|
||||||
- 前端:`npm test`,3 passed;`npm run build`,TypeScript 检查与 Vite 生产构建退出 0。
|
- 前端:`npm test`,3 passed;`npm run build`,TypeScript 检查与 Vite 生产构建退出 0。
|
||||||
- Edge 实际浏览器:独立端口 8766、独立 `.runtime/qa/db`,验证导入→科目筛选→收藏→对比→历史趋势→Excel 下载;任务/代理/设置页面;设置保存;390px 手机宽度无页面溢出;无 pageerror。测试数据均标明「自动验收样例」,没有写入正式数据库。
|
- Edge 实际浏览器:独立端口 8766、独立 `.runtime/qa/db`,验证导入→科目筛选→收藏→对比→历史趋势→Excel 下载;任务/代理/设置页面;设置保存;390px 手机宽度无页面溢出;无 pageerror。测试数据均标明「自动验收样例」,没有写入正式数据库。
|
||||||
- 启动脚本:Windows PowerShell 首启、重复启动、停止、重新启动均退出 0;正式服务在 127.0.0.1:8765,健康检查应用身份和项目路径匹配。
|
- 启动脚本:Windows PowerShell 首启、重复启动、停止、重新启动均退出 0;正式服务在 127.0.0.1:8765,健康检查应用身份和项目路径匹配。
|
||||||
@@ -20,3 +20,11 @@
|
|||||||
```
|
```
|
||||||
|
|
||||||
正式服务不会自动开始采集,需要用户在网页创建任务。
|
正式服务不会自动开始采集,需要用户在网页创建任务。
|
||||||
|
|
||||||
|
代理池服务控制修复(2026-10-11):
|
||||||
|
|
||||||
|
- 复现原因:代理池 API / 调度器以管理员权限运行,普通权限的 CIM 读取不到命令行;旧调用误判为其他进程,且把 PowerShell 本地编码按 UTF-8 解码。新适配层分别显示状态、权限和外部 Redis,不改写原代理池项目。
|
||||||
|
- 14 项新增回归覆盖受限状态、重复启动、无权停止、中文输出、错误/过期 PID、停止权限、操作超时、Python 引号、真正的脚本入口、并发配置快照和后台输出管道。后端合计 49 passed。
|
||||||
|
- `tools/proxy_service_qa.py`:复制管理脚本到临时测试目录,启动无抓取行为的假 API / 调度器;实际验证普通权限 start → 重复 start → status → stop → API 关闭,PASS。仅只读检查现有 Redis,正式代理池未停止。
|
||||||
|
- 独立代码审查确认真实脚本入口校验和一次操作使用同一配置快照;最终无 Critical / Important。
|
||||||
|
- 正式界面 Edge 验收:显示 API 正常、两个进程的管理员权限、外部 Redis;点击检查状态成功,点击启动复用现有进程,无乱码或原错误堆栈,状态检查与复用保留当前路由,停止按钮给出权限限制说明。390px 代理页无溢出,无浏览器错误;截图保存为「代理池修复预览.png」。完整独立测试库浏览器流程亦再次通过。
|
||||||
|
|||||||
+92
-17
@@ -46,6 +46,8 @@ import type {
|
|||||||
Log,
|
Log,
|
||||||
Config,
|
Config,
|
||||||
Proxy,
|
Proxy,
|
||||||
|
PoolServiceStatus,
|
||||||
|
PoolManageResult,
|
||||||
Auth,
|
Auth,
|
||||||
History,
|
History,
|
||||||
Filters,
|
Filters,
|
||||||
@@ -954,11 +956,17 @@ function ImportDialog({
|
|||||||
if (year) body.set("year", year);
|
if (year) body.set("year", year);
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
try {
|
try {
|
||||||
const result = await api<{ records: number; scores: number; indexed_schools: number }>("/import", {
|
const result = await api<{
|
||||||
|
records: number;
|
||||||
|
scores: number;
|
||||||
|
indexed_schools: number;
|
||||||
|
}>("/import", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body,
|
body,
|
||||||
});
|
});
|
||||||
notify(`已导入 ${result.records} 条目录/计划、${result.scores} 条分数线${result.indexed_schools ? `,${result.indexed_schools} 所院校索引` : ""}`);
|
notify(
|
||||||
|
`已导入 ${result.records} 条目录/计划、${result.scores} 条分数线${result.indexed_schools ? `,${result.indexed_schools} 所院校索引` : ""}`,
|
||||||
|
);
|
||||||
done();
|
done();
|
||||||
close();
|
close();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -996,8 +1004,8 @@ function ImportDialog({
|
|||||||
</label>
|
</label>
|
||||||
<div className="info-box">
|
<div className="info-box">
|
||||||
文件本身有年份时使用文件年份。yzw CSV 和 YAM
|
文件本身有年份时使用文件年份。yzw CSV 和 YAM
|
||||||
无年份目录必须明确填写;分数线仍保留其原始年份。
|
无年份目录必须明确填写;分数线仍保留其原始年份。 无详情的 YAM
|
||||||
无详情的 YAM 种子仅导入院校索引。
|
种子仅导入院校索引。
|
||||||
</div>
|
</div>
|
||||||
<button className="primary" disabled={busy || !file}>
|
<button className="primary" disabled={busy || !file}>
|
||||||
{busy ? <Loader2 className="spin" size={17} /> : <FileUp size={17} />}
|
{busy ? <Loader2 className="spin" size={17} /> : <FileUp size={17} />}
|
||||||
@@ -1594,33 +1602,43 @@ function Tasks({ overview, majors, notify, refresh }: Shared) {
|
|||||||
|
|
||||||
function ProxyPanel({ notify }: { notify: Notify }) {
|
function ProxyPanel({ notify }: { notify: Notify }) {
|
||||||
const [data, setData] = useState<Proxy | null>(null),
|
const [data, setData] = useState<Proxy | null>(null),
|
||||||
|
[services, setServices] = useState<PoolServiceStatus | null>(null),
|
||||||
[busy, setBusy] = useState(false),
|
[busy, setBusy] = useState(false),
|
||||||
[output, setOutput] = useState("");
|
[output, setOutput] = useState("");
|
||||||
const load = useCallback(
|
const load = useCallback(
|
||||||
async () => setData(await api<Proxy>("/proxy?refresh=true")),
|
async () => setData(await api<Proxy>("/proxy?refresh=true")),
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
|
const loadServices = useCallback(
|
||||||
|
async () => setServices(await api<PoolServiceStatus>("/proxy/services")),
|
||||||
|
[],
|
||||||
|
);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
load().catch((e) => notify(e.message, true));
|
Promise.all([load(), loadServices()]).catch((e) => notify(e.message, true));
|
||||||
}, [load, notify]);
|
}, [load, loadServices, notify]);
|
||||||
async function operate(action: string) {
|
async function operate(action: string) {
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
try {
|
try {
|
||||||
if (action === "rotate") {
|
if (action === "rotate") {
|
||||||
setData(await post<Proxy>("/proxy/rotate"));
|
setData(await post<Proxy>("/proxy/rotate"));
|
||||||
notify("已切换当前路由");
|
notify("已切换当前路由");
|
||||||
} else if (action === "refresh") await load();
|
} else if (action === "refresh")
|
||||||
|
await Promise.all([load(), loadServices()]);
|
||||||
else {
|
else {
|
||||||
const result = await post<{ output: string; ok: boolean }>(
|
const result = await post<PoolManageResult>("/proxy/manage", {
|
||||||
"/proxy/manage",
|
action,
|
||||||
{ action },
|
});
|
||||||
);
|
setServices(result.status);
|
||||||
setOutput(result.output);
|
setOutput(action === "status" ? "" : result.output);
|
||||||
notify(
|
notify(
|
||||||
result.ok ? "代理池操作完成" : "代理池返回异常,请查看输出",
|
result.ok
|
||||||
|
? result.message
|
||||||
|
: result.status.warning
|
||||||
|
? "服务操作受限,请查看下方说明"
|
||||||
|
: result.message,
|
||||||
!result.ok,
|
!result.ok,
|
||||||
);
|
);
|
||||||
await load();
|
if (result.changed) await load();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
notify((e as Error).message, true);
|
notify((e as Error).message, true);
|
||||||
@@ -1730,8 +1748,46 @@ function ProxyPanel({ notify }: { notify: Notify }) {
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
<section className="card pool-controls">
|
<section className="card pool-controls">
|
||||||
|
<div className="card-title">
|
||||||
<h2>本机代理池服务</h2>
|
<h2>本机代理池服务</h2>
|
||||||
<p>通过现有项目的 manage.ps1 控制 Redis、API 与抓取调度器。</p>
|
<Badge tone={services?.api_ready ? "green" : ""}>
|
||||||
|
{services
|
||||||
|
? services.api_ready
|
||||||
|
? "API 正常"
|
||||||
|
: "API 未响应"
|
||||||
|
: "正在查询"}
|
||||||
|
</Badge>
|
||||||
|
</div>
|
||||||
|
<p>查看 API、代理抓取与检测、Redis 的运行状态,管理本机服务。</p>
|
||||||
|
{services && (
|
||||||
|
<ul className="pool-service-list">
|
||||||
|
{services.items.map((service) => (
|
||||||
|
<li key={service.name}>
|
||||||
|
<div>
|
||||||
|
<strong>{service.label}</strong>
|
||||||
|
{service.pid && <small>PID {service.pid}</small>}
|
||||||
|
</div>
|
||||||
|
<Badge
|
||||||
|
tone={
|
||||||
|
["running", "restricted", "external"].includes(
|
||||||
|
service.state,
|
||||||
|
)
|
||||||
|
? service.state === "restricted"
|
||||||
|
? "amber"
|
||||||
|
: "green"
|
||||||
|
: ["conflict", "invalid", "unknown"].includes(
|
||||||
|
service.state,
|
||||||
|
)
|
||||||
|
? "red"
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{service.detail}
|
||||||
|
</Badge>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
)}
|
||||||
<div className="button-group">
|
<div className="button-group">
|
||||||
<button disabled={busy} onClick={() => operate("start")}>
|
<button disabled={busy} onClick={() => operate("start")}>
|
||||||
<Play size={15} />
|
<Play size={15} />
|
||||||
@@ -1740,7 +1796,15 @@ function ProxyPanel({ notify }: { notify: Notify }) {
|
|||||||
<button disabled={busy} onClick={() => operate("status")}>
|
<button disabled={busy} onClick={() => operate("status")}>
|
||||||
检查状态
|
检查状态
|
||||||
</button>
|
</button>
|
||||||
<button disabled={busy} onClick={() => operate("stop")}>
|
<button
|
||||||
|
disabled={busy || !services?.can_stop}
|
||||||
|
title={
|
||||||
|
services?.can_stop
|
||||||
|
? "停止本项目管理的代理池服务"
|
||||||
|
: services?.warning || "没有可停止的本项目服务"
|
||||||
|
}
|
||||||
|
onClick={() => operate("stop")}
|
||||||
|
>
|
||||||
<Square size={14} />
|
<Square size={14} />
|
||||||
停止
|
停止
|
||||||
</button>
|
</button>
|
||||||
@@ -1751,7 +1815,18 @@ function ProxyPanel({ notify }: { notify: Notify }) {
|
|||||||
正在处理…
|
正在处理…
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
{output && <pre className="service-output">{output}</pre>}
|
{services?.warning && (
|
||||||
|
<div className="info-box warning service-warning">
|
||||||
|
<p>{services.warning}</p>
|
||||||
|
{services.admin_command && <code>{services.admin_command}</code>}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{output && (
|
||||||
|
<details className="pool-service-log">
|
||||||
|
<summary>查看操作输出</summary>
|
||||||
|
<pre className="service-output">{output}</pre>
|
||||||
|
</details>
|
||||||
|
)}
|
||||||
<div className="info-box">
|
<div className="info-box">
|
||||||
切换 IP
|
切换 IP
|
||||||
保留原代理;连接失败仅在本次工作台运行中隔离。代理池的持续可用性由其检测服务维护。
|
保留原代理;连接失败仅在本次工作台运行中隔离。代理池的持续可用性由其检测服务维护。
|
||||||
|
|||||||
@@ -1073,6 +1073,52 @@ code {
|
|||||||
.pool-controls .info-box {
|
.pool-controls .info-box {
|
||||||
margin-top: 21px;
|
margin-top: 21px;
|
||||||
}
|
}
|
||||||
|
.pool-service-list {
|
||||||
|
list-style: none;
|
||||||
|
margin: 18px 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
.pool-service-list li {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 12px;
|
||||||
|
padding: 12px 0;
|
||||||
|
border-top: 1px solid var(--line);
|
||||||
|
}
|
||||||
|
.pool-service-list li > div {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 8px;
|
||||||
|
align-items: baseline;
|
||||||
|
}
|
||||||
|
.pool-service-list strong {
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
.pool-service-list small {
|
||||||
|
color: var(--muted);
|
||||||
|
font-size: 11px;
|
||||||
|
}
|
||||||
|
.pool-service-list .badge {
|
||||||
|
white-space: normal;
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
.service-warning p {
|
||||||
|
margin: 0 0 10px;
|
||||||
|
}
|
||||||
|
.service-warning code {
|
||||||
|
display: block;
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
user-select: all;
|
||||||
|
}
|
||||||
|
.pool-service-log {
|
||||||
|
margin-top: 16px;
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
.pool-service-log summary {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
.service-output {
|
.service-output {
|
||||||
font-size: 10px;
|
font-size: 10px;
|
||||||
padding: 15px;
|
padding: 15px;
|
||||||
|
|||||||
@@ -106,6 +106,35 @@ export type Proxy = {
|
|||||||
error: string;
|
error: string;
|
||||||
proxies: { url: string; failed: boolean; active: boolean }[];
|
proxies: { url: string; failed: boolean; active: boolean }[];
|
||||||
};
|
};
|
||||||
|
export type PoolServiceStatus = {
|
||||||
|
api_ready: boolean;
|
||||||
|
items: {
|
||||||
|
name: string;
|
||||||
|
label: string;
|
||||||
|
pid: number | null;
|
||||||
|
state:
|
||||||
|
| "running"
|
||||||
|
| "restricted"
|
||||||
|
| "stopped"
|
||||||
|
| "external"
|
||||||
|
| "unknown"
|
||||||
|
| "invalid"
|
||||||
|
| "conflict";
|
||||||
|
verified: boolean;
|
||||||
|
elevated: boolean;
|
||||||
|
detail: string;
|
||||||
|
}[];
|
||||||
|
can_stop: boolean;
|
||||||
|
warning: string;
|
||||||
|
admin_command: string;
|
||||||
|
};
|
||||||
|
export type PoolManageResult = {
|
||||||
|
ok: boolean;
|
||||||
|
output: string;
|
||||||
|
message: string;
|
||||||
|
changed: boolean;
|
||||||
|
status: PoolServiceStatus;
|
||||||
|
};
|
||||||
export type Auth = {
|
export type Auth = {
|
||||||
saved: boolean;
|
saved: boolean;
|
||||||
valid: boolean;
|
valid: boolean;
|
||||||
|
|||||||
+10
-10
@@ -1,6 +1,5 @@
|
|||||||
"""Local-only HTTP workbench and production SPA host."""
|
"""Local-only HTTP workbench and production SPA host."""
|
||||||
import json
|
import json
|
||||||
import subprocess
|
|
||||||
import uuid
|
import uuid
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -15,6 +14,7 @@ from studio.imports import import_file, export_sheet
|
|||||||
from studio.jobs import JobManager
|
from studio.jobs import JobManager
|
||||||
from studio.network import ProxyRouter, Transport, ProxyUnavailable, RemoteUnavailable, RateLimited
|
from studio.network import ProxyRouter, Transport, ProxyUnavailable, RemoteUnavailable, RateLimited
|
||||||
from studio.settings import SettingsStore
|
from studio.settings import SettingsStore
|
||||||
|
from studio.pool_service import PoolService
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parent.parent
|
ROOT = Path(__file__).resolve().parent.parent
|
||||||
|
|
||||||
@@ -24,6 +24,7 @@ def create_app(data_dir=None):
|
|||||||
directory.mkdir(parents=True, exist_ok=True)
|
directory.mkdir(parents=True, exist_ok=True)
|
||||||
db = Database(directory / "studio.db")
|
db = Database(directory / "studio.db")
|
||||||
settings = SettingsStore(directory)
|
settings = SettingsStore(directory)
|
||||||
|
pool = PoolService(settings)
|
||||||
router = ProxyRouter(settings)
|
router = ProxyRouter(settings)
|
||||||
transport = Transport(settings, router)
|
transport = Transport(settings, router)
|
||||||
auth = AuthService(transport, CookieVault(directory))
|
auth = AuthService(transport, CookieVault(directory))
|
||||||
@@ -39,6 +40,7 @@ def create_app(data_dir=None):
|
|||||||
app = FastAPI(title="考研数据工作台", lifespan=lifespan, docs_url=None, redoc_url=None)
|
app = FastAPI(title="考研数据工作台", lifespan=lifespan, docs_url=None, redoc_url=None)
|
||||||
app.state.db, app.state.jobs = db, jobs
|
app.state.db, app.state.jobs = db, jobs
|
||||||
app.state.settings, app.state.router = settings, router
|
app.state.settings, app.state.router = settings, router
|
||||||
|
app.state.pool = pool
|
||||||
|
|
||||||
@app.middleware("http")
|
@app.middleware("http")
|
||||||
async def local_access(request: Request, call_next):
|
async def local_access(request: Request, call_next):
|
||||||
@@ -162,18 +164,16 @@ def create_app(data_dir=None):
|
|||||||
router.rotate()
|
router.rotate()
|
||||||
return router.status()
|
return router.status()
|
||||||
|
|
||||||
|
@app.get("/api/proxy/services")
|
||||||
|
def pool_status():
|
||||||
|
return pool.status()
|
||||||
|
|
||||||
@app.post("/api/proxy/manage")
|
@app.post("/api/proxy/manage")
|
||||||
def manage(body: dict):
|
def manage(body: dict):
|
||||||
action = body.get("action")
|
result = pool.manage(body.get("action"))
|
||||||
if action not in ("start", "stop", "status"):
|
if result["changed"]:
|
||||||
raise ValueError("代理池操作无效")
|
|
||||||
script = Path(settings.read()["pool_project"]) / "manage.ps1"
|
|
||||||
if not script.is_file():
|
|
||||||
raise ValueError("代理池目录没有 manage.ps1,请检查配置;也可手动启动原代理池")
|
|
||||||
result = subprocess.run(["powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(script), action], capture_output=True, timeout=55, creationflags=subprocess.CREATE_NO_WINDOW)
|
|
||||||
output = (result.stdout + result.stderr).decode("utf-8", errors="replace")[-5000:]
|
|
||||||
router.reset()
|
router.reset()
|
||||||
return {"ok": result.returncode == 0, "output": output, "action": action}
|
return result
|
||||||
|
|
||||||
@app.post("/api/import")
|
@app.post("/api/import")
|
||||||
def upload(file: UploadFile = File(...), year: int | None = Form(None)):
|
def upload(file: UploadFile = File(...), year: int | None = Form(None)):
|
||||||
|
|||||||
@@ -0,0 +1,278 @@
|
|||||||
|
"""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
|
||||||
@@ -0,0 +1,238 @@
|
|||||||
|
"""Regressions for Windows pool permissions and PowerShell output encoding."""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import requests
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from studio.api import create_app
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def pool_client(tmp_path):
|
||||||
|
project = tmp_path / "proxy pool"
|
||||||
|
project.mkdir()
|
||||||
|
(project / "manage.ps1").write_text("param([string]$Action)\nWrite-Output '代理池已就绪'\n", encoding="utf-8-sig")
|
||||||
|
app = create_app(tmp_path / "workbench")
|
||||||
|
app.state.settings.update({"pool_project": str(project), "pool_url": "http://127.0.0.1:1"})
|
||||||
|
with TestClient(app, base_url="http://127.0.0.1:8765") as client:
|
||||||
|
yield client, app, project
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def restricted_pool(pool_client, monkeypatch):
|
||||||
|
client, app, project = pool_client
|
||||||
|
runtime = project / ".runtime"
|
||||||
|
runtime.mkdir()
|
||||||
|
(runtime / "server.pid").write_text("100020")
|
||||||
|
(runtime / "schedule.pid").write_text("100030")
|
||||||
|
# CIM returns no command line when a normal user inspects elevated Python.
|
||||||
|
facts = {
|
||||||
|
"server": {"pid": 100020, "exists": True, "executable": str(project / ".venv/Scripts/python.exe"), "command": None},
|
||||||
|
"schedule": {"pid": 100030, "exists": True, "executable": str(project / ".venv/Scripts/python.exe"), "command": None},
|
||||||
|
"redis": {"pid": None, "exists": False},
|
||||||
|
}
|
||||||
|
|
||||||
|
def shell(command, **kwargs):
|
||||||
|
if "-EncodedCommand" in command:
|
||||||
|
return subprocess.CompletedProcess(command, 0, json.dumps(facts).encode(), b"")
|
||||||
|
return subprocess.CompletedProcess(command, 1, b"", b"PID 100020 belongs to another process.")
|
||||||
|
|
||||||
|
def counts(session, url, **kwargs):
|
||||||
|
response = requests.Response()
|
||||||
|
response.status_code = 200
|
||||||
|
response._content = b'{"count": 12, "http_type": {"https": 4, "http": 8}}'
|
||||||
|
return response
|
||||||
|
|
||||||
|
monkeypatch.setattr(subprocess, "run", shell)
|
||||||
|
monkeypatch.setattr(requests.Session, "get", counts)
|
||||||
|
return client, app, project
|
||||||
|
|
||||||
|
|
||||||
|
def test_restricted_running_pool_status_is_readable_and_keeps_selected_proxy(restricted_pool):
|
||||||
|
client, app, _ = restricted_pool
|
||||||
|
app.state.router.active = "http://1.2.3.4:8080"
|
||||||
|
response = client.post("/api/proxy/manage", json={"action": "status"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
result = response.json()
|
||||||
|
assert result["ok"] is True
|
||||||
|
assert result["status"]["api_ready"] is True
|
||||||
|
assert result["status"]["items"][0]["state"] == "restricted"
|
||||||
|
assert result["status"]["can_stop"] is False
|
||||||
|
assert "权限" in result["status"]["warning"]
|
||||||
|
assert "belongs to another process" not in result["output"]
|
||||||
|
assert app.state.router.active == "http://1.2.3.4:8080"
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_reuses_existing_restricted_pool_without_duplicate_processes(restricted_pool):
|
||||||
|
client, app, project = restricted_pool
|
||||||
|
app.state.router.failed.add("http://1.2.3.4:8080")
|
||||||
|
result = client.post("/api/proxy/manage", json={"action": "start"}).json()
|
||||||
|
assert result["ok"] is True
|
||||||
|
assert result["changed"] is False
|
||||||
|
assert result["status"]["items"][1]["state"] == "restricted"
|
||||||
|
assert (project / ".runtime/server.pid").read_text() == "100020"
|
||||||
|
assert "http://1.2.3.4:8080" in app.state.router.failed
|
||||||
|
|
||||||
|
|
||||||
|
def test_stop_explains_permission_limit_and_preserves_restricted_pid_records(restricted_pool):
|
||||||
|
client, _, project = restricted_pool
|
||||||
|
result = client.post("/api/proxy/manage", json={"action": "stop"}).json()
|
||||||
|
assert result["ok"] is False
|
||||||
|
assert "权限" in result["message"]
|
||||||
|
assert result["changed"] is False
|
||||||
|
assert (project / ".runtime/server.pid").read_text() == "100020"
|
||||||
|
assert (project / ".runtime/schedule.pid").read_text() == "100030"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(os.name != "nt", reason="Windows PowerShell integration")
|
||||||
|
def test_service_output_retains_chinese_characters(pool_client):
|
||||||
|
client, _, _ = pool_client
|
||||||
|
result = client.post("/api/proxy/manage", json={"action": "start"}).json()
|
||||||
|
assert result["ok"] is True
|
||||||
|
assert "代理池已就绪" in result["output"]
|
||||||
|
assert "\ufffd" not in result["output"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(os.name != "nt", reason="Windows process identity integration")
|
||||||
|
def test_wrong_live_pid_is_not_delegated_to_stop_script(pool_client):
|
||||||
|
client, _, project = pool_client
|
||||||
|
runtime = project / ".runtime"
|
||||||
|
runtime.mkdir()
|
||||||
|
(runtime / "server.pid").write_text(str(os.getpid()))
|
||||||
|
(project / "manage.ps1").write_text("param([string]$Action)\nSet-Content -LiteralPath (Join-Path $PSScriptRoot 'stop-was-called') -Value $Action\n", encoding="utf-8-sig")
|
||||||
|
result = client.post("/api/proxy/manage", json={"action": "stop"}).json()
|
||||||
|
assert result["ok"] is False
|
||||||
|
assert result["status"]["items"][0]["state"] == "conflict"
|
||||||
|
assert (runtime / "server.pid").read_text() == str(os.getpid())
|
||||||
|
assert not (project / "stop-was-called").exists()
|
||||||
|
|
||||||
|
|
||||||
|
def test_service_timeout_returns_actionable_result_instead_of_server_error(pool_client, monkeypatch):
|
||||||
|
client, app, _ = pool_client
|
||||||
|
app.state.router.active = "http://1.2.3.4:8080"
|
||||||
|
def shell(command, **kwargs):
|
||||||
|
if "-EncodedCommand" in command:
|
||||||
|
facts = {name: {"pid": None, "exists": False} for name in ("server", "schedule", "redis")}
|
||||||
|
return subprocess.CompletedProcess(command, 0, json.dumps(facts).encode(), b"")
|
||||||
|
raise subprocess.TimeoutExpired(command, 55)
|
||||||
|
monkeypatch.setattr(subprocess, "run", shell)
|
||||||
|
response = client.post("/api/proxy/manage", json={"action": "start"})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json()["ok"] is False
|
||||||
|
assert "超时" in response.json()["message"]
|
||||||
|
assert response.json()["changed"] is True
|
||||||
|
assert app.state.router.active is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_stale_pid_is_not_shown_as_the_running_service_pid(restricted_pool, monkeypatch):
|
||||||
|
client, _, project = restricted_pool
|
||||||
|
(project / ".runtime/redis.pid").write_text("100040")
|
||||||
|
def shell(command, **kwargs):
|
||||||
|
facts = {
|
||||||
|
"server": {"pid": 100020, "exists": True, "executable": str(project / ".venv/Scripts/python.exe"), "command": None},
|
||||||
|
"schedule": {"pid": 100030, "exists": True, "executable": str(project / ".venv/Scripts/python.exe"), "command": None},
|
||||||
|
"redis": {"pid": 100040, "exists": False},
|
||||||
|
}
|
||||||
|
return subprocess.CompletedProcess(command, 0, json.dumps(facts).encode(), b"")
|
||||||
|
monkeypatch.setattr(subprocess, "run", shell)
|
||||||
|
result = client.get("/api/proxy/services").json()
|
||||||
|
redis = next(item for item in result["items"] if item["name"] == "redis")
|
||||||
|
assert redis["state"] == "external"
|
||||||
|
assert redis["pid"] is None
|
||||||
|
assert (project / ".runtime/redis.pid").read_text() == "100040"
|
||||||
|
|
||||||
|
|
||||||
|
def test_readable_command_line_does_not_imply_permission_to_stop(restricted_pool, monkeypatch):
|
||||||
|
client, _, project = restricted_pool
|
||||||
|
def shell(command, **kwargs):
|
||||||
|
facts = {
|
||||||
|
name: {"pid": pid, "exists": True, "executable": str(project / ".venv/Scripts/python.exe"), "command": f'python.exe -u "{project / "proxyPool.py"}" {name}'}
|
||||||
|
for name, pid in (("server", 100020), ("schedule", 100030))
|
||||||
|
}
|
||||||
|
facts["redis"] = {"pid": None, "exists": False}
|
||||||
|
return subprocess.CompletedProcess(command, 0, json.dumps(facts).encode(), b"")
|
||||||
|
monkeypatch.setattr(subprocess, "run", shell)
|
||||||
|
monkeypatch.setattr("studio.pool_service._native_process", lambda pid: {"elevated": True, "can_terminate": False})
|
||||||
|
result = client.get("/api/proxy/services").json()
|
||||||
|
assert result["can_stop"] is False
|
||||||
|
assert result["items"][0]["state"] == "restricted"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(os.name != "nt" or not shutil.which("pwsh.exe"), reason="PowerShell 7 native-argument integration")
|
||||||
|
def test_pool_python_commands_keep_embedded_quotes(pool_client):
|
||||||
|
client, _, project = pool_client
|
||||||
|
python = sys.executable.replace("'", "''")
|
||||||
|
script = "param([string]$Action)\n$pythonPath = '" + python + "'\n$checkCode = @'\nprint(\"代理池已就绪\")\n'@\n& $pythonPath -X utf8 -c $checkCode\n"
|
||||||
|
(project / "manage.ps1").write_text(script, encoding="utf-8-sig")
|
||||||
|
result = client.post("/api/proxy/manage", json={"action": "start"}).json()
|
||||||
|
assert result["ok"] is True
|
||||||
|
assert "代理池已就绪" in result["output"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("invocation", [
|
||||||
|
'python.exe unrelated.py "{entry}" {name}',
|
||||||
|
'python.exe -c "pass" "{entry}" {name}',
|
||||||
|
'python.exe -m unrelated "{entry}" {name}',
|
||||||
|
])
|
||||||
|
def test_extra_script_argument_cannot_impersonate_pool_process(restricted_pool, monkeypatch, invocation):
|
||||||
|
client, _, project = restricted_pool
|
||||||
|
def shell(command, **kwargs):
|
||||||
|
facts = {
|
||||||
|
name: {"pid": pid, "exists": True, "executable": str(project / ".venv/Scripts/python.exe"), "command": invocation.format(entry=project / "proxyPool.py", name=name)}
|
||||||
|
for name, pid in (("server", 100020), ("schedule", 100030))
|
||||||
|
}
|
||||||
|
facts["redis"] = {"pid": None, "exists": False}
|
||||||
|
return subprocess.CompletedProcess(command, 0, json.dumps(facts).encode(), b"")
|
||||||
|
monkeypatch.setattr(subprocess, "run", shell)
|
||||||
|
status = client.get("/api/proxy/services").json()
|
||||||
|
assert status["items"][0]["state"] == "conflict"
|
||||||
|
assert status["items"][0]["verified"] is False
|
||||||
|
assert status["can_stop"] is False
|
||||||
|
result = client.post("/api/proxy/manage", json={"action": "stop"}).json()
|
||||||
|
assert result["ok"] is False
|
||||||
|
assert result["changed"] is False
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(os.name != "nt", reason="Windows PowerShell integration")
|
||||||
|
def test_action_uses_one_project_snapshot_when_settings_change(pool_client, monkeypatch):
|
||||||
|
client, app, project = pool_client
|
||||||
|
other = project.parent / "another pool"
|
||||||
|
other.mkdir()
|
||||||
|
script = "param([string]$Action)\nSet-Content -LiteralPath (Join-Path $PSScriptRoot 'ran-here') -Value $Action\n"
|
||||||
|
for root in (project, other):
|
||||||
|
(root / "manage.ps1").write_text(script, encoding="utf-8-sig")
|
||||||
|
def read_processes(root):
|
||||||
|
app.state.settings.update({"pool_project": str(other)})
|
||||||
|
return {name: {"pid": None, "exists": False} for name in ("server", "schedule", "redis")}
|
||||||
|
monkeypatch.setattr(app.state.pool, "_processes", read_processes)
|
||||||
|
result = client.post("/api/proxy/manage", json={"action": "start"}).json()
|
||||||
|
assert result["ok"] is True
|
||||||
|
assert (project / "ran-here").exists()
|
||||||
|
assert (project / "ran-here").read_text().strip() == "start"
|
||||||
|
assert not (other / "ran-here").exists()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(os.name != "nt", reason="Windows background process integration")
|
||||||
|
def test_background_service_does_not_keep_management_response_open(pool_client, monkeypatch):
|
||||||
|
from studio.pool_service import _native_process, _powershell
|
||||||
|
client, app, project = pool_client
|
||||||
|
worker = project / "background.py"
|
||||||
|
worker.write_text("import time\ntime.sleep(8)\n", encoding="utf-8")
|
||||||
|
python = sys.executable.replace("'", "''")
|
||||||
|
script = "param([string]$Action)\n$process = Start-Process -FilePath '" + python + "' -ArgumentList ('\"' + (Join-Path $PSScriptRoot 'background.py') + '\"') -WindowStyle Hidden -PassThru -RedirectStandardOutput (Join-Path $PSScriptRoot 'worker.out.log') -RedirectStandardError (Join-Path $PSScriptRoot 'worker.err.log')\nSet-Content -LiteralPath (Join-Path $PSScriptRoot 'worker.pid') -Value $process.Id\nWrite-Output 'started'\n"
|
||||||
|
(project / "manage.ps1").write_text(script, encoding="utf-8-sig")
|
||||||
|
monkeypatch.setattr(app.state.pool, "_processes", lambda root: {name: {"pid": None, "exists": False} for name in ("server", "schedule", "redis")})
|
||||||
|
try:
|
||||||
|
result = client.post("/api/proxy/manage", json={"action": "start"}).json()
|
||||||
|
assert result["ok"] is True
|
||||||
|
pid = int((project / "worker.pid").read_text(encoding="utf-8-sig").strip())
|
||||||
|
assert _native_process(pid).get("executable"), "Management waited until the background service exited"
|
||||||
|
finally:
|
||||||
|
if (project / "worker.pid").exists():
|
||||||
|
environment = {**os.environ, "KAOYAN_QA_PID": (project / "worker.pid").read_text(encoding="utf-8-sig").strip(), "KAOYAN_QA_WORKER": str(worker)}
|
||||||
|
subprocess.run([_powershell(), "-NoProfile", "-Command", "$p = Get-CimInstance Win32_Process -Filter ('ProcessId=' + $env:KAOYAN_QA_PID); if ($p -and $p.CommandLine.Contains($env:KAOYAN_QA_WORKER)) { Stop-Process -Id $p.ProcessId }"], env=environment, capture_output=True, timeout=10)
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
"""Exercise pool controls with isolated dummy processes, never real pool stops."""
|
||||||
|
import json
|
||||||
|
import socket
|
||||||
|
import tempfile
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from studio.pool_service import PoolService
|
||||||
|
from studio.settings import SettingsStore
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
pool_root = Path(SettingsStore(ROOT / ".runtime").read()["pool_project"])
|
||||||
|
source = (pool_root / "manage.ps1").read_text(encoding="utf-8-sig")
|
||||||
|
qa_root = (ROOT / ".runtime/qa").resolve()
|
||||||
|
qa_root.mkdir(parents=True, exist_ok=True)
|
||||||
|
with socket.socket() as listener:
|
||||||
|
listener.bind(("127.0.0.1", 0))
|
||||||
|
port = listener.getsockname()[1]
|
||||||
|
url = f"http://127.0.0.1:{port}"
|
||||||
|
with tempfile.TemporaryDirectory(prefix="pool manager ", dir=qa_root) as temporary:
|
||||||
|
project = Path(temporary).resolve()
|
||||||
|
assert project.is_relative_to(qa_root)
|
||||||
|
python = str(pool_root / ".venv/Scripts/python.exe").replace("'", "''")
|
||||||
|
script = source.replace("$pythonPath = Join-Path $projectRoot '.venv\\Scripts\\python.exe'", "$pythonPath = '" + python + "'").replace("http://127.0.0.1:5010", url)
|
||||||
|
(project / "manage.ps1").write_text(script, encoding="utf-8-sig")
|
||||||
|
(project / "proxyPool.py").write_text(f'''
|
||||||
|
import sys, time
|
||||||
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||||
|
class Handler(BaseHTTPRequestHandler):
|
||||||
|
def do_GET(self):
|
||||||
|
payload = b'{{"count":0,"http_type":{{"http":0,"https":0}}}}'
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header('Content-Type', 'application/json')
|
||||||
|
self.send_header('Content-Length', str(len(payload)))
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(payload)
|
||||||
|
def log_message(self, *args): pass
|
||||||
|
if sys.argv[1] == 'server':
|
||||||
|
ThreadingHTTPServer(('127.0.0.1', {port}), Handler).serve_forever()
|
||||||
|
else:
|
||||||
|
while True: time.sleep(.2)
|
||||||
|
''', encoding="utf-8")
|
||||||
|
settings = SettingsStore(project / "workbench")
|
||||||
|
settings.update({"pool_project": str(project), "pool_url": url})
|
||||||
|
manager = PoolService(settings)
|
||||||
|
try:
|
||||||
|
start = manager.manage("start")
|
||||||
|
assert start["ok"], start
|
||||||
|
assert start["status"]["api_ready"], start
|
||||||
|
assert start["status"]["can_stop"], start
|
||||||
|
assert all(item["state"] == "running" for item in start["status"]["items"][:2]), start
|
||||||
|
original_pids = [item["pid"] for item in start["status"]["items"][:2]]
|
||||||
|
repeat = manager.manage("start")
|
||||||
|
assert repeat["ok"] and not repeat["changed"], repeat
|
||||||
|
assert [item["pid"] for item in repeat["status"]["items"][:2]] == original_pids
|
||||||
|
status = manager.manage("status")
|
||||||
|
assert status["ok"] and status["status"]["api_ready"], status
|
||||||
|
stop = manager.manage("stop")
|
||||||
|
assert stop["ok"], stop
|
||||||
|
assert not stop["status"]["api_ready"], stop
|
||||||
|
assert all(item["state"] == "stopped" for item in stop["status"]["items"]), stop
|
||||||
|
print(json.dumps({"result": "PASS", "checks": ["isolated start", "idempotent start", "status", "stop", "API closed"], "port": port, "real_pool_untouched": True}))
|
||||||
|
finally:
|
||||||
|
if manager.status()["can_stop"]:
|
||||||
|
manager.manage("stop")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user