Repository navigation
Expand file tree
/
Copy pathqoder-admin.py
More file actions
1516 lines (1347 loc) · 57.3 KB
/
Copy pathqoder-admin.py
File metadata and controls
1516 lines (1347 loc) · 57.3 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Qoder Administration Web Console & Automation Daemon."""
from concurrent.futures import ThreadPoolExecutor
from http.cookies import SimpleCookie
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import json
import os
import random
import re
import secrets
import subprocess
import sys
import threading
import time
from urllib.error import HTTPError, URLError
from urllib.parse import parse_qs, quote, urlsplit
from urllib.request import Request, urlopen
VERSION = "1.0.0"
LISTEN_HOST = os.environ.get("QODER_ADMIN_HOST", "127.0.0.1")
LISTEN_PORT = int(os.environ.get("QODER_ADMIN_PORT", 8792))
LISTEN = (LISTEN_HOST, LISTEN_PORT)
ADMIN_DIR = os.environ.get("QODER_ADMIN_DIR", "/opt/qoder2api/admin")
QODER_CFG_FILE = os.environ.get("QODER_CFG_FILE", os.path.join(ADMIN_DIR, "qoder.json"))
QODER_BLOCK_FILE = os.environ.get("QODER_BLOCK_FILE", "/opt/qoder2api/blocked-models.json")
QODER_SCHEDULE_FILE = os.environ.get("QODER_SCHEDULE_FILE", os.path.join(ADMIN_DIR, "qoder-schedule.json"))
KEYS_FILE = os.environ.get("QODER_KEYS_FILE", os.path.join(ADMIN_DIR, "keys.json"))
CRED_FILE = os.environ.get("QODER_CRED_FILE", os.path.join(ADMIN_DIR, "credentials.txt"))
HASH_FILE = os.environ.get("QODER_HASH_FILE", os.path.join(ADMIN_DIR, "basicauth.hash"))
CADDY_FILE = os.environ.get("CADDY_FILE", "/etc/caddy/Caddyfile")
KEY_PREFIX = "sk-qd-"
ADMIN_PASSWORD_MIN = 8
RECENT_USAGE_LIMIT = 100
_STATE_LOCK = threading.RLock()
_PANEL_CACHE = {"token": "", "at": 0.0}
_SESSIONS = {} # session_token -> expire_time
QODER_SCHEDULE_LOCK = threading.RLock()
QODER_SCHEDULE_STATE = {"fired": "", "last_ok": None, "last_error": "", "last_at": 0}
# --- Helper utilities
def _atomic_write_json(filepath, data, mode=0o600):
os.makedirs(os.path.dirname(os.path.abspath(filepath)), exist_ok=True)
tmp = filepath + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
os.replace(tmp, filepath)
try:
os.chmod(filepath, mode)
except Exception:
pass
def _num(v, default=0):
try:
return int(v)
except (TypeError, ValueError):
return default
def _float(v):
try:
return float(v)
except (TypeError, ValueError):
return None
# --- Credentials & Session Security
def get_admin_password():
if os.path.exists(CRED_FILE):
try:
with open(CRED_FILE, "r", encoding="utf-8") as f:
for line in f:
s = line.strip()
if s.startswith("管理页登录密码:"):
return s.split("管理页登录密码:", 1)[1].strip()
if s.startswith("password:"):
return s.split("password:", 1)[1].strip()
except Exception:
pass
# Fallback / initialize default
default_pw = "admin123456"
try:
os.makedirs(ADMIN_DIR, exist_ok=True)
with open(CRED_FILE, "w", encoding="utf-8") as f:
f.write("管理页登录账号: admin\n")
f.write(f"管理页登录密码: {default_pw}\n")
os.chmod(CRED_FILE, 0o600)
except Exception:
pass
return default_pw
def verify_admin_password(password):
current = get_admin_password()
return bool(password) and secrets.compare_digest(current, str(password).strip())
def sync_caddy_hash(new_password):
try:
res = subprocess.run(
["caddy", "hash-password", "--plaintext", new_password],
capture_output=True, text=True, timeout=10
)
if res.returncode == 0:
newhash = res.stdout.strip()
with open(HASH_FILE, "w", encoding="utf-8") as f:
f.write(newhash)
try:
os.chmod(HASH_FILE, 0o600)
except Exception:
pass
if os.path.exists(CADDY_FILE):
with open(CADDY_FILE, "r", encoding="utf-8") as f:
content = f.read()
pattern = re.compile(r"(admin\s+)(\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53})")
new_content, n = pattern.subn(lambda m: m.group(1) + newhash, content)
if n > 0:
with open(CADDY_FILE, "w", encoding="utf-8") as f:
f.write(new_content)
subprocess.run(["caddy", "reload", "--config", CADDY_FILE], capture_output=True, check=False)
return True, "Synced Caddy hash successfully"
except Exception as e:
return False, str(e)
return False, "caddy binary not available"
def change_admin_password(old_pw, new_pw):
if not verify_admin_password(old_pw):
return False, "原密码错误"
new_pw = str(new_pw or "").strip()
if len(new_pw) < ADMIN_PASSWORD_MIN:
return False, f"密码至少需要 {ADMIN_PASSWORD_MIN} 位"
os.makedirs(ADMIN_DIR, exist_ok=True)
with open(CRED_FILE, "w", encoding="utf-8") as f:
f.write("管理页登录账号: admin\n")
f.write(f"管理页登录密码: {new_pw}\n")
try:
os.chmod(CRED_FILE, 0o600)
except Exception:
pass
sync_caddy_hash(new_pw)
return True, "密码已更新"
def create_session():
token = secrets.token_hex(24)
with _STATE_LOCK:
_SESSIONS[token] = time.time() + 86400 * 7 # 7 days
return token
def validate_session(token):
if not token:
return False
now = time.time()
with _STATE_LOCK:
expire = _SESSIONS.get(token)
if expire and expire > now:
return True
if token in _SESSIONS:
del _SESSIONS[token]
return False
# --- Qoder Upstream Gateway Calls
def qoder_conf():
try:
with open(QODER_CFG_FILE, "r", encoding="utf-8") as f:
d = json.load(f)
return d if isinstance(d, dict) else {}
except Exception:
return {}
def _qoder_panel_token():
now = time.time()
with _STATE_LOCK:
if _PANEL_CACHE["token"] and now - _PANEL_CACHE["at"] < 1800:
return _PANEL_CACHE["token"]
cfg = qoder_conf()
base = str(cfg.get("base_url") or "http://127.0.0.1:8790").rstrip("/")
pwd = str(cfg.get("panel_password") or "")
if not base or not pwd:
raise RuntimeError("未配置 Qoder 网关面板密码(qoder.json 缺少 panel_password)")
req = Request(
base + "/panel/login",
data=json.dumps({"password": pwd}).encode("utf-8"),
headers={"Content-Type": "application/json"}
)
with urlopen(req, timeout=15) as resp:
d = json.loads(resp.read().decode("utf-8", "ignore"))
tok = str((d or {}).get("token") or "")
if not tok:
raise RuntimeError("Qoder 面板登录未返回令牌")
_PANEL_CACHE["token"] = tok
_PANEL_CACHE["at"] = now
return tok
def _qoder_call(path, method="GET", body=None, panel=False, timeout=30):
cfg = qoder_conf()
base = str(cfg.get("base_url") or "http://127.0.0.1:8790").rstrip("/")
if not base:
raise RuntimeError("未配置 Qoder 网关 base_url")
headers = {"Accept": "application/json"}
key = str(cfg.get("api_key") or "")
if key:
headers["Authorization"] = "Bearer " + key
data = None
if body is not None:
data = json.dumps(body).encode("utf-8")
headers["Content-Type"] = "application/json"
if panel:
headers["X-Panel-Token"] = _qoder_panel_token()
req = Request(base + path, data=data, headers=headers, method=method)
with urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read().decode("utf-8", "ignore"))
# --- Accounts Pool & Credits
def qoder_accounts_info():
d = _qoder_call("/accounts", panel=True)
accounts = []
for a in (d.get("accounts") or []):
if not isinstance(a, dict):
continue
uid = str(a.get("uid") or a.get("id") or "")
accounts.append({
"uid": uid,
"name": str(a.get("nickname") or a.get("name") or uid or "账号"),
"realm": str(a.get("realm") or ""),
"enabled": bool(a.get("enabled", True)),
"plan": str(a.get("plan") or a.get("plan_name") or a.get("platform") or ""),
})
try:
summary = _qoder_call("/credits/summary")
totals = (summary or {}).get("totals") or {}
except Exception:
totals = {}
return {
"ok": True,
"accounts": accounts,
"usable": d.get("usable"),
"totals": totals,
"configured": bool(qoder_conf().get("base_url"))
}
def qoder_login_start(realm="cn"):
d = _qoder_call("/accounts/login/start", method="POST",
body={"platform": "CLI", "realm": realm}, panel=True)
url = str(d.get("authUrl") or "")
if not url:
raise RuntimeError("Qoder 未返回授权链接")
return {
"ok": True,
"auth_url": url,
"state": str(d.get("state") or ""),
"realm": str(d.get("realm") or realm)
}
def qoder_login_poll(state):
q = quote(str(state or ""))
return _qoder_call("/accounts/login/poll?state=" + q, panel=True, timeout=20)
def qoder_login_cancel(state):
return _qoder_call("/accounts/login/cancel", method="POST",
body={"state": str(state or "")}, panel=True, timeout=20)
def qoder_checkin():
with _STATE_LOCK:
return _qoder_call("/accounts/checkin", method="POST", body={}, panel=True, timeout=180)
def qoder_account_action(uid, action):
uid = str(uid or "").strip()
if not uid:
raise ValueError("缺少账号 uid")
if action == "delete":
return _qoder_call("/accounts/delete", method="POST", body={"uid": uid}, panel=True)
if action in ("enable", "disable"):
return _qoder_call("/accounts/set", method="POST",
body={"uid": uid, "enabled": action == "enable"}, panel=True)
if action == "refresh":
return _qoder_call("/accounts/refresh", method="POST", body={"uid": uid}, panel=True, timeout=90)
raise ValueError("未知账号操作: " + str(action))
def qoder_pool():
d = _qoder_call("/accounts", panel=True)
accounts = []
t_remain = t_used = t_size = 0
for a in (d.get("accounts") or []):
if not isinstance(a, dict):
continue
c = a.get("credits") or {}
remain = _num(c.get("remain"))
used = _num(c.get("used"))
size = _num(c.get("size"))
t_remain += remain
t_used += used
t_size += size
pkgs = []
for p in (c.get("packages") or []):
if isinstance(p, dict):
pkgs.append({
"name": str(p.get("name") or ""),
"remain": _num(p.get("remain")),
"size": _num(p.get("size"))
})
accounts.append({
"uid": str(a.get("uid") or ""),
"nickname": str(a.get("nickname") or a.get("uid") or "账号"),
"realm": str(a.get("realm") or ""),
"domain": str(a.get("domain") or ""),
"plan": str(a.get("platform") or ""),
"enabled": bool(a.get("enabled", True)),
"remain": remain, "used": used, "size": size,
"exceeded": bool(c.get("exceeded")),
"packages": pkgs,
"last_error": str(a.get("lastError") or "")[:120],
"in_cooldown": bool(a.get("inCooldown")),
})
pct = int(round(t_remain * 100.0 / t_size)) if t_size > 0 else 0
checkin = {}
try:
t = _qoder_call("/tasks", panel=True, timeout=60)
s = t.get("summary") or {}
checkin = {
"streak_days": s.get("streak_days"),
"energy": s.get("energy"),
"tasks": [{
"code": str(x.get("task_code") or ""),
"name": str(x.get("name") or ""),
"desc": str(x.get("description") or ""),
"url": str(x.get("jump_url") or ""),
"status": str(x.get("status") or ""),
} for x in (t.get("tasks") or []) if isinstance(x, dict)],
}
except Exception as e:
checkin = {"error": str(e)[:200]}
return {
"ok": True,
"configured": bool(qoder_conf().get("base_url")),
"totals": {
"remain": t_remain, "used": t_used, "size": t_size,
"accounts": len(accounts), "pct": pct
},
"accounts": accounts,
"checkin": checkin
}
def qoder_credits_refresh():
return _qoder_call("/accounts/credits", method="POST", body={}, panel=True, timeout=180)
def qoder_usage():
out = {"ok": True}
try:
u = _qoder_call("/usage", panel=True, timeout=30)
out["all_time"] = {k: u.get(k) for k in (
"requests", "errors", "prompt_tokens", "completion_tokens",
"reasoning_tokens", "cached_tokens", "total_tokens", "credit"
)}
out["since"] = str(u.get("since") or "")
out["by_model"] = u.get("by_model") or {}
except Exception as e:
out["all_time"] = {}
out["usage_error"] = str(e)[:150]
return out
def qoder_recent_usage(limit=RECENT_USAGE_LIMIT):
d = _qoder_call(f"/usage/recent?limit={max(1, int(limit))}", panel=True, timeout=30)
key_dict = {
str(k.get("key", "")).strip(): str(k.get("name", "")).strip()
for k in load_keys().get("keys", [])
if isinstance(k, dict) and k.get("key") and k.get("name")
}
names = {}
try:
for a in (_qoder_call("/accounts", panel=True, timeout=25).get("accounts") or []):
if isinstance(a, dict) and a.get("uid"):
names[str(a["uid"])] = str(a.get("nickname") or a["uid"])
except Exception:
names = {}
out = []
for r in (d.get("rows") or []):
if not isinstance(r, dict):
continue
uid = str(r.get("account") or "")
prompt = _num(r.get("prompt_tokens"))
cached = _num(r.get("cached_tokens"))
rate = None
if cached is not None and prompt > 0:
rate = round(float(cached) / float(prompt), 4)
raw_key = str(r.get("api_key") or r.get("key") or "")
masked_key = key_dict.get(raw_key) or (raw_key[:8] + "..." if len(raw_key) > 8 else raw_key)
out.append({
"at": str(r.get("iso") or ""),
"uid": uid,
"nickname": names.get(uid, uid),
"api_key": masked_key,
"realm": str(r.get("realm") or ""),
"model": str(r.get("model") or ""),
"mode": "stream" if r.get("stream") else "sync",
"status": r.get("status", 200),
"input_tokens": prompt,
"cached_input_tokens": cached,
"cache_hit_rate": rate,
"output_tokens": _num(r.get("completion_tokens")),
"total_tokens": _num(r.get("total_tokens")),
"credits": r.get("credit"),
})
return {"ok": True, "records": out}
# --- Model Catalog & Blocked Filtering
def _qoder_rate_text(m):
eff = _float(m.get("price_factor"))
peak = _float(m.get("price_factor_peak"))
valley = _float(m.get("price_factor_valley"))
orig = _float(m.get("original_price_factor"))
if eff == 0.0:
base = orig if orig is not None else peak
return "免费" + (f" (原 x{base:g})" if base else "")
if peak is not None and valley is not None and peak != valley:
return f"峰 x{peak:g} / 谷 x{valley:g}"
v = eff if eff is not None else (peak if peak is not None else valley)
return f"x{v:g}" if v is not None else "未返回"
def qoder_blocked_models_conf():
try:
with open(QODER_BLOCK_FILE, "r", encoding="utf-8") as f:
d = json.load(f)
except Exception:
return {"models": [], "context_length": None}
if isinstance(d, list):
return {"models": [str(x).strip() for x in d if str(x).strip()], "context_length": None}
if isinstance(d, dict):
return {
"models": [str(x).strip() for x in (d.get("models") or []) if str(x).strip()],
"context_length": d.get("context_length")
}
return {"models": [], "context_length": None}
def save_qoder_blocked_models(models, context_length=None):
clean_models = sorted(list({str(m).strip() for m in (models or []) if str(m).strip()}))
ctx = None
if context_length is not None and str(context_length).strip() != "":
try:
ctx = int(context_length)
except (ValueError, TypeError):
ctx = None
payload = {"models": clean_models, "context_length": ctx}
_atomic_write_json(QODER_BLOCK_FILE, payload, mode=0o644)
return payload
def qoder_model_rows():
cfg = qoder_conf()
base = str(cfg.get("base_url") or "http://127.0.0.1:8790").rstrip("/")
if not base:
raise RuntimeError("未配置 Qoder 网关(qoder.json)")
req = Request(base + "/v1/models", headers={
"Authorization": "Bearer " + str(cfg.get("api_key") or ""),
"Accept": "application/json",
})
with urlopen(req, timeout=30) as resp:
d = json.loads(resp.read().decode("utf-8", "ignore"))
blocked_info = qoder_blocked_models_conf()
blocked_set = {x.lower() for x in blocked_info["models"]}
rows = []
for m in (d.get("data") or []):
if not isinstance(m, dict):
continue
mid = str(m.get("id") or "").strip()
if not mid:
continue
rows.append({
"id": mid,
"blocked": mid.lower() in blocked_set,
"rate_text": _qoder_rate_text(m),
"context_length": m.get("context_length"),
"max_input_tokens": m.get("max_input_tokens"),
"aliases": m.get("aliases") or []
})
return {
"ok": True,
"models": rows,
"blocked_count": len(blocked_set),
"context_override": blocked_info.get("context_length")
}
# --- Schedule Management
def _qoder_norm_times(values):
out = set()
for v in (values or []):
s = str(v).strip()
if not s:
continue
if s.isdigit():
h = int(s)
if not (0 <= h <= 23):
raise ValueError("小时必须在 0-23")
out.add("%02d:00" % h)
continue
m = re.match(r"^(\d{1,2}):(\d{2})$", s)
if not m:
raise ValueError("时间点格式应为 HH:MM 或纯数字小时")
h, mi = int(m.group(1)), int(m.group(2))
if not (0 <= h <= 23 and 0 <= mi <= 59):
raise ValueError("时间点超出合法范围")
out.add("%02d:%02d" % (h, mi))
return sorted(out)
def qoder_schedule_conf():
cfg = {"enabled": True, "times": ["10:05", "21:30"], "jitter_minutes": 5}
try:
with open(QODER_SCHEDULE_FILE, "r", encoding="utf-8") as f:
d = json.load(f)
if isinstance(d, dict):
cfg["enabled"] = bool(d.get("enabled", True))
raw = d.get("times") or d.get("hours")
ts = _qoder_norm_times(raw) if raw else []
if ts:
cfg["times"] = ts
cfg["jitter_minutes"] = max(0, min(60, _num(d.get("jitter_minutes"), 5)))
except Exception:
pass
return cfg
def save_qoder_schedule(enabled, times, jitter_minutes=5):
ts = _qoder_norm_times(times)
if not ts:
raise ValueError("至少需要一个执行时点")
if len(ts) > 24:
raise ValueError("最多配置 24 个执行时点")
jitter = max(0, min(60, _num(jitter_minutes, 5)))
cfg = {"enabled": bool(enabled), "times": ts, "jitter_minutes": jitter}
with QODER_SCHEDULE_LOCK:
_atomic_write_json(QODER_SCHEDULE_FILE, cfg, mode=0o644)
return cfg
def _qoder_next_run(times, now=None):
if not times:
return ""
now = now or time.localtime()
cur = now.tm_hour * 60 + now.tm_min
ts = sorted(times)
for t in ts:
if int(t[:2]) * 60 + int(t[3:]) > cur:
return time.strftime("%Y-%m-%d ", now) + t
nxt = time.localtime(time.mktime(now) + 86400)
return time.strftime("%Y-%m-%d ", nxt) + ts[0]
def qoder_schedule_info():
cfg = qoder_schedule_conf()
with QODER_SCHEDULE_LOCK:
st = dict(QODER_SCHEDULE_STATE)
return {
"ok": True,
"enabled": cfg["enabled"],
"times": cfg["times"],
"jitter_minutes": cfg.get("jitter_minutes", 0),
"next_run": _qoder_next_run(cfg["times"]) if cfg["enabled"] else "",
"last_fired": st.get("fired") or "",
"last_ok": st.get("last_ok"),
"last_error": st.get("last_error") or "",
"last_at": st.get("last_at") or 0,
"server_time": time.strftime("%Y-%m-%d %H:%M:%S")
}
def _qoder_schedule_due(cfg, now, jitter_offset=0):
if not cfg.get("enabled"):
return False
# Target minute adjusted by jitter
target_min = now.tm_hour * 60 + now.tm_min - jitter_offset
if target_min < 0:
return False
h = target_min // 60
m = target_min % 60
return ("%02d:%02d" % (h, m)) in (cfg.get("times") or [])
def _qoder_schedule_loop():
current_jitter = 0
last_jitter_day = -1
while True:
try:
time.sleep(30)
cfg = qoder_schedule_conf()
if not cfg.get("enabled"):
continue
now = time.localtime()
if now.tm_yday != last_jitter_day:
max_j = cfg.get("jitter_minutes", 0)
current_jitter = random.randint(0, max_j) if max_j > 0 else 0
last_jitter_day = now.tm_yday
if not _qoder_schedule_due(cfg, now, current_jitter):
continue
stamp = time.strftime("%Y-%m-%d %H:%M", now)
with QODER_SCHEDULE_LOCK:
if QODER_SCHEDULE_STATE["fired"] == stamp:
continue
QODER_SCHEDULE_STATE["fired"] = stamp
try:
qoder_checkin()
with QODER_SCHEDULE_LOCK:
QODER_SCHEDULE_STATE["last_ok"] = True
QODER_SCHEDULE_STATE["last_error"] = ""
QODER_SCHEDULE_STATE["last_at"] = int(time.time())
except Exception as e:
with QODER_SCHEDULE_LOCK:
QODER_SCHEDULE_STATE["last_ok"] = False
QODER_SCHEDULE_STATE["last_error"] = str(e)[:200]
QODER_SCHEDULE_STATE["last_at"] = int(time.time())
except Exception:
pass
def start_qoder_scheduler():
t = threading.Thread(target=_qoder_schedule_loop, name="qoder-scheduler", daemon=True)
t.start()
return t
# --- Multi-tenant API Keys Management
def load_keys():
try:
with open(KEYS_FILE, "r", encoding="utf-8") as f:
d = json.load(f)
if isinstance(d.get("keys"), list):
return d
except Exception:
pass
return {"keys": []}
def save_keys(d):
_atomic_write_json(KEYS_FILE, d, mode=0o600)
def create_key(name):
name = str(name or "").strip()[:40] or "Default Key"
raw_key = KEY_PREFIX + secrets.token_urlsafe(24)
item = {
"id": secrets.token_hex(6),
"name": name,
"key": raw_key,
"platform": "qoder",
"enabled": True,
"created_at": int(time.time())
}
with _STATE_LOCK:
data = load_keys()
data["keys"].insert(0, item)
save_keys(data)
return item
def toggle_key(key_id):
with _STATE_LOCK:
data = load_keys()
for k in data.get("keys", []):
if k.get("id") == key_id or k.get("key") == key_id:
k["enabled"] = not k.get("enabled", True)
save_keys(data)
return {"ok": True, "enabled": k["enabled"]}
raise ValueError("Key not found")
def delete_key(key_id):
with _STATE_LOCK:
data = load_keys()
before = len(data.get("keys", []))
data["keys"] = [k for k in data.get("keys", []) if k.get("id") != key_id and k.get("key") != key_id]
if len(data["keys"]) != before:
save_keys(data)
return {"ok": True}
raise ValueError("Key not found")
# --- Obsidian Minimal UI Template
UI_HTML = r"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Qoder API Control Panel</title>
<style>
:root {
--bg-main: #111113;
--bg-card: #18181b;
--bg-card-sub: #222227;
--border: #2e2e36;
--accent: #7c3aed;
--accent-hover: #6d28d9;
--text-main: #f4f4f5;
--text-muted: #a1a1aa;
--success: #10b981;
--danger: #ef4444;
--warning: #f59e0b;
}
* { box-sizing: border-box; margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
body { background: var(--bg-main); color: var(--text-main); font-size: 14px; min-height: 100vh; display: flex; flex-direction: column; }
header { background: var(--bg-card); border-bottom: 1px solid var(--border); padding: 12px 24px; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 100; }
.logo-box { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 16px; }
.logo-badge { background: var(--accent); color: #fff; padding: 2px 8px; border-radius: 4px; font-size: 11px; text-transform: uppercase; font-weight: 700; letter-spacing: 0.5px; }
nav { display: flex; gap: 8px; flex-wrap: wrap; }
.tab-btn { background: transparent; border: 1px solid transparent; color: var(--text-muted); padding: 6px 14px; border-radius: 6px; cursor: pointer; font-size: 13px; font-weight: 500; transition: all 0.15s ease; }
.tab-btn:hover { color: var(--text-main); background: var(--bg-card-sub); }
.tab-btn.active { color: #fff; background: var(--accent); border-color: var(--accent-hover); }
main { flex: 1; padding: 24px; max-width: 1200px; width: 100%; margin: 0 auto; }
.card { background: var(--bg-card); border: 1px solid var(--border); border-radius: 8px; padding: 20px; margin-bottom: 20px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); }
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px solid var(--border); }
.card-title { font-size: 15px; font-weight: 600; color: var(--text-main); }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 20px; }
.stat-pill { background: var(--bg-card-sub); border: 1px solid var(--border); border-radius: 6px; padding: 14px; }
.stat-label { font-size: 12px; color: var(--text-muted); margin-bottom: 4px; }
.stat-value { font-size: 22px; font-weight: 700; color: var(--text-main); }
.progress-bar-bg { width: 100%; height: 6px; background: var(--border); border-radius: 3px; margin-top: 8px; overflow: hidden; }
.progress-bar-fill { height: 100%; background: var(--accent); border-radius: 3px; transition: width 0.3s ease; }
table { width: 100%; border-collapse: collapse; text-align: left; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--border); font-size: 13px; }
th { color: var(--text-muted); font-weight: 500; }
tr:hover td { background: var(--bg-card-sub); }
.btn { background: var(--bg-card-sub); border: 1px solid var(--border); color: var(--text-main); padding: 6px 12px; border-radius: 4px; cursor: pointer; font-size: 12px; font-weight: 500; transition: all 0.15s ease; display: inline-flex; align-items: center; gap: 4px; }
.btn:hover { background: var(--border); }
.btn-primary { background: var(--accent); border-color: var(--accent-hover); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { background: rgba(239, 68, 68, 0.15); border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.badge { display: inline-block; padding: 2px 6px; border-radius: 4px; font-size: 11px; font-weight: 600; }
.badge-success { background: rgba(16, 185, 129, 0.15); color: var(--success); border: 1px solid var(--success); }
.badge-danger { background: rgba(239, 68, 68, 0.15); color: var(--danger); border: 1px solid var(--danger); }
.badge-muted { background: var(--bg-card-sub); color: var(--text-muted); border: 1px solid var(--border); }
.input { background: var(--bg-card-sub); border: 1px solid var(--border); color: var(--text-main); padding: 8px 12px; border-radius: 4px; font-size: 13px; outline: none; }
.input:focus { border-color: var(--accent); }
.toast { position: fixed; bottom: 20px; right: 20px; padding: 10px 16px; border-radius: 6px; font-size: 13px; font-weight: 500; color: #fff; z-index: 1000; box-shadow: 0 4px 12px rgba(0,0,0,0.5); opacity: 0; transform: translateY(10px); transition: all 0.2s ease; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
.tab-content { display: none; }
.tab-content.active { display: block; }
.modal { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.7); z-index: 500; align-items: center; justify-content: center; }
.modal.show { display: flex; }
.modal-box { background: var(--bg-card); border: 1px solid var(--border); border-radius: 8px; width: 90%; max-width: 480px; padding: 24px; }
.modal-title { font-size: 16px; font-weight: 600; margin-bottom: 16px; }
.field-group { margin-bottom: 14px; }
.field-label { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
</style>
</head>
<body>
<header>
<div class="logo-box">
<span>Qoder Hub</span>
<span class="logo-badge">Pro Stack</span>
</div>
<nav>
<button class="tab-btn active" onclick="switchTab('pool')">账号与积分</button>
<button class="tab-btn" onclick="switchTab('models')">模型与改写</button>
<button class="tab-btn" onclick="switchTab('scheduler')">签到调度</button>
<button class="tab-btn" onclick="switchTab('keys')">API Keys</button>
<button class="tab-btn" onclick="switchTab('security')">安全设置</button>
</nav>
<button class="btn btn-danger" onclick="logout()">退出</button>
</header>
<main>
<!-- TAB: Pool -->
<div id="tab-pool" class="tab-content active">
<div class="stats-grid" id="pool-stats">
<div class="stat-pill">
<div class="stat-label">剩余 Credits</div>
<div class="stat-value" id="val-remain">-</div>
</div>
<div class="stat-pill">
<div class="stat-label">已消耗 Credits</div>
<div class="stat-value" id="val-used">-</div>
</div>
<div class="stat-pill">
<div class="stat-label">可用账号数</div>
<div class="stat-value" id="val-accounts">-</div>
</div>
<div class="stat-pill">
<div class="stat-label">总可用占比</div>
<div class="stat-value" id="val-pct">-%</div>
<div class="progress-bar-bg"><div class="progress-bar-fill" id="val-pct-bar" style="width:0%"></div></div>
</div>
</div>
<div class="card">
<div class="card-header">
<div class="card-title">Qoder 账号池</div>
<div style="display:flex;gap:8px">
<button class="btn btn-primary" onclick="startOAuth()">+ 添加账号 (OAuth)</button>
<button class="btn" onclick="manualCheckin()">立即一键签到</button>
<button class="btn" onclick="refreshPool()">刷新</button>
</div>
</div>
<table>
<thead>
<tr>
<th>UID</th>
<th>名称</th>
<th>Realm</th>
<th>剩余 Credits</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody id="accounts-tbody"></tbody>
</table>
</div>
<div class="card">
<div class="card-header">
<div class="card-title">最近请求记录 (Recent Usage)</div>
<button class="btn" onclick="loadRecentUsage()">刷新记录</button>
</div>
<table>
<thead>
<tr>
<th>时间</th>
<th>账号</th>
<th>模型</th>
<th>Tokens (Prompt/Cached/Out)</th>
<th>Cache率</th>
<th>消耗 Credits</th>
</tr>
</thead>
<tbody id="usage-tbody"></tbody>
</table>
</div>
</div>
<!-- TAB: Models -->
<div id="tab-models" class="tab-content">
<div class="card">
<div class="card-header">
<div class="card-title">模型改写与屏蔽设置</div>
<div style="display:flex;align-items:center;gap:12px">
<label style="font-size:13px;color:var(--text-muted)">Context Length 覆盖:</label>
<input type="number" id="ctx-override-input" class="input" style="width:140px" placeholder="例: 360000">
<button class="btn btn-primary" onclick="saveModelFilters()">保存配置</button>
</div>
</div>
<p style="color:var(--text-muted);font-size:13px;margin-bottom:14px">
勾选“屏蔽”将阻止该模型在 /v1/models 显示,并在调用时直接拦截返回 404。Context Length 覆盖将重写 context_length 与 max_input_tokens。
</p>
<table>
<thead>
<tr>
<th style="width:80px">屏蔽</th>
<th>模型 ID</th>
<th>倍率 (Rate)</th>
<th>上游 Context</th>
<th>别名</th>
</tr>
</thead>
<tbody id="models-tbody"></tbody>
</table>
</div>
</div>
<!-- TAB: Scheduler -->
<div id="tab-scheduler" class="tab-content">
<div class="card">
<div class="card-header">
<div class="card-title">签到保活调度器</div>
<button class="btn btn-primary" onclick="saveSchedule()">保存调度策略</button>
</div>
<div class="field-group">
<label><input type="checkbox" id="sched-enabled"> 启用自动定时签到</label>
</div>
<div class="field-group">
<label class="field-label">执行时点 (逗号分隔,格式 HH:MM,如 10:05, 21:30)</label>
<input type="text" id="sched-times" class="input" style="width:100%" placeholder="10:05, 21:30">
</div>
<div class="field-group">
<label class="field-label">防风控随机抖动 (Jitter 范围: 0-30 分钟)</label>
<input type="number" id="sched-jitter" class="input" style="width:140px" min="0" max="30">
</div>
<div style="margin-top:20px;padding:14px;background:var(--bg-card-sub);border-radius:6px;border:1px solid var(--border)">
<div><strong>下次预计运行:</strong> <span id="sched-next">-</span></div>
<div style="margin-top:6px"><strong>上次执行时间:</strong> <span id="sched-last-fired">-</span></div>
<div style="margin-top:6px"><strong>上次执行状态:</strong> <span id="sched-last-status">-</span></div>
</div>
</div>
</div>
<!-- TAB: Keys -->
<div id="tab-keys" class="tab-content">
<div class="card">
<div class="card-header">
<div class="card-title">多租户 API Keys</div>
<div style="display:flex;gap:8px">
<input type="text" id="new-key-name" class="input" placeholder="Key 备注名称 (例: Claude Code)">
<button class="btn btn-primary" onclick="addKey()">+ 生成新 Key</button>
</div>
</div>
<table>
<thead>
<tr>
<th>名称</th>
<th>Key (点击复制)</th>
<th>创建时间</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody id="keys-tbody"></tbody>
</table>
</div>
</div>
<!-- TAB: Security -->
<div id="tab-security" class="tab-content">
<div class="card" style="max-width:500px">
<div class="card-header">
<div class="card-title">控制台密码管理</div>
</div>
<div class="field-group">
<label class="field-label">原密码</label>
<input type="password" id="old-pass" class="input" style="width:100%">
</div>
<div class="field-group">
<label class="field-label">新密码 (至少 8 位)</label>
<input type="password" id="new-pass" class="input" style="width:100%">
</div>
<button class="btn btn-primary" onclick="updatePassword()">更新密码并同步</button>
</div>
</div>
</main>
<div id="toast" class="toast"></div>
<!-- OAuth Modal -->
<div id="oauth-modal" class="modal">
<div class="modal-box">
<div class="modal-title">添加 Qoder 账号 (OAuth)</div>
<p style="color:var(--text-muted);font-size:13px;margin-bottom:14px">请在外部浏览器打开如下链接完成登录授权:</p>
<div style="margin-bottom:14px">
<input type="text" id="oauth-url" class="input" style="width:100%" readonly>
</div>
<div style="display:flex;gap:8px;margin-bottom:14px">
<button class="btn btn-primary" onclick="copyOAuthUrl()">复制链接</button>
<a id="oauth-link" class="btn" href="#" target="_blank">直接打开</a>
</div>
<div style="font-size:13px;color:var(--warning)" id="oauth-status">等待授权中...</div>
<div style="margin-top:16px;text-align:right">
<button class="btn" onclick="closeOAuth()">取消</button>
</div>
</div>
</div>
<script>
let currentTab = 'pool';
let oauthPollTimer = null;
let currentOAuthState = null;
function showToast(msg, isError) {
const t = document.getElementById('toast');
t.textContent = msg;
t.className = 'toast show ' + (isError ? 'toast-error' : 'toast-success');