cjovs commited on
Commit
d933d48
·
verified ·
1 Parent(s): 821145f

Support DEEPSEEK_ACCOUNT2 and DEEPSEEK_ACCOUNT3 secrets

Browse files
Files changed (2) hide show
  1. app/config.py +48 -18
  2. start_hf.py +18 -7
app/config.py CHANGED
@@ -7,15 +7,15 @@ logger = logging.getLogger(__name__)
7
  CONFIG_PATH = os.getenv("CONFIG_PATH", "config.json")
8
  SENSITIVE_ACCOUNT_FIELDS = {"password", "token", "hif_dliq", "hif_leim"}
9
  KEYS_PLACEHOLDER = "$DEEPSEEK_API_KEY"
10
- ACCOUNT_PLACEHOLDER = "$DEEPSEEK_ACCOUNT"
11
  PASSWORD_PLACEHOLDER = "$DEEPSEEK_PASSWORD"
 
12
 
13
 
14
- def _account_from_env() -> dict:
15
- account = os.getenv("DEEPSEEK_ACCOUNT", "").strip()
16
- password = os.getenv("DEEPSEEK_PASSWORD", "").strip()
17
- if not account or not password:
18
- return {}
19
  env_account = {"password": password, "token": ""}
20
  if "@" in account:
21
  env_account["email"] = account
@@ -24,6 +24,21 @@ def _account_from_env() -> dict:
24
  return env_account
25
 
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def _merge_env_secrets(config: dict) -> dict:
28
  merged = dict(config) if isinstance(config, dict) else {}
29
 
@@ -31,33 +46,42 @@ def _merge_env_secrets(config: dict) -> dict:
31
  if api_key:
32
  merged["keys"] = [api_key]
33
 
34
- env_account = _account_from_env()
35
- if env_account:
36
  accounts = merged.get("accounts")
37
  if not isinstance(accounts, list):
38
  accounts = []
39
- desired_identity = env_account.get("email") or env_account.get("mobile")
40
- identity_key = "email" if "email" in env_account else "mobile"
41
- replaced = False
 
 
42
  merged_accounts = []
43
  for account in accounts:
44
  if not isinstance(account, dict):
45
  continue
46
  current_identity = account.get("email") or account.get("mobile")
47
- if current_identity in {desired_identity, ACCOUNT_PLACEHOLDER}:
 
 
 
 
 
 
 
48
  updated = {
49
  k: v
50
  for k, v in account.items()
51
  if k not in {"email", "mobile", "password", "token", "hif_dliq", "hif_leim"}
52
  }
53
  updated[identity_key] = desired_identity
54
- updated["password"] = env_account["password"]
55
  merged_accounts.append(updated)
56
- replaced = True
57
  else:
58
  merged_accounts.append(account)
59
- if not replaced:
60
- merged_accounts.append(env_account)
61
  merged["accounts"] = merged_accounts
62
 
63
  return merged
@@ -79,8 +103,14 @@ def _sanitize_for_persistence(obj):
79
  continue
80
  if key in SENSITIVE_ACCOUNT_FIELDS:
81
  continue
82
- if key in {"mobile", "email"} and os.getenv("DEEPSEEK_ACCOUNT", "").strip():
83
- sanitized[key] = ACCOUNT_PLACEHOLDER
 
 
 
 
 
 
84
  continue
85
  sanitized[key] = _sanitize_for_persistence(value)
86
  return sanitized
 
7
  CONFIG_PATH = os.getenv("CONFIG_PATH", "config.json")
8
  SENSITIVE_ACCOUNT_FIELDS = {"password", "token", "hif_dliq", "hif_leim"}
9
  KEYS_PLACEHOLDER = "$DEEPSEEK_API_KEY"
 
10
  PASSWORD_PLACEHOLDER = "$DEEPSEEK_PASSWORD"
11
+ ACCOUNT_ENV_NAMES = ["DEEPSEEK_ACCOUNT", "DEEPSEEK_ACCOUNT2", "DEEPSEEK_ACCOUNT3"]
12
 
13
 
14
+ def _account_placeholders() -> set[str]:
15
+ return {f"${name}" for name in ACCOUNT_ENV_NAMES}
16
+
17
+
18
+ def _account_from_value(account: str, password: str) -> dict:
19
  env_account = {"password": password, "token": ""}
20
  if "@" in account:
21
  env_account["email"] = account
 
24
  return env_account
25
 
26
 
27
+ def _accounts_from_env() -> list[dict]:
28
+ password = os.getenv("DEEPSEEK_PASSWORD", "").strip()
29
+ if not password:
30
+ return []
31
+ accounts = []
32
+ seen = set()
33
+ for env_name in ACCOUNT_ENV_NAMES:
34
+ account = os.getenv(env_name, "").strip()
35
+ if not account or account in seen:
36
+ continue
37
+ seen.add(account)
38
+ accounts.append(_account_from_value(account, password))
39
+ return accounts
40
+
41
+
42
  def _merge_env_secrets(config: dict) -> dict:
43
  merged = dict(config) if isinstance(config, dict) else {}
44
 
 
46
  if api_key:
47
  merged["keys"] = [api_key]
48
 
49
+ env_accounts = _accounts_from_env()
50
+ if env_accounts:
51
  accounts = merged.get("accounts")
52
  if not isinstance(accounts, list):
53
  accounts = []
54
+ placeholders = _account_placeholders()
55
+ desired_accounts = {
56
+ (account.get("email") or account.get("mobile")): account
57
+ for account in env_accounts
58
+ }
59
  merged_accounts = []
60
  for account in accounts:
