File size: 13,410 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
import os
import queue
import signal
import subprocess
import sys
import threading
import time
from typing import Optional

from launcher.config import ENDPOINT_CAPTURE_TIMEOUT, PYTHON_EXECUTABLE, ws_regex

logger = logging.getLogger("CamoufoxLauncher")


def _enqueue_output(
    stream, stream_name, output_queue, process_pid_for_log="<unknown PID>"
):
    log_prefix = f"[ReadThread-{stream_name}-PID:{process_pid_for_log}]"
    try:
        for line_bytes in iter(stream.readline, b""):
            if not line_bytes:
                break
            try:
                line_str = line_bytes.decode("utf-8", errors="replace")
                output_queue.put((stream_name, line_str))
            except Exception as decode_err:
                logger.warning(
                    f"{log_prefix} Decode error: {decode_err}. Raw data (first 100 bytes): {line_bytes[:100]}"
                )
                output_queue.put(
                    (
                        stream_name,
                        f"[Decode error: {decode_err}] {line_bytes[:100]}...\n",
                    )
                )
    except ValueError:
        logger.debug(f"{log_prefix} ValueError (stream may be closed).")
    except Exception as e:
        logger.error(
            f"{log_prefix} Unexpected error reading stream: {e}", exc_info=True
        )
    finally:
        output_queue.put((stream_name, None))
        if hasattr(stream, "close") and not stream.closed:
            try:
                stream.close()
            except Exception:
                pass
        logger.debug(f"{log_prefix} Thread exiting.")


def build_launch_command(
    final_launch_mode: str,
    effective_active_auth_json_path: Optional[str],
    simulated_os_for_camoufox: str,
    camoufox_debug_port: int,
    internal_camoufox_proxy: Optional[str],
) -> list[str]:
    """
    Build the command-line arguments for launching the internal Camoufox process.

    This is a pure function (no I/O) that can be easily unit tested.

    Args:
        final_launch_mode: The launch mode (headless, virtual_headless, debug)
        effective_active_auth_json_path: Path to auth file, or None
        simulated_os_for_camoufox: OS to simulate (linux, windows, macos)
        camoufox_debug_port: Debug port for Camoufox
        internal_camoufox_proxy: Proxy configuration, or None

    Returns:
        List of command-line arguments for subprocess.Popen
    """
    cmd = [
        PYTHON_EXECUTABLE,
        "-u",
        sys.argv[0],
        "--internal-launch-mode",
        final_launch_mode,
    ]

    if effective_active_auth_json_path:
        cmd.extend(["--internal-auth-file", effective_active_auth_json_path])

    cmd.extend(["--internal-camoufox-os", simulated_os_for_camoufox])
    cmd.extend(["--internal-camoufox-port", str(camoufox_debug_port)])

    if internal_camoufox_proxy is not None:
        cmd.extend(["--internal-camoufox-proxy", internal_camoufox_proxy])

    return cmd


