-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_agent.py
More file actions
executable file
·330 lines (307 loc) · 11.4 KB
/
Copy pathnode_agent.py
File metadata and controls
executable file
·330 lines (307 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
"""Register a fixed local launcher allowlist with a CaptchaMesh v2 hub."""
from __future__ import annotations
import argparse
import os
import platform
import signal
# Process execution is deliberate and restricted to the machine-local allowlist.
import subprocess # nosec B404
import threading
import time
from pathlib import Path
from typing import Any
import requests
from broker import PROTOCOL_VERSION, load_registrations
class AgentError(RuntimeError):
def __init__(self, code: str, description: str, http_status: int = 0):
super().__init__(f"{code}: {description}")
self.code = code
self.description = description
self.http_status = http_status
class NodeAgent:
def __init__(
self,
hub_url: str,
api_key: str,
node_key: str,
node_id: str,
name: str,
registry_path: Path,
) -> None:
self.hub_url = hub_url.rstrip("/")
self.api_key = api_key
self.node_key = node_key
self.node_id = node_id
self.name = name
self.registry_path = registry_path
self.registrations = load_registrations(registry_path)
self.node_token = "" # nosec B105
self.processes: dict[str, subprocess.Popen[bytes]] = {}
self.stopping: set[str] = set()
self.process_lock = threading.Lock()
self.shutdown = threading.Event()
def _request(
self,
method: str,
path: str,
*,
json_body: dict[str, Any] | None = None,
authorization: str,
timeout: float = 35,
) -> dict[str, Any] | None:
try:
response = requests.request(
method,
self.hub_url + path,
json=json_body,
headers={"Accept": "application/json", "Authorization": authorization},
timeout=timeout,
)
except requests.RequestException as exc:
raise AgentError("ERROR_TRANSPORT", str(exc)) from exc
if response.status_code == 204:
return None
try:
payload = response.json()
except ValueError:
payload = {}
if response.status_code >= 400:
raise AgentError(
str(payload.get("errorCode", f"HTTP_{response.status_code}")),
str(payload.get("errorDescription", "hub request failed")),
response.status_code,
)
return payload
def _offers(self) -> list[dict[str, Any]]:
offers: list[dict[str, Any]] = []
for registration in self.registrations.values():
offers.append(
{
"id": registration["id"],
"name": registration["name"],
"summary": registration["summary"],
"provides": registration["provides"],
"details": registration["details"],
"description": registration["description"],
"captchaTypes": registration["captchaTypes"],
"enabled": bool(
registration["enabled"] and registration["cwd"].is_dir()
),
}
)
return offers
def join(self) -> None:
payload = self._request(
"POST",
"/v1/nodes/join",
json_body={
"nodeId": self.node_id,
"name": self.name,
"version": PROTOCOL_VERSION,
"device": platform.node(),
"registrations": self._offers(),
},
authorization=f"NodeKey {self.node_key}",
)
if not payload or not payload.get("nodeToken"):
raise AgentError("ERROR_BAD_JOIN", "hub did not return a node token")
self.node_token = str(payload["nodeToken"])
print(f"joined node={self.node_id} registrations={payload.get('registered', 0)}")
def poll(self) -> dict[str, Any] | None:
return self._request(
"POST",
"/v1/nodes/poll",
json_body={"waitSeconds": 25},
authorization=f"Node {self.node_token}",
timeout=35,
)
def report(
self,
command_id: str,
run_id: str,
status: str,
exit_code: int | None = None,
) -> None:
body: dict[str, Any] = {
"commandId": command_id,
"runId": run_id,
"status": status,
}
if exit_code is not None:
body["exitCode"] = exit_code
self._request(
"POST",
"/v1/nodes/report",
json_body=body,
authorization=f"Node {self.node_token}",
)
def _watch(self, command_id: str, run_id: str, process: subprocess.Popen[bytes]) -> None:
exit_code = process.wait()
with self.process_lock:
cancelled = run_id in self.stopping
self.processes.pop(run_id, None)
self.stopping.discard(run_id)
status = "cancelled" if cancelled else ("succeeded" if exit_code == 0 else "failed")
for attempt in range(3):
try:
self.report(command_id, run_id, status, exit_code)
print(f"finished run={run_id} status={status} exit={exit_code}")
return
except AgentError as exc:
if exc.http_status == 401:
self.node_token = "" # nosec B105
try:
self.join()
except AgentError:
pass
if attempt == 2:
print(f"report failed run={run_id} code={exc.code}")
return
time.sleep(2**attempt)
def start(self, command: dict[str, Any]) -> None:
command_id = str(command.get("commandId", ""))
run_id = str(command.get("runId", ""))
registration_id = str(command.get("registrationId", ""))
registration = self.registrations.get(registration_id)
if registration is None or not registration["enabled"]:
self.report(command_id, run_id, "failed", 127)
return
with self.process_lock:
if run_id in self.processes:
self.report(command_id, run_id, "running")
return
environment = os.environ.copy()
environment.update(
{
"CAPTCHAMESH_URL": self.hub_url,
"CAPTCHAMESH_API_KEY": self.api_key,
"CAPTCHAMESH_RUN_ID": run_id,
"CAPTCHAMESH_REGISTRATION_ID": registration_id,
}
)
project_root = str(Path(__file__).resolve().parent)
existing_python_path = environment.get("PYTHONPATH", "")
environment["PYTHONPATH"] = project_root + (
os.pathsep + existing_python_path if existing_python_path else ""
)
try:
# The argv and cwd come only from registrations.json; no shell is used.
process = subprocess.Popen( # nosec B603
registration["command"],
cwd=registration["cwd"],
env=environment,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
start_new_session=True,
)
except (OSError, ValueError):
self.report(command_id, run_id, "failed", 126)
return
with self.process_lock:
self.processes[run_id] = process
self.report(command_id, run_id, "running")
threading.Thread(
target=self._watch,
args=(command_id, run_id, process),
name=f"captchamesh-{run_id[-8:]}",
daemon=True,
).start()
print(f"started run={run_id} registration={registration_id}")
def stop(self, command: dict[str, Any]) -> None:
command_id = str(command.get("commandId", ""))
run_id = str(command.get("runId", ""))
with self.process_lock:
process = self.processes.get(run_id)
if process is not None:
self.stopping.add(run_id)
if process is None or process.poll() is not None:
self.report(command_id, run_id, "cancelled")
return
try:
os.killpg(process.pid, signal.SIGTERM)
process.wait(timeout=5)
except subprocess.TimeoutExpired:
os.killpg(process.pid, signal.SIGKILL)
except ProcessLookupError:
pass
self.report(command_id, run_id, "cancelled")
def run(self) -> None:
backoff = 1
while not self.shutdown.is_set():
try:
if not self.node_token:
self.join()
command = self.poll()
backoff = 1
if command is None:
continue
action = command.get("action")
if action == "start":
self.start(command)
elif action == "stop":
self.stop(command)
except AgentError as exc:
if exc.http_status == 401:
self.node_token = "" # nosec B105
print(f"node loop error code={exc.code}; retrying")
self.shutdown.wait(backoff)
backoff = min(backoff * 2, 20)
def close(self) -> None:
self.shutdown.set()
with self.process_lock:
running = list(self.processes.items())
self.stopping.update(run_id for run_id, _ in running)
for _, process in running:
if process.poll() is not None:
continue
try:
os.killpg(process.pid, signal.SIGTERM)
except ProcessLookupError:
continue
deadline = time.monotonic() + 5
for _, process in running:
if process.poll() is not None:
continue
try:
process.wait(timeout=max(0.1, deadline - time.monotonic()))
except subprocess.TimeoutExpired:
try:
os.killpg(process.pid, signal.SIGKILL)
except ProcessLookupError:
pass
def _read_secret(path: str | None, env_name: str) -> str:
if path:
value = Path(path).expanduser().read_text().strip()
else:
value = os.environ.get(env_name, "").strip()
if not value:
raise SystemExit(f"{env_name} or its key file is required")
return value
def main() -> None:
parser = argparse.ArgumentParser(description="Advertise fixed registration launchers to a hub")
parser.add_argument("--hub", default=os.environ.get("CAPTCHAMESH_URL", ""))
parser.add_argument("--api-key-file")
parser.add_argument("--node-key-file")
parser.add_argument("--node-id", required=True)
parser.add_argument("--name", required=True)
parser.add_argument("--registry", type=Path, default=Path(__file__).parent / "registrations.json")
args = parser.parse_args()
if not args.hub:
parser.error("--hub or CAPTCHAMESH_URL is required")
agent = NodeAgent(
args.hub,
_read_secret(args.api_key_file, "CAPTCHAMESH_API_KEY"),
_read_secret(args.node_key_file, "CAPTCHAMESH_NODE_KEY"),
args.node_id,
args.name,
args.registry,
)
try:
agent.run()
except KeyboardInterrupt:
pass
finally:
agent.close()
if __name__ == "__main__":
main()