61
  if not isinstance(account, dict):
62
  continue
63
  current_identity = account.get("email") or account.get("mobile")
64
+ if current_identity in desired_accounts or current_identity in placeholders:
65
+ desired_account = desired_accounts.get(current_identity)
66
+ if desired_account is None and len(desired_accounts) == 1:
67
+ desired_account = next(iter(desired_accounts.values()))
68
+ if desired_account is None:
69
+ continue
70
+ desired_identity = desired_account.get("email") or desired_account.get("mobile")
71
+ identity_key = "email" if "email" in desired_account else "mobile"
72
  updated = {
73
  k: v
74
  for k, v in account.items()
75
  if k not in {"email", "mobile", "password", "token", "hif_dliq", "hif_leim"}
76
  }
77
  updated[identity_key] = desired_identity
78
+ updated["password"] = desired_account["password"]
79
  merged_accounts.append(updated)
80
+ desired_accounts.pop(desired_identity, None)
81
  else:
82
  merged_accounts.append(account)
83
+ for remaining in desired_accounts.values():
84
+ merged_accounts.append(remaining)
85
  merged["accounts"] = merged_accounts
86
 
87
  return merged
 
103
  continue
104
  if key in SENSITIVE_ACCOUNT_FIELDS:
105
  continue
106
+ if key in {"mobile", "email"} and isinstance(value, str):
107
+ for env_name in ACCOUNT_ENV_NAMES:
108
+ env_value = os.getenv(env_name, "").strip()
109
+ if env_value and value == env_value:
110
+ sanitized[key] = f"${env_name}"
111
+ break
112
+ else:
113
+ sanitized[key] = _sanitize_for_persistence(value)
114
  continue
115
  sanitized[key] = _sanitize_for_persistence(value)
116
  return sanitized
start_hf.py CHANGED
@@ -7,8 +7,8 @@ from pathlib import Path
7
 
8
  SENSITIVE_FIELDS = {"keys", "password", "token", "hif_dliq", "hif_leim"}
9
  KEYS_PLACEHOLDER = "$DEEPSEEK_API_KEY"
10
- ACCOUNT_PLACEHOLDER = "$DEEPSEEK_ACCOUNT"
11
  PASSWORD_PLACEHOLDER = "$DEEPSEEK_PASSWORD"
 
12
 
13
 
14
  def _sanitize_for_persistence(obj):
@@ -27,8 +27,14 @@ def _sanitize_for_persistence(obj):
27
  continue
28
  if key in SENSITIVE_FIELDS:
29
  continue
30
- if key in {"mobile", "email"} and os.getenv("DEEPSEEK_ACCOUNT", "").strip():
31
- sanitized[key] = ACCOUNT_PLACEHOLDER
 
 
 
 
 
 
32
  continue
33
  sanitized[key] = _sanitize_for_persistence(value)
34
  return sanitized
@@ -55,10 +61,15 @@ def prepare_runtime_files() -> None:
55
 
56
  if os.getenv("DEEPSEEK_API_KEY", "").strip():
57
  config.setdefault("keys", [])
58
- if os.getenv("DEEPSEEK_ACCOUNT", "").strip() and os.getenv("DEEPSEEK_PASSWORD", "").strip():
59
- accounts = config.get("accounts")
60
- if not isinstance(accounts, list) or not accounts:
61
- config["accounts"] = [{"mobile": ACCOUNT_PLACEHOLDER, "password": PASSWORD_PLACEHOLDER}]
 
 
 
 
 
62
 
63
  config_path.write_text(
64
  json.dumps(_sanitize_for_persistence(config), ensure_ascii=False, indent=2),
 
7
 
8
  SENSITIVE_FIELDS = {"keys", "password", "token", "hif_dliq", "hif_leim"}
9
  KEYS_PLACEHOLDER = "$DEEPSEEK_API_KEY"
 
10
  PASSWORD_PLACEHOLDER = "$DEEPSEEK_PASSWORD"
11
+ ACCOUNT_ENV_NAMES = ["DEEPSEEK_ACCOUNT", "DEEPSEEK_ACCOUNT2", "DEEPSEEK_ACCOUNT3"]
12
 
13
 
14
  def _sanitize_for_persistence(obj):
 
27
  continue
28
  if key in SENSITIVE_FIELDS:
29
  continue
30
+ if key in {"mobile", "email"} and isinstance(value, str):
31
+ for env_name in ACCOUNT_ENV_NAMES:
32
+ env_value = os.getenv(env_name, "").strip()
33
+ if env_value and value == env_value:
34
+ sanitized[key] = f"${env_name}"
35
+ break
36
+ else:
37
+ sanitized[key] = _sanitize_for_persistence(value)
38
  continue
39
  sanitized[key] = _sanitize_for_persistence(value)
40
  return sanitized
 
61
 
62
  if os.getenv("DEEPSEEK_API_KEY", "").strip():
63
  config.setdefault("keys", [])
64
+ if os.getenv("DEEPSEEK_PASSWORD", "").strip():
65
+ placeholder_accounts = []
66
+ for env_name in ACCOUNT_ENV_NAMES:
67
+ if os.getenv(env_name, "").strip():
68
+ placeholder_accounts.append({"mobile": f"${env_name}", "password": PASSWORD_PLACEHOLDER})
69
+ if placeholder_accounts:
70
+ accounts = config.get("accounts")
71
+ if not isinstance(accounts, list) or not accounts:
72
+ config["accounts"] = placeholder_accounts
73
 
74
  config_path.write_text(
75
  json.dumps(_sanitize_for_persistence(config), ensure_ascii=False, indent=2),