File size: 1,446 Bytes
48d895c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Proxy clearance config helpers shared by control and dataplane code."""

from dataclasses import dataclass
from typing import Any

from app.platform.config.snapshot import get_config


@dataclass(frozen=True)
class ClearanceConfig:
    cf_cookies: str = ""
    user_agent: str = ""
    cf_clearance: str = ""
    browser: str = ""


def _cfg_str(cfg: Any, key: str) -> str:
    value = cfg.get_str(key, "")
    return value if value.strip() else ""


def first_config_str(cfg: Any, *keys: str) -> str:
    for key in keys:
        value = _cfg_str(cfg, key)
        if value:
            return value
    return ""


def resolve_clearance_config(cfg: Any | None = None) -> ClearanceConfig:
    cfg = cfg or get_config()
    return ClearanceConfig(
        cf_cookies=first_config_str(
            cfg,
            "proxy.cf_cookies",
            "proxy.clearance.cf_cookies",
        ),
        user_agent=first_config_str(
            cfg,
            "proxy.user_agent",
            "proxy.clearance.user_agent",
        ),
        cf_clearance=first_config_str(
            cfg,
            "proxy.cf_clearance",
            "proxy.clearance.cf_clearance",
        ),
        browser=first_config_str(
            cfg,
            "proxy.browser",
            "proxy.clearance.browser",
        ),
    )


__all__ = ["ClearanceConfig", "first_config_str", "resolve_clearance_config"]