captchakraken 3.2.1 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/AGENTS.md +7 -11
  2. package/LICENSE +165 -145
  3. package/NOTICE +4 -4
  4. package/README.md +12 -13
  5. package/bin/captchakraken.js +25 -0
  6. package/dist/cli-invocation.d.ts +6 -3
  7. package/dist/cli-invocation.js +26 -10
  8. package/dist/humanize.d.ts +37 -1
  9. package/dist/humanize.js +143 -9
  10. package/dist/index.d.ts +13 -12
  11. package/dist/index.js +25 -33
  12. package/dist/kinds.d.ts +15 -2
  13. package/dist/kinds.js +12 -4
  14. package/dist/playwright-types.d.ts +3 -0
  15. package/dist/selectors.d.ts +93 -11
  16. package/dist/selectors.js +226 -116
  17. package/dist/solver.d.ts +94 -12
  18. package/dist/solver.js +569 -203
  19. package/dist/types.d.ts +53 -8
  20. package/dist/verdicts.d.ts +3 -0
  21. package/dist/verdicts.js +40 -0
  22. package/package.json +8 -4
  23. package/python/AGENTS.md +9 -13
  24. package/python/LICENSE +165 -145
  25. package/python/NOTICE +4 -4
  26. package/python/README.md +13 -12
  27. package/python/pyproject.toml +27 -13
  28. package/python/src/captchakraken/__init__.py +43 -46
  29. package/python/src/captchakraken/action_types.py +32 -1
  30. package/python/src/captchakraken/captchakraken.config.json +416 -0
  31. package/python/src/captchakraken/cli.py +141 -31
  32. package/python/src/captchakraken/clipvideo.py +258 -0
  33. package/python/src/captchakraken/config.py +72 -7
  34. package/python/src/captchakraken/demo.py +149 -0
  35. package/python/src/captchakraken/humanize.py +141 -10
  36. package/python/src/captchakraken/image_processor.py +3 -7
  37. package/python/src/captchakraken/keyframes.py +96 -14
  38. package/python/src/captchakraken/kinds.py +14 -2
  39. package/python/src/captchakraken/models.json +37 -3
  40. package/python/src/captchakraken/page_solver.py +600 -252
  41. package/python/src/captchakraken/planner.py +311 -49
  42. package/python/src/captchakraken/pointer/__init__.py +20 -0
  43. package/python/src/captchakraken/pointer/detector.py +333 -0
  44. package/python/src/captchakraken/pointer/elements.py +273 -0
  45. package/python/src/captchakraken/pointer/heads.py +163 -0
  46. package/python/src/captchakraken/pointer/layout.py +319 -0
  47. package/python/src/captchakraken/pointer/marks.py +184 -0
  48. package/python/src/captchakraken/pointer/server.py +150 -0
  49. package/python/src/captchakraken/pointer/service.py +553 -0
  50. package/python/src/captchakraken/pointer/vllm_plugin.py +114 -0
  51. package/python/src/captchakraken/prompts.py +111 -34
  52. package/python/src/captchakraken/selectors.py +221 -135
  53. package/python/src/captchakraken/selfhost.py +423 -0
  54. package/python/src/captchakraken/server_manager.py +139 -11
  55. package/python/src/captchakraken/solver.py +169 -88
  56. package/python/src/captchakraken/tool_calls/board_painted.py +11 -6
  57. package/python/src/captchakraken/tool_calls/fill_tracker.py +152 -0
  58. package/python/src/captchakraken/tool_calls/find_checkbox.py +14 -59
  59. package/python/src/captchakraken/tool_calls/find_grid.py +273 -1191
  60. package/python/src/captchakraken/tool_calls/slide_tracker.py +1102 -0
  61. package/python/src/captchakraken/verdicts.py +52 -0
  62. package/dist/slide-geometry.d.ts +0 -6
  63. package/dist/slide-geometry.js +0 -38
  64. package/python/src/captchakraken/tool_calls/track_piece.py +0 -120
@@ -14,6 +14,7 @@ from typing import Any, Callable, List, Mapping, Optional, Sequence, Tuple
14
14
  from .kinds import Verdict, Vendor
15
15
 
16
16
  # The vendors' own HTTP refusal: hCaptcha's frame words a 429 as "Your computer or network has sent too many requests".
17
+ _SCRIPTED_REQUESTS = ("xhr", "fetch", "script")
17
18
  _TOO_MANY_REQUESTS = 429