class CamoufoxProcessManager:
    def __init__(self):
        self.camoufox_proc = None
        self.captured_ws_endpoint = None

    def start(
        self,
        final_launch_mode,
        effective_active_auth_json_path,
        simulated_os_for_camoufox,
        args,
    ):
        # Build Camoufox internal launch command (from dev)
        camoufox_internal_cmd_args = build_launch_command(
            final_launch_mode,
            effective_active_auth_json_path,
            simulated_os_for_camoufox,
            args.camoufox_debug_port,
            args.internal_camoufox_proxy,
        )

        camoufox_popen_kwargs = {
            "stdout": subprocess.PIPE,
            "stderr": subprocess.PIPE,
            "env": os.environ.copy(),
        }
        camoufox_popen_kwargs["env"]["PYTHONIOENCODING"] = "utf-8"
        if sys.platform != "win32" and final_launch_mode != "debug":
            camoufox_popen_kwargs["start_new_session"] = True
        elif sys.platform == "win32" and (
            final_launch_mode == "headless" or final_launch_mode == "virtual_headless"
        ):
            camoufox_popen_kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW

        try:
            logger.debug(
                f"Executing Camoufox internal launch command: {' '.join(camoufox_internal_cmd_args)}"
            )
            self.camoufox_proc = subprocess.Popen(
                camoufox_internal_cmd_args, **camoufox_popen_kwargs
            )
            logger.info(
                f"Camoufox internal process started (PID: {self.camoufox_proc.pid}). Waiting for WebSocket endpoint output (max {ENDPOINT_CAPTURE_TIMEOUT} seconds)..."
            )

            camoufox_output_q = queue.Queue()
            camoufox_stdout_reader = threading.Thread(
                target=_enqueue_output,
                args=(
                    self.camoufox_proc.stdout,
                    "stdout",
                    camoufox_output_q,
                    self.camoufox_proc.pid,
                ),
                daemon=True,
            )
            camoufox_stderr_reader = threading.Thread(
                target=_enqueue_output,
                args=(
                    self.camoufox_proc.stderr,
                    "stderr",
                    camoufox_output_q,
                    self.camoufox_proc.pid,
                ),
                daemon=True,
            )
            camoufox_stdout_reader.start()
            camoufox_stderr_reader.start()

            ws_capture_start_time = time.time()
            camoufox_ended_streams_count = 0
            while time.time() - ws_capture_start_time < ENDPOINT_CAPTURE_TIMEOUT:
                if self.camoufox_proc.poll() is not None:
                    logger.error(
                        f"  Camoufox internal process (PID: {self.camoufox_proc.pid}) unexpectedly exited while waiting for WebSocket endpoint, exit code: {self.camoufox_proc.poll()}."
                    )
                    break
                try:
                    stream_name, line_from_camoufox = camoufox_output_q.get(timeout=0.2)
                    if line_from_camoufox is None:
                        camoufox_ended_streams_count += 1
                        logger.debug(
                            f"  [InternalCamoufox-{stream_name}-PID:{self.camoufox_proc.pid}] Output stream closed (EOF)."
                        )
                        if camoufox_ended_streams_count >= 2:
                            logger.info(
                                f"  Camoufox internal process (PID: {self.camoufox_proc.pid}) all output streams closed."
                            )
                            break
                        continue

                    # Skip the ugly prefix, just log the content
                    log_content = line_from_camoufox.rstrip()
                    # Skip verbose startup messages (move to debug)
                    if (
                        "[InternalCamoufoxStartup]" in log_content
                        or "passed to launch_server" in log_content
                    ):
                        logger.debug(f"(Camoufox) {log_content}")
                    elif (
                        stream_name == "stderr" or "ERROR" in line_from_camoufox.upper()
                    ):
                        logger.info(f"(Camoufox) {log_content}")
                    else:
                        logger.debug(f"(Camoufox) {log_content}")

                    ws_match = ws_regex.search(line_from_camoufox)
                    if ws_match:
                        self.captured_ws_endpoint = ws_match.group(1)
                        logger.debug(
                            f"Successfully captured WebSocket endpoint from Camoufox internal process: {self.captured_ws_endpoint[:40]}..."
                        )
                        logger.info("[Core] WebSocket endpoint obtained successfully")
                        break
                except queue.Empty:
                    continue

            if camoufox_stdout_reader.is_alive():
                camoufox_stdout_reader.join(timeout=1.0)
            if camoufox_stderr_reader.is_alive():
                camoufox_stderr_reader.join(timeout=1.0)

            if not self.captured_ws_endpoint and (
                self.camoufox_proc and self.camoufox_proc.poll() is None
            ):
                logger.error(
                    f"  Failed to capture WebSocket endpoint from Camoufox internal process (PID: {self.camoufox_proc.pid}) within {ENDPOINT_CAPTURE_TIMEOUT} seconds."
                )
                logger.error(
                    "  Camoufox internal process is still running but did not output expected WebSocket endpoint. Please check its logs or behavior."
                )
                self.cleanup()
                sys.exit(1)
            elif not self.captured_ws_endpoint and (
                self.camoufox_proc and self.camoufox_proc.poll() is not None
            ):
                logger.error(
                    "Camoufox internal process exited and failed to capture WebSocket endpoint."
                )
                sys.exit(1)
            elif not self.captured_ws_endpoint:
                logger.error("Failed to capture WebSocket endpoint.")
                sys.exit(1)

        except Exception as e_launch_camoufox_internal:
            logger.critical(
                f"  Fatal error launching Camoufox internally or capturing its WebSocket endpoint: {e_launch_camoufox_internal}",
                exc_info=True,
            )
            self.cleanup()
            sys.exit(1)

        return self.captured_ws_endpoint

    def cleanup(self):
        logger.info("--- Starting cleanup procedure (CamoufoxProcessManager) ---")
        if self.camoufox_proc and self.camoufox_proc.poll() is None:
            pid = self.camoufox_proc.pid
            logger.info(f"Terminating Camoufox process tree (PID: {pid})...")
            try:
                if sys.platform == "win32":
                    # Windows: Force terminate directly, don't try graceful shutdown (headless browsers often hang)
                    try:
                        subprocess.run(
                            ["taskkill", "/F", "/T", "/PID", str(pid)],
                            capture_output=True,
                            text=True,
                            check=False,
                            timeout=5,
                        )
                        logger.info("Process tree successfully terminated.")
                    except subprocess.TimeoutExpired:
                        logger.warning(
                            "taskkill timed out, process may have terminated."
                        )
                    except Exception as e:
                        logger.warning(f"taskkill execution exception: {e}")
                elif hasattr(os, "getpgid") and hasattr(os, "killpg"):
                    # Unix: Try SIGTERM, then SIGKILL on timeout
                    try:
                        pgid = os.getpgid(pid)
                        os.killpg(pgid, signal.SIGTERM)
                        self.camoufox_proc.wait(timeout=5)
                        logger.info(
                            f"Process group (PGID: {pgid}) successfully terminated via SIGTERM."
                        )
                    except subprocess.TimeoutExpired:
                        logger.warning("SIGTERM timed out, sending SIGKILL...")
                        try:
                            os.killpg(os.getpgid(pid), signal.SIGKILL)
                            self.camoufox_proc.wait(timeout=2)
                            logger.info(
                                "Process group successfully terminated via SIGKILL."
                            )
                        except Exception:
                            pass
                    except ProcessLookupError:
                        logger.info("Process group not found, may have already exited.")
                else:
                    # Fallback: Terminate process directly
                    self.camoufox_proc.terminate()
                    try:
                        self.camoufox_proc.wait(timeout=5)
                        logger.info("Process successfully terminated.")
                    except subprocess.TimeoutExpired:
                        self.camoufox_proc.kill()
                        logger.info("Process forcefully terminated.")
            except Exception as e_term:
                logger.warning(f"Error terminating process: {e_term}")
            finally:
                # Clean up streams
                for stream in [self.camoufox_proc.stdout, self.camoufox_proc.stderr]:
                    if stream and not stream.closed:
                        try:
                            stream.close()
                        except Exception:
                            pass
            self.camoufox_proc = None
        elif self.camoufox_proc:
            logger.info(
                f"Camoufox internal subprocess previously exited on its own, exit code: {self.camoufox_proc.poll()}."
            )
            self.camoufox_proc = None
        else:
            logger.info(
                "Camoufox internal subprocess not running or already cleaned up."
            )
        logger.info("--- Cleanup procedure completed (CamoufoxProcessManager) ---")