dsh-plugin-image-workbench 1.1.5 → 1.1.6

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 (172) hide show
  1. package/LICENSE +21 -21
  2. package/README.en.md +5 -3
  3. package/README.md +7 -3
  4. package/client.js +241 -241
  5. package/cordis.patch.yml +14 -14
  6. package/host.js +51 -0
  7. package/install.mjs +333 -387
  8. package/package.json +3 -2
  9. package/scripts/fetch-style-images.mjs +104 -0
  10. package/scripts/vendor-snapshot.py +154 -154
  11. package/server/access.py +26 -26
  12. package/server/app.py +1329 -1329
  13. package/server/catalog.py +147 -147
  14. package/server/composer.py +391 -391
  15. package/server/drafts.py +158 -158
  16. package/server/history.py +575 -575
  17. package/server/imaging.py +172 -172
  18. package/server/pool.py +1335 -1335
  19. package/server/pool_admin.py +921 -921
  20. package/server/pool_service.py +1073 -1073
  21. package/server/requirements.txt +6 -6
  22. package/server/smoke_check.py +402 -402
  23. package/server/static/access.html +202 -202
  24. package/vendor/chatgpt2api/source/.env.example +82 -82
  25. package/vendor/chatgpt2api/source/.python-version +1 -1
  26. package/vendor/chatgpt2api/source/LICENSE +21 -21
  27. package/vendor/chatgpt2api/source/README.md +391 -391
  28. package/vendor/chatgpt2api/source/VERSION +1 -1
  29. package/vendor/chatgpt2api/source/api/__init__.py +2 -2
  30. package/vendor/chatgpt2api/source/api/accounts.py +537 -537
  31. package/vendor/chatgpt2api/source/api/ai.py +217 -217
  32. package/vendor/chatgpt2api/source/api/app.py +62 -62
  33. package/vendor/chatgpt2api/source/api/errors.py +46 -46
  34. package/vendor/chatgpt2api/source/api/image_inputs.py +309 -309
  35. package/vendor/chatgpt2api/source/api/image_tasks.py +120 -120
  36. package/vendor/chatgpt2api/source/api/support.py +134 -134
  37. package/vendor/chatgpt2api/source/api/system.py +365 -365
  38. package/vendor/chatgpt2api/source/assets/kunrouai.svg +50 -50
  39. package/vendor/chatgpt2api/source/config.default.json +108 -108
  40. package/vendor/chatgpt2api/source/main.py +9 -9
  41. package/vendor/chatgpt2api/source/pyproject.toml +27 -27
  42. package/vendor/chatgpt2api/source/scripts/init_proxy_config.py +186 -186
  43. package/vendor/chatgpt2api/source/scripts/migrate_storage.py +163 -163
  44. package/vendor/chatgpt2api/source/scripts/privoxy-warp.conf +13 -13
  45. package/vendor/chatgpt2api/source/scripts/test_outlook_token_mailbox.py +385 -385
  46. package/vendor/chatgpt2api/source/scripts/test_storage.py +129 -129
  47. package/vendor/chatgpt2api/source/scripts/verify_oauth_refresh.py +92 -92
  48. package/vendor/chatgpt2api/source/services/account_service.py +1757 -1757
  49. package/vendor/chatgpt2api/source/services/auth_service.py +253 -253
  50. package/vendor/chatgpt2api/source/services/backup_service.py +671 -671
  51. package/vendor/chatgpt2api/source/services/config.py +667 -667
  52. package/vendor/chatgpt2api/source/services/content_filter.py +242 -242
  53. package/vendor/chatgpt2api/source/services/cpa_service.py +315 -315
  54. package/vendor/chatgpt2api/source/services/editable_file_task_service.py +260 -260
  55. package/vendor/chatgpt2api/source/services/image_service.py +376 -376
  56. package/vendor/chatgpt2api/source/services/image_storage_service.py +405 -405
  57. package/vendor/chatgpt2api/source/services/image_tags_service.py +76 -76
  58. package/vendor/chatgpt2api/source/services/image_task_service.py +550 -550
  59. package/vendor/chatgpt2api/source/services/log_service.py +337 -337
  60. package/vendor/chatgpt2api/source/services/model_service.py +182 -182
  61. package/vendor/chatgpt2api/source/services/oauth_login_service.py +267 -267
  62. package/vendor/chatgpt2api/source/services/openai_backend_api.py +2763 -2763
  63. package/vendor/chatgpt2api/source/services/openai_oauth.py +32 -32
  64. package/vendor/chatgpt2api/source/services/protocol/__init__.py +2 -2
  65. package/vendor/chatgpt2api/source/services/protocol/anthropic_v1_messages.py +307 -307
  66. package/vendor/chatgpt2api/source/services/protocol/chat_completion_cache.py +238 -238
  67. package/vendor/chatgpt2api/source/services/protocol/conversation.py +1644 -1644
  68. package/vendor/chatgpt2api/source/services/protocol/error_response.py +124 -124
  69. package/vendor/chatgpt2api/source/services/protocol/openai_search.py +19 -19
  70. package/vendor/chatgpt2api/source/services/protocol/openai_v1_chat_complete.py +316 -316
  71. package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_edit.py +86 -86
  72. package/vendor/chatgpt2api/source/services/protocol/openai_v1_image_generations.py +42 -42
  73. package/vendor/chatgpt2api/source/services/protocol/openai_v1_models.py +53 -53
  74. package/vendor/chatgpt2api/source/services/protocol/openai_v1_response.py +453 -453
  75. package/vendor/chatgpt2api/source/services/protocol/web_search_tool.py +160 -160
  76. package/vendor/chatgpt2api/source/services/proxy_service.py +648 -648
  77. package/vendor/chatgpt2api/source/services/storage/__init__.py +5 -5
  78. package/vendor/chatgpt2api/source/services/storage/base.py +38 -38
  79. package/vendor/chatgpt2api/source/services/storage/database_storage.py +194 -194
  80. package/vendor/chatgpt2api/source/services/storage/factory.py +104 -104
  81. package/vendor/chatgpt2api/source/services/storage/git_storage.py +183 -183
  82. package/vendor/chatgpt2api/source/services/storage/json_storage.py +95 -95
  83. package/vendor/chatgpt2api/source/services/sub2api_service.py +531 -531
  84. package/vendor/chatgpt2api/source/utils/helper.py +469 -469
  85. package/vendor/chatgpt2api/source/utils/image_tokens.py +346 -346
  86. package/vendor/chatgpt2api/source/utils/log.py +110 -110
  87. package/vendor/chatgpt2api/source/utils/pkce.py +19 -19
  88. package/vendor/chatgpt2api/source/utils/pow.py +207 -207
  89. package/vendor/chatgpt2api/source/utils/sentinel.py +159 -159
  90. package/vendor/chatgpt2api/source/utils/turnstile.py +184 -184
  91. package/vendor/chatgpt2api/source/uv.lock +904 -904
  92. package/vendor/chatgpt2api/source/web/bun.lock +1501 -1501
  93. package/vendor/chatgpt2api/source/web/components.json +24 -24
  94. package/vendor/chatgpt2api/source/web/eslint.config.mjs +17 -17
  95. package/vendor/chatgpt2api/source/web/next.config.ts +40 -40
  96. package/vendor/chatgpt2api/source/web/package.json +58 -58
  97. package/vendor/chatgpt2api/source/web/postcss.config.mjs +5 -5
  98. package/vendor/chatgpt2api/source/web/src/app/accounts/components/account-import-dialog.tsx +925 -925
  99. package/vendor/chatgpt2api/source/web/src/app/accounts/page.tsx +1307 -1307
  100. package/vendor/chatgpt2api/source/web/src/app/debug/components/chat-panel.tsx +224 -224
  101. package/vendor/chatgpt2api/source/web/src/app/debug/components/editable-file-panel.tsx +444 -444
  102. package/vendor/chatgpt2api/source/web/src/app/debug/components/ppt-panel.tsx +9 -9
  103. package/vendor/chatgpt2api/source/web/src/app/debug/components/psd-panel.tsx +9 -9
  104. package/vendor/chatgpt2api/source/web/src/app/debug/components/search-panel.tsx +172 -172
  105. package/vendor/chatgpt2api/source/web/src/app/debug/components/skill-panel.tsx +169 -169
  106. package/vendor/chatgpt2api/source/web/src/app/debug/components/types.ts +39 -39
  107. package/vendor/chatgpt2api/source/web/src/app/debug/page.tsx +59 -59
  108. package/vendor/chatgpt2api/source/web/src/app/globals.css +230 -230
  109. package/vendor/chatgpt2api/source/web/src/app/image/components/image-composer.tsx +542 -542
  110. package/vendor/chatgpt2api/source/web/src/app/image/components/image-results.tsx +554 -554
  111. package/vendor/chatgpt2api/source/web/src/app/image/components/image-sidebar.tsx +176 -176
  112. package/vendor/chatgpt2api/source/web/src/app/image/page.tsx +1799 -1799
  113. package/vendor/chatgpt2api/source/web/src/app/image-manager/page.tsx +732 -732
  114. package/vendor/chatgpt2api/source/web/src/app/layout.tsx +50 -50
  115. package/vendor/chatgpt2api/source/web/src/app/login/page.tsx +101 -101
  116. package/vendor/chatgpt2api/source/web/src/app/logs/page.tsx +356 -356
  117. package/vendor/chatgpt2api/source/web/src/app/page.tsx +30 -30
  118. package/vendor/chatgpt2api/source/web/src/app/settings/components/api-docs-card.tsx +341 -341
  119. package/vendor/chatgpt2api/source/web/src/app/settings/components/backup-settings-card.tsx +447 -447
  120. package/vendor/chatgpt2api/source/web/src/app/settings/components/base-url-card.tsx +71 -71
  121. package/vendor/chatgpt2api/source/web/src/app/settings/components/config-card.tsx +491 -491
  122. package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pool-dialog.tsx +108 -108
  123. package/vendor/chatgpt2api/source/web/src/app/settings/components/cpa-pools-card.tsx +169 -169
  124. package/vendor/chatgpt2api/source/web/src/app/settings/components/import-browser-dialog.tsx +185 -185
  125. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-runtime-card.tsx +379 -379
  126. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings-card.tsx +127 -127
  127. package/vendor/chatgpt2api/source/web/src/app/settings/components/proxy-settings.tsx +200 -200
  128. package/vendor/chatgpt2api/source/web/src/app/settings/components/settings-header.tsx +12 -12
  129. package/vendor/chatgpt2api/source/web/src/app/settings/components/sub2api-connections.tsx +886 -886
  130. package/vendor/chatgpt2api/source/web/src/app/settings/components/third-party-apps-card.tsx +81 -81
  131. package/vendor/chatgpt2api/source/web/src/app/settings/components/user-keys-card.tsx +422 -422
  132. package/vendor/chatgpt2api/source/web/src/app/settings/page.tsx +135 -135
  133. package/vendor/chatgpt2api/source/web/src/app/settings/store.ts +1069 -1069
  134. package/vendor/chatgpt2api/source/web/src/components/date-range-filter.tsx +49 -49
  135. package/vendor/chatgpt2api/source/web/src/components/header-actions.tsx +24 -24
  136. package/vendor/chatgpt2api/source/web/src/components/image-lightbox.tsx +434 -434
  137. package/vendor/chatgpt2api/source/web/src/components/image-thumbnail.tsx +46 -46
  138. package/vendor/chatgpt2api/source/web/src/components/theme-script.tsx +18 -18
  139. package/vendor/chatgpt2api/source/web/src/components/theme-toggle.tsx +14 -14
  140. package/vendor/chatgpt2api/source/web/src/components/top-nav.tsx +263 -263
  141. package/vendor/chatgpt2api/source/web/src/components/ui/animated-theme-toggler.tsx +256 -256
  142. package/vendor/chatgpt2api/source/web/src/components/ui/badge.tsx +41 -41
  143. package/vendor/chatgpt2api/source/web/src/components/ui/button.tsx +59 -59
  144. package/vendor/chatgpt2api/source/web/src/components/ui/calendar.tsx +46 -46
  145. package/vendor/chatgpt2api/source/web/src/components/ui/card.tsx +61 -61
  146. package/vendor/chatgpt2api/source/web/src/components/ui/checkbox.tsx +27 -27
  147. package/vendor/chatgpt2api/source/web/src/components/ui/dia-text-reveal.tsx +267 -267
  148. package/vendor/chatgpt2api/source/web/src/components/ui/dialog.tsx +132 -132
  149. package/vendor/chatgpt2api/source/web/src/components/ui/field.tsx +13 -13
  150. package/vendor/chatgpt2api/source/web/src/components/ui/input.tsx +19 -19
  151. package/vendor/chatgpt2api/source/web/src/components/ui/label.tsx +24 -24
  152. package/vendor/chatgpt2api/source/web/src/components/ui/morphing-text.tsx +149 -149
  153. package/vendor/chatgpt2api/source/web/src/components/ui/popover.tsx +38 -38
  154. package/vendor/chatgpt2api/source/web/src/components/ui/select.tsx +175 -175
  155. package/vendor/chatgpt2api/source/web/src/components/ui/sheet.tsx +143 -143
  156. package/vendor/chatgpt2api/source/web/src/components/ui/table.tsx +29 -29
  157. package/vendor/chatgpt2api/source/web/src/components/ui/tabs.tsx +91 -91
  158. package/vendor/chatgpt2api/source/web/src/components/ui/text-3d-flip.tsx +269 -269
  159. package/vendor/chatgpt2api/source/web/src/components/ui/textarea.tsx +18 -18
  160. package/vendor/chatgpt2api/source/web/src/components/version-release-dialog.tsx +127 -127
  161. package/vendor/chatgpt2api/source/web/src/constants/common-env.ts +6 -6
  162. package/vendor/chatgpt2api/source/web/src/hooks/use-version-check.ts +85 -85
  163. package/vendor/chatgpt2api/source/web/src/lib/api.ts +926 -926
  164. package/vendor/chatgpt2api/source/web/src/lib/auth-session.ts +26 -26
  165. package/vendor/chatgpt2api/source/web/src/lib/release.ts +25 -25
  166. package/vendor/chatgpt2api/source/web/src/lib/request.ts +93 -93
  167. package/vendor/chatgpt2api/source/web/src/lib/use-auth-guard.ts +89 -89
  168. package/vendor/chatgpt2api/source/web/src/lib/utils.ts +6 -6
  169. package/vendor/chatgpt2api/source/web/src/store/auth.ts +112 -112
  170. package/vendor/chatgpt2api/source/web/src/store/editable-file-history.ts +33 -33
  171. package/vendor/chatgpt2api/source/web/src/store/image-conversations.ts +318 -318
  172. package/vendor/chatgpt2api/source/web/tsconfig.json +42 -42