18
19
 
19
20
 
@@ -64,6 +65,38 @@ def _jsonp_result(body: str) -> Optional[Verdict]:
64
65
  return {"success": Verdict.ACCEPTED, "fail": Verdict.REJECTED}.get(result) if isinstance(result, str) else None
65
66
 
66
67
 
68
+ def _tencent_ticket(body: str) -> Optional[Verdict]:
69
+ """`{"errorCode": "0", "ticket": ...}` when taken; `{"errorCode": "50", "ticket": ""}` when a wrong slide is refused.
70
+ Any other code (rate limits, expired sessions) has not been recorded, so the DOM judges it."""
71
+ data = _json(body)
72
+ if not isinstance(data, dict):
73
+ return None
74
+ code = str(data.get("errorCode"))
75
+ if code == "0" and data.get("ticket"):
76
+ return Verdict.ACCEPTED
77
+ return Verdict.REJECTED if code == "50" and not data.get("ticket") else None
78
+
79
+
80
+ def _yidun_check(body: str) -> Optional[Verdict]:
81
+ """`callback({"data": {"result": true, ...}, "error": 0})` when taken, `"result": false` with `"error": 0` when refused."""
82
+ start, end = body.find("("), body.rfind(")")
83
+ data = _json(body[start + 1:end]) if 0 <= start < end else None
84
+ if not isinstance(data, dict) or data.get("error") != 0:
85
+ return None
86
+ result = (data.get("data") or {}).get("result")
87
+ return {True: Verdict.ACCEPTED, False: Verdict.REJECTED}.get(result) if isinstance(result, bool) else None
88
+
89
+
90
+ def _arkose_answer(body: str) -> Optional[Verdict]:
91
+ """`solved: true` when taken; `solved: false` with `time_end` set when the session ended refused."""
92
+ data = _json(body)
93
+ if not isinstance(data, dict):
94
+ return None
95
+ if data.get("solved") is True:
96
+ return Verdict.ACCEPTED
97
+ return Verdict.REJECTED if data.get("solved") is False and data.get("time_end") is not None else None
98
+
99
+
67
100
  def _url_only(_body: str) -> Optional[Verdict]:
68
101
  return Verdict.NEW_CHALLENGE
69
102
 
@@ -84,6 +117,10 @@ VERDICT_ENDPOINTS: Mapping[Vendor, Sequence[VerdictEndpoint]] = {
84
117
  VerdictEndpoint("/recaptcha/api2/reload", _new_board)),
85
118
  Vendor.GEETEST: (VerdictEndpoint("geetest.com/verify", _jsonp_result),
86
119
  VerdictEndpoint("geetest.com/load", _url_only)),
120
+ Vendor.TENCENT: (VerdictEndpoint("/cap_union_new_verify", _tencent_ticket),),
121
+ Vendor.YIDUN: (VerdictEndpoint("dun.163.com/api/v3/check", _yidun_check),),
122
+ # Sites serve this from their own hostnames, so the path is what identifies it.
123
+ Vendor.FUNCAPTCHA: (VerdictEndpoint("/fc/ca/", _arkose_answer),),
87
124
  }
88
125
 
89
126
 
@@ -104,6 +141,13 @@ def read_verdict(url: str, status: int, body: str) -> Optional[RoundVerdict]:
104
141
  return RoundVerdict(vendor, verdict) if verdict is not None else None
105
142
 
106
143
 
144
+ def _is_a_scripted_request(response: Any) -> bool:
145
+ try:
146
+ return response.request.resource_type in _SCRIPTED_REQUESTS
147
+ except Exception:
148
+ return False
149
+
150
+
107
151
  class VerdictLog:
108
152
  """Every verdict the page's network carries while a solve runs.
109
153
 
@@ -113,6 +157,8 @@ class VerdictLog:
113
157
 
114
158
  def __init__(self, page: Any) -> None:
115
159
  self.verdicts: List[RoundVerdict] = []
160
+ #: Requests the page's scripts have made. A board that answers a press with none has refused nothing.
161
+ self.exchanges = 0
116
162
  self._read = 0
117
163
  self._page = page
118
164
  self._attached = False
@@ -125,6 +171,8 @@ class VerdictLog:
125
171
  def _on_response(self, response: Any) -> None:
126
172
  try:
127
173
  url, status = response.url, response.status
174
+ if _is_a_scripted_request(response):
175
+ self.exchanges += 1
128
176
  if endpoint_for(url) is None:
129
177
  return
130
178
  # Decoded leniently, as a browser's text() is: one deal endpoint answers in ciphertext, and its URL alone
@@ -136,6 +184,10 @@ class VerdictLog:
136
184
  if verdict is not None:
137
185
  self.verdicts.append(verdict)
138
186
 
187
+ @property
188
+ def listening(self) -> bool:
189
+ return self._attached
190
+
139
191
  def fresh(self) -> List[RoundVerdict]:
140
192
  """The verdicts that arrived since the last call."""
141
193
  new, self._read = self.verdicts[self._read:], len(self.verdicts)
@@ -1,6 +0,0 @@
1
- export declare function solveSlideGeometry(widths: Array<[number, number]>, widgetWidth: number): {
2
- pieceWidth: number | null;
3
- ratio: number;
4
- };
5
- export declare const MIN_PIECE_PX = 3;
6
- export declare const MAX_PIECE_FRACTION = 0.6;
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MAX_PIECE_FRACTION = exports.MIN_PIECE_PX = void 0;
4
- exports.solveSlideGeometry = solveSlideGeometry;
5
- // Literals here match page_solver.py::_solve_slide_geometry; the ratio is assumed 1:1 unless two probes prove otherwise,
6
- // which is true of every vendor observed.
7
- function solveSlideGeometry(widths, widgetWidth) {
8
- if (!widths.length)
9
- return { pieceWidth: null, ratio: 1 };
10
- let ratio = 1;
11
- let pieceWidth = null;
12
- // The widest-apart pair, not the last two: 1px rounding between neighbouring probes once gave a ratio of 0.5.
13
- const sorted = [...widths].sort((a, b) => a[0] - b[0]);
14
- const [o1, w1] = sorted[0];
15
- const [o2, w2] = sorted[sorted.length - 1];
16
- if (o2 - o1 >= MIN_OFFSET_SPREAD_PX) {
17
- const candidate = (w2 - w1) / (o2 - o1);
18
- if (candidate >= MIN_RATIO && candidate <= MAX_RATIO) {
19
- ratio = candidate;
20
- pieceWidth = w1 - ratio * o1;
21
- }
22
- }
23
- if (pieceWidth === null) {
24
- const [o, w] = widths[widths.length - 1];
25
- pieceWidth = w - ratio * o;
26
- }
27
- if (pieceWidth < exports.MIN_PIECE_PX || pieceWidth > widgetWidth * exports.MAX_PIECE_FRACTION) {
28
- return { pieceWidth: null, ratio };
29
- }
30
- return { pieceWidth, ratio };
31
- }
32
- // Box widths are ±1px, so the ratio error is ±2/spread: ±0.13 at 16px against ±0.5 at 4px, multiplied into every correction.
33
- const MIN_OFFSET_SPREAD_PX = 16.0;
34
- // A ratio of 0.02 from a redraw sends the handle off the track and, under camoufox, into a mouse move that never returns.
35
- const MIN_RATIO = 0.2;
36
- const MAX_RATIO = 3.0;
37
- exports.MIN_PIECE_PX = 3.0;
38
- exports.MAX_PIECE_FRACTION = 0.6;
@@ -1,120 +0,0 @@
1
- """Track a slide captcha's piece by what MOVED between two shots, not by what it is.
2
-
3
- Vendors vary the handle-to-piece ratio so the offset cannot be computed from the gap, so the driver sweeps,
4
- looks at what changed, and corrects. The changed box's left edge is the vacated ground and its right edge the
5
- piece's current edge; a sweep that clears the ground leaves two marks, and the right one IS the piece.
6
- """
7
-
8
- from __future__ import annotations
9
-
10
- from typing import Optional, Sequence
11
-
12
- import cv2
13
- import numpy as np
14
-
15
- # Low enough to catch a piece over similar-luminance ground, above JPEG ringing and antialiased-edge jitter.
16
- _DIFF_THRESHOLD = 18
17
-
18
- # A press that has not moved yet leaves stray pixels; calling that a piece hands the caller a box it steers by.
19
- _MIN_CHANGED_PIXELS = 40
20
-
21
- # Two marks are the piece and its ghost only if they are the same object, so the same width to a quarter.
22
- _SAME_WIDTH_TOLERANCE = 0.25
23
-
24
- # Two regimes: a mark no more than half the travel is the piece, a mark of about the travel is the sliver at
25
- # either end of an overlap. `travel` is only believed, so the regimes have to sit far apart.
26
- _CLEARED_MARK_FRACTION = 0.5
27
-
28
- # A piece with a notch can go still for several columns; the two marks are a whole sweep apart.
29
- _MARK_GAP_PX = 10
30
-
31
- _MIN_COLUMN_PIXELS = 2
32
-
33
-
34
- def _change_mask(
35
- before_path: str,
36
- after_path: str,
37
- exclude: Optional[Sequence[float]] = None,
38
- ):
39
- a = cv2.imread(before_path, cv2.IMREAD_GRAYSCALE)
40
- b = cv2.imread(after_path, cv2.IMREAD_GRAYSCALE)
41
- if a is None or b is None or a.shape != b.shape:
42
- return None
43
-
44
- mask = (cv2.absdiff(a, b) > _DIFF_THRESHOLD).astype(np.uint8)
45
-
46
- if exclude is not None:
47
- x1, y1, x2, y2 = (int(round(float(v))) for v in exclude)
48
- x1, y1 = max(0, x1), max(0, y1)
49
- x2, y2 = min(mask.shape[1], x2), min(mask.shape[0], y2)
50
- if x2 > x1 and y2 > y1:
51
- mask[y1:y2, x1:x2] = 0
52
-
53
- # Erode speckle before measuring: the box is an EXTREME of the mask, so one stray pixel moves it as much as the piece.
54
- mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
55
-
56
- if int(mask.sum()) < _MIN_CHANGED_PIXELS:
57
- return None
58
- return mask
59
-
60
-
61
- def _marks(mask) -> list:
62
- """X-projection, not 2-D components: a piece that splits vertically (a notch, a highlight) is still one mark."""
63
- on = (mask.sum(axis=0) >= _MIN_COLUMN_PIXELS)
64
- marks: list = []
65
- for x in np.nonzero(on)[0]:
66
- x = int(x)
67
- if marks and x - marks[-1][1] <= _MARK_GAP_PX:
68
- marks[-1][1] = x + 1
69
- else:
70
- marks.append([x, x + 1])
71
- return marks
72
-
73
-
74
- def changed_bbox(
75
- before_path: str,
76
- after_path: str,
77
- exclude: Optional[Sequence[float]] = None,
78
- ) -> Optional[list]:
79
- """`exclude` is the handle's own box: left in, the handle and progress bar would dominate the union."""
80
- mask = _change_mask(before_path, after_path, exclude)
81
- if mask is None:
82
- return None
83
- ys, xs = np.nonzero(mask)
84
- return [int(xs.min()), int(ys.min()), int(xs.max()) + 1, int(ys.max()) + 1]
85
-
86
-
87
- def locate_piece(
88
- before_path: str,
89
- after_path: str,
90
- travel: float,
91
- exclude: Optional[Sequence[float]] = None,
92
- ) -> Optional[dict]:
93
- mask = _change_mask(before_path, after_path, exclude)
94
- if mask is None:
95
- return None
96
- marks = _marks(mask)
97
- if not marks:
98
- return None
99
-
100
- # The piece is the RIGHTMOST mark that is still the same object; searched rather than `marks[-1]` because
101
- # a board animating a badge elsewhere puts a mark in the frame that is neither.
102
- ghost_w = float(marks[0][1] - marks[0][0])
103
- for x1, x2 in reversed(marks[1:]):
104
- piece_w = float(x2 - x1)
105
- if (piece_w <= _CLEARED_MARK_FRACTION * travel
106
- and abs(piece_w - ghost_w) <= max(3.0, _SAME_WIDTH_TOLERANCE * ghost_w)):
107
- return {"centre": (x1 + x2) / 2.0, "width": piece_w}
108
-
109
- left, right = float(marks[0][0]), float(marks[-1][1])
110
- width = right - left
111
- # Too narrow to hold both: one mark only. If it were the piece it began a `travel` to the left, and when that
112
- # is off the board it cannot be, so it is the vacated ground and the piece is a travel beyond it.
113
- if travel > 0 and width < travel:
114
- centre = left + width / 2.0
115
- return {"centre": centre + travel if centre - travel < 0 else centre,
116
- "width": width}
117
- piece_w = width - max(0.0, travel)
118
- if piece_w <= 0:
119
- return None
120
- return {"centre": right - piece_w / 2.0, "width": piece_w}