-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
1520 lines (1382 loc) · 57 KB
/
Copy pathworker.js
File metadata and controls
1520 lines (1382 loc) · 57 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
import { PAGE_HTML } from "./page_asset.js";
import { DEFAULT_STRATEGY_PROFILES } from "./strategy_profiles_asset.js";
const DEFAULT_REPOSITORY = "QuantStrategyLab/QuantRuntimeSettings";
const DEFAULT_WORKFLOW = "manual-strategy-switch.yml";
const SESSION_COOKIE = "qsl_switch_session";
const OAUTH_STATE_COOKIE = "qsl_switch_oauth_state";
const SESSION_TTL_SECONDS = 8 * 60 * 60;
const AUTH_CONFIG_KEY = "auth_config";
const ACCOUNT_OPTIONS_KEY = "account_options";
const STRATEGY_PROFILES_KEY = "strategy_profiles";
const AUDIT_LOG_KEY = "audit_log";
const AUDIT_LOG_LIMIT = 50;
const CURRENT_STRATEGIES_TIMEOUT_MS = 3500;
const SUPPORTED_PLATFORMS = ["longbridge", "ibkr", "schwab", "firstrade"];
const SUPPORTED_STRATEGY_DOMAINS = ["us_equity", "hk_equity"];
const PLATFORM_REPOSITORIES = {
longbridge: "QuantStrategyLab/LongBridgePlatform",
ibkr: "QuantStrategyLab/InteractiveBrokersPlatform",
schwab: "QuantStrategyLab/CharlesSchwabPlatform",
firstrade: "QuantStrategyLab/FirstradePlatform",
};
const DEFAULT_VARIABLE_SCOPE = {
longbridge: "environment",
ibkr: "repository",
schwab: "repository",
firstrade: "repository",
};
const SECURITY_HEADERS = {
"Content-Security-Policy": [
"default-src 'self'",
"base-uri 'none'",
"object-src 'none'",
"frame-ancestors 'none'",
"form-action 'self'",
"img-src 'self' data:",
"connect-src 'self'",
"script-src 'self' 'unsafe-inline'",
"style-src 'self' 'unsafe-inline'",
].join("; "),
"Permissions-Policy": "camera=(), microphone=(), geolocation=(), payment=(), usb=()",
"Referrer-Policy": "no-referrer",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
};
export default {
async fetch(request, env) {
const url = new URL(request.url);
try {
if (url.pathname === "/login") return startLogin(request, env);
if (url.pathname === "/callback") return finishLogin(request, env);
if (url.pathname === "/admin") return adminPage(request, env);
if (url.pathname === "/api/session") return json(await sessionPayload(request, env));
if (url.pathname === "/api/strategy-profiles") return json(await strategyProfilesPayload(env));
if (url.pathname === "/api/config") return json(await configPayload(request, env));
if (url.pathname === "/api/admin/config" && request.method === "GET") return adminConfigResponse(request, env);
if (url.pathname === "/api/admin/config" && request.method === "POST") {
return saveAdminConfig(request, env);
}
if (url.pathname === "/api/logout" && request.method === "POST") return logout(request);
if (url.pathname === "/api/switch" && request.method === "POST") return dispatchSwitch(request, env);
return html(PAGE_HTML);
} catch (error) {
return json({ ok: false, error: error.message || "unexpected error" }, error.status || 500);
}
},
};
class HttpError extends Error {
constructor(message, status) {
super(message);
this.status = status;
}
}
async function startLogin(request, env) {
requireEnv(env, "GITHUB_CLIENT_ID");
const url = new URL(request.url);
const state = randomToken();
const authorizeUrl = new URL("https://github.com/login/oauth/authorize");
authorizeUrl.searchParams.set("client_id", env.GITHUB_CLIENT_ID);
authorizeUrl.searchParams.set("redirect_uri", `${url.origin}/callback`);
authorizeUrl.searchParams.set("scope", "read:user read:org");
authorizeUrl.searchParams.set("state", state);
return redirect(authorizeUrl.toString(), {
"Set-Cookie": cookie(OAUTH_STATE_COOKIE, state, 600),
});
}
async function finishLogin(request, env) {
requireEnv(env, "GITHUB_CLIENT_ID");
requireEnv(env, "GITHUB_CLIENT_SECRET");
requireEnv(env, "SESSION_SECRET");
const url = new URL(request.url);
const code = url.searchParams.get("code") || "";
const state = url.searchParams.get("state") || "";
const cookies = parseCookies(request.headers.get("Cookie") || "");
if (!code || !state || cookies[OAUTH_STATE_COOKIE] !== state) {
return html(renderMessage("登录失败", "OAuth state 校验失败,请重新登录。"), 400, clearOAuthCookie());
}
const tokenResponse = await fetch("https://github.com/login/oauth/access_token", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
client_id: env.GITHUB_CLIENT_ID,
client_secret: env.GITHUB_CLIENT_SECRET,
code,
redirect_uri: `${url.origin}/callback`,
}),
});
const tokenPayload = await tokenResponse.json();
if (!tokenResponse.ok || !tokenPayload.access_token) {
return html(renderMessage("登录失败", "GitHub token exchange 失败。"), 502, clearOAuthCookie());
}
const userResponse = await fetch("https://api.github.com/user", {
headers: githubHeaders(tokenPayload.access_token),
});
const user = await userResponse.json();
const login = String(user.login || "").toLowerCase();
if (!userResponse.ok || !login) {
return html(renderMessage("登录失败", "无法读取 GitHub 用户。"), 502, clearOAuthCookie());
}
const authConfig = await loadAuthConfig(env);
const orgLogins = await fetchGithubOrgLogins(tokenPayload.access_token);
if (!isAllowedPrincipal(login, orgLogins, authConfig)) {
return html(renderMessage("没有权限", `${login} 不在允许登录名单或组织中。`), 403, clearOAuthCookie());
}
const session = await makeSession(login, authorizedOrgLogins(orgLogins, authConfig), env);
return redirect("/", {
"Set-Cookie": [
cookie(SESSION_COOKIE, session, SESSION_TTL_SECONDS),
clearCookie(OAUTH_STATE_COOKIE),
],
});
}
async function sessionPayload(request, env) {
const session = await readSession(request, env);
return {
authenticated: Boolean(session),
login: session?.login || null,
allowed: Boolean(session?.allowed),
admin: Boolean(session?.admin),
};
}
async function adminPage(request, env) {
const session = await requireAdminSession(request, env);
if (session instanceof Response) return session;
return html(await renderAdminPage(await buildAdminState(session, env)));
}
async function adminConfigResponse(request, env) {
const session = await readSession(request, env);
if (!session) return json({ ok: false, error: "login required" }, 401);
if (!session.admin) return json({ ok: false, error: "admin required" }, 403);
return json(await buildAdminState(session, env));
}
async function saveAdminConfig(request, env) {
requireSameOrigin(request, { requireOrigin: true });
const session = await readSession(request, env);
if (!session) return json({ ok: false, error: "login required" }, 401);
if (!session.admin) return json({ ok: false, error: "admin required" }, 403);
if (!hasConfigStore(env)) {
return json({ ok: false, error: "STRATEGY_SWITCH_CONFIG KV binding is required to save admin config" }, 400);
}
let raw;
try {
raw = await request.json();
} catch (error) {
return json({ ok: false, error: "request body must be valid JSON" }, 400);
}
const bootstrapAdmins = parseLoginList(env.STRATEGY_SWITCH_ADMIN_LOGINS || "", "STRATEGY_SWITCH_ADMIN_LOGINS");
const bootstrapAdminOrgs = parseOrgList(env.STRATEGY_SWITCH_ADMIN_ORGS || "", "STRATEGY_SWITCH_ADMIN_ORGS");
const allowedLogins = normalizeLoginList(raw.allowed_logins, "allowed_logins");
const allowedOrgs = normalizeOrgList(raw.allowed_orgs, "allowed_orgs");
const submittedAdmins = normalizeLoginList(raw.admin_logins, "admin_logins");
const submittedAdminOrgs = normalizeOrgList(raw.admin_orgs, "admin_orgs");
const effectiveAdmins = uniqueStrings([...bootstrapAdmins, ...submittedAdmins]);
const effectiveAdminOrgs = uniqueStrings([...bootstrapAdminOrgs, ...submittedAdminOrgs]);
if (!effectiveAdmins.includes(session.login) && !hasOrgMatch(session.orgs, effectiveAdminOrgs)) {
throw new Error("current admin login or org must remain in admin config");
}
const authConfig = {
allowed_logins: uniqueStrings([...allowedLogins, ...effectiveAdmins]),
allowed_orgs: allowedOrgs,
admin_logins: effectiveAdmins,
admin_orgs: effectiveAdminOrgs,
};
const accountOptions = normalizeAccountOptionsInput(raw.account_options, "account_options");
await writeConfigJson(env, AUTH_CONFIG_KEY, authConfig);
await writeConfigJson(env, ACCOUNT_OPTIONS_KEY, accountOptions);
await appendAuditLog(env, {
ts: new Date().toISOString(),
login: session.login,
action: "save_config",
allowed_count: authConfig.allowed_logins.length,
allowed_org_count: authConfig.allowed_orgs.length,
admin_count: authConfig.admin_logins.length,
admin_org_count: authConfig.admin_orgs.length,
account_counts: accountCounts(accountOptions),
});
return json(await buildAdminState(session, env));
}
async function requireAdminSession(request, env) {
const session = await readSession(request, env);
if (!session) return redirect("/login");
if (!session.admin) {
return html(renderMessage("没有管理权限", `${session.login} 不在管理员名单中。`), 403);
}
return session;
}
async function buildAdminState(session, env) {
const authConfig = await loadAuthConfig(env);
const accountConfig = await loadAccountOptionsConfig(env);
return {
ok: true,
session: { login: session.login, admin: true },
kvAvailable: hasConfigStore(env),
authConfig,
accountOptions: accountConfig.options || {},
accountOptionSource: accountConfig.source,
auditLog: await loadAuditLog(env),
};
}
async function renderAdminPage(state) {
const disabled = state.kvAvailable ? "" : " disabled";
const statusClass = state.kvAvailable ? "ready" : "warn";
const statusText = state.kvAvailable ? "KV 已连接 / KV connected" : "KV 未绑定,只读 / Read-only";
const sourceText = state.accountOptionSource === "kv"
? "KV"
: (state.accountOptionSource === "secret" ? "Worker secret" : "none");
const accountRows = SUPPORTED_PLATFORMS.map((platform) => {
const count = Array.isArray(state.accountOptions[platform]) ? state.accountOptions[platform].length : 0;
return `<tr><td>${escapeHtml(platform)}</td><td>${count}</td></tr>`;
}).join("");
const auditRows = state.auditLog.length
? state.auditLog.map((entry) => (
`<tr><td>${escapeHtml(entry.ts || "")}</td><td>${escapeHtml(entry.login || "")}</td><td>${escapeHtml(entry.action || "")}</td></tr>`
)).join("")
: `<tr><td colspan="3">暂无记录 / No records</td></tr>`;
return `<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light">
<title>Strategy Switch Login Management</title>
<style>
:root {
--bg: #f5f6f8;
--surface: #ffffff;
--ink: #16191f;
--muted: #66707c;
--line: #dce1e7;
--accent: #136f63;
--warn: #9a5b13;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; min-height: 100svh; background: var(--bg); color: var(--ink); letter-spacing: 0; }
button, textarea { font: inherit; letter-spacing: 0; }
.topbar {
min-height: 68px; display: flex; align-items: center; justify-content: space-between; gap: 16px;
padding: 16px 28px; border-bottom: 1px solid var(--line); background: rgba(250, 251, 252, 0.94);
position: sticky; top: 0; z-index: 10; backdrop-filter: blur(14px);
}
.brand { display: grid; gap: 3px; min-width: 0; }
h1 { margin: 0; font-size: 21px; line-height: 1.12; font-weight: 780; overflow-wrap: anywhere; }
.brand p, .muted { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.45; }
.actions { display: flex; align-items: center; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }
a, button { color: inherit; }
.btn {
min-height: 36px; display: inline-flex; align-items: center; justify-content: center; gap: 7px;
padding: 0 13px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
color: var(--muted); text-decoration: none; white-space: nowrap; font-size: 13px; font-weight: 740; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #ffffff; }
.btn:disabled { opacity: 0.48; cursor: not-allowed; }
.shell { width: min(1080px, calc(100vw - 36px)); margin: 0 auto; padding: 24px 0 34px; display: grid; gap: 18px; }
.status {
display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px;
padding: 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
}
.metric { display: grid; gap: 5px; min-width: 0; }
.metric strong { font-size: 15px; line-height: 1.2; overflow-wrap: anywhere; }
.metric span { color: var(--muted); font-size: 12px; line-height: 1.35; overflow-wrap: anywhere; }
.ready strong { color: var(--accent); }
.warn strong { color: var(--warn); }
form { display: grid; gap: 18px; }
section { display: grid; gap: 12px; padding: 18px 0; border-top: 1px solid var(--line); }
section:first-child { border-top: 0; padding-top: 0; }
h2 { margin: 0; font-size: 16px; line-height: 1.25; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
label { display: grid; gap: 7px; color: var(--muted); font-size: 12px; font-weight: 740; }
textarea {
width: 100%; min-height: 118px; resize: vertical; border: 1px solid var(--line); border-radius: 8px;
background: var(--surface); color: var(--ink); padding: 11px 12px; line-height: 1.45; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
textarea.json { min-height: 320px; }
.panel { padding: 18px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 10px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); font-size: 12px; }
.form-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
#status { color: var(--muted); font-size: 13px; line-height: 1.45; min-height: 20px; }
@media (max-width: 760px) {
.topbar { align-items: flex-start; flex-direction: column; padding: 15px 18px; }
.shell { width: min(100% - 24px, 1080px); padding-top: 16px; }
.status, .grid { grid-template-columns: 1fr; }
textarea.json { min-height: 260px; }
}
</style>
</head>
<body>
<header class="topbar">
<div class="brand">
<h1>登录管理 / Login Management</h1>
<p>GitHub OAuth 2.0 管理策略切换权限。</p>
</div>
<div class="actions">
<a class="btn" href="/">返回切换页</a>
<button class="btn" id="logout-button" type="button">退出</button>
</div>
</header>
<main class="shell">
<div class="status">
<div class="metric">
<strong>${escapeHtml(state.session.login)}</strong>
<span>当前管理员 / Current admin</span>
</div>
<div class="metric ${statusClass}">
<strong>${escapeHtml(statusText)}</strong>
<span>保存后台配置需要 Cloudflare KV。</span>
</div>
<div class="metric">
<strong>${escapeHtml(sourceText)}</strong>
<span>账号配置来源 / Account source</span>
</div>
</div>
<form class="panel" id="admin-form">
<section>
<h2>登录权限 / Login Access</h2>
<p class="muted">每行一个 GitHub 用户名或组织名。管理员会自动拥有切换权限;secret 里的管理员和管理员组织始终保留为兜底入口。</p>
<div class="grid">
<label>
可切换用户 / Allowed logins
<textarea id="allowed-logins"${disabled}>${escapeHtml(state.authConfig.allowed_logins.join("\n"))}</textarea>
</label>
<label>
可切换组织 / Allowed orgs
<textarea id="allowed-orgs"${disabled}>${escapeHtml(state.authConfig.allowed_orgs.join("\n"))}</textarea>
</label>
<label>
管理员 / Admin logins
<textarea id="admin-logins"${disabled}>${escapeHtml(state.authConfig.admin_logins.join("\n"))}</textarea>
</label>
<label>
管理员组织 / Admin orgs
<textarea id="admin-orgs"${disabled}>${escapeHtml(state.authConfig.admin_orgs.join("\n"))}</textarea>
</label>
</div>
</section>
<section>
<h2>账号下拉 / Account Options</h2>
<p class="muted">这里只保存账号路由,不保存 broker 密码、token、API key 或云密钥。</p>
<textarea class="json" id="account-options"${disabled}>${escapeHtml(JSON.stringify(state.accountOptions, null, 2))}</textarea>
</section>
<div class="form-actions">
<button class="btn primary" id="save-button" type="submit"${disabled}>保存配置</button>
<span id="status">${state.kvAvailable ? "" : "当前未绑定 STRATEGY_SWITCH_CONFIG KV,只能查看。"} </span>
</div>
</form>
<div class="panel">
<h2>账号数量 / Account Counts</h2>
<table>
<thead><tr><th>Platform</th><th>Accounts</th></tr></thead>
<tbody>${accountRows}</tbody>
</table>
</div>
<div class="panel">
<h2>最近修改 / Recent Changes</h2>
<table>
<thead><tr><th>Time</th><th>Login</th><th>Action</th></tr></thead>
<tbody id="audit-rows">${auditRows}</tbody>
</table>
</div>
</main>
<script>
const kvAvailable = ${JSON.stringify(state.kvAvailable)};
const statusNode = document.getElementById("status");
const setStatus = (message) => { statusNode.textContent = message; };
const parseLogins = (value) => value.split(/[\\s,]+/).map((item) => item.trim()).filter(Boolean);
document.getElementById("logout-button").addEventListener("click", async () => {
await fetch("/api/logout", { method: "POST" });
window.location.href = "/";
});
document.getElementById("admin-form").addEventListener("submit", async (event) => {
event.preventDefault();
if (!kvAvailable) return;
let accountOptions;
try {
accountOptions = JSON.parse(document.getElementById("account-options").value);
} catch {
setStatus("账号 JSON 无效 / Account JSON is invalid");
return;
}
setStatus("正在保存 / Saving...");
try {
const response = await fetch("/api/admin/config", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
allowed_logins: parseLogins(document.getElementById("allowed-logins").value),
allowed_orgs: parseLogins(document.getElementById("allowed-orgs").value),
admin_logins: parseLogins(document.getElementById("admin-logins").value),
admin_orgs: parseLogins(document.getElementById("admin-orgs").value),
account_options: accountOptions,
}),
});
const payload = await response.json();
if (!response.ok || !payload.ok) throw new Error(payload.error || "save failed");
setStatus("已保存 / Saved");
} catch (error) {
setStatus("保存失败 / Save failed: " + error.message);
}
});
</script>
</body>
</html>`;
}
async function configPayload(request, env) {
const session = await readSession(request, env);
if (!session?.allowed) return { accountOptions: null };
const accountConfig = await loadAccountOptionsConfig(env);
const strategyProfiles = await loadStrategyProfilesConfig(env);
return {
accountOptions: accountConfig.options,
strategyProfiles,
currentStrategies: await loadCurrentStrategiesSafely(accountConfig.options, env),
};
}
async function strategyProfilesPayload(env) {
return {
strategyProfiles: await loadStrategyProfilesConfig(env),
};
}
async function loadCurrentStrategies(accountOptions, env) {
const token = env.RUNTIME_SETTINGS_DISPATCH_TOKEN;
if (!token || !accountOptions) return {};
const variableCache = new Map();
const readVariable = (repository, scope, githubEnvironment, name) => {
const cacheKey = [repository, scope, githubEnvironment || "", name].join("|");
if (!variableCache.has(cacheKey)) {
variableCache.set(cacheKey, fetchGithubVariable(token, repository, scope, githubEnvironment, name));
}
return variableCache.get(cacheKey);
};
const currentStrategies = {};
const platformResults = await Promise.all(SUPPORTED_PLATFORMS.map(async (platform) => {
const options = Array.isArray(accountOptions[platform]) ? accountOptions[platform] : [];
if (!options.length) return [platform, {}];
const repository = PLATFORM_REPOSITORIES[platform];
if (!repository) return [platform, {}];
const optionResults = await Promise.all(options.map(async (option) => {
const current = await resolveCurrentStrategyForAccount({
platform,
option,
optionsCount: options.length,
repository,
readVariable,
});
return [option.key, current];
}));
const platformStrategies = {};
for (const [key, current] of optionResults) {
if (current) platformStrategies[key] = current;
}
return [platform, platformStrategies];
}));
for (const [platform, platformStrategies] of platformResults) {
if (Object.keys(platformStrategies).length) currentStrategies[platform] = platformStrategies;
}
return currentStrategies;
}
async function loadCurrentStrategiesSafely(accountOptions, env) {
try {
return await withTimeout(
loadCurrentStrategies(accountOptions, env),
CURRENT_STRATEGIES_TIMEOUT_MS,
{},
);
} catch {
return {};
}
}
function withTimeout(promise, timeoutMs, fallback) {
let timeoutId;
const timeout = new Promise((resolve) => {
timeoutId = setTimeout(() => resolve(fallback), timeoutMs);
});
return Promise.race([promise, timeout]).finally(() => clearTimeout(timeoutId));
}
async function resolveCurrentStrategyForAccount({ platform, option, optionsCount, repository, readVariable }) {
const serviceTargetsValue = await readVariable(repository, "repository", "", "CLOUD_RUN_SERVICE_TARGETS_JSON");
const serviceTarget = runtimeTargetFromServiceTargets(serviceTargetsValue, platform, option);
const serviceTargetProfile = cleanCurrentStrategy(serviceTarget?.strategy_profile);
if (serviceTargetProfile) {
return {
strategy_profile: serviceTargetProfile,
...runtimeModePayload(serviceTarget),
source: "CLOUD_RUN_SERVICE_TARGETS_JSON",
variable_scope: "repository",
};
}
const variableScope = resolveVariableScope(platform, option);
const githubEnvironment = resolveGithubEnvironment(platform, option, variableScope);
const runtimeTargetValue = await readVariable(repository, variableScope, githubEnvironment, "RUNTIME_TARGET_JSON");
const runtimeTarget = parseJsonObject(runtimeTargetValue);
const runtimeTargetMatches = runtimeTarget && runtimeTargetMatchesAccount(runtimeTarget, platform, option);
const runtimeTargetProfile = runtimeTargetMatches ? cleanCurrentStrategy(runtimeTarget.strategy_profile) : "";
if (runtimeTargetProfile) {
return {
strategy_profile: runtimeTargetProfile,
...runtimeModePayload(runtimeTarget),
source: "RUNTIME_TARGET_JSON",
variable_scope: variableScope,
github_environment: githubEnvironment || "",
};
}
if (variableScope === "environment" || optionsCount <= 1) {
const profileValue = await readVariable(repository, variableScope, githubEnvironment, "STRATEGY_PROFILE");
const profile = cleanCurrentStrategy(profileValue);
if (profile) {
const current = {
strategy_profile: profile,
source: "STRATEGY_PROFILE",
variable_scope: variableScope,
github_environment: githubEnvironment || "",
};
if (variableScope === "environment" && normalizeMatchValue(option?.target_name) === "paper") {
current.execution_mode = "paper";
}
return current;
}
}
return null;
}
function logout(request) {
requireSameOrigin(request, { requireOrigin: true });
return json({ ok: true }, 200, {
"Set-Cookie": clearCookie(SESSION_COOKIE),
});
}
async function dispatchSwitch(request, env) {
requireEnv(env, "RUNTIME_SETTINGS_DISPATCH_TOKEN");
requireSameOrigin(request, { requireOrigin: true });
const session = await readSession(request, env);
if (!session?.allowed) return json({ ok: false, error: "login required" }, 401);
const rawInput = await request.json();
const inputs = normalizeSwitchInputs(rawInput);
assertSwitchIntent(inputs);
const accountConfig = await loadAccountOptionsConfig(env);
const accountOption = assertConfiguredAccount(inputs, accountConfig.options);
assertStrategyAllowedForAccount(inputs, accountOption, await loadStrategyProfilesConfig(env));
const repository = env.RUNTIME_SETTINGS_REPO || DEFAULT_REPOSITORY;
const workflow = env.RUNTIME_SETTINGS_WORKFLOW || DEFAULT_WORKFLOW;
const apiUrl = `https://api.github.com/repos/${repository}/actions/workflows/${workflow}/dispatches`;
const response = await fetch(apiUrl, {
method: "POST",
headers: githubHeaders(env.RUNTIME_SETTINGS_DISPATCH_TOKEN),
body: JSON.stringify({
ref: env.RUNTIME_SETTINGS_REF || "main",
inputs,
}),
});
if (!response.ok) {
const text = await response.text();
return json({ ok: false, error: `GitHub dispatch failed: ${text.slice(0, 600)}` }, 502);
}
return json({
ok: true,
repository,
workflow,
actions_url: `https://github.com/${repository}/actions/workflows/${workflow}`,
inputs,
});
}
function normalizeSwitchInputs(raw) {
const platform = cleanChoice(raw.platform, SUPPORTED_PLATFORMS, "platform");
const targetName = cleanSlug(raw.target_name, "target_name");
const strategyProfile = cleanSlug(raw.strategy_profile, "strategy_profile").toLowerCase();
const executionMode = cleanChoice(raw.execution_mode || "live", ["live", "paper"], "execution_mode");
const pluginMode = cleanChoice(raw.plugin_mode || "auto", ["auto", "none", "custom"], "plugin_mode");
const variableScope = cleanChoice(
raw.variable_scope || "default",
["default", "repository", "environment"],
"variable_scope",
);
const apply = cleanBoolean(raw.apply);
const triggerPlatformSync = cleanBoolean(raw.trigger_platform_sync) && apply;
const extraVariablesJson = cleanOptionalJsonObject(raw.extra_variables_json || "", "extra_variables_json");
const inputs = {
platform,
target_name: targetName,
strategy_profile: strategyProfile,
execution_mode: executionMode,
variable_scope: variableScope,
plugin_mode: pluginMode,
service_targets_mode: "auto",
apply: apply ? "true" : "false",
trigger_platform_sync: triggerPlatformSync ? "true" : "false",
confirm_apply: apply ? (triggerPlatformSync ? "APPLY_AND_SYNC" : "APPLY") : "",
platform_sync_workflow: "sync-cloud-run-env.yml",
};
addOptional(inputs, "github_environment", raw.github_environment, cleanSlug);
addOptional(inputs, "deployment_selector", raw.deployment_selector, cleanSlug);
addOptional(inputs, "account_selector", raw.account_selector, cleanCsv);
addOptional(inputs, "account_scope", raw.account_scope, cleanSlug);
addOptional(inputs, "service_name", raw.service_name, cleanSlug);
addOptional(inputs, "custom_plugin_mounts_json", raw.custom_plugin_mounts_json, cleanJson);
if (extraVariablesJson) inputs.extra_variables_json = extraVariablesJson;
return inputs;
}
function assertSwitchIntent(inputs) {
if (
inputs.apply !== "true" ||
inputs.trigger_platform_sync !== "true" ||
inputs.confirm_apply !== "APPLY_AND_SYNC"
) {
throw new Error("switch endpoint requires apply=true and APPLY_AND_SYNC");
}
}
function assertConfiguredAccount(inputs, accountOptions) {
if (!accountOptions) throw new Error("account options are not configured");
const options = accountOptions[inputs.platform] || [];
if (!options.length) throw new Error(`no account options configured for ${inputs.platform}`);
const matched = options.find((option) => accountOptionMatchesInputs(option, inputs));
if (!matched) throw new Error("switch inputs do not match configured account options");
return matched;
}
function assertStrategyAllowedForAccount(inputs, accountOption, strategyProfiles) {
const strategy = strategyProfiles.find((item) => item.profile === inputs.strategy_profile);
if (!strategy || strategy.runtime_enabled !== true) {
throw new Error(`strategy ${inputs.strategy_profile} is not live-enabled`);
}
const supportedDomains = supportedDomainsForAccount(inputs.platform, accountOption);
if (!supportedDomains.includes(strategy.domain)) {
throw new Error(
`strategy domain ${strategy.domain} is not supported by ${inputs.platform}/${accountOption.key}`,
);
}
}
function accountOptionMatchesInputs(option, inputs) {
if (option.target_name !== inputs.target_name) return false;
const fields = [
"account_selector",
"deployment_selector",
"account_scope",
"service_name",
"github_environment",
"variable_scope",
"plugin_mode",
];
for (const field of fields) {
const expected = option[field] || "";
const actual = inputs[field] || "";
if (expected && actual !== expected) return false;
if (!expected && actual && !["default", "auto"].includes(actual)) return false;
}
return true;
}
function parseAccountOptions(raw, fieldName = "account options") {
const text = String(raw || "").trim();
if (!text) return null;
let payload;
try {
payload = JSON.parse(text);
} catch (error) {
throw new Error(`${fieldName} must be valid JSON`);
}
return normalizeAccountOptionsPayload(payload, fieldName);
}
function normalizeAccountOptionsInput(value, fieldName) {
if (typeof value === "string") return parseAccountOptions(value, fieldName) || {};
return normalizeAccountOptionsPayload(value, fieldName);
}
function parseStrategyProfiles(raw, fieldName = "strategy profiles") {
const text = String(raw || "").trim();
if (!text) return null;
let payload;
try {
payload = JSON.parse(text);
} catch (error) {
throw new Error(`${fieldName} must be valid JSON`);
}
return normalizeStrategyProfilesPayload(payload, fieldName);
}
function normalizeStrategyProfilesPayload(payload, fieldName = "strategy profiles") {
if (!Array.isArray(payload) || payload.length > 100) {
throw new Error(`${fieldName} must be an array with at most 100 items`);
}
const result = [];
const seen = new Set();
for (const [index, item] of payload.entries()) {
if (!item || Array.isArray(item) || typeof item !== "object") {
throw new Error(`${fieldName}[${index}] must be an object`);
}
const profile = cleanCurrentStrategy(item.profile || item.strategy_profile);
if (!profile) throw new Error(`${fieldName}[${index}].profile is invalid`);
if (seen.has(profile)) continue;
seen.add(profile);
const entry = {
profile,
label: cleanLabel(item.label || item.display_name || profile, `${fieldName}[${index}].label`),
runtime_enabled: cleanProfileBoolean(item.runtime_enabled ?? item.live_enabled ?? true),
};
entry.domain = cleanStrategyDomain(item.domain || "us_equity", `${fieldName}[${index}].domain`);
result.push(entry);
}
return result;
}
function cleanProfileBoolean(value) {
if (value === true || value === "true" || value === "1" || value === 1) return true;
if (value === false || value === "false" || value === "0" || value === 0) return false;
throw new Error("runtime_enabled must be boolean");
}
function normalizeAccountOptionsPayload(payload, fieldName = "account options") {
if (!payload || Array.isArray(payload) || typeof payload !== "object") {
throw new Error(`${fieldName} must be an object`);
}
const result = {};
for (const platform of SUPPORTED_PLATFORMS) {
const items = payload[platform];
if (items === undefined) continue;
if (!Array.isArray(items) || items.length > 20) {
throw new Error(`${fieldName}.${platform} must be an array with at most 20 items`);
}
result[platform] = items.map((item, index) => cleanAccountOption(item, platform, index));
}
return result;
}
function cleanAccountOption(item, platform, index) {
if (!item || Array.isArray(item) || typeof item !== "object") {
throw new Error(`account option ${platform}[${index}] must be an object`);
}
const key = cleanSlug(item.key || item.target_name || `${platform}-${index}`, "account key");
const label = cleanLabel(item.label || item.target_name || key, "account label");
const option = {
key,
label,
target_name: cleanSlug(item.target_name || key, "target_name"),
};
addConfigOptional(option, "account_selector", item.account_selector, cleanCsv);
addConfigOptional(option, "deployment_selector", item.deployment_selector, cleanSlug);
addConfigOptional(option, "account_scope", item.account_scope, cleanSlug);
addConfigOptional(option, "service_name", item.service_name, cleanSlug);
addConfigOptional(option, "default_strategy_profile", item.default_strategy_profile || item.strategy_profile, cleanSlug);
addConfigOptional(option, "github_environment", item.github_environment, cleanSlug);
addConfigOptional(option, "variable_scope", item.variable_scope, (value, field) =>
cleanChoice(value || "default", ["default", "repository", "environment"], field),
);
addConfigOptional(option, "plugin_mode", item.plugin_mode, (value, field) =>
cleanChoice(value || "auto", ["auto", "none"], field),
);
option.supported_domains = shouldInferSupportedDomains(item.supported_domains)
? inferAccountSupportedDomains(platform, option)
: normalizeSupportedDomains(item.supported_domains, `account option ${platform}[${index}].supported_domains`);
return option;
}
function shouldInferSupportedDomains(value) {
if (value === undefined || value === null) return true;
if (Array.isArray(value)) return value.length === 0;
return String(value).trim() === "";
}
function supportedDomainsForAccount(platform, option) {
if (Array.isArray(option?.supported_domains) && option.supported_domains.length) {
return normalizeSupportedDomains(option.supported_domains, "supported_domains");
}
return inferAccountSupportedDomains(platform, option || {});
}
function inferAccountSupportedDomains(platform, option) {
void option;
if (platform === "longbridge" || platform === "ibkr") return ["us_equity", "hk_equity"];
return ["us_equity"];
}
function normalizeSupportedDomains(value, fieldName) {
const items = Array.isArray(value)
? value
: String(value || "").split(/[\s,;]+/);
if (!items.length || items.length > SUPPORTED_STRATEGY_DOMAINS.length) {
throw new Error(`${fieldName} must list one or more strategy domains`);
}
const result = [];
for (const item of items) {
const domain = cleanStrategyDomain(item, fieldName);
if (!result.includes(domain)) result.push(domain);
}
if (!result.length) throw new Error(`${fieldName} must list one or more strategy domains`);
return result;
}
function cleanStrategyDomain(value, fieldName) {
return cleanChoice(value, SUPPORTED_STRATEGY_DOMAINS, fieldName);
}
function addConfigOptional(target, key, value, cleaner) {
if (value === undefined || value === null || String(value).trim() === "") return;
target[key] = cleaner(value, key);
}
function addOptional(target, key, value, cleaner) {
if (value === undefined || value === null || String(value).trim() === "") return;
target[key] = cleaner(value, key);
}
function cleanChoice(value, allowed, field) {
const text = String(value || "").trim();
if (!allowed.includes(text)) throw new Error(`${field} is invalid`);
return text;
}
function cleanBoolean(value) {
if (value === true || value === "true") return true;
if (value === false || value === "false" || value === "" || value === undefined || value === null) return false;
throw new Error("boolean input is invalid");
}
function cleanSlug(value, field) {
const text = String(value || "").trim();
if (!text || text.length > 120 || !/^[A-Za-z0-9._=-]+$/.test(text)) {
throw new Error(`${field} must use letters, numbers, dot, underscore, dash, or equals`);
}
return text;
}
function cleanLabel(value, field) {
const text = String(value || "").trim();
if (!text || text.length > 80 || /[<>{}]/.test(text)) {
throw new Error(`${field} is invalid`);
}
return text;
}
function requireSameOrigin(request, options = {}) {
const origin = request.headers.get("Origin");
if (!origin) {
if (options.requireOrigin) throw new HttpError("Origin header is required", 403);
return;
}
if (origin !== new URL(request.url).origin) throw new HttpError("cross-origin request rejected", 403);
}
async function fetchGithubVariable(token, repository, scope, githubEnvironment, name) {
const apiUrl = githubVariableUrl(repository, scope, githubEnvironment, name);
if (!apiUrl) return "";
try {
const response = await fetch(apiUrl, {
headers: githubHeaders(token),
});
if (response.status === 404 || response.status === 403) return "";
if (!response.ok) return "";
const payload = await response.json();
return String(payload?.value || "");
} catch {
return "";
}
}
function githubVariableUrl(repository, scope, githubEnvironment, name) {
const [owner, repo] = String(repository || "").split("/");
if (!owner || !repo) return "";
const base = `https://api.github.com/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}`;
const variableName = encodeURIComponent(name);
if (scope === "environment") {
if (!githubEnvironment) return "";
return `${base}/environments/${encodeURIComponent(githubEnvironment)}/variables/${variableName}`;
}
return `${base}/actions/variables/${variableName}`;
}
function resolveVariableScope(platform, option) {
const configured = String(option?.variable_scope || "").trim();
if (configured && configured !== "default") return configured;
return DEFAULT_VARIABLE_SCOPE[platform] || "repository";
}
function resolveGithubEnvironment(platform, option, variableScope) {
if (variableScope !== "environment") return "";
const configured = String(option?.github_environment || "").trim();
if (configured) return configured;
const targetName = String(option?.target_name || option?.key || "").trim();
if (!targetName) return "";
if (platform === "longbridge") return `longbridge-${targetName.toLowerCase()}`;
return targetName;
}
function runtimeTargetFromServiceTargets(rawValue, platform, option) {
const payload = parseJsonObject(rawValue);
const targets = Array.isArray(payload?.targets) ? payload.targets : [];
for (const entry of targets) {
if (!entry || Array.isArray(entry) || typeof entry !== "object") continue;
const runtimeTarget = entry.runtime_target && typeof entry.runtime_target === "object"
? entry.runtime_target
: {};
if (!runtimeTargetMatchesAccount(runtimeTarget, platform, option, entry)) continue;
return {
...runtimeTarget,
strategy_profile: runtimeTarget.strategy_profile || entry.strategy_profile,
};
}
return null;
}
function runtimeModePayload(runtimeTarget) {
const executionMode = normalizeRuntimeExecutionMode(runtimeTarget?.execution_mode, runtimeTarget?.dry_run_only);
const payload = {};
if (executionMode) payload.execution_mode = executionMode;
const dryRunOnly = cleanOptionalBoolean(runtimeTarget?.dry_run_only);
if (dryRunOnly !== null) payload.dry_run_only = dryRunOnly;
return payload;
}
function normalizeRuntimeExecutionMode(value, dryRunOnly) {
const mode = String(value || "").trim().toLowerCase();
if (mode === "live" || mode === "paper") return mode;
const dryRun = cleanOptionalBoolean(dryRunOnly);
if (dryRun === true) return "paper";
if (dryRun === false) return "live";
return "";
}
function cleanOptionalBoolean(value) {
if (value === true || value === "true" || value === "1" || value === 1) return true;
if (value === false || value === "false" || value === "0" || value === 0) return false;
return null;
}
function runtimeTargetMatchesAccount(runtimeTarget, platform, option, entry = {}) {
const runtimePlatform = String(runtimeTarget?.platform_id || "").trim().toLowerCase();
if (runtimePlatform && runtimePlatform !== platform) return false;
const serviceName = String(option?.service_name || defaultCurrentServiceName(platform, option?.target_name || option?.key) || "");