Repository navigation
Expand file tree
/
Copy pathhome.py
More file actions
188 lines (139 loc) · 5.55 KB
/
Copy pathhome.py
File metadata and controls
188 lines (139 loc) · 5.55 KB
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
"""运行时 home 目录(``$CODING_PROXY_HOME``,默认 ``~/.coding-proxy``)与私有文件工具.
所有落盘路径(config.db / master.key / admin_token / usage.db / compat.db /
tokens.json / 旧 config.yaml)统一由 :func:`proxy_home` 推导,代码中不再写死
``~/.coding-proxy``。``CODING_PROXY_HOME`` 同时充当多实例 profile 与测试隔离手段。
"""
from __future__ import annotations
import os
import re
import secrets
from pathlib import Path
ENV_HOME = "CODING_PROXY_HOME"
_DEFAULT_HOME = "~/.coding-proxy"
def proxy_home() -> Path:
"""返回 home 目录(纯函数,不创建目录)."""
raw = os.environ.get(ENV_HOME, "").strip()
if raw:
return Path(raw).expanduser().resolve()
return Path(_DEFAULT_HOME).expanduser()
def ensure_home() -> Path:
"""创建 home 目录(0700);目录属主为当前用户时收紧权限至 0700."""
home = proxy_home()
try:
home.mkdir(mode=0o700, parents=True, exist_ok=True)
except OSError as exc:
from .store import ConfigStoreError
raise ConfigStoreError(
f"无法创建配置目录 {home}({exc.strerror or exc})。"
"请检查 CODING_PROXY_HOME 指向的路径及其父目录权限"
) from None
getuid = getattr(os, "getuid", None) # Windows 无 getuid
try:
if getuid is not None and home.stat().st_uid == getuid():
home.chmod(0o700)
except OSError:
pass
return home
def config_db_path() -> Path:
return proxy_home() / "config.db"
def master_key_path() -> Path:
return proxy_home() / "master.key"
def admin_token_path() -> Path:
return proxy_home() / "admin_token"
def default_usage_db_path() -> Path:
return proxy_home() / "usage.db"
def default_compat_db_path() -> Path:
return proxy_home() / "compat.db"
def default_token_store_path() -> Path:
return proxy_home() / "tokens.json"
def legacy_home_yaml_path() -> Path:
"""旧版 home 下的 config.yaml(仅作为一次性导入源与只读兼容读取)."""
return proxy_home() / "config.yaml"
# ── Pydantic default_factory(字符串形态,保持与旧字面量展开后一致) ──
def default_usage_db_str() -> str:
return str(default_usage_db_path())
def default_compat_db_str() -> str:
return str(default_compat_db_path())
def default_token_store_str() -> str:
return str(default_token_store_path())
# ── 私有文件 ────────────────────────────────────────────────────
def create_private_file(path: Path, data: bytes) -> None:
"""以 O_EXCL + 0600 创建并写入文件;文件已存在时抛 FileExistsError."""
try:
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
except FileExistsError:
raise
except OSError as exc:
from .store import StoreAccessError
raise StoreAccessError(path, f"创建文件失败: {exc.strerror or exc}") from None
try:
with os.fdopen(fd, "wb") as fh:
fd = -1
fh.write(data)
finally:
if fd >= 0:
os.close(fd)
def write_private_atomic(path: Path, data: bytes) -> None:
"""原子写入私有文件:同目录 0600 临时文件 + ``os.replace``."""
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_name(f".{path.name}.{secrets.token_hex(6)}.tmp")
create_private_file(tmp, data)
try:
os.replace(tmp, path)
except BaseException:
tmp.unlink(missing_ok=True)
raise
ADMIN_TOKEN_MIN_LENGTH = 32
_TOKEN_RE = re.compile(rf"[A-Za-z0-9_-]{{{ADMIN_TOKEN_MIN_LENGTH},}}")
def is_valid_admin_token(token: str) -> bool:
"""token 至少 32 个 urlsafe 字符;空串 / 过短一律无效(调用方不得视为匹配)."""
return bool(_TOKEN_RE.fullmatch(token))
def _read_admin_token(path: Path) -> str:
try:
return path.read_text(encoding="utf-8").strip()
except (FileNotFoundError, UnicodeDecodeError):
return ""
def load_or_create_admin_token() -> str:
"""读取 admin token;缺失或无效(空 / 过短 / 非 urlsafe)时原子生成新 token.
新 token(32 字节 urlsafe)先写入同目录 0600 临时文件:目标不存在时 ``os.link``
(不存在才创建,并发首启只落地一个完整 token),已存在但无效时 ``os.replace``;
读取方只会看到完整文件。
"""
path = admin_token_path()
token = _read_admin_token(path)
if is_valid_admin_token(token):
return token
ensure_home()
tmp = path.with_name(f".{path.name}.{secrets.token_hex(6)}.tmp")
create_private_file(tmp, secrets.token_urlsafe(32).encode())
try:
try:
os.link(tmp, path)
except FileExistsError:
if not is_valid_admin_token(_read_admin_token(path)):
os.replace(tmp, path)
except OSError: # 文件系统不支持硬链接
os.replace(tmp, path)
finally:
tmp.unlink(missing_ok=True)
return _read_admin_token(path)
__all__ = [
"ENV_HOME",
"proxy_home",
"ensure_home",
"config_db_path",
"master_key_path",
"admin_token_path",
"default_usage_db_path",
"default_compat_db_path",
"default_token_store_path",
"legacy_home_yaml_path",
"default_usage_db_str",
"default_compat_db_str",
"default_token_store_str",
"create_private_file",
"write_private_atomic",
"ADMIN_TOKEN_MIN_LENGTH",
"is_valid_admin_token",
"load_or_create_admin_token",
]