@@ -1,671 +1,671 @@
1
- from __future__ import annotations
2
-
3
- import hashlib
4
- import hmac
5
- import io
6
- import json
7
- import os
8
- import random
9
- import subprocess
10
- import tarfile
11
- import threading
12
- from datetime import UTC, datetime
13
- from pathlib import Path
14
- from urllib.parse import quote, urlencode
15
-
16
- from curl_cffi import requests
17
-
18
- from services.config import BASE_DIR, CONFIG_FILE, DATA_DIR, config, load_backup_state, save_backup_state
19
- from services.image_storage_service import IMAGE_INDEX_FILE
20
- from services.image_tags_service import TAGS_FILE
21
-
22
-
23
- def _utc_now() -> datetime:
24
- return datetime.now(UTC)
25
-
26
-
27
- def _iso_now() -> str:
28
- return _utc_now().replace(microsecond=0).isoformat().replace("+00:00", "Z")
29
-
30
-
31
- def _clean(value: object) -> str:
32
- return str(value or "").strip()
33
-
34
-
35
- def _sha256_hex(value: bytes) -> str:
36
- return hashlib.sha256(value).hexdigest()
37
-
38
-
39
- def _is_backup_object(key: object) -> bool:
40
- name = _clean(key).rsplit("/", 1)[-1]
41
- return name.startswith("backup-") and (name.endswith(".tar.gz") or name.endswith(".tar.gz.enc"))
42
-
43
-
44
- def _hmac_sha256(key: bytes, message: str) -> bytes:
45
- return hmac.new(key, message.encode("utf-8"), hashlib.sha256).digest()
46
-
47
-
48
- def _openssl_encrypt(data: bytes, passphrase: str) -> bytes:
49
- env = dict(os.environ)
50
- env["CHATGPT2API_BACKUP_PASSPHRASE"] = passphrase
51
- try:
52
- result = subprocess.run(
53
- [
54
- "openssl",
55
- "enc",
56
- "-aes-256-cbc",
57
- "-pbkdf2",
58
- "-salt",
59
- "-md",
60
- "sha256",
61
- "-pass",
62
- "env:CHATGPT2API_BACKUP_PASSPHRASE",
63
- ],
64
- input=data,
65
- stdout=subprocess.PIPE,
66
- stderr=subprocess.PIPE,
67
- check=True,
68
- env=env,
69
- )
70
- except FileNotFoundError as exc:
71
- raise BackupError("当前环境缺少 openssl,无法执行加密备份") from exc
72
- except subprocess.CalledProcessError as exc:
73
- detail = (exc.stderr or b"").decode("utf-8", errors="replace").strip()
74
- raise BackupError(f"加密备份失败:{detail or 'openssl 执行失败'}") from exc
75
- return result.stdout
76
-
77
-
78
- def _openssl_decrypt(data: bytes, passphrase: str) -> bytes:
79
- env = dict(os.environ)
80
- env["CHATGPT2API_BACKUP_PASSPHRASE"] = passphrase
81
- try:
82
- result = subprocess.run(
83
- [
84
- "openssl",
85
- "enc",
86
- "-d",
87
- "-aes-256-cbc",
88
- "-pbkdf2",
89
- "-md",
90
- "sha256",
91
- "-pass",
92
- "env:CHATGPT2API_BACKUP_PASSPHRASE",
93
- ],
94
- input=data,
95
- stdout=subprocess.PIPE,
96
- stderr=subprocess.PIPE,
97
- check=True,
98
- env=env,
99
- )
100
- except FileNotFoundError as exc:
101
- raise BackupError("当前环境缺少 openssl,无法解密备份内容") from exc
102
- except subprocess.CalledProcessError as exc:
103
- detail = (exc.stderr or b"").decode("utf-8", errors="replace").strip()
104
- raise BackupError(f"解密备份失败:{detail or 'openssl 执行失败'}") from exc
105
- return result.stdout
106
-
107
-
108
- def _guess_content_type(name: str) -> str:
109
- if name.endswith(".json"):
110
- return "application/json"
111
- if name.endswith(".jsonl"):
112
- return "application/x-ndjson"
113
- if name.endswith(".tar.gz"):
114
- return "application/gzip"
115
- if name.endswith(".gz"):
116
- return "application/gzip"
117
- return "application/octet-stream"
118
-
119
-
120
- def _json_bytes(value: object) -> bytes:
121
- return json.dumps(value, ensure_ascii=False, indent=2).encode("utf-8")
122
-
123
-
124
- def _count_items(value: object) -> int:
125
- if isinstance(value, list):
126
- return len(value)
127
- if isinstance(value, dict):
128
- return len(value)
129
- return 0
130
-
131
-
132
- class BackupError(RuntimeError):
133
- pass
134
-
135
-
136
- class CloudflareR2Client:
137
- def __init__(self, settings: dict[str, object]) -> None:
138
- self.account_id = _clean(settings.get("account_id"))
139
- self.access_key_id = _clean(settings.get("access_key_id"))
140
- self.secret_access_key = _clean(settings.get("secret_access_key"))
141
- self.bucket = _clean(settings.get("bucket"))
142
- self.prefix = _clean(settings.get("prefix")) or "backups"
143
- self.session = requests.Session(impersonate="chrome", verify=True)
144
-
145
- def validate(self) -> None:
146
- missing = []
147
- if not self.account_id:
148
- missing.append("Account ID")
149
- if not self.access_key_id:
150
- missing.append("Access Key ID")
151
- if not self.secret_access_key:
152
- missing.append("Secret Access Key")
153
- if not self.bucket:
154
- missing.append("Bucket")
155
- if missing:
156
- raise BackupError(f"R2 配置不完整:缺少 {'、'.join(missing)}")
157
-
158
- @property
159
- def endpoint(self) -> str:
160
- return f"https://{self.account_id}.r2.cloudflarestorage.com"
161
-
162
- def _aws_v4_headers(
163
- self,
164
- method: str,
165
- path: str,
166
- *,
167
- query: dict[str, str] | None = None,
168
- body: bytes = b"",
169
- extra_headers: dict[str, str] | None = None,
170
- ) -> tuple[str, dict[str, str]]:
171
- now = _utc_now()
172
- amz_date = now.strftime("%Y%m%dT%H%M%SZ")
173
- date_stamp = now.strftime("%Y%m%d")
174
- encoded_query = urlencode(sorted((query or {}).items()))
175
- payload_hash = _sha256_hex(body)
176
- host = f"{self.account_id}.r2.cloudflarestorage.com"
177
- headers = {
178
- "host": host,
179
- "x-amz-content-sha256": payload_hash,
180
- "x-amz-date": amz_date,
181
- }
182
- if extra_headers:
183
- for key, value in extra_headers.items():
184
- headers[key.lower()] = value.strip()
185
- sorted_items = sorted((key.lower(), " ".join(str(value).strip().split())) for key, value in headers.items())
186
- canonical_headers = "".join(f"{key}:{value}\n" for key, value in sorted_items)
187
- signed_headers = ";".join(key for key, _ in sorted_items)
188
- canonical_request = "\n".join([
189
- method.upper(),
190
- path,
191
- encoded_query,
192
- canonical_headers,
193
- signed_headers,
194
- payload_hash,
195
- ])
196
- credential_scope = f"{date_stamp}/auto/s3/aws4_request"
197
- string_to_sign = "\n".join([
198
- "AWS4-HMAC-SHA256",
199
- amz_date,
200
- credential_scope,
201
- _sha256_hex(canonical_request.encode("utf-8")),
202
- ])
203
- k_date = _hmac_sha256(("AWS4" + self.secret_access_key).encode("utf-8"), date_stamp)
204
- k_region = hmac.new(k_date, b"auto", hashlib.sha256).digest()
205
- k_service = hmac.new(k_region, b"s3", hashlib.sha256).digest()
206
- k_signing = hmac.new(k_service, b"aws4_request", hashlib.sha256).digest()
207
- signature = hmac.new(k_signing, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
208
- authorization = (
209
- "AWS4-HMAC-SHA256 "
210
- f"Credential={self.access_key_id}/{credential_scope}, "
211
- f"SignedHeaders={signed_headers}, "
212
- f"Signature={signature}"
213
- )
214
- request_headers = {key: value for key, value in headers.items()}
215
- request_headers["authorization"] = authorization
216
- return encoded_query, request_headers
217
-
218
- def _request(
219
- self,
220
- method: str,
221
- key: str = "",
222
- *,
223
- query: dict[str, str] | None = None,
224
- body: bytes = b"",
225
- extra_headers: dict[str, str] | None = None,
226
- timeout: float = 60.0,
227
- ):
228
- object_path = f"/{self.bucket}"
229
- if key:
230
- object_path += f"/{quote(key.lstrip('/'), safe='/')}"
231
- encoded_query, headers = self._aws_v4_headers(method, object_path, query=query, body=body, extra_headers=extra_headers)
232
- url = f"{self.endpoint}{object_path}"
233
- if encoded_query:
234
- url += f"?{encoded_query}"
235
- response = self.session.request(method.upper(), url, headers=headers, data=body, timeout=timeout)
236
- return response
237
-
238
- def test_connection(self) -> dict[str, object]:
239
- self.validate()
240
- response = self._request("GET", query={"list-type": "2", "max-keys": "1"}, timeout=30.0)
241
- if response.status_code >= 400:
242
- raise BackupError(f"连接 R2 失败:HTTP {response.status_code}")
243
- return {"ok": True, "status": int(response.status_code)}
244
-
245
- def upload_bytes(self, key: str, payload: bytes, *, content_type: str, metadata: dict[str, str] | None = None) -> dict[str, object]:
246
- headers = {"content-type": content_type}
247
- if metadata:
248
- for item_key, item_value in metadata.items():
249
- headers[f"x-amz-meta-{item_key}"] = str(item_value)
250
- response = self._request("PUT", key, body=payload, extra_headers=headers)
251
- if response.status_code >= 400:
252
- raise BackupError(f"上传备份失败:HTTP {response.status_code}")
253
- return {"key": key, "etag": str(response.headers.get("etag") or "").strip('"')}
254
-
255
- def delete_object(self, key: str) -> None:
256
- response = self._request("DELETE", key, timeout=30.0)
257
- if response.status_code >= 400 and response.status_code != 404:
258
- raise BackupError(f"删除备份失败:HTTP {response.status_code}")
259
-
260
- def download_bytes(self, key: str) -> bytes:
261
- response = self._request("GET", key, timeout=60.0)
262
- if response.status_code >= 400:
263
- raise BackupError(f"读取备份失败:HTTP {response.status_code}")
264
- return bytes(response.content or b"")
265
-
266
- def list_objects(self) -> list[dict[str, object]]:
267
- items: list[dict[str, object]] = []
268
- continuation = ""
269
- while True:
270
- query = {"list-type": "2", "prefix": f"{self.prefix.rstrip('/')}/", "max-keys": "1000"}
271
- if continuation:
272
- query["continuation-token"] = continuation
273
- response = self._request("GET", query=query, timeout=30.0)
274
- if response.status_code >= 400:
275
- raise BackupError(f"获取备份列表失败:HTTP {response.status_code}")
276
- text = response.text
277
- for block in text.split("<Contents>")[1:]:
278
- key = _clean(block.split("<Key>", 1)[1].split("</Key>", 1)[0]) if "<Key>" in block else ""
279
- if not key:
280
- continue
281
- size_text = _clean(block.split("<Size>", 1)[1].split("</Size>", 1)[0]) if "<Size>" in block else "0"
282
- updated = _clean(block.split("<LastModified>", 1)[1].split("</LastModified>", 1)[0]) if "<LastModified>" in block else ""
283
- items.append({
284
- "key": key,
285
- "size": int(size_text or 0),
286
- "updated_at": updated,
287
- })
288
- truncated = "<IsTruncated>true</IsTruncated>" in text
289
- if not truncated:
290
- break
291
- if "<NextContinuationToken>" not in text:
292
- break
293
- continuation = _clean(text.split("<NextContinuationToken>", 1)[1].split("</NextContinuationToken>", 1)[0])
294
- if not continuation:
295
- break
296
- items.sort(key=lambda item: str(item.get("updated_at") or ""), reverse=True)
297
- return items
298
-
299
- def close(self) -> None:
300
- self.session.close()
301
-
302
-
303
- class BackupService:
304
- def __init__(self) -> None:
305
- self._lock = threading.RLock()
306
- self._stop_event = threading.Event()
307
- self._thread: threading.Thread | None = None
308
- self._running = False
309
-
310
- def start(self) -> None:
311
- with self._lock:
312
- if self._thread and self._thread.is_alive():
313
- return
314
- self._stop_event.clear()
315
- self._thread = threading.Thread(target=self._run, daemon=True, name="r2-backup-scheduler")
316
- self._thread.start()
317
-
318
- def stop(self) -> None:
319
- with self._lock:
320
- self._stop_event.set()
321
- thread = self._thread
322
- self._thread = None
323
- if thread and thread.is_alive():
324
- thread.join(timeout=2)
325
-
326
- def _run(self) -> None:
327
- while not self._stop_event.is_set():
328
- try:
329
- self.run_scheduled_backup_if_needed()
330
- except Exception:
331
- pass
332
- self._stop_event.wait(30)
333
-
334
- def run_scheduled_backup_if_needed(self) -> None:
335
- settings = config.get_backup_settings()
336
- if not settings.get("enabled"):
337
- return
338
- state = self.get_status()
339
- if state.get("running"):
340
- return
341
- interval_minutes = int(settings.get("interval_minutes") or 360)
342
- last_finished_raw = _clean(state.get("last_finished_at"))
343
- if last_finished_raw:
344
- try:
345
- last_finished = datetime.fromisoformat(last_finished_raw.replace("Z", "+00:00"))
346
- elapsed = (_utc_now() - last_finished.astimezone(UTC)).total_seconds()
347
- if elapsed < interval_minutes * 60:
348
- return
349
- except Exception:
350
- pass
351
- self.run_backup(trigger="schedule")
352
-
353
- def get_status(self) -> dict[str, object]:
354
- return {
355
- **load_backup_state(),
356
- "running": self._running,
357
- }
358
-
359
- def is_configured(self) -> bool:
360
- settings = config.get_backup_settings()
361
- return all([
362
- _clean(settings.get("account_id")),
363
- _clean(settings.get("access_key_id")),
364
- _clean(settings.get("secret_access_key")),
365
- _clean(settings.get("bucket")),
366
- ])
367
-
368
- def get_settings(self) -> dict[str, object]:
369
- settings = dict(config.get_backup_settings())
370
- settings["secret_access_key"] = "********" if _clean(settings.get("secret_access_key")) else ""
371
- settings["passphrase"] = "********" if _clean(settings.get("passphrase")) else ""
372
- return settings
373
-
374
- def update_settings(self, payload: dict[str, object]) -> dict[str, object]:
375
- current = config.get_backup_settings()
376
- merged = dict(current)
377
- merged.update(dict(payload or {}))
378
- if "include" in payload and isinstance(payload.get("include"), dict):
379
- include = dict(current.get("include") or {})
380
- include.update(payload.get("include") or {})
381
- merged["include"] = include
382
- if payload.get("secret_access_key") == "********":
383
- merged["secret_access_key"] = current.get("secret_access_key")
384
- if payload.get("passphrase") == "********":
385
- merged["passphrase"] = current.get("passphrase")
386
- updated = config.update({"backup": merged})
387
- return dict(updated.get("backup") or {})
388
-
389
- def test_connection(self) -> dict[str, object]:
390
- client = CloudflareR2Client(config.get_backup_settings())
391
- try:
392
- return client.test_connection()
393
- finally:
394
- client.close()
395
-
396
- def list_backups(self) -> list[dict[str, object]]:
397
- if not self.is_configured():
398
- return []
399
- client = CloudflareR2Client(config.get_backup_settings())
400
- try:
401
- items = client.list_objects()
402
- finally:
403
- client.close()
404
- parsed: list[dict[str, object]] = []
405
- for item in items:
406
- key = _clean(item.get("key"))
407
- name = key.rsplit("/", 1)[-1]
408
- encrypted = name.endswith(".enc")
409
- parsed.append({
410
- "key": key,
411
- "name": name,
412
- "size": int(item.get("size") or 0),
413
- "updated_at": item.get("updated_at"),
414
- "encrypted": encrypted,
415
- })
416
- return parsed
417
-
418
- def delete_backup(self, key: str) -> None:
419
- candidate = _clean(key)
420
- if not candidate:
421
- raise BackupError("备份对象 key 不能为空")
422
- client = CloudflareR2Client(config.get_backup_settings())
423
- try:
424
- client.delete_object(candidate)
425
- finally:
426
- client.close()
427
-
428
- def download_backup(self, key: str) -> dict[str, object]:
429
- candidate = _clean(key)
430
- if not candidate:
431
- raise BackupError("备份对象 key 不能为空")
432
- client = CloudflareR2Client(config.get_backup_settings())
433
- try:
434
- payload = client.download_bytes(candidate)
435
- finally:
436
- client.close()
437
- name = candidate.rsplit("/", 1)[-1] or "backup.bin"
438
- if candidate.endswith(".enc"):
439
- passphrase = _clean(config.get_backup_settings().get("passphrase"))
440
- if not passphrase:
441
- raise BackupError("当前未配置加密口令,无法下载并解密已加密备份")
442
- payload = _openssl_decrypt(payload, passphrase)
443
- if name.endswith(".enc"):
444
- name = name[:-4] or "backup.tar.gz"
445
- return {
446
- "key": candidate,
447
- "name": name,
448
- "content_type": _guess_content_type(name),
449
- "payload": payload,
450
- "size": len(payload),
451
- }
452
-
453
- def get_backup_detail(self, key: str) -> dict[str, object]:
454
- candidate = _clean(key)
455
- if not candidate:
456
- raise BackupError("备份对象 key 不能为空")
457
- client = CloudflareR2Client(config.get_backup_settings())
458
- try:
459
- payload = client.download_bytes(candidate)
460
- finally:
461
- client.close()
462
- detail = self._decode_backup_payload(candidate, payload)
463
- detail["key"] = candidate
464
- detail["name"] = candidate.rsplit("/", 1)[-1]
465
- detail["encrypted"] = candidate.endswith(".enc")
466
- return detail
467
-
468
- def run_backup(self, *, trigger: str = "manual") -> dict[str, object]:
469
- with self._lock:
470
- current = self.get_status()
471
- if self._running:
472
- raise BackupError("当前已有备份任务正在执行")
473
- started_at = _iso_now()
474
- self._running = True
475
- save_backup_state({
476
- "last_started_at": started_at,
477
- "last_finished_at": current.get("last_finished_at"),
478
- "last_status": "idle",
479
- "last_error": None,
480
- "last_object_key": current.get("last_object_key"),
481
- })
482
- try:
483
- result = self._run_backup_once(trigger=trigger)
484
- save_backup_state({
485
- "last_started_at": started_at,
486
- "last_finished_at": _iso_now(),
487
- "last_status": "success",
488
- "last_error": None,
489
- "last_object_key": result["key"],
490
- })
491
- return result
492
- except Exception as exc:
493
- save_backup_state({
494
- "last_started_at": started_at,
495
- "last_finished_at": _iso_now(),
496
- "last_status": "error",
497
- "last_error": str(exc) or exc.__class__.__name__,
498
- "last_object_key": current.get("last_object_key"),
499
- })
500
- raise
501
- finally:
502
- self._running = False
503
-
504
- def _run_backup_once(self, *, trigger: str) -> dict[str, object]:
505
- settings = config.get_backup_settings()
506
- client = CloudflareR2Client(settings)
507
- client.validate()
508
- payload_raw = self._build_backup_archive(settings, trigger=trigger)
509
- encrypted = bool(settings.get("encrypt"))
510
- if encrypted:
511
- passphrase = _clean(settings.get("passphrase"))
512
- if not passphrase:
513
- raise BackupError("已启用备份加密,但未设置加密口令")
514
- payload = _openssl_encrypt(payload_raw, passphrase)
515
- suffix = ".tar.gz.enc"
516
- else:
517
- payload = payload_raw
518
- suffix = ".tar.gz"
519
- timestamp = _utc_now().strftime("%Y%m%dT%H%M%SZ")
520
- random_tag = f"{random.randint(0, 0xFFFF):04x}"
521
- object_key = f"{client.prefix.rstrip('/')}/backup-{timestamp}-{random_tag}{suffix}"
522
- metadata = {
523
- "created-at": _iso_now(),
524
- "encrypted": "true" if encrypted else "false",
525
- "trigger": trigger,
526
- }
527
- try:
528
- result = client.upload_bytes(object_key, payload, content_type="application/octet-stream", metadata=metadata)
529
- self._apply_rotation(client, int(settings.get("rotation_keep") or 0))
530
- return {
531
- "key": result["key"],
532
- "size": len(payload),
533
- "encrypted": encrypted,
534
- }
535
- finally:
536
- client.close()
537
-
538
- def _decode_backup_payload(self, key: str, payload: bytes) -> dict[str, object]:
539
- decoded = payload
540
- if key.endswith(".enc"):
541
- passphrase = _clean(config.get_backup_settings().get("passphrase"))
542
- if not passphrase:
543
- raise BackupError("当前未配置加密口令,无法查看已加密备份")
544
- decoded = _openssl_decrypt(decoded, passphrase)
545
- return self._decode_archive_detail(decoded)
546
-
547
- def _apply_rotation(self, client: CloudflareR2Client, keep: int) -> None:
548
- if keep <= 0:
549
- return
550
- items = [item for item in client.list_objects() if _is_backup_object(item.get("key"))]
551
- if len(items) <= keep:
552
- return
553
- for item in items[keep:]:
554
- key = _clean(item.get("key"))
555
- if key:
556
- client.delete_object(key)
557
-
558
- def _decode_archive_detail(self, payload: bytes) -> dict[str, object]:
559
- files: list[dict[str, object]] = []
560
- snapshots: list[dict[str, object]] = []
561
- metadata: dict[str, object] = {}
562
- try:
563
- with tarfile.open(fileobj=io.BytesIO(payload), mode="r:gz") as archive:
564
- members = [member for member in archive.getmembers() if member.isfile()]
565
- for member in members:
566
- extracted = archive.extractfile(member)
567
- if extracted is None:
568
- continue
569
- raw = extracted.read()
570
- name = member.name
571
- if name == "backup-metadata.json":
572
- try:
573
- parsed = json.loads(raw.decode("utf-8"))
574
- if isinstance(parsed, dict):
575
- metadata = parsed
576
- except Exception:
577
- metadata = {}
578
- continue
579
- if name.startswith("snapshots/") and name.endswith(".json"):
580
- count = 0
581
- try:
582
- parsed_snapshot = json.loads(raw.decode("utf-8"))
583
- count = _count_items(parsed_snapshot)
584
- except Exception:
585
- count = 0
586
- snapshots.append({
587
- "name": name.removeprefix("snapshots/").removesuffix(".json"),
588
- "count": count,
589
- })
590
- continue
591
- files.append({
592
- "name": name,
593
- "exists": True,
594
- "content_type": _guess_content_type(name),
595
- "size": len(raw),
596
- "sha256": _sha256_hex(raw),
597
- })
598
- except tarfile.TarError as exc:
599
- raise BackupError("解析备份压缩包失败,备份可能已损坏") from exc
600
- files.sort(key=lambda item: str(item.get("name") or ""))
601
- snapshots.sort(key=lambda item: str(item.get("name") or ""))
602
- return {
603
- "created_at": metadata.get("created_at"),
604
- "trigger": metadata.get("trigger"),
605
- "app_version": metadata.get("app_version"),
606
- "storage_backend": metadata.get("storage_backend"),
607
- "files": files,
608
- "snapshots": snapshots,
609
- }
610
-
611
- def _build_backup_archive(self, settings: dict[str, object], *, trigger: str) -> bytes:
612
- include = settings.get("include") if isinstance(settings.get("include"), dict) else {}
613
- metadata = {
614
- "version": 2,
615
- "created_at": _iso_now(),
616
- "trigger": trigger,
617
- "app_version": config.app_version,
618
- "storage_backend": config.get_storage_backend().get_backend_info(),
619
- }
620
- buffer = io.BytesIO()
621
- with tarfile.open(fileobj=buffer, mode="w:gz") as archive:
622
- self._add_bytes_to_archive(archive, "backup-metadata.json", _json_bytes(metadata))
623
- if include.get("config"):
624
- self._add_file_to_archive(archive, CONFIG_FILE, "config.json")
625
- if include.get("cpa"):
626
- self._add_file_to_archive(archive, DATA_DIR / "cpa_config.json", "data/cpa_config.json")
627
- if include.get("sub2api"):
628
- self._add_file_to_archive(archive, DATA_DIR / "sub2api_config.json", "data/sub2api_config.json")
629
- if include.get("logs"):
630
- self._add_file_to_archive(archive, DATA_DIR / "logs.jsonl", "data/logs.jsonl")
631
- if include.get("image_tasks"):
632
- self._add_file_to_archive(archive, DATA_DIR / "image_tasks.json", "data/image_tasks.json")
633
- self._add_file_to_archive(archive, IMAGE_INDEX_FILE, "data/image_index.json")
634
- if include.get("accounts_snapshot"):
635
- self._add_bytes_to_archive(
636
- archive,
637
- "snapshots/accounts.json",
638
- _json_bytes(config.get_storage_backend().load_accounts()),
639
- )
640
- if include.get("auth_keys_snapshot"):
641
- self._add_bytes_to_archive(
642
- archive,
643
- "snapshots/auth_keys.json",
644
- _json_bytes(config.get_storage_backend().load_auth_keys()),
645
- )
646
- if include.get("images"):
647
- self._add_file_to_archive(archive, TAGS_FILE, "data/image_tags.json")
648
- self._add_directory_to_archive(archive, config.images_dir, "data/images")
649
- return buffer.getvalue()
650
-
651
- def _add_bytes_to_archive(self, archive: tarfile.TarFile, name: str, payload: bytes) -> None:
652
- info = tarfile.TarInfo(name=name)
653
- info.size = len(payload)
654
- info.mtime = int(_utc_now().timestamp())
655
- archive.addfile(info, io.BytesIO(payload))
656
-
657
- def _add_file_to_archive(self, archive: tarfile.TarFile, source: Path, arcname: str) -> None:
658
- if not source.exists() or not source.is_file():
659
- return
660
- archive.add(source, arcname=arcname)
661
-
662
- def _add_directory_to_archive(self, archive: tarfile.TarFile, source_dir: Path, arcname_root: str) -> None:
663
- if not source_dir.exists() or not source_dir.is_dir():
664
- return
665
- for path in sorted(source_dir.rglob("*")):
666
- if path.is_file():
667
- relative = path.relative_to(source_dir).as_posix()
668
- archive.add(path, arcname=f"{arcname_root}/{relative}")
669
-
670
-
671
- backup_service = BackupService()
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import hmac
5
+ import io
6
+ import json
7
+ import os
8
+ import random
9
+ import subprocess
10
+ import tarfile
11
+ import threading
12
+ from datetime import UTC, datetime
13
+ from pathlib import Path
14
+ from urllib.parse import quote, urlencode
15
+
16
+ from curl_cffi import requests
17
+
18
+ from services.config import BASE_DIR, CONFIG_FILE, DATA_DIR, config, load_backup_state, save_backup_state
19
+ from services.image_storage_service import IMAGE_INDEX_FILE
20
+ from services.image_tags_service import TAGS_FILE
21
+
22
+
23
+ def _utc_now() -> datetime:
24
+ return datetime.now(UTC)
25
+
26
+
27
+ def _iso_now() -> str:
28
+ return _utc_now().replace(microsecond=0).isoformat().replace("+00:00", "Z")
29
+
30
+
31
+ def _clean(value: object) -> str:
32
+ return str(value or "").strip()
33
+
34
+
35
+ def _sha256_hex(value: bytes) -> str:
36
+ return hashlib.sha256(value).hexdigest()
37
+
38
+
39
+ def _is_backup_object(key: object) -> bool:
40
+ name = _clean(key).rsplit("/", 1)[-1]
41
+ return name.startswith("backup-") and (name.endswith(".tar.gz") or name.endswith(".tar.gz.enc"))
42
+
43
+
44
+ def _hmac_sha256(key: bytes, message: str) -> bytes:
45
+ return hmac.new(key, message.encode("utf-8"), hashlib.sha256).digest()
46
+
47
+
48
+ def _openssl_encrypt(data: bytes, passphrase: str) -> bytes:
49
+ env = dict(os.environ)
50
+ env["CHATGPT2API_BACKUP_PASSPHRASE"] = passphrase
51
+ try:
52
+ result = subprocess.run(
53
+ [
54
+ "openssl",
55
+ "enc",
56
+ "-aes-256-cbc",
57
+ "-pbkdf2",
58
+ "-salt",
59
+ "-md",
60
+ "sha256",
61
+ "-pass",
62
+ "env:CHATGPT2API_BACKUP_PASSPHRASE",
63
+ ],
64
+ input=data,
65
+ stdout=subprocess.PIPE,
66
+ stderr=subprocess.PIPE,
67
+ check=True,
68
+ env=env,
69
+ )
70
+ except FileNotFoundError as exc:
71
+ raise BackupError("当前环境缺少 openssl,无法执行加密备份") from exc
72
+ except subprocess.CalledProcessError as exc:
73
+ detail = (exc.stderr or b"").decode("utf-8", errors="replace").strip()
74
+ raise BackupError(f"加密备份失败:{detail or 'openssl 执行失败'}") from exc
75
+ return result.stdout
76
+
77
+
78
+ def _openssl_decrypt(data: bytes, passphrase: str) -> bytes:
79
+ env = dict(os.environ)
80
+ env["CHATGPT2API_BACKUP_PASSPHRASE"] = passphrase
81
+ try:
82
+ result = subprocess.run(
83
+ [
84
+ "openssl",
85
+ "enc",
86
+ "-d",
87
+ "-aes-256-cbc",
88
+ "-pbkdf2",
89
+ "-md",
90
+ "sha256",
91
+ "-pass",
92
+ "env:CHATGPT2API_BACKUP_PASSPHRASE",
93
+ ],
94
+ input=data,
95
+ stdout=subprocess.PIPE,
96
+ stderr=subprocess.PIPE,
97
+ check=True,
98
+ env=env,
99
+ )
100
+ except FileNotFoundError as exc:
101
+ raise BackupError("当前环境缺少 openssl,无法解密备份内容") from exc
102
+ except subprocess.CalledProcessError as exc:
103
+ detail = (exc.stderr or b"").decode("utf-8", errors="replace").strip()
104
+ raise BackupError(f"解密备份失败:{detail or 'openssl 执行失败'}") from exc
105
+ return result.stdout
106
+
107
+
108
+ def _guess_content_type(name: str) -> str:
109
+ if name.endswith(".json"):
110
+ return "application/json"
111
+ if name.endswith(".jsonl"):
112
+ return "application/x-ndjson"
113
+ if name.endswith(".tar.gz"):
114
+ return "application/gzip"
115
+ if name.endswith(".gz"):
116
+ return "application/gzip"
117
+ return "application/octet-stream"
118
+
119
+
120
+ def _json_bytes(value: object) -> bytes:
121
+ return json.dumps(value, ensure_ascii=False, indent=2).encode("utf-8")
122
+
123
+
124
+ def _count_items(value: object) -> int:
125
+ if isinstance(value, list):
126
+ return len(value)
127
+ if isinstance(value, dict):
128
+ return len(value)
129
+ return 0
130
+
131
+
132
+ class BackupError(RuntimeError):
133
+ pass
134
+
135
+
136
+ class CloudflareR2Client:
137
+ def __init__(self, settings: dict[str, object]) -> None:
138
+ self.account_id = _clean(settings.get("account_id"))
139
+ self.access_key_id = _clean(settings.get("access_key_id"))
140
+ self.secret_access_key = _clean(settings.get("secret_access_key"))
141
+ self.bucket = _clean(settings.get("bucket"))
142
+ self.prefix = _clean(settings.get("prefix")) or "backups"
143
+ self.session = requests.Session(impersonate="chrome", verify=True)
144
+
145
+ def validate(self) -> None:
146
+ missing = []
147
+ if not self.account_id:
148
+ missing.append("Account ID")
149
+ if not self.access_key_id:
150
+ missing.append("Access Key ID")
151
+ if not self.secret_access_key:
152
+ missing.append("Secret Access Key")
153
+ if not self.bucket:
154
+ missing.append("Bucket")
155
+ if missing:
156
+ raise BackupError(f"R2 配置不完整:缺少 {'、'.join(missing)}")
157
+
158
+ @property
159
+ def endpoint(self) -> str:
160
+ return f"https://{self.account_id}.r2.cloudflarestorage.com"
161
+
162
+ def _aws_v4_headers(
163
+ self,
164
+ method: str,
165
+ path: str,
166
+ *,
167
+ query: dict[str, str] | None = None,
168
+ body: bytes = b"",
169
+ extra_headers: dict[str, str] | None = None,
170
+ ) -> tuple[str, dict[str, str]]:
171
+ now = _utc_now()
172
+ amz_date = now.strftime("%Y%m%dT%H%M%SZ")
173
+ date_stamp = now.strftime("%Y%m%d")
174
+ encoded_query = urlencode(sorted((query or {}).items()))
175
+ payload_hash = _sha256_hex(body)
176
+ host = f"{self.account_id}.r2.cloudflarestorage.com"
177
+ headers = {
178
+ "host": host,
179
+ "x-amz-content-sha256": payload_hash,
180
+ "x-amz-date": amz_date,
181
+ }
182
+ if extra_headers:
183
+ for key, value in extra_headers.items():
184
+ headers[key.lower()] = value.strip()
185
+ sorted_items = sorted((key.lower(), " ".join(str(value).strip().split())) for key, value in headers.items())
186
+ canonical_headers = "".join(f"{key}:{value}\n" for key, value in sorted_items)
187
+ signed_headers = ";".join(key for key, _ in sorted_items)
188
+ canonical_request = "\n".join([
189
+ method.upper(),
190
+ path,
191
+ encoded_query,
192
+ canonical_headers,
193
+ signed_headers,
194
+ payload_hash,
195
+ ])
196
+ credential_scope = f"{date_stamp}/auto/s3/aws4_request"
197
+ string_to_sign = "\n".join([
198
+ "AWS4-HMAC-SHA256",
199
+ amz_date,
200
+ credential_scope,
201
+ _sha256_hex(canonical_request.encode("utf-8")),
202
+ ])
203
+ k_date = _hmac_sha256(("AWS4" + self.secret_access_key).encode("utf-8"), date_stamp)
204
+ k_region = hmac.new(k_date, b"auto", hashlib.sha256).digest()
205
+ k_service = hmac.new(k_region, b"s3", hashlib.sha256).digest()
206
+ k_signing = hmac.new(k_service, b"aws4_request", hashlib.sha256).digest()
207
+ signature = hmac.new(k_signing, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
208
+ authorization = (
209
+ "AWS4-HMAC-SHA256 "
210
+ f"Credential={self.access_key_id}/{credential_scope}, "
211
+ f"SignedHeaders={signed_headers}, "
212
+ f"Signature={signature}"
213
+ )
214
+ request_headers = {key: value for key, value in headers.items()}
215
+ request_headers["authorization"] = authorization
216
+ return encoded_query, request_headers
217
+
218
+ def _request(
219
+ self,
220
+ method: str,
221
+ key: str = "",
222
+ *,
223
+ query: dict[str, str] | None = None,
224
+ body: bytes = b"",
225
+ extra_headers: dict[str, str] | None = None,
226
+ timeout: float = 60.0,
227
+ ):
228
+ object_path = f"/{self.bucket}"
229
+ if key:
230
+ object_path += f"/{quote(key.lstrip('/'), safe='/')}"
231
+ encoded_query, headers = self._aws_v4_headers(method, object_path, query=query, body=body, extra_headers=extra_headers)
232
+ url = f"{self.endpoint}{object_path}"
233
+ if encoded_query:
234
+ url += f"?{encoded_query}"
235
+ response = self.session.request(method.upper(), url, headers=headers, data=body, timeout=timeout)
236
+ return response
237
+
238
+ def test_connection(self) -> dict[str, object]:
239
+ self.validate()
240
+ response = self._request("GET", query={"list-type": "2", "max-keys": "1"}, timeout=30.0)
241
+ if response.status_code >= 400:
242
+ raise BackupError(f"连接 R2 失败:HTTP {response.status_code}")
243
+ return {"ok": True, "status": int(response.status_code)}
244
+
245
+ def upload_bytes(self, key: str, payload: bytes, *, content_type: str, metadata: dict[str, str] | None = None) -> dict[str, object]:
246
+ headers = {"content-type": content_type}
247
+ if metadata:
248
+ for item_key, item_value in metadata.items():
249
+ headers[f"x-amz-meta-{item_key}"] = str(item_value)
250
+ response = self._request("PUT", key, body=payload, extra_headers=headers)
251
+ if response.status_code >= 400:
252
+ raise BackupError(f"上传备份失败:HTTP {response.status_code}")
253
+ return {"key": key, "etag": str(response.headers.get("etag") or "").strip('"')}
254
+
255
+ def delete_object(self, key: str) -> None:
256
+ response = self._request("DELETE", key, timeout=30.0)
257
+ if response.status_code >= 400 and response.status_code != 404:
258
+ raise BackupError(f"删除备份失败:HTTP {response.status_code}")
259
+
260
+ def download_bytes(self, key: str) -> bytes:
261
+ response = self._request("GET", key, timeout=60.0)
262
+ if response.status_code >= 400:
263
+ raise BackupError(f"读取备份失败:HTTP {response.status_code}")
264
+ return bytes(response.content or b"")
265
+
266
+ def list_objects(self) -> list[dict[str, object]]:
267
+ items: list[dict[str, object]] = []
268
+ continuation = ""
269
+ while True:
270
+ query = {"list-type": "2", "prefix": f"{self.prefix.rstrip('/')}/", "max-keys": "1000"}
271
+ if continuation:
272
+ query["continuation-token"] = continuation
273
+ response = self._request("GET", query=query, timeout=30.0)
274
+ if response.status_code >= 400:
275
+ raise BackupError(f"获取备份列表失败:HTTP {response.status_code}")
276
+ text = response.text
277
+ for block in text.split("<Contents>")[1:]:
278
+ key = _clean(block.split("<Key>", 1)[1].split("</Key>", 1)[0]) if "<Key>" in block else ""
279
+ if not key:
280
+ continue
281
+ size_text = _clean(block.split("<Size>", 1)[1].split("</Size>", 1)[0]) if "<Size>" in block else "0"
282
+ updated = _clean(block.split("<LastModified>", 1)[1].split("</LastModified>", 1)[0]) if "<LastModified>" in block else ""
283
+ items.append({
284
+ "key": key,
285
+ "size": int(size_text or 0),
286
+ "updated_at": updated,
287
+ })
288
+ truncated = "<IsTruncated>true</IsTruncated>" in text
289
+ if not truncated:
290
+ break
291
+ if "<NextContinuationToken>" not in text:
292
+ break
293
+ continuation = _clean(text.split("<NextContinuationToken>", 1)[1].split("</NextContinuationToken>", 1)[0])
294
+ if not continuation:
295
+ break
296
+ items.sort(key=lambda item: str(item.get("updated_at") or ""), reverse=True)
297
+ return items
298
+
299
+ def close(self) -> None:
300
+ self.session.close()
301
+
302
+
303
+ class BackupService:
304
+ def __init__(self) -> None:
305
+ self._lock = threading.RLock()
306
+ self._stop_event = threading.Event()
307
+ self._thread: threading.Thread | None = None
308
+ self._running = False
309
+
310
+ def start(self) -> None:
311
+ with self._lock:
312
+ if self._thread and self._thread.is_alive():
313
+ return
314
+ self._stop_event.clear()
315
+ self._thread = threading.Thread(target=self._run, daemon=True, name="r2-backup-scheduler")
316
+ self._thread.start()
317
+
318
+ def stop(self) -> None:
319
+ with self._lock:
320
+ self._stop_event.set()
321
+ thread = self._thread
322
+ self._thread = None
323
+ if thread and thread.is_alive():
324
+ thread.join(timeout=2)
325
+
326
+ def _run(self) -> None:
327
+ while not self._stop_event.is_set():
328
+ try:
329
+ self.run_scheduled_backup_if_needed()
330
+ except Exception:
331
+ pass
332
+ self._stop_event.wait(30)
333
+
334
+ def run_scheduled_backup_if_needed(self) -> None:
335
+ settings = config.get_backup_settings()
336
+ if not settings.get("enabled"):
337
+ return
338
+ state = self.get_status()
339
+ if state.get("running"):
340
+ return
341
+ interval_minutes = int(settings.get("interval_minutes") or 360)
342
+ last_finished_raw = _clean(state.get("last_finished_at"))
343
+ if last_finished_raw:
344
+ try:
345
+ last_finished = datetime.fromisoformat(last_finished_raw.replace("Z", "+00:00"))
346
+ elapsed = (_utc_now() - last_finished.astimezone(UTC)).total_seconds()
347
+ if elapsed < interval_minutes * 60:
348
+ return
349
+ except Exception:
350
+ pass
351
+ self.run_backup(trigger="schedule")
352
+
353
+ def get_status(self) -> dict[str, object]:
354
+ return {
355
+ **load_backup_state(),
356
+ "running": self._running,
357
+ }
358
+
359
+ def is_configured(self) -> bool:
360
+ settings = config.get_backup_settings()
361
+ return all([
362
+ _clean(settings.get("account_id")),
363
+ _clean(settings.get("access_key_id")),
364
+ _clean(settings.get("secret_access_key")),
365
+ _clean(settings.get("bucket")),
366
+ ])
367
+
368
+ def get_settings(self) -> dict[str, object]:
369
+ settings = dict(config.get_backup_settings())
370
+ settings["secret_access_key"] = "********" if _clean(settings.get("secret_access_key")) else ""
371
+ settings["passphrase"] = "********" if _clean(settings.get("passphrase")) else ""
372
+ return settings
373
+
374
+ def update_settings(self, payload: dict[str, object]) -> dict[str, object]:
375
+ current = config.get_backup_settings()
376
+ merged = dict(current)
377
+ merged.update(dict(payload or {}))
378
+ if "include" in payload and isinstance(payload.get("include"), dict):
379
+ include = dict(current.get("include") or {})
380
+ include.update(payload.get("include") or {})
381
+ merged["include"] = include
382
+ if payload.get("secret_access_key") == "********":
383
+ merged["secret_access_key"] = current.get("secret_access_key")
384
+ if payload.get("passphrase") == "********":
385
+ merged["passphrase"] = current.get("passphrase")
386
+ updated = config.update({"backup": merged})
387
+ return dict(updated.get("backup") or {})
388
+
389
+ def test_connection(self) -> dict[str, object]:
390
+ client = CloudflareR2Client(config.get_backup_settings())
391
+ try:
392
+ return client.test_connection()
393
+ finally:
394
+ client.close()
395
+
396
+ def list_backups(self) -> list[dict[str, object]]:
397
+ if not self.is_configured():
398
+ return []
399
+ client = CloudflareR2Client(config.get_backup_settings())
400
+ try:
401
+ items = client.list_objects()
402
+ finally:
403
+ client.close()
404
+ parsed: list[dict[str, object]] = []
405
+ for item in items:
406
+ key = _clean(item.get("key"))
407
+ name = key.rsplit("/", 1)[-1]
408
+ encrypted = name.endswith(".enc")
409
+ parsed.append({
410
+ "key": key,
411
+ "name": name,
412
+ "size": int(item.get("size") or 0),
413
+ "updated_at": item.get("updated_at"),
414
+ "encrypted": encrypted,
415
+ })
416
+ return parsed
417
+
418
+ def delete_backup(self, key: str) -> None:
419
+ candidate = _clean(key)
420
+ if not candidate:
421
+ raise BackupError("备份对象 key 不能为空")
422
+ client = CloudflareR2Client(config.get_backup_settings())
423
+ try:
424
+ client.delete_object(candidate)
425
+ finally:
426
+ client.close()
427
+
428
+ def download_backup(self, key: str) -> dict[str, object]:
429
+ candidate = _clean(key)
430
+ if not candidate:
431
+ raise BackupError("备份对象 key 不能为空")
432
+ client = CloudflareR2Client(config.get_backup_settings())
433
+ try:
434
+ payload = client.download_bytes(candidate)
435
+ finally:
436
+ client.close()
437
+ name = candidate.rsplit("/", 1)[-1] or "backup.bin"
438
+ if candidate.endswith(".enc"):
439
+ passphrase = _clean(config.get_backup_settings().get("passphrase"))
440
+ if not passphrase:
441
+ raise BackupError("当前未配置加密口令,无法下载并解密已加密备份")
442
+ payload = _openssl_decrypt(payload, passphrase)
443
+ if name.endswith(".enc"):
444
+ name = name[:-4] or "backup.tar.gz"
445
+ return {
446
+ "key": candidate,
447
+ "name": name,
448
+ "content_type": _guess_content_type(name),
449
+ "payload": payload,
450
+ "size": len(payload),
451
+ }
452
+
453
+ def get_backup_detail(self, key: str) -> dict[str, object]:
454
+ candidate = _clean(key)
455
+ if not candidate:
456
+ raise BackupError("备份对象 key 不能为空")
457
+ client = CloudflareR2Client(config.get_backup_settings())
458
+ try:
459
+ payload = client.download_bytes(candidate)
460
+ finally:
461
+ client.close()
462
+ detail = self._decode_backup_payload(candidate, payload)
463
+ detail["key"] = candidate
464
+ detail["name"] = candidate.rsplit("/", 1)[-1]
465
+ detail["encrypted"] = candidate.endswith(".enc")
466
+ return detail
467
+
468
+ def run_backup(self, *, trigger: str = "manual") -> dict[str, object]:
469
+ with self._lock:
470
+ current = self.get_status()
471
+ if self._running:
472
+ raise BackupError("当前已有备份任务正在执行")
473
+ started_at = _iso_now()
474
+ self._running = True
475
+ save_backup_state({
476
+ "last_started_at": started_at,
477
+ "last_finished_at": current.get("last_finished_at"),
478
+ "last_status": "idle",
479
+ "last_error": None,
480
+ "last_object_key": current.get("last_object_key"),
481
+ })
482
+ try:
483
+ result = self._run_backup_once(trigger=trigger)
484
+ save_backup_state({
485
+ "last_started_at": started_at,
486
+ "last_finished_at": _iso_now(),
487
+ "last_status": "success",
488
+ "last_error": None,
489
+ "last_object_key": result["key"],
490
+ })
491
+ return result
492
+ except Exception as exc:
493
+ save_backup_state({
494
+ "last_started_at": started_at,
495
+ "last_finished_at": _iso_now(),
496
+ "last_status": "error",
497
+ "last_error": str(exc) or exc.__class__.__name__,
498
+ "last_object_key": current.get("last_object_key"),
499
+ })
500
+ raise
501
+ finally:
502
+ self._running = False
503
+
504
+ def _run_backup_once(self, *, trigger: str) -> dict[str, object]:
505
+ settings = config.get_backup_settings()
506
+ client = CloudflareR2Client(settings)
507
+ client.validate()
508
+ payload_raw = self._build_backup_archive(settings, trigger=trigger)
509
+ encrypted = bool(settings.get("encrypt"))
510
+ if encrypted:
511
+ passphrase = _clean(settings.get("passphrase"))
512
+ if not passphrase:
513
+ raise BackupError("已启用备份加密,但未设置加密口令")
514
+ payload = _openssl_encrypt(payload_raw, passphrase)
515
+ suffix = ".tar.gz.enc"
516
+ else:
517
+ payload = payload_raw
518
+ suffix = ".tar.gz"
519
+ timestamp = _utc_now().strftime("%Y%m%dT%H%M%SZ")
520
+ random_tag = f"{random.randint(0, 0xFFFF):04x}"
521
+ object_key = f"{client.prefix.rstrip('/')}/backup-{timestamp}-{random_tag}{suffix}"
522
+ metadata = {
523
+ "created-at": _iso_now(),
524
+ "encrypted": "true" if encrypted else "false",
525
+ "trigger": trigger,
526
+ }
527
+ try:
528
+ result = client.upload_bytes(object_key, payload, content_type="application/octet-stream", metadata=metadata)
529
+ self._apply_rotation(client, int(settings.get("rotation_keep") or 0))
530
+ return {
531
+ "key": result["key"],
532
+ "size": len(payload),
533
+ "encrypted": encrypted,
534
+ }
535
+ finally:
536
+ client.close()
537
+
538
+ def _decode_backup_payload(self, key: str, payload: bytes) -> dict[str, object]:
539
+ decoded = payload
540
+ if key.endswith(".enc"):
541
+ passphrase = _clean(config.get_backup_settings().get("passphrase"))
542
+ if not passphrase:
543
+ raise BackupError("当前未配置加密口令,无法查看已加密备份")
544
+ decoded = _openssl_decrypt(decoded, passphrase)
545
+ return self._decode_archive_detail(decoded)
546
+
547
+ def _apply_rotation(self, client: CloudflareR2Client, keep: int) -> None:
548
+ if keep <= 0:
549
+ return
550
+ items = [item for item in client.list_objects() if _is_backup_object(item.get("key"))]
551
+ if len(items) <= keep:
552
+ return
553
+ for item in items[keep:]:
554
+ key = _clean(item.get("key"))
555
+ if key:
556
+ client.delete_object(key)
557
+
558
+ def _decode_archive_detail(self, payload: bytes) -> dict[str, object]:
559
+ files: list[dict[str, object]] = []
560
+ snapshots: list[dict[str, object]] = []
561
+ metadata: dict[str, object] = {}
562
+ try:
563
+ with tarfile.open(fileobj=io.BytesIO(payload), mode="r:gz") as archive:
564
+ members = [member for member in archive.getmembers() if member.isfile()]
565
+ for member in members:
566
+ extracted = archive.extractfile(member)
567
+ if extracted is None:
568
+ continue
569
+ raw = extracted.read()
570
+ name = member.name
571
+ if name == "backup-metadata.json":
572
+ try:
573
+ parsed = json.loads(raw.decode("utf-8"))
574
+ if isinstance(parsed, dict):
575
+ metadata = parsed
576
+ except Exception:
577
+ metadata = {}
578
+ continue
579
+ if name.startswith("snapshots/") and name.endswith(".json"):
580
+ count = 0
581
+ try:
582
+ parsed_snapshot = json.loads(raw.decode("utf-8"))
583
+ count = _count_items(parsed_snapshot)
584
+ except Exception:
585
+ count = 0
586
+ snapshots.append({
587
+ "name": name.removeprefix("snapshots/").removesuffix(".json"),
588
+ "count": count,
589
+ })
590
+ continue
591
+ files.append({
592
+ "name": name,
593
+ "exists": True,
594
+ "content_type": _guess_content_type(name),
595
+ "size": len(raw),
596
+ "sha256": _sha256_hex(raw),
597
+ })
598
+ except tarfile.TarError as exc:
599
+ raise BackupError("解析备份压缩包失败,备份可能已损坏") from exc
600
+ files.sort(key=lambda item: str(item.get("name") or ""))
601
+ snapshots.sort(key=lambda item: str(item.get("name") or ""))
602
+ return {
603
+ "created_at": metadata.get("created_at"),
604
+ "trigger": metadata.get("trigger"),
605
+ "app_version": metadata.get("app_version"),
606
+ "storage_backend": metadata.get("storage_backend"),
607
+ "files": files,
608
+ "snapshots": snapshots,
609
+ }
610
+
611
+ def _build_backup_archive(self, settings: dict[str, object], *, trigger: str) -> bytes:
612
+ include = settings.get("include") if isinstance(settings.get("include"), dict) else {}
613
+ metadata = {
614
+ "version": 2,
615
+ "created_at": _iso_now(),
616
+ "trigger": trigger,
617
+ "app_version": config.app_version,
618
+ "storage_backend": config.get_storage_backend().get_backend_info(),
619
+ }
620
+ buffer = io.BytesIO()
621
+ with tarfile.open(fileobj=buffer, mode="w:gz") as archive:
622
+ self._add_bytes_to_archive(archive, "backup-metadata.json", _json_bytes(metadata))
623
+ if include.get("config"):
624
+ self._add_file_to_archive(archive, CONFIG_FILE, "config.json")
625
+ if include.get("cpa"):
626
+ self._add_file_to_archive(archive, DATA_DIR / "cpa_config.json", "data/cpa_config.json")
627
+ if include.get("sub2api"):
628
+ self._add_file_to_archive(archive, DATA_DIR / "sub2api_config.json", "data/sub2api_config.json")
629
+ if include.get("logs"):
630
+ self._add_file_to_archive(archive, DATA_DIR / "logs.jsonl", "data/logs.jsonl")
631
+ if include.get("image_tasks"):
632
+ self._add_file_to_archive(archive, DATA_DIR / "image_tasks.json", "data/image_tasks.json")
633
+ self._add_file_to_archive(archive, IMAGE_INDEX_FILE, "data/image_index.json")
634
+ if include.get("accounts_snapshot"):
635
+ self._add_bytes_to_archive(
636
+ archive,
637
+ "snapshots/accounts.json",
638
+ _json_bytes(config.get_storage_backend().load_accounts()),
639
+ )
640
+ if include.get("auth_keys_snapshot"):
641
+ self._add_bytes_to_archive(
642
+ archive,
643
+ "snapshots/auth_keys.json",
644
+ _json_bytes(config.get_storage_backend().load_auth_keys()),
645
+ )
646
+ if include.get("images"):
647
+ self._add_file_to_archive(archive, TAGS_FILE, "data/image_tags.json")
648
+ self._add_directory_to_archive(archive, config.images_dir, "data/images")
649
+ return buffer.getvalue()
650
+
651
+ def _add_bytes_to_archive(self, archive: tarfile.TarFile, name: str, payload: bytes) -> None:
652
+ info = tarfile.TarInfo(name=name)
653
+ info.size = len(payload)
654
+ info.mtime = int(_utc_now().timestamp())
655
+ archive.addfile(info, io.BytesIO(payload))
656
+
657
+ def _add_file_to_archive(self, archive: tarfile.TarFile, source: Path, arcname: str) -> None:
658
+ if not source.exists() or not source.is_file():
659
+ return
660
+ archive.add(source, arcname=arcname)
661
+
662
+ def _add_directory_to_archive(self, archive: tarfile.TarFile, source_dir: Path, arcname_root: str) -> None:
663
+ if not source_dir.exists() or not source_dir.is_dir():
664
+ return
665
+ for path in sorted(source_dir.rglob("*")):
666
+ if path.is_file():
667
+ relative = path.relative_to(source_dir).as_posix()
668
+ archive.add(path, arcname=f"{arcname_root}/{relative}")
669
+
670
+
671
+ backup_service = BackupService()