dsh-ris-ide 1.7.2 → 1.8.0
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.
- package/README.md +43 -4
- package/bin/check-listing.mjs +247 -0
- package/bin/proxy-janitor.mjs +183 -0
- package/bin/risd.mjs +287 -0
- package/bin/run-with-log.ps1 +43 -5
- package/lib/client-codemirror.js +92 -66
- package/lib/client.js +774 -89
- package/lib/index.js +2256 -21
- package/lib/janitor.js +377 -0
- package/lib/proxy.js +1577 -0
- package/lib/proxybridge-bundle.js +798 -0
- package/lib/redirector.js +2558 -0
- package/lib/risd/build.js +91 -0
- package/lib/risd/debug.js +374 -0
- package/lib/risd/git.js +207 -0
- package/lib/risd/leases.js +124 -0
- package/lib/risd/lifecycle.js +148 -0
- package/lib/risd/log.js +59 -0
- package/lib/risd/ping.js +94 -0
- package/lib/risd/programs.js +348 -0
- package/lib/risd/server.js +1478 -0
- package/lib/risd/state.js +137 -0
- package/lib/risd/ui.js +1705 -0
- package/lib/risd/view.js +79 -0
- package/lib/risd-client.js +282 -0
- package/lib/standalone/window.html +14 -16
- package/lib/startup.js +114 -0
- package/package.json +7 -2
- package/skills/dsh-ris-ide-debug/SKILL.md +37 -2
- package/test/client-build.test.mjs +113 -0
- package/test/client-viewer.test.mjs +314 -20
- package/test/companion.test.mjs +65 -0
- package/test/dsh-origin.test.mjs +140 -0
- package/test/dsh-window-refresh.test.mjs +100 -0
- package/test/harness.test.mjs +9 -0
- package/test/host-noloop.test.mjs +74 -0
- package/test/host-tail.test.mjs +54 -5
- package/test/host-window.test.mjs +4 -4
- package/test/inherit-engine.test.mjs +175 -0
- package/test/install-phase.test.mjs +36 -0
- package/test/janitor.test.mjs +172 -0
- package/test/lifecycle.test.mjs +109 -0
- package/test/merge-owners.test.mjs +155 -0
- package/test/proxy-restart.test.mjs +37 -0
- package/test/proxy-route.test.mjs +599 -0
- package/test/proxy-window.test.mjs +45 -0
- package/test/proxy.test.mjs +914 -0
- package/test/proxybridge-bundle.test.mjs +687 -0
- package/test/redirector-bundle.test.mjs +391 -0
- package/test/redirector.test.mjs +571 -0
- package/test/risd-api.test.mjs +470 -0
- package/test/risd-bridge-degrade.test.mjs +101 -0
- package/test/risd-build.test.mjs +20 -0
- package/test/risd-cli-home.test.mjs +50 -0
- package/test/risd-client.test.mjs +130 -0
- package/test/risd-debug.test.mjs +261 -0
- package/test/risd-election.test.mjs +104 -0
- package/test/risd-engine.test.mjs +132 -0
- package/test/risd-entry.test.mjs +118 -0
- package/test/risd-git.test.mjs +109 -0
- package/test/risd-handoff.test.mjs +116 -0
- package/test/risd-heal.test.mjs +50 -0
- package/test/risd-helpers.mjs +108 -0
- package/test/risd-host-p2.test.mjs +147 -0
- package/test/risd-janitor-guard.test.mjs +46 -0
- package/test/risd-lease.test.mjs +83 -0
- package/test/risd-lifecycle.test.mjs +210 -0
- package/test/risd-log-follow.test.mjs +227 -0
- package/test/risd-mode.test.mjs +161 -0
- package/test/risd-ping.test.mjs +99 -0
- package/test/risd-programs.test.mjs +169 -0
- package/test/risd-review-p2.test.mjs +217 -0
- package/test/risd-shutdown.test.mjs +97 -0
- package/test/risd-teardown.test.mjs +87 -0
- package/test/risd-tools.test.mjs +211 -0
- package/test/risd-ui.test.mjs +445 -0
- package/test/risd-write.test.mjs +170 -0
- package/test/startup-reconcile.test.mjs +100 -0
- package/test/windows.test.mjs +37 -2
package/lib/risd/ui.js
ADDED
|
@@ -0,0 +1,1705 @@
|
|
|
1
|
+
// The daemon's own maintenance page (Phase 3-lite, triggered by a real gap):
|
|
2
|
+
// when no dsh is open there is no panel, yet the daemon may still own a proxy
|
|
3
|
+
// engine, running programs (JVMs) and a debug session. Those must be stoppable.
|
|
4
|
+
//
|
|
5
|
+
// Phase 2-lite follow-up: the page now has three top tabs —
|
|
6
|
+
// 概览 · proxy state + running programs grouped by workspace + debug sessions
|
|
7
|
+
// 代理 · the machine-wide rule table (independent of dsh: CAS save via /v1/proxy,
|
|
8
|
+
// engine start/stop, bridge restart, proxy log tail)
|
|
9
|
+
// IDE · embeds the dsh IDE window when a dsh window is answering, greyed out
|
|
10
|
+
// with the reason otherwise (GET /v1/dsh reads dsh-window.json)
|
|
11
|
+
//
|
|
12
|
+
// One self-contained HTML file: no DSH client bundle, no npm dependency, no build
|
|
13
|
+
// step. It is served only to loopback requests (Host check) and carries the token
|
|
14
|
+
// inline — anyone able to load it can already read `risd.json` anyway.
|
|
15
|
+
|
|
16
|
+
/** Escape text for the inline `<script>` token assignment. */
|
|
17
|
+
function jsonForScript(value) {
|
|
18
|
+
return JSON.stringify(value).replace(/</g, '\\u003c')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Render the maintenance page.
|
|
23
|
+
* @param options - `{ token, version, home, apiVersion }`.
|
|
24
|
+
* @returns the HTML document.
|
|
25
|
+
*/
|
|
26
|
+
export function risdUiHtml(options) {
|
|
27
|
+
const token = jsonForScript(options.token ?? '')
|
|
28
|
+
const version = jsonForScript(options.version ?? '')
|
|
29
|
+
const home = jsonForScript(options.home ?? '')
|
|
30
|
+
const apiVersion = jsonForScript(String(options.apiVersion ?? '1'))
|
|
31
|
+
// The code THIS process runs (see risd/build.js): the page compares it with what
|
|
32
|
+
// /v1/health reports and offers a refresh once a restarted daemon has new code.
|
|
33
|
+
const uiBuild = jsonForScript(String(options.uiBuild?.loaded ?? ''))
|
|
34
|
+
// Which daemon PROCESS rendered this page. A restart may keep the same code but
|
|
35
|
+
// issue a new token: the old page then answers 401 to everything, and comparing
|
|
36
|
+
// this value with /v1/health's `instance` is how the page notices (review C3-P1-3).
|
|
37
|
+
const uiInstance = jsonForScript(String(options.uiInstance ?? ''))
|
|
38
|
+
return `<!doctype html>
|
|
39
|
+
<html lang="zh-CN">
|
|
40
|
+
<head>
|
|
41
|
+
<meta charset="utf-8">
|
|
42
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
43
|
+
<title>risd — 本机代理与程序维护</title>
|
|
44
|
+
<style>
|
|
45
|
+
:root { color-scheme: light; --b:#e4e7ec; --muted:#6b7280; --bg:#f7f8fa; --card:#fff; --accent:#2f7cf6; --danger:#dc2626; --ok:#15803d; }
|
|
46
|
+
* { box-sizing: border-box; }
|
|
47
|
+
body { margin:0; background:#fff; font:14px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"PingFang SC","Microsoft YaHei",sans-serif; color:#111; }
|
|
48
|
+
header { display:flex; align-items:center; gap:14px; padding:12px 24px; background:var(--card); border-bottom:1px solid var(--b); position:sticky; top:0; z-index:5; }
|
|
49
|
+
header h1 { position:relative; z-index:1; font-size:15px; margin:0 0 0 6px; font-weight:650; letter-spacing:.2px; }
|
|
50
|
+
.side-toggle { width:26px; height:26px; flex:0 0 26px; display:flex; align-items:center; justify-content:center;
|
|
51
|
+
border:0; border-radius:6px; background:transparent; color:var(--muted); font-size:16px; line-height:1; cursor:pointer; }
|
|
52
|
+
.side-toggle:hover { background:#f1f3f6; color:#111; }
|
|
53
|
+
.side-toggle:disabled { opacity:.35; cursor:default; }
|
|
54
|
+
.side-toggle:disabled:hover { background:transparent; color:var(--muted); }
|
|
55
|
+
header .meta { color:var(--muted); font-size:12px; }
|
|
56
|
+
header .spacer { flex:1; }
|
|
57
|
+
nav { position:absolute; left:50%; transform:translateX(-50%); display:flex; gap:4px; }
|
|
58
|
+
nav button { font:inherit; font-size:13px; padding:5px 14px; border:1px solid transparent; border-radius:7px; background:transparent; color:var(--muted); cursor:pointer; }
|
|
59
|
+
nav button:hover { background:#f1f3f6; color:#111; }
|
|
60
|
+
nav button[aria-selected="true"] { background:#eef4ff; border-color:#d7e4ff; color:var(--accent); font-weight:600; }
|
|
61
|
+
nav button:disabled { opacity:.4; cursor:not-allowed; }
|
|
62
|
+
nav button:disabled:hover { background:transparent; color:var(--muted); }
|
|
63
|
+
main { padding:0; }
|
|
64
|
+
.view#view-overview, .view#view-proxy { max-width:1120px; margin:0 auto; padding:18px 24px 60px; display:flex; flex-direction:column; gap:14px; }
|
|
65
|
+
.view#view-ide { height:calc(100vh - 53px); }
|
|
66
|
+
.ide-stale { display:flex; align-items:center; gap:8px; padding:6px 12px; background:#fff8e6; border-bottom:1px solid #f0d8a8; color:#8a5a00; font-size:12px; }
|
|
67
|
+
.ide-stale[hidden] { display:none; }
|
|
68
|
+
/* 「页面代码变了但 daemon 还是旧模块」提示:纯静态页面本身没有热更新,daemon
|
|
69
|
+
import 过的模块也不会再读盘,只能重启 daemon;这条横幅把「为什么刷新没用」
|
|
70
|
+
说出来,而不是让用户反复强刷。 */
|
|
71
|
+
.ui-drift { display:flex; align-items:center; gap:8px; padding:6px 12px; background:#eef4ff; border-bottom:1px solid #d7e4ff; color:#1d4ed8; font-size:12px; }
|
|
72
|
+
.ui-drift[hidden] { display:none; }
|
|
73
|
+
.ui-drift span { flex:1; }
|
|
74
|
+
.ui-drift button { border:0; background:transparent; color:inherit; font:inherit; cursor:pointer; padding:0 4px; }
|
|
75
|
+
.ui-drift #ui-drift-save { border:1px solid #bcd3ff; background:#fff; border-radius:6px; padding:2px 10px; }
|
|
76
|
+
.ui-drift #ui-drift-save:disabled { opacity:.5; cursor:default; }
|
|
77
|
+
.ui-drift #ui-drift-save[hidden] { display:none; }
|
|
78
|
+
.ui-drift #ui-drift-reload { border:1px solid #bcd3ff; background:#fff; border-radius:6px; padding:2px 10px; }
|
|
79
|
+
.ide-stale span { flex:1; }
|
|
80
|
+
.ide-stale button { border:0; background:transparent; color:#8a5a00; font-size:15px; line-height:1; padding:0 2px; cursor:pointer; }
|
|
81
|
+
.ide-stale button:hover { color:#5c3c00; }
|
|
82
|
+
.view[hidden] { display:none !important; }
|
|
83
|
+
section { background:var(--card); border:1px solid var(--b); border-radius:10px; padding:14px 18px; }
|
|
84
|
+
section h2 { margin:0 0 10px; font-size:13px; font-weight:650; display:flex; align-items:center; gap:8px; }
|
|
85
|
+
section h2 .sub { color:var(--muted); font-weight:400; font-size:12px; }
|
|
86
|
+
.row { display:flex; align-items:center; gap:8px; flex-wrap:wrap; }
|
|
87
|
+
.grow { flex:1; }
|
|
88
|
+
button { font:inherit; padding:5px 12px; border-radius:6px; border:1px solid var(--b); background:#fff; cursor:pointer; }
|
|
89
|
+
button:hover { border-color:#c9ced6; }
|
|
90
|
+
button.primary { background:var(--accent); border-color:var(--accent); color:#fff; }
|
|
91
|
+
button.danger { color:var(--danger); border-color:#f3c7c7; }
|
|
92
|
+
button:disabled { opacity:.45; cursor:default; }
|
|
93
|
+
/* Long operations (sudo/UAC behind start) keep the button visibly busy instead of
|
|
94
|
+
looking dead; the label says which half is running. */
|
|
95
|
+
button[aria-busy="true"] { opacity:.7; cursor:progress; }
|
|
96
|
+
input, select { font:inherit; padding:5px 8px; border:1px solid var(--b); border-radius:6px; background:#fff; }
|
|
97
|
+
input.narrow { width:110px; }
|
|
98
|
+
table { width:100%; border-collapse:collapse; font-size:13px; }
|
|
99
|
+
th, td { text-align:left; padding:6px 8px; border-bottom:1px solid #f0f1f4; vertical-align:middle; }
|
|
100
|
+
th { color:var(--muted); font-weight:500; font-size:12px; }
|
|
101
|
+
td input[type="text"] { width:100%; }
|
|
102
|
+
td.actions { white-space:nowrap; text-align:right; }
|
|
103
|
+
.pill { display:inline-block; padding:1px 8px; border-radius:999px; font-size:11px; border:1px solid var(--b); color:var(--muted); }
|
|
104
|
+
.pill.on { color:var(--ok); border-color:#bbe3c8; background:#f0fdf4; }
|
|
105
|
+
.pill.off { color:var(--muted); }
|
|
106
|
+
.pill.warn { color:#b45309; border-color:#fde3b3; background:#fffbeb; }
|
|
107
|
+
/* SOCKS reachability chip: quiet while probing, green when the greeting answered,
|
|
108
|
+
amber when the host answered but is not a usable SOCKS5. */
|
|
109
|
+
#proxy-ping.busy { color:var(--muted); }
|
|
110
|
+
#proxy-ping.on { color:var(--ok); border-color:#bbe3c8; background:#f0fdf4; }
|
|
111
|
+
#proxy-ping.warn { color:#b45309; border-color:#fde3b3; background:#fffbeb; }
|
|
112
|
+
#proxy-ping.err { color:#b91c1c; border-color:#fecaca; background:#fef2f2; }
|
|
113
|
+
#bridge-pill.on { color:var(--ok); border-color:#bbe3c8; background:#f0fdf4; }
|
|
114
|
+
#bridge-pill.warn { color:#b45309; border-color:#fde3b3; background:#fffbeb; }
|
|
115
|
+
#bridge-pill.err { color:#b91c1c; border-color:#fecaca; background:#fef2f2; }
|
|
116
|
+
.muted { color:var(--muted); }
|
|
117
|
+
.empty { color:var(--muted); padding:6px 0; }
|
|
118
|
+
pre { margin:8px 0 0; background:#0f172a; color:#d6deeb; padding:10px 12px; border-radius:8px; max-height:340px; overflow:auto; font:12px/1.45 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace; white-space:pre-wrap; word-break:break-all; }
|
|
119
|
+
ol.sessions, ul.programs { list-style:none; margin:0; padding:0; }
|
|
120
|
+
ul.programs li, ol.sessions li { display:flex; align-items:center; gap:10px; padding:6px 0; border-bottom:1px solid #f0f1f4; }
|
|
121
|
+
ul.programs li:last-child, ol.sessions li:last-child { border-bottom:0; }
|
|
122
|
+
.group-title { font-weight:600; font-size:12px; color:#374151; margin:10px 0 2px; }
|
|
123
|
+
.ide-layout { display:flex; height:100%; min-height:0; }
|
|
124
|
+
.ide-side { flex:0 0 208px; width:208px; border-right:1px solid var(--b); display:flex; flex-direction:column; min-height:0; background:#fbfbfc; }
|
|
125
|
+
.ide-side[hidden] { display:none; }
|
|
126
|
+
.ide-side ul { list-style:none; margin:0; padding:6px; overflow:auto; flex:1; }
|
|
127
|
+
.ide-side li button { width:100%; text-align:left; border:0; background:transparent; padding:6px 8px; border-radius:6px; font-size:12.5px; font-weight:400; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; color:#111; }
|
|
128
|
+
.ide-side li button:hover { background:#f1f3f6; }
|
|
129
|
+
.ide-side li button.active { background:#eef4ff; color:var(--accent); font-weight:600; }
|
|
130
|
+
.ide-side .note { padding:0 14px 10px; }
|
|
131
|
+
.ide-main { flex:1; min-width:0; display:flex; flex-direction:column; min-height:0; }
|
|
132
|
+
.ide-body { flex:1; min-height:0; display:flex; flex-direction:column; }
|
|
133
|
+
.ide-body .ide-frame { flex:1; width:100%; height:100%; border:0; border-radius:0; }
|
|
134
|
+
.ide-body .ide-frame[hidden] { display:none; }
|
|
135
|
+
.ide-body .ide-msg { flex:1; min-height:0; overflow:auto; }
|
|
136
|
+
.ide-body .empty, .ide-body .banner { margin:18px; }
|
|
137
|
+
|
|
138
|
+
.ide-frame { width:100%; height:70vh; border:1px solid var(--b); border-radius:8px; background:#fff; }
|
|
139
|
+
.note { font-size:12px; color:var(--muted); }
|
|
140
|
+
.life { display:flex; align-items:center; gap:10px; padding:6px 0; font-size:13px; }
|
|
141
|
+
.life b { min-width:96px; font-weight:600; }
|
|
142
|
+
.life input { margin:0; }
|
|
143
|
+
.modal { position:fixed; inset:0; display:flex; align-items:center; justify-content:center; background:rgba(17,24,39,.45); z-index:50; }
|
|
144
|
+
.modal[hidden] { display:none; }
|
|
145
|
+
.modal-card { width:440px; max-width:calc(100% - 32px); background:#fff; border-radius:12px; padding:20px 22px; box-shadow:0 24px 48px rgba(17,24,39,.22); }
|
|
146
|
+
.modal-card h3 { margin:0 0 10px; font-size:16px; }
|
|
147
|
+
.modal-card .modal-body { font-size:13px; color:#374151; line-height:1.7; }
|
|
148
|
+
.modal-card .modal-body b { color:#111827; }
|
|
149
|
+
.modal-card .modal-actions { display:flex; justify-content:flex-end; gap:8px; margin-top:18px; }
|
|
150
|
+
.banner { padding:8px 12px; border-radius:8px; font-size:13px; }
|
|
151
|
+
/* An empty banner wrapper is still a flex item, so the 14px gap counted it and
|
|
152
|
+
pushed 概览 content down one gap lower than 代理设置 (m11445). Hidden when empty. */
|
|
153
|
+
#overview-banner:empty, #proxy-banner:empty { display:none; }
|
|
154
|
+
.banner.warn { background:#fffbeb; border:1px solid #fde3b3; color:#92400e; }
|
|
155
|
+
.banner.ok { background:#f0fdf4; border:1px solid #bbe3c8; color:#166534; }
|
|
156
|
+
.banner.err { background:#fef2f2; border:1px solid #fecaca; color:#991b1b; }
|
|
157
|
+
</style>
|
|
158
|
+
</head>
|
|
159
|
+
<body>
|
|
160
|
+
<header>
|
|
161
|
+
<button type="button" id="ide-side-toggle" class="side-toggle" title="展开工作区面板" aria-label="展开工作区面板" aria-expanded="false">›</button>
|
|
162
|
+
<h1>risd</h1>
|
|
163
|
+
<span class="spacer"></span>
|
|
164
|
+
<nav role="tablist" aria-label="维护页">
|
|
165
|
+
<button role="tab" data-tab="ide" aria-selected="false">开发环境</button>
|
|
166
|
+
<button role="tab" data-tab="proxy" aria-selected="false">代理设置</button>
|
|
167
|
+
<button role="tab" data-tab="overview" aria-selected="true">概览</button>
|
|
168
|
+
</nav>
|
|
169
|
+
</header>
|
|
170
|
+
<div id="ui-drift" class="ui-drift" hidden>
|
|
171
|
+
<span id="ui-drift-text">插件版本已更新:点「重启并刷新」后生效。</span>
|
|
172
|
+
<button type="button" id="ui-drift-save" title="保存开发环境里所有未保存的文件" hidden>保存所有文件</button>
|
|
173
|
+
<button type="button" id="ui-drift-reload" title="重新加载本页">刷新</button>
|
|
174
|
+
<button type="button" id="ui-drift-close" title="关闭提示" aria-label="关闭提示">×</button>
|
|
175
|
+
</div>
|
|
176
|
+
<main>
|
|
177
|
+
<div class="view" id="view-overview">
|
|
178
|
+
<div id="overview-banner"></div>
|
|
179
|
+
<section>
|
|
180
|
+
<h2>守护进程 <span class="sub" id="daemon-sub"></span></h2>
|
|
181
|
+
<div class="row">
|
|
182
|
+
<span class="pill" id="daemon-pill">未知</span>
|
|
183
|
+
<span class="muted" id="daemon-detail"></span>
|
|
184
|
+
<span class="grow"></span>
|
|
185
|
+
<button class="danger" id="daemon-stop">停止</button>
|
|
186
|
+
</div>
|
|
187
|
+
</section>
|
|
188
|
+
<section>
|
|
189
|
+
<h2>代理运行状态 <span class="sub" id="proxy-sub"></span></h2>
|
|
190
|
+
<div class="row">
|
|
191
|
+
<span class="pill" id="proxy-pill">未知</span>
|
|
192
|
+
<span class="muted" id="proxy-detail"></span>
|
|
193
|
+
<span class="grow"></span>
|
|
194
|
+
<button id="engine-toggle" disabled>启动</button>
|
|
195
|
+
</div>
|
|
196
|
+
</section>
|
|
197
|
+
<section>
|
|
198
|
+
<h2>运行中的项目 <span class="sub">按工作区分组;只有运行中的才会出现</span></h2>
|
|
199
|
+
<div id="projects"><p class="empty">加载中…</p></div>
|
|
200
|
+
</section>
|
|
201
|
+
<section>
|
|
202
|
+
<h2>调试中的进程 <span class="sub">JDWP 会话由 daemon 持有</span></h2>
|
|
203
|
+
<div id="debugs"><p class="empty">加载中…</p></div>
|
|
204
|
+
</section>
|
|
205
|
+
<section>
|
|
206
|
+
<h2>生命周期 <span class="sub">只配置是否跟随 dsh;所有资源强制跟随 daemon(停止 daemon 时一并停止)</span></h2>
|
|
207
|
+
<div id="lifecycle-rows">
|
|
208
|
+
<label class="life"><b>守护进程</b><input type="checkbox" data-life="daemon.followDsh"><span class="muted">跟随 dsh:最后一个 dsh 窗口退出后,空闲 10 分钟自动退出;关掉则一直运行到手动停止</span></label>
|
|
209
|
+
<label class="life"><b>代理引擎</b><input type="checkbox" data-life="proxy.followDsh"><span class="muted">跟随 dsh:没有 dsh 租约时拆除隧道(含系统层拦截)</span></label>
|
|
210
|
+
<label class="life"><b>程序(JVM)</b><input type="checkbox" data-life="programs.followDsh"><span class="muted">跟随 dsh:最后一个窗口退出后停止程序</span></label>
|
|
211
|
+
<label class="life"><b>调试会话</b><input type="checkbox" data-life="debug.followDsh"><span class="muted">跟随 dsh:最后一个窗口退出后断开调试</span></label>
|
|
212
|
+
</div>
|
|
213
|
+
<div class="row" style="margin-top:10px">
|
|
214
|
+
<span class="grow"></span>
|
|
215
|
+
<span class="muted" id="lifecycle-note"></span>
|
|
216
|
+
<button id="lifecycle-save">保存生命周期</button>
|
|
217
|
+
</div>
|
|
218
|
+
</section>
|
|
219
|
+
</div>
|
|
220
|
+
|
|
221
|
+
<div class="view" id="view-proxy" hidden>
|
|
222
|
+
<section>
|
|
223
|
+
<h2>ProxyBridge 组件 <span class="sub">系统层拦截所需;按需下载,缓存复用,不随插件打包</span></h2>
|
|
224
|
+
<div class="row">
|
|
225
|
+
<span id="bridge-pill" class="pill off">未知</span>
|
|
226
|
+
<span class="muted grow" id="bridge-detail"></span>
|
|
227
|
+
<button id="bridge-download">下载组件</button>
|
|
228
|
+
<button id="bridge-upload">选择 tgz 上传</button>
|
|
229
|
+
<input id="bridge-file" type="file" accept=".tgz,.tar.gz,application/gzip" hidden>
|
|
230
|
+
</div>
|
|
231
|
+
<div class="muted" id="bridge-hint" style="margin-top:6px"></div>
|
|
232
|
+
</section>
|
|
233
|
+
<section>
|
|
234
|
+
<h2>规则表 <span class="sub">全局规则:按机器生效、不区分工作区;保存经 daemon 的 CAS,多窗口同时改不会互相覆盖</span></h2>
|
|
235
|
+
<div class="row" style="margin-bottom:10px">
|
|
236
|
+
<span class="pill off" id="proxy-engine-pill">未知</span>
|
|
237
|
+
<span class="grow"></span>
|
|
238
|
+
<button id="proxy-engine-toggle" disabled>启动</button>
|
|
239
|
+
<span class="muted" id="proxy-revision"></span>
|
|
240
|
+
</div>
|
|
241
|
+
<div class="row" style="margin-bottom:10px">
|
|
242
|
+
<label class="muted">SOCKS 主机</label><input id="proxy-socks-host" class="narrow" placeholder="host">
|
|
243
|
+
<label class="muted">端口</label><input id="proxy-socks-port" class="narrow" placeholder="1080">
|
|
244
|
+
<span id="proxy-ping" class="pill off" role="status" title="">检测中…</span>
|
|
245
|
+
<label class="muted">监听端口</label><input id="proxy-listen" class="narrow" placeholder="11081">
|
|
246
|
+
</div>
|
|
247
|
+
<div id="proxy-banner"></div>
|
|
248
|
+
<table>
|
|
249
|
+
<thead>
|
|
250
|
+
<tr><th style="width:56px">启用</th><th style="width:170px">程序 / IP</th><th>目标主机</th><th style="width:120px">端口</th><th style="width:88px">状态</th><th style="width:90px">来源</th><th></th></tr>
|
|
251
|
+
</thead>
|
|
252
|
+
<tbody id="proxy-rules"></tbody>
|
|
253
|
+
</table>
|
|
254
|
+
<div class="row" style="margin-top:10px">
|
|
255
|
+
<button id="proxy-add">+ 添加规则</button>
|
|
256
|
+
<span class="grow"></span>
|
|
257
|
+
<button id="bridge-restart" hidden>重启系统层拦截(可能提权)</button>
|
|
258
|
+
<button id="proxy-log-toggle">代理日志</button>
|
|
259
|
+
</div>
|
|
260
|
+
<pre id="proxy-log" hidden></pre>
|
|
261
|
+
</section>
|
|
262
|
+
</div>
|
|
263
|
+
|
|
264
|
+
<div class="view" id="view-ide" hidden>
|
|
265
|
+
<div class="ide-layout">
|
|
266
|
+
<aside class="ide-side" id="ide-side">
|
|
267
|
+
<ul id="ide-ws-list"></ul>
|
|
268
|
+
<p class="note">列表来自正在运行的 dsh</p>
|
|
269
|
+
</aside>
|
|
270
|
+
<div class="ide-main">
|
|
271
|
+
<div id="ide-stale" class="ide-stale" hidden><span id="ide-stale-text"></span><button id="ide-stale-close" type="button" title="关闭提示" aria-label="关闭提示">×</button></div>
|
|
272
|
+
<div id="ide-body" class="ide-body"><div id="ide-msg" class="ide-msg"><p class="empty">检查 dsh 状态…</p></div></div>
|
|
273
|
+
</div>
|
|
274
|
+
</div>
|
|
275
|
+
</div>
|
|
276
|
+
</main>
|
|
277
|
+
<div class="modal" id="confirm" hidden>
|
|
278
|
+
<div class="modal-card" role="dialog" aria-modal="true" aria-labelledby="confirm-title">
|
|
279
|
+
<h3 id="confirm-title">停止守护进程?</h3>
|
|
280
|
+
<div class="modal-body" id="confirm-body"></div>
|
|
281
|
+
<div class="modal-actions">
|
|
282
|
+
<button id="confirm-cancel">取消</button>
|
|
283
|
+
<button class="danger" id="confirm-ok">停止</button>
|
|
284
|
+
</div>
|
|
285
|
+
</div>
|
|
286
|
+
</div>
|
|
287
|
+
<script>
|
|
288
|
+
(function () {
|
|
289
|
+
var TOKEN = ${token};
|
|
290
|
+
var VERSION = ${version};
|
|
291
|
+
var HOME = ${home};
|
|
292
|
+
var API_VERSION = ${apiVersion};
|
|
293
|
+
// 本进程运行的页面代码指纹(见 risd/build.js):/v1/health 的 uiBuild.loaded
|
|
294
|
+
// 变了说明 daemon 重启并加载了新代码,刷新即可;只有 disk 变说明源码改了但
|
|
295
|
+
// daemon 还是旧模块,刷新不会有效果,只能提示重启。
|
|
296
|
+
var PAGE_BUILD = ${uiBuild};
|
|
297
|
+
// 渲染本页的 daemon 进程身份:daemon 重启后 token 会变,本页的所有请求都会 401。
|
|
298
|
+
// 与 /v1/health 的 instance 对比即可发现自己是个「旧页面」并提示刷新。
|
|
299
|
+
var PAGE_INSTANCE = ${uiInstance};
|
|
300
|
+
var initialCwd = '';
|
|
301
|
+
try { initialCwd = new URLSearchParams(location.search).get('cwd') || ''; } catch { /* no URL API */ }
|
|
302
|
+
var state = { tab: 'overview', cwd: initialCwd || localStorage.getItem('risd.cwd') || '', view: null, logOn: false, logTimer: null, pollTimer: null, pingTimer: null, pingSeq: 0, dsh: null, dshAvailable: null, lifecycleDirty: false, proxyDirty: false, proxyRunning: false, summary: { proxyRunning: false, programs: 0, debugs: 0 }, staleDismissed: {},
|
|
303
|
+
};
|
|
304
|
+
// The drift banner's current message and the message the user closed: a closed
|
|
305
|
+
// hint must not pop back every 5 s, but a NEW kind of drift still has to warn.
|
|
306
|
+
var driftMessage = '';
|
|
307
|
+
var driftDismissedFor = '';
|
|
308
|
+
// m11421: 源码比进程新(插件版本已更新)时光刷新没用——旧进程的模块图不会变,
|
|
309
|
+
// 必须先重启 daemon 再加载本页,所以横幅的按钮在 restart 模式下是「重启并刷新」。
|
|
310
|
+
var driftMode = 'reload';
|
|
311
|
+
// One name per page load, not per iframe document: the embedded IDE claims a Web
|
|
312
|
+
// Lock named after this value, and rebuilding the iframe (workspace switch, tab
|
|
313
|
+
// re-entry) must not race the previous document's lock and flash 「RIS-IDE 已在
|
|
314
|
+
// 另一个窗口打开」. The standalone IDE window keeps its workspace-keyed name, so
|
|
315
|
+
// the two paths never contend (review C-P2-6).
|
|
316
|
+
var IDE_WNAME = 'risd-ide-' + Math.random().toString(36).slice(2, 8);
|
|
317
|
+
|
|
318
|
+
function api(path, options) {
|
|
319
|
+
options = options || {};
|
|
320
|
+
var headers = { authorization: 'Bearer ' + TOKEN, 'x-risd-api-version': API_VERSION };
|
|
321
|
+
if (options.body !== undefined) headers['content-type'] = 'application/json';
|
|
322
|
+
return fetch(path, {
|
|
323
|
+
method: options.method || 'GET',
|
|
324
|
+
headers: headers,
|
|
325
|
+
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
326
|
+
}).then(function (res) {
|
|
327
|
+
return res.json().catch(function () { return {}; }).then(function (body) {
|
|
328
|
+
if (!res.ok || body.ok === false) {
|
|
329
|
+
// 401 = the daemon restarted and re-issued its token: this page can never
|
|
330
|
+
// recover on its own, so say so instead of failing silently on every call.
|
|
331
|
+
// Do not clobber a richer message from checkBuild (it may be counting
|
|
332
|
+
// unsaved editor buffers and showing 「保存所有文件」): the 401 timers
|
|
333
|
+
// fire every few seconds, the drift check every five.
|
|
334
|
+
if (res.status === 401 && driftMessage.indexOf('未保存') === -1 && driftMessage.indexOf('无法确认') === -1) {
|
|
335
|
+
showDrift('维护页令牌已失效(daemon 已重启):点右侧刷新');
|
|
336
|
+
}
|
|
337
|
+
var error = new Error(body.message || body.error || ('HTTP ' + res.status));
|
|
338
|
+
error.status = res.status;
|
|
339
|
+
error.body = body;
|
|
340
|
+
throw error;
|
|
341
|
+
}
|
|
342
|
+
return body;
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function el(id) { return document.getElementById(id); }
|
|
348
|
+
function esc(value) {
|
|
349
|
+
return String(value === undefined || value === null ? '' : value)
|
|
350
|
+
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
351
|
+
.replace(/"/g, '"').replace(/'/g, ''');
|
|
352
|
+
}
|
|
353
|
+
// The class is NOT free text: a rule's source field is written to proxy.json by
|
|
354
|
+
// whatever plugin/editor touched it, so an unescaped class could break out of
|
|
355
|
+
// the attribute and run script in a page that carries the daemon token
|
|
356
|
+
// (review C-P2-4).
|
|
357
|
+
function pill(text, cls) { return '<span class="pill ' + esc(cls || '') + '">' + esc(text) + '</span>'; }
|
|
358
|
+
function hhmmss(ms) {
|
|
359
|
+
var total = Math.max(0, Math.floor(ms / 1000));
|
|
360
|
+
var h = Math.floor(total / 3600), m = Math.floor((total % 3600) / 60), s = total % 60;
|
|
361
|
+
return (h > 0 ? h + 'h' : '') + m + 'm' + s + 's';
|
|
362
|
+
}
|
|
363
|
+
function banner(kind, text) {
|
|
364
|
+
// Same reasoning as pill(): the kind argument reaches the class attribute, so only the
|
|
365
|
+
// three shapes this page uses may pass (review C-P2-4).
|
|
366
|
+
var shape = kind === 'err' || kind === 'warn' || kind === 'ok' ? kind : 'warn';
|
|
367
|
+
return text ? '<div class="banner ' + shape + '">' + esc(text) + '</div>' : '';
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// ---- header ----
|
|
371
|
+
function loadDaemon() {
|
|
372
|
+
return api('/v1/health').then(function (body) {
|
|
373
|
+
var home = body.home || HOME;
|
|
374
|
+
el('daemon-pill').className = 'pill on';
|
|
375
|
+
el('daemon-pill').textContent = '运行中';
|
|
376
|
+
el('daemon-sub').textContent = 'pid ' + body.pid + ' · 已运行 ' + hhmmss(body.uptimeMs);
|
|
377
|
+
el('daemon-detail').textContent = 'v' + body.version + ' · API v' + body.apiVersion +
|
|
378
|
+
' · ' + (body.port ? '127.0.0.1:' + body.port : '') + ' · 租约 ' + body.leases.count +
|
|
379
|
+
' · ' + (body.engine && body.engine.owned === true ? '引擎由本 daemon 托管' : '引擎由插件托管');
|
|
380
|
+
}).catch(function (error) {
|
|
381
|
+
el('daemon-pill').className = 'pill warn';
|
|
382
|
+
el('daemon-pill').textContent = '不可达';
|
|
383
|
+
el('daemon-sub').textContent = '';
|
|
384
|
+
el('daemon-detail').textContent = error.message;
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// ---- overview ----
|
|
389
|
+
function proxySummary() {
|
|
390
|
+
return api('/v1/proxy?cwd=' + encodeURIComponent(state.cwd || HOME)).then(function (view) {
|
|
391
|
+
el('proxy-pill').className = 'pill ' + (view.running ? 'on' : 'off');
|
|
392
|
+
el('proxy-pill').textContent = view.running ? '运行中' : '未运行';
|
|
393
|
+
el('proxy-sub').textContent = view.running
|
|
394
|
+
? 'pid ' + view.pid + ' · 监听 ' + view.listenPort
|
|
395
|
+
: '监听端口 ' + view.listenPort + ' · 规则 ' + (view.rules || []).length + ' 条';
|
|
396
|
+
el('proxy-detail').textContent = 'revision ' + view.revision;
|
|
397
|
+
state.summary.proxyRunning = view.running === true;
|
|
398
|
+
if (engineBusy) {
|
|
399
|
+
paintEngineButtons();
|
|
400
|
+
} else {
|
|
401
|
+
var engineToggle = el('engine-toggle');
|
|
402
|
+
if (engineToggle) {
|
|
403
|
+
var running = state.summary.proxyRunning === true;
|
|
404
|
+
engineToggle.disabled = false;
|
|
405
|
+
engineToggle.removeAttribute('aria-busy');
|
|
406
|
+
engineToggle.textContent = running ? '停止' : '启动';
|
|
407
|
+
engineToggle.className = running ? 'danger' : '';
|
|
408
|
+
engineToggle.title = running ? '停止引擎、系统层拦截与 git 代理' : '按当前规则表启动代理引擎(系统层拦截可能需要提权)';
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
state.cwd = view.workspace || state.cwd;
|
|
412
|
+
if (state.cwd) localStorage.setItem('risd.cwd', state.cwd);
|
|
413
|
+
return view;
|
|
414
|
+
}).catch(function (error) {
|
|
415
|
+
el('proxy-pill').className = 'pill warn';
|
|
416
|
+
el('proxy-pill').textContent = '未知';
|
|
417
|
+
el('proxy-sub').textContent = error.message;
|
|
418
|
+
return null;
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function loadProjects() {
|
|
423
|
+
var box = el('projects');
|
|
424
|
+
return api('/v1/workspaces').then(function (body) {
|
|
425
|
+
var list = (body.workspaces || []).slice();
|
|
426
|
+
if (state.cwd && list.indexOf(state.cwd) === -1) list.unshift(state.cwd);
|
|
427
|
+
if (list.length === 0) { box.innerHTML = '<p class="empty">还没有记录到工作区:先在 dsh 面板里打开一次,或在「代理」页签里填路径。</p>'; return; }
|
|
428
|
+
return Promise.all(list.map(function (cwd) {
|
|
429
|
+
return api('/v1/programs?cwd=' + encodeURIComponent(cwd)).then(function (listed) {
|
|
430
|
+
return { cwd: cwd, programs: (listed.programs || []).filter(function (row) { return row.running && row.running.alive !== false; }) };
|
|
431
|
+
}).catch(function (error) { return { cwd: cwd, error: error.message }; });
|
|
432
|
+
})).then(function (groups) {
|
|
433
|
+
var html = '';
|
|
434
|
+
groups.forEach(function (group) {
|
|
435
|
+
if (group.error) { html += '<div class="group-title">' + esc(group.cwd) + '</div><p class="empty">' + esc(group.error) + '</p>'; return; }
|
|
436
|
+
if (group.programs.length === 0) return;
|
|
437
|
+
html += '<div class="group-title">' + esc(group.cwd) + '</div><ul class="programs">';
|
|
438
|
+
group.programs.forEach(function (row) {
|
|
439
|
+
html += '<li><span style="min-width:220px">' + esc(row.name || row.id) + '</span>' + pill(row.kind || '') +
|
|
440
|
+
'<span class="muted">pid ' + esc(row.running && row.running.pid || '') + '</span><span class="grow"></span>' +
|
|
441
|
+
'<button data-stop-program="' + esc(row.id) + '" data-cwd="' + esc(group.cwd) + '">停止</button></li>';
|
|
442
|
+
});
|
|
443
|
+
html += '</ul>';
|
|
444
|
+
});
|
|
445
|
+
box.innerHTML = html || '<p class="empty">没有正在运行的项目。</p>';
|
|
446
|
+
state.summary.programs = groups.reduce(function (total, group) { return total + (group.programs ? group.programs.length : 0); }, 0);
|
|
447
|
+
});
|
|
448
|
+
}).catch(function (error) { box.innerHTML = '<p class="empty">' + esc(error.message) + '</p>'; });
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function loadDebugs() {
|
|
452
|
+
var box = el('debugs');
|
|
453
|
+
return api('/v1/debug', { method: 'POST', body: { action: 'sessions' } }).then(function (body) {
|
|
454
|
+
var rows = body.sessions || [];
|
|
455
|
+
var stale = body.stale || [];
|
|
456
|
+
state.summary.debugs = rows.length;
|
|
457
|
+
if (rows.length === 0 && stale.length === 0) { box.innerHTML = '<p class="empty">没有调试中的进程。</p>'; return; }
|
|
458
|
+
var html = '';
|
|
459
|
+
if (stale.length > 0) {
|
|
460
|
+
// Review D3: a session kept only on disk (no JVM, no attach) used to be
|
|
461
|
+
// invisible here, so its breakpoint set had no page to clear it from.
|
|
462
|
+
html += '<ul class="programs">';
|
|
463
|
+
stale.forEach(function (row) {
|
|
464
|
+
html += '<li><span style="min-width:220px">' + esc(row.logPath) + '</span>' +
|
|
465
|
+
'<span class="muted">已断开 · 断点 ' + esc((row.breakpoints || []).length) + ' 个</span><span class="grow"></span>' +
|
|
466
|
+
'<button data-forget-debug="' + esc(row.logPath) + '">清除记录</button></li>';
|
|
467
|
+
});
|
|
468
|
+
html += '</ul>';
|
|
469
|
+
}
|
|
470
|
+
if (rows.length > 0) html += '<ul class="programs">';
|
|
471
|
+
rows.forEach(function (row) {
|
|
472
|
+
var target = row.name || row.id || row.logPath;
|
|
473
|
+
html += '<li><span style="min-width:220px">' + esc(target) + '</span>' +
|
|
474
|
+
(row.port ? '<span class="muted">JDWP ' + esc(row.port) + '</span>' : '') +
|
|
475
|
+
'<span class="muted">' + esc(row.cwd || '') + '</span><span class="grow"></span>' +
|
|
476
|
+
(row.cwd && row.id ? '<button class="danger" data-stop-debug="' + esc(row.id) + '" data-cwd="' + esc(row.cwd) + '">停止</button>' : '') +
|
|
477
|
+
'<button data-detach="' + esc(row.logPath) + '">断开</button></li>';
|
|
478
|
+
});
|
|
479
|
+
if (rows.length > 0) html += '</ul>';
|
|
480
|
+
box.innerHTML = html;
|
|
481
|
+
}).catch(function (error) { box.innerHTML = '<p class="empty">' + esc(error.message) + '</p>'; });
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/** Fill the lifecycle checkboxes from the daemon's config (GET returns it). */
|
|
485
|
+
function loadLifecycle() {
|
|
486
|
+
// The 4 s overview refresh runs while the form is open; it must not overwrite a
|
|
487
|
+
// checkbox the user just toggled, or the form looks broken ("勾了又弹回去").
|
|
488
|
+
// Saving clears the flag (review C-P2-3).
|
|
489
|
+
if (state.lifecycleDirty === true) return Promise.resolve(state.lifecycle || null);
|
|
490
|
+
return api('/v1/config').then(function (body) {
|
|
491
|
+
var policy = body.lifecycle || {};
|
|
492
|
+
document.querySelectorAll('#lifecycle-rows input[data-life]').forEach(function (box) {
|
|
493
|
+
var parts = box.getAttribute('data-life').split('.');
|
|
494
|
+
var resource = policy[parts[0]] || {};
|
|
495
|
+
box.checked = resource[parts[1]] === true;
|
|
496
|
+
});
|
|
497
|
+
state.lifecycle = policy;
|
|
498
|
+
el('lifecycle-note').textContent = '';
|
|
499
|
+
return policy;
|
|
500
|
+
}).catch(function (error) { el('lifecycle-note').textContent = error.message; });
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function lifecycleFromBoxes() {
|
|
504
|
+
var policy = { daemon: {}, proxy: {}, programs: {}, debug: {} };
|
|
505
|
+
document.querySelectorAll('#lifecycle-rows input[data-life]').forEach(function (box) {
|
|
506
|
+
var parts = box.getAttribute('data-life').split('.');
|
|
507
|
+
if (policy[parts[0]] === undefined) policy[parts[0]] = {};
|
|
508
|
+
policy[parts[0]][parts[1]] = box.checked;
|
|
509
|
+
});
|
|
510
|
+
return policy;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function saveLifecycle() {
|
|
514
|
+
var policy = lifecycleFromBoxes();
|
|
515
|
+
return api('/v1/config', { method: 'POST', body: { lifecycle: policy } }).then(function (body) {
|
|
516
|
+
state.lifecycle = body.lifecycle || policy;
|
|
517
|
+
state.lifecycleDirty = false;
|
|
518
|
+
el('lifecycle-note').textContent = '已保存';
|
|
519
|
+
setTimeout(function () { if (el('lifecycle-note')) el('lifecycle-note').textContent = ''; }, 2500);
|
|
520
|
+
return loadLifecycle();
|
|
521
|
+
}).catch(function (error) { el('lifecycle-note').textContent = '保存失败:' + error.message; });
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function refreshOverview() {
|
|
525
|
+
loadDaemon();
|
|
526
|
+
return Promise.all([proxySummary(), loadProjects(), loadDebugs(), loadLifecycle()]);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// ---- proxy tab ----
|
|
530
|
+
/** 1521 / 1521,1000-1010 / *;其它写法是引擎用不了的笔误。 */
|
|
531
|
+
function portSpecOk(value) {
|
|
532
|
+
var spec = String(value === undefined || value === null ? '' : value).trim();
|
|
533
|
+
if (spec === '') return false;
|
|
534
|
+
if (spec === '*') return true;
|
|
535
|
+
return spec.split(',').every(function (part) {
|
|
536
|
+
part = part.trim();
|
|
537
|
+
if (/^[0-9]+$/.test(part)) {
|
|
538
|
+
var single = Number(part);
|
|
539
|
+
return single >= 1 && single <= 65535;
|
|
540
|
+
}
|
|
541
|
+
var range = /^([0-9]+)-([0-9]+)$/.exec(part);
|
|
542
|
+
if (range === null) return false;
|
|
543
|
+
var low = Number(range[1]);
|
|
544
|
+
var high = Number(range[2]);
|
|
545
|
+
return low >= 1 && high <= 65535 && low <= high;
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* What the engine will do with this row right now — the answer to 「这行到底生效了
|
|
550
|
+
* 没有」 without reading the log. Order matters: an incomplete row is never
|
|
551
|
+
* 「生效」, and a complete row waits for the engine.
|
|
552
|
+
*/
|
|
553
|
+
function ruleStatus(rule) {
|
|
554
|
+
if (rule.enabled === false) return { cls: 'off', text: '未启用' };
|
|
555
|
+
var missing = [];
|
|
556
|
+
if (!String(rule.program || '').trim()) missing.push('程序');
|
|
557
|
+
if (!String(rule.hosts || '').trim()) missing.push('IP');
|
|
558
|
+
if (!String(rule.ports || '').trim()) missing.push('端口');
|
|
559
|
+
if (missing.length > 0) return { cls: 'err', text: '缺' + missing.join('/') };
|
|
560
|
+
if (!portSpecOk(rule.ports)) return { cls: 'err', text: '端口格式' };
|
|
561
|
+
return state.proxyRunning === true
|
|
562
|
+
? { cls: 'on', text: '生效' }
|
|
563
|
+
: { cls: 'off', text: '待启动' };
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* A row's origin at a glance. The side matters on WSL: java.exe and java are two
|
|
567
|
+
* rows because each side's ProxyBridge matches a different image name.
|
|
568
|
+
*/
|
|
569
|
+
function sourceText(rule) {
|
|
570
|
+
var source = String(rule.source || 'user');
|
|
571
|
+
if (rule.side === 'local') return source + '·wsl';
|
|
572
|
+
if (rule.side === 'windows') return source + '·win';
|
|
573
|
+
return source;
|
|
574
|
+
}
|
|
575
|
+
function ruleRow(rule, index) {
|
|
576
|
+
var status = ruleStatus(rule);
|
|
577
|
+
return '<tr data-index="' + index + '">' +
|
|
578
|
+
'<td><input type="checkbox" data-field="enabled"' + (rule.enabled === false ? '' : ' checked') + '></td>' +
|
|
579
|
+
'<td><input type="text" data-field="program" value="' + esc(rule.program) + '"></td>' +
|
|
580
|
+
'<td><input type="text" data-field="hosts" value="' + esc(rule.hosts) + '"></td>' +
|
|
581
|
+
'<td><input type="text" data-field="ports" value="' + esc(rule.ports) + '"></td>' +
|
|
582
|
+
'<td>' + pill(status.text, status.cls) + '</td>' +
|
|
583
|
+
'<td>' + pill(sourceText(rule)) + (rule.shadow ? ' ' + pill('shadow', 'warn') : '') + (rule.direct ? ' ' + pill('direct', 'warn') : '') + '</td>' +
|
|
584
|
+
'<td class="actions"><button data-remove="' + index + '">删除</button></td>' +
|
|
585
|
+
'</tr>';
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* The restart button is noise when nothing is stale, and the whole point when
|
|
590
|
+
* something is: show it only for a failed/incomplete reload and put the reason in
|
|
591
|
+
* the tooltip (a click costs a UAC prompt on Windows and sudo on this side).
|
|
592
|
+
*/
|
|
593
|
+
function setBridgeStale(reason) {
|
|
594
|
+
var stale = /未重载|旧规则|需要提权|未能重载|未重启|未能停止/.test(String(reason || ''));
|
|
595
|
+
var button = el('bridge-restart');
|
|
596
|
+
button.hidden = !stale;
|
|
597
|
+
button.title = stale
|
|
598
|
+
? '为什么要重启:' + reason + '(点一下会停止系统层拦截并按当前规则表重新启动;Windows 会弹一次 UAC,本侧需要 sudo)'
|
|
599
|
+
: '';
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
function renderProxyTab(view) {
|
|
603
|
+
state.view = view;
|
|
604
|
+
state.proxyRunning = view.running === true;
|
|
605
|
+
setBridgeStale(state.lastReloadNote || '');
|
|
606
|
+
el('proxy-socks-host').value = view.socks ? view.socks.host : '';
|
|
607
|
+
el('proxy-socks-port').value = view.socks ? view.socks.port : '';
|
|
608
|
+
el('proxy-listen').value = view.listenPort;
|
|
609
|
+
el('proxy-revision').textContent = 'revision ' + view.revision;
|
|
610
|
+
var enginePill = el('proxy-engine-pill');
|
|
611
|
+
if (enginePill) {
|
|
612
|
+
enginePill.className = 'pill ' + (state.proxyRunning ? 'on' : 'off');
|
|
613
|
+
enginePill.textContent = state.proxyRunning ? '代理运行中' : '代理未运行';
|
|
614
|
+
enginePill.title = state.proxyRunning ? '引擎在跑:合规的行已经生效' : '保存后如果规则完整会自动启动';
|
|
615
|
+
}
|
|
616
|
+
if (engineBusy) {
|
|
617
|
+
paintEngineButtons();
|
|
618
|
+
} else {
|
|
619
|
+
var engineToggle = el('proxy-engine-toggle');
|
|
620
|
+
if (engineToggle) {
|
|
621
|
+
engineToggle.disabled = false;
|
|
622
|
+
engineToggle.removeAttribute('aria-busy');
|
|
623
|
+
engineToggle.textContent = state.proxyRunning ? '停止' : '启动';
|
|
624
|
+
engineToggle.className = state.proxyRunning ? 'danger' : '';
|
|
625
|
+
engineToggle.title = state.proxyRunning
|
|
626
|
+
? '停止引擎、系统层拦截与 git 代理'
|
|
627
|
+
: '按当前规则表启动(保存后规则完整会自动启动;系统层拦截可能需要提权)';
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
var rules = view.rules || [];
|
|
631
|
+
el('proxy-rules').innerHTML = rules.length === 0
|
|
632
|
+
? '<tr><td colspan="7" class="empty">没有规则。系统代理至少需要一条启用且带程序/IP/端口的规则。</td></tr>'
|
|
633
|
+
: rules.map(ruleRow).join('');
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
function rulesFromTable() {
|
|
637
|
+
var rows = [];
|
|
638
|
+
el('proxy-rules').querySelectorAll('tr[data-index]').forEach(function (tr) {
|
|
639
|
+
var base = (state.view && state.view.rules || [])[Number(tr.getAttribute('data-index'))] || {};
|
|
640
|
+
var rule = Object.assign({}, base);
|
|
641
|
+
if (!rule.id) rule.id = 'user-' + Math.random().toString(36).slice(2, 9);
|
|
642
|
+
rule.program = tr.querySelector('[data-field="program"]').value.trim();
|
|
643
|
+
rule.hosts = tr.querySelector('[data-field="hosts"]').value.trim();
|
|
644
|
+
rule.ports = tr.querySelector('[data-field="ports"]').value.trim();
|
|
645
|
+
rule.enabled = tr.querySelector('[data-field="enabled"]').checked;
|
|
646
|
+
if (!rule.source) rule.source = 'user';
|
|
647
|
+
rows.push(rule);
|
|
648
|
+
});
|
|
649
|
+
return rows;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/** Everything the engine needs before a save is worth writing (and before auto-start). */
|
|
653
|
+
function proxyFormIssues() {
|
|
654
|
+
var issues = [];
|
|
655
|
+
var host = el('proxy-socks-host').value.trim();
|
|
656
|
+
var socksPort = Number(el('proxy-socks-port').value);
|
|
657
|
+
if (host === '') issues.push('代理服务器地址为空');
|
|
658
|
+
else if (/\s/.test(host)) issues.push('代理服务器地址含空格');
|
|
659
|
+
if (!(socksPort >= 1 && socksPort <= 65535)) issues.push('代理服务器端口需为 1-65535');
|
|
660
|
+
var listenPort = Number(el('proxy-listen').value);
|
|
661
|
+
if (!(listenPort >= 1 && listenPort <= 65535)) issues.push('监听端口需为 1-65535');
|
|
662
|
+
rulesFromTable().forEach(function (rule, index) {
|
|
663
|
+
if (rule.enabled === false) return;
|
|
664
|
+
var missing = [];
|
|
665
|
+
if (!rule.program) missing.push('程序');
|
|
666
|
+
if (!rule.hosts) missing.push('IP');
|
|
667
|
+
if (!rule.ports) missing.push('端口');
|
|
668
|
+
if (missing.length > 0) issues.push('第 ' + (index + 1) + ' 行缺' + missing.join('/'));
|
|
669
|
+
else if (!portSpecOk(rule.ports)) issues.push('第 ' + (index + 1) + ' 行端口格式不对');
|
|
670
|
+
});
|
|
671
|
+
return issues;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
/** A complete, enabled row is what makes starting the engine worth doing. */
|
|
675
|
+
function hasActionableRule(view) {
|
|
676
|
+
return (view && view.rules || []).some(function (rule) {
|
|
677
|
+
return rule.enabled !== false && String(rule.program || '').trim() !== '' &&
|
|
678
|
+
String(rule.hosts || '').trim() !== '' && portSpecOk(rule.ports);
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* 「勾选的配置不为空、有效就自动保存并启动」:editing is the save. A partially
|
|
684
|
+
* filled row is not an error the user must dismiss, so the banner explains what is
|
|
685
|
+
* still missing and the save happens once it is complete.
|
|
686
|
+
*/
|
|
687
|
+
var autoSaveTimer = null;
|
|
688
|
+
// Rapid edits can outrun a POST: the second write would carry the revision of the
|
|
689
|
+
// first (409 against ourselves). One save in flight, then re-run if the form moved.
|
|
690
|
+
var saveInFlight = null;
|
|
691
|
+
var savePending = false;
|
|
692
|
+
function scheduleProxySave() {
|
|
693
|
+
state.proxyDirty = true;
|
|
694
|
+
state.editVersion = (state.editVersion || 0) + 1;
|
|
695
|
+
if (autoSaveTimer !== null) clearTimeout(autoSaveTimer);
|
|
696
|
+
autoSaveTimer = setTimeout(function () {
|
|
697
|
+
autoSaveTimer = null;
|
|
698
|
+
autoSaveProxy();
|
|
699
|
+
}, 700);
|
|
700
|
+
}
|
|
701
|
+
function autoSaveProxy() {
|
|
702
|
+
if (state.tab !== 'proxy') return;
|
|
703
|
+
var issues = proxyFormIssues();
|
|
704
|
+
if (issues.length > 0) {
|
|
705
|
+
el('proxy-banner').innerHTML = banner('warn', '还没保存:' + issues.join(';'));
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
saveRules({ auto: true });
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* After a successful save, bring the tunnel up — that is the 「并启动」 half. A
|
|
713
|
+
* bridge that needs elevation is reported as a note (the restart button appears
|
|
714
|
+
* from setBridgeStale), never as a silent failure.
|
|
715
|
+
*/
|
|
716
|
+
function maybeStartProxy(view) {
|
|
717
|
+
if (!view || view.running === true || hasActionableRule(view) === false) return Promise.resolve(null);
|
|
718
|
+
var version = state.editVersion || 0;
|
|
719
|
+
el('proxy-banner').innerHTML = banner('ok', '规则已保存,正在启动代理…');
|
|
720
|
+
return api('/v1/action', { method: 'POST', body: { action: 'start', cwd: state.cwd } }).then(function (response) {
|
|
721
|
+
var result = (response && response.result) || {};
|
|
722
|
+
var redirector = result.redirector || {};
|
|
723
|
+
var skipped = Array.isArray(redirector.skipped) ? redirector.skipped : [];
|
|
724
|
+
var note = skipped.length > 0
|
|
725
|
+
? '系统层拦截需要提权:' + skipped.map(function (item) { return item.side + ':' + item.reason; }).join(';')
|
|
726
|
+
: (typeof result.reloadNote === 'string' ? result.reloadNote : '');
|
|
727
|
+
state.lastReloadNote = note;
|
|
728
|
+
if ((state.editVersion || 0) !== version) return null;
|
|
729
|
+
return loadProxyTab().then(function () {
|
|
730
|
+
el('proxy-banner').innerHTML = note
|
|
731
|
+
? banner('warn', '规则已保存;' + note + '(可点「重启系统层拦截」重试)')
|
|
732
|
+
: banner('ok', '规则已保存并已启动代理');
|
|
733
|
+
});
|
|
734
|
+
}).catch(function (error) {
|
|
735
|
+
el('proxy-banner').innerHTML = banner('warn', '规则已保存,但代理启动失败:' + error.message);
|
|
736
|
+
return null;
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
function loadProxyTab() {
|
|
741
|
+
// The rule table is machine-wide: no workspace picker. The workspace only tags
|
|
742
|
+
// rows for bookkeeping, so the daemon falls back to its own home when omitted.
|
|
743
|
+
var cwd = state.cwd || '';
|
|
744
|
+
return api('/v1/proxy' + (cwd ? '?cwd=' + encodeURIComponent(cwd) : '')).then(function (view) {
|
|
745
|
+
state.cwd = view.workspace || cwd;
|
|
746
|
+
localStorage.setItem('risd.cwd', state.cwd);
|
|
747
|
+
renderProxyTab(view);
|
|
748
|
+
// A view that just came from the daemon is the saved state by definition.
|
|
749
|
+
state.proxyDirty = false;
|
|
750
|
+
el('proxy-banner').innerHTML = '';
|
|
751
|
+
refreshPing();
|
|
752
|
+
loadBundle();
|
|
753
|
+
// A save switches enforcement in the background; a failure from an earlier
|
|
754
|
+
// save is reported here rather than blocking that save (m10585).
|
|
755
|
+
if (view.bridge && view.bridge.ok === false && view.bridge.note) setBridgeStale(view.bridge.note);
|
|
756
|
+
}).catch(function (error) { el('proxy-banner').innerHTML = banner('err', error.message); });
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/**
|
|
760
|
+
* Refresh the base revision without repainting: a 409 usually means our own earlier
|
|
761
|
+
* save bumped the table while the user kept typing, and repainting here would
|
|
762
|
+
* replace that newer text with the version we failed to write.
|
|
763
|
+
*/
|
|
764
|
+
function refreshProxyView() {
|
|
765
|
+
var cwd = state.cwd || '';
|
|
766
|
+
return api('/v1/proxy' + (cwd ? '?cwd=' + encodeURIComponent(cwd) : '')).then(function (view) {
|
|
767
|
+
state.cwd = view.workspace || cwd;
|
|
768
|
+
state.view = view;
|
|
769
|
+
return view;
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
function saveRules(options) {
|
|
774
|
+
var auto = !!(options && options.auto);
|
|
775
|
+
if (saveInFlight !== null) { savePending = true; return saveInFlight; }
|
|
776
|
+
var version = state.editVersion || 0;
|
|
777
|
+
var cwd = state.cwd || '';
|
|
778
|
+
var body = {
|
|
779
|
+
cwd: cwd || undefined,
|
|
780
|
+
revision: state.view ? state.view.revision : '',
|
|
781
|
+
config: {
|
|
782
|
+
socks: { host: el('proxy-socks-host').value.trim(), port: Number(el('proxy-socks-port').value) || 0 },
|
|
783
|
+
listenPort: Number(el('proxy-listen').value) || 0,
|
|
784
|
+
rules: rulesFromTable(),
|
|
785
|
+
},
|
|
786
|
+
};
|
|
787
|
+
var run = api('/v1/proxy', { method: 'POST', body: body }).then(function (result) {
|
|
788
|
+
state.lastReloadNote = result.reloadNote || '';
|
|
789
|
+
// Typing during the POST is a fresh edit, not a save of the old text: repainting
|
|
790
|
+
// now would throw the new text away (it also overwrote the input the user was
|
|
791
|
+
// still typing in). Write the newer form instead.
|
|
792
|
+
if ((state.editVersion || 0) !== version) {
|
|
793
|
+
savePending = true;
|
|
794
|
+
return null;
|
|
795
|
+
}
|
|
796
|
+
state.proxyDirty = false;
|
|
797
|
+
// Reload first, then show the outcome: loadProxyTab clears the banner itself.
|
|
798
|
+
return loadProxyTab().then(function () {
|
|
799
|
+
el('proxy-banner').innerHTML = banner(result.reloadNote ? 'warn' : 'ok',
|
|
800
|
+
(auto ? '已自动保存' : '已保存') + '(revision ' + result.revision + ')' +
|
|
801
|
+
(result.reloadNote ? ' · ' + result.reloadNote : ''));
|
|
802
|
+
return maybeStartProxy(state.view);
|
|
803
|
+
});
|
|
804
|
+
});
|
|
805
|
+
saveInFlight = run.then(function (value) {
|
|
806
|
+
saveInFlight = null;
|
|
807
|
+
if (savePending === true) { savePending = false; scheduleProxySave(); }
|
|
808
|
+
return value;
|
|
809
|
+
}, function (error) {
|
|
810
|
+
saveInFlight = null;
|
|
811
|
+
var retryPending = savePending === true;
|
|
812
|
+
savePending = false;
|
|
813
|
+
if (error.status === 409) {
|
|
814
|
+
// Our own in-flight save bumped the revision: our edits are newer than the
|
|
815
|
+
// table we wrote, so reload and write again once (a real foreign edit also
|
|
816
|
+
// lands here, and the retry then reports the fresh table instead of a loop).
|
|
817
|
+
// Rebasing on the fresh revision (no repaint) keeps the text the user is
|
|
818
|
+
// still typing and writes exactly one more time.
|
|
819
|
+
if (options && options.retried !== true) {
|
|
820
|
+
return refreshProxyView().then(function () { return saveRules({ auto: auto, retried: true }); });
|
|
821
|
+
}
|
|
822
|
+
// Reload first, then show the conflict: loadProxyTab() rewrites the banner,
|
|
823
|
+
// so the old order made the 409 message visible for ~50 ms (review C-P1-4).
|
|
824
|
+
setTimeout(function () {
|
|
825
|
+
loadProxyTab().then(function () { el('proxy-banner').innerHTML = banner('err', error.message) })
|
|
826
|
+
}, 50)
|
|
827
|
+
return
|
|
828
|
+
}
|
|
829
|
+
if (retryPending === true) scheduleProxySave(); else el('proxy-banner').innerHTML = banner('err', error.message)
|
|
830
|
+
});
|
|
831
|
+
return saveInFlight;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
function logToggle() {
|
|
835
|
+
state.logOn = !state.logOn;
|
|
836
|
+
el('proxy-log').hidden = !state.logOn;
|
|
837
|
+
el('proxy-log-toggle').textContent = state.logOn ? '停止日志' : '代理日志';
|
|
838
|
+
syncLogTimer();
|
|
839
|
+
}
|
|
840
|
+
// The tail costs up to 40 KB every 2 s: poll only while the pane is actually on
|
|
841
|
+
// screen (proxy tab, page visible) and pause on a hidden tab instead of keeping
|
|
842
|
+
// the timer alive off-screen (review C-P2-3).
|
|
843
|
+
function syncLogTimer() {
|
|
844
|
+
var wanted = state.logOn === true && state.tab === 'proxy' && document.hidden !== true;
|
|
845
|
+
if (wanted && state.logTimer === null) {
|
|
846
|
+
pollLog();
|
|
847
|
+
state.logTimer = setInterval(pollLog, 2000);
|
|
848
|
+
} else if (!wanted && state.logTimer !== null) {
|
|
849
|
+
clearInterval(state.logTimer);
|
|
850
|
+
state.logTimer = null;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
function pollLog() {
|
|
854
|
+
api('/v1/proxy/log?bytes=40960').then(function (body) {
|
|
855
|
+
var pre = el('proxy-log');
|
|
856
|
+
pre.textContent = body.text || '(空)';
|
|
857
|
+
pre.scrollTop = pre.scrollHeight;
|
|
858
|
+
}).catch(function (error) { el('proxy-log').textContent = error.message; });
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// ---- ProxyBridge bundle (m09120 ③) ----
|
|
862
|
+
// The binaries are no longer shipped in the package: they are downloaded once into
|
|
863
|
+
// <proxy home>/proxybridge/bundle and reused. When the mirror is unreachable the
|
|
864
|
+
// card names the URL and accepts the same .tgz by hand.
|
|
865
|
+
var bundlePoll = null;
|
|
866
|
+
// Remembered across renders: a failed download repeats only its error, so without
|
|
867
|
+
// these the card would lose the manual URLs that make it useful in the first place
|
|
868
|
+
// (review C3-P2-12), and the reinstall button would not know it is a reinstall.
|
|
869
|
+
var lastBundleUrls = [];
|
|
870
|
+
var lastBundleInstalled = false;
|
|
871
|
+
function renderBundle(status) {
|
|
872
|
+
var pill = el('bridge-pill');
|
|
873
|
+
var detail = el('bridge-detail');
|
|
874
|
+
var hint = el('bridge-hint');
|
|
875
|
+
var button = el('bridge-download');
|
|
876
|
+
var upload = el('bridge-upload');
|
|
877
|
+
if (!pill) return;
|
|
878
|
+
var installed = status.installed === true;
|
|
879
|
+
var cls = 'pill';
|
|
880
|
+
var text = '未知';
|
|
881
|
+
if (status.downloading === true) { cls += ' warn'; text = '下载中…'; }
|
|
882
|
+
else if (installed) { cls += ' on'; text = '已安装'; }
|
|
883
|
+
else if (status.result && status.result.ok === false) { cls += ' err'; text = '下载失败'; }
|
|
884
|
+
else { cls += ' off'; text = '未安装'; }
|
|
885
|
+
pill.className = cls;
|
|
886
|
+
pill.textContent = text;
|
|
887
|
+
lastBundleInstalled = installed;
|
|
888
|
+
// The install directory is an implementation detail; the card answers 「装没装、
|
|
889
|
+
// 几个文件」 and keeps the paths in the hint only when something is missing.
|
|
890
|
+
detail.textContent = installed
|
|
891
|
+
? '已安装 · 文件 ' + (status.files || []).length + ' 个'
|
|
892
|
+
: (status.downloading === true ? '正在下载并校验…' : '系统层拦截在首次使用时需要这组文件');
|
|
893
|
+
// Installed is the steady state: the card then only reports 「装了几个文件」.
|
|
894
|
+
// Download/upload are recovery paths for a missing bundle, so they disappear
|
|
895
|
+
// once it is installed (m10585).
|
|
896
|
+
button.hidden = installed;
|
|
897
|
+
upload.hidden = installed;
|
|
898
|
+
button.disabled = status.downloading === true;
|
|
899
|
+
button.textContent = '下载组件';
|
|
900
|
+
var urls = Array.isArray(status.urls) === true && status.urls.length > 0
|
|
901
|
+
? status.urls
|
|
902
|
+
: [status.url].filter(function (item) { return typeof item === 'string' && item !== ''; });
|
|
903
|
+
if (urls.length > 0) lastBundleUrls = urls.slice();
|
|
904
|
+
else urls = lastBundleUrls.slice();
|
|
905
|
+
if (installed) { hint.textContent = ''; hint.style.whiteSpace = ''; return; }
|
|
906
|
+
var lines = ['下载地址(优先 Gitee,失败自动换 GitHub):'];
|
|
907
|
+
urls.forEach(function (item) { lines.push(' ' + item); });
|
|
908
|
+
if (status.result && status.result.ok === false && status.result.error) lines.push('失败原因:' + status.result.error);
|
|
909
|
+
if (urls.length > 0) lines.push('也可在浏览器打开上面的地址下载 tgz,再点「选择 tgz 上传」。');
|
|
910
|
+
hint.textContent = lines.join('\\n');
|
|
911
|
+
hint.style.whiteSpace = 'pre-line';
|
|
912
|
+
}
|
|
913
|
+
function loadBundle() {
|
|
914
|
+
return api('/v1/proxybridge').then(function (status) {
|
|
915
|
+
renderBundle(status);
|
|
916
|
+
if (status.downloading === true) startBundlePoll();
|
|
917
|
+
return status;
|
|
918
|
+
}).catch(function (error) { el('bridge-hint').textContent = error.message; });
|
|
919
|
+
}
|
|
920
|
+
function startBundlePoll() {
|
|
921
|
+
if (bundlePoll !== null) return;
|
|
922
|
+
bundlePoll = setInterval(function () {
|
|
923
|
+
api('/v1/proxybridge').then(function (status) {
|
|
924
|
+
renderBundle(status);
|
|
925
|
+
if (status.downloading !== true) { clearInterval(bundlePoll); bundlePoll = null; }
|
|
926
|
+
}).catch(function () { clearInterval(bundlePoll); bundlePoll = null; });
|
|
927
|
+
}, 1000);
|
|
928
|
+
}
|
|
929
|
+
function downloadBundle() {
|
|
930
|
+
// Reinstalling an existing bundle must not be answered with fromCache without
|
|
931
|
+
// touching the files: ask the daemon to force the download (the endpoint hands
|
|
932
|
+
// 'force' to ensureBundle).
|
|
933
|
+
var force = lastBundleInstalled === true;
|
|
934
|
+
renderBundle({ downloading: true });
|
|
935
|
+
// Poll from the start: if the POST itself stalls, the card still follows the
|
|
936
|
+
// daemon-side download instead of sitting on 「下载中…」 forever.
|
|
937
|
+
startBundlePoll();
|
|
938
|
+
return api('/v1/proxybridge/download', { method: 'POST', body: force === true ? { force: true } : {} }).then(function (status) {
|
|
939
|
+
renderBundle(status);
|
|
940
|
+
startBundlePoll();
|
|
941
|
+
// Finished before the first poll (cache hit or a fast mirror).
|
|
942
|
+
return loadBundle();
|
|
943
|
+
}).catch(function (error) { renderBundle({ downloading: false, result: { ok: false, error: error.message } }); });
|
|
944
|
+
}
|
|
945
|
+
function uploadBundle(file) {
|
|
946
|
+
if (!file) return;
|
|
947
|
+
el('bridge-hint').textContent = '正在上传 ' + file.name + '…';
|
|
948
|
+
return file.arrayBuffer().then(function (bytes) {
|
|
949
|
+
return fetch('/v1/proxybridge/upload', {
|
|
950
|
+
method: 'POST',
|
|
951
|
+
headers: { authorization: 'Bearer ' + TOKEN, 'x-risd-api-version': API_VERSION, 'content-type': 'application/gzip' },
|
|
952
|
+
body: bytes,
|
|
953
|
+
}).then(function (res) {
|
|
954
|
+
return res.json().catch(function () { return {}; }).then(function (body) {
|
|
955
|
+
if (!res.ok || body.ok === false) throw new Error(body.message || body.error || ('HTTP ' + res.status));
|
|
956
|
+
return loadBundle();
|
|
957
|
+
});
|
|
958
|
+
});
|
|
959
|
+
}).catch(function (error) { el('bridge-hint').textContent = '上传失败:' + error.message; });
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// ---- SOCKS reachability chip ----
|
|
963
|
+
// A wrong SOCKS host/port used to look identical to a healthy one until the first
|
|
964
|
+
// request failed. The chip probes the upstream every 10 s while the proxy tab is on
|
|
965
|
+
// screen: green = SOCKS5 greeting answered, amber = the port answered but is not a
|
|
966
|
+
// usable SOCKS5, red = unreachable (the tooltip says why).
|
|
967
|
+
function renderPing(result) {
|
|
968
|
+
var chip = el('proxy-ping');
|
|
969
|
+
if (!chip) return;
|
|
970
|
+
var cls = 'pill';
|
|
971
|
+
var text = '';
|
|
972
|
+
if (result === null || result === undefined) { cls += ' busy'; text = '检测中…'; }
|
|
973
|
+
// 没填 SOCKS 地址不是故障:灰色「未配置」,别用红点吓人(review C3-P2-9)。
|
|
974
|
+
else if (result.configured === false) { cls += ' off'; text = '未配置'; }
|
|
975
|
+
else if (result.ok === true) { cls += ' on'; text = '连通 ' + ((result.ms || 0) + ' ms'); }
|
|
976
|
+
else if (result.socks === true) { cls += ' warn'; text = '需认证'; }
|
|
977
|
+
else { cls += ' err'; text = '无法连接'; }
|
|
978
|
+
chip.className = cls;
|
|
979
|
+
chip.textContent = text;
|
|
980
|
+
chip.title = result === null || result === undefined
|
|
981
|
+
? '正在检测代理服务器连通性…'
|
|
982
|
+
: (result.configured === false
|
|
983
|
+
? '还没有填写代理服务器地址'
|
|
984
|
+
: (result.ok === true ? '代理服务器可达(' + (result.host || '') + ':' + (result.port || '') + ',' + result.at + ')' : String(result.error || '连接失败')));
|
|
985
|
+
}
|
|
986
|
+
function refreshPing() {
|
|
987
|
+
var host = el('proxy-socks-host').value.trim();
|
|
988
|
+
var port = el('proxy-socks-port').value.trim();
|
|
989
|
+
var token = state.pingSeq = (state.pingSeq || 0) + 1;
|
|
990
|
+
// 空地址不发请求:/v1/proxy/ping 必然失败,10 s 一次的必然失败探测没有意义。
|
|
991
|
+
if (host === '') { renderPing({ configured: false }); return Promise.resolve(null); }
|
|
992
|
+
renderPing(null);
|
|
993
|
+
return api('/v1/proxy/ping?host=' + encodeURIComponent(host) + '&port=' + encodeURIComponent(port)).then(function (body) {
|
|
994
|
+
if (token !== state.pingSeq) return;
|
|
995
|
+
renderPing(body);
|
|
996
|
+
}).catch(function (error) {
|
|
997
|
+
if (token !== state.pingSeq) return;
|
|
998
|
+
renderPing({ ok: false, socks: false, error: error.message, host: host, port: port });
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
function syncPingTimer() {
|
|
1002
|
+
var wanted = state.tab === 'proxy' && document.hidden !== true;
|
|
1003
|
+
if (wanted && state.pingTimer === null) {
|
|
1004
|
+
if (el('proxy-socks-host').value.trim() !== '') refreshPing();
|
|
1005
|
+
state.pingTimer = setInterval(refreshPing, 10000);
|
|
1006
|
+
} else if (!wanted && state.pingTimer !== null) {
|
|
1007
|
+
clearInterval(state.pingTimer);
|
|
1008
|
+
state.pingTimer = null;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
// ---- IDE tab ----
|
|
1013
|
+
// One page can drive every workspace: the daemon reports each live dsh window
|
|
1014
|
+
// (cwd + sessionId); the left panel switches between them, and it can collapse
|
|
1015
|
+
// so the iframe gets the whole width.
|
|
1016
|
+
function ideTargetUrl(win) {
|
|
1017
|
+
var base = String(win.url || '');
|
|
1018
|
+
while (base.length > 1 && base.slice(-1) === '/') base = base.slice(0, -1);
|
|
1019
|
+
var cwd = win.cwd || state.cwd || '';
|
|
1020
|
+
return base + '/dsh-ris-ide/window?cwd=' + encodeURIComponent(cwd) +
|
|
1021
|
+
(win.sessionId ? '&sessionId=' + encodeURIComponent(win.sessionId) : '') + '&wname=' + encodeURIComponent(IDE_WNAME);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// Collapsed by default (m08228): the IDE needs the width; expand from the header.
|
|
1025
|
+
function sideVisible() { return localStorage.getItem('risd.ideSide') === 'on'; }
|
|
1026
|
+
/** Repaint the sidebar and its header toggle (one control, both states). */
|
|
1027
|
+
function applySide() {
|
|
1028
|
+
var visible = sideVisible();
|
|
1029
|
+
el('ide-side').hidden = !visible;
|
|
1030
|
+
var toggle = el('ide-side-toggle');
|
|
1031
|
+
if (toggle) {
|
|
1032
|
+
toggle.textContent = visible ? '‹' : '›';
|
|
1033
|
+
var label = visible ? '收起工作区面板' : '展开工作区面板';
|
|
1034
|
+
toggle.title = label;
|
|
1035
|
+
toggle.setAttribute('aria-label', label);
|
|
1036
|
+
toggle.setAttribute('aria-expanded', visible ? 'true' : 'false');
|
|
1037
|
+
toggle.disabled = el('view-ide').hidden;
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
function workspaceBase(cwd) {
|
|
1042
|
+
var parts = String(cwd || '').split('/').filter(Boolean);
|
|
1043
|
+
return parts.length > 0 ? parts[parts.length - 1] : String(cwd || '');
|
|
1044
|
+
}
|
|
1045
|
+
/** A short, stable label: the folder name, with the parent when two collide. */
|
|
1046
|
+
function workspaceLabel(cwd, windows) {
|
|
1047
|
+
var base = workspaceBase(cwd);
|
|
1048
|
+
var same = windows.filter(function (win) { return workspaceBase(win.cwd) === base; }).length;
|
|
1049
|
+
if (same <= 1) return base || '(未知工作区)';
|
|
1050
|
+
var parts = String(cwd || '').split('/').filter(Boolean);
|
|
1051
|
+
return parts.slice(-2).join('/');
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
function focusIdeFrame() {
|
|
1055
|
+
var frame = el('ide-frame');
|
|
1056
|
+
if (frame) { try { frame.focus(); } catch { /* focus is best effort */ } }
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
function selectIdeWindow(windows, cwd) {
|
|
1060
|
+
var win = windows.filter(function (row) { return (row.cwd || '') === cwd; })[0] || windows[0];
|
|
1061
|
+
if (!win) return;
|
|
1062
|
+
var key = win.cwd || '';
|
|
1063
|
+
if (win.live !== false) delete state.staleDismissed[key];
|
|
1064
|
+
var stale = el('ide-stale');
|
|
1065
|
+
if (stale) {
|
|
1066
|
+
stale.hidden = win.live !== false || state.staleDismissed[key] === true;
|
|
1067
|
+
var staleText = el('ide-stale-text');
|
|
1068
|
+
if (staleText) staleText.textContent = '该工作区最近没有活动(dsh 窗口可能已关闭),IDE 仍会尝试加载;若失败请重新打开 dsh 面板。';
|
|
1069
|
+
}
|
|
1070
|
+
if (win.cwd) { state.cwd = win.cwd; localStorage.setItem('risd.cwd', win.cwd); }
|
|
1071
|
+
var frame = ensureIdeFrame();
|
|
1072
|
+
frame.hidden = false;
|
|
1073
|
+
var url = ideTargetUrl(win);
|
|
1074
|
+
// Keep the document alive when the target did not change: switching tabs must
|
|
1075
|
+
// not throw away unsaved edits, the debugger selection or the scroll position
|
|
1076
|
+
// (review C-P0-1).
|
|
1077
|
+
if (frame.getAttribute('src') !== url) frame.src = url;
|
|
1078
|
+
// Move keyboard focus into the embedded IDE: cross-origin iframes receive their
|
|
1079
|
+
// own key events natively, but only while they hold focus. This also runs when
|
|
1080
|
+
// the document was reused (workspace switch), where Ctrl+F must stay inside.
|
|
1081
|
+
try { frame.focus(); } catch { /* focus is best effort */ }
|
|
1082
|
+
el('ide-ws-list').querySelectorAll('[data-cwd]').forEach(function (button) {
|
|
1083
|
+
button.classList.toggle('active', button.getAttribute('data-cwd') === (win.cwd || ''));
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
function ensureIdeFrame() {
|
|
1088
|
+
var frame = el('ide-frame');
|
|
1089
|
+
if (frame) return frame;
|
|
1090
|
+
frame = document.createElement('iframe');
|
|
1091
|
+
frame.id = 'ide-frame';
|
|
1092
|
+
frame.className = 'ide-frame';
|
|
1093
|
+
frame.title = 'RIS-IDE';
|
|
1094
|
+
el('ide-body').appendChild(frame);
|
|
1095
|
+
return frame;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
function ideMessage(kind, text, note) {
|
|
1099
|
+
var box = el('ide-msg');
|
|
1100
|
+
box.hidden = false;
|
|
1101
|
+
box.innerHTML = banner(kind, text) + (note ? '<p class="note">' + esc(note) + '</p>' : '');
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
function clearIdeMessage() {
|
|
1105
|
+
var box = el('ide-msg');
|
|
1106
|
+
if (box) {
|
|
1107
|
+
box.hidden = true;
|
|
1108
|
+
box.innerHTML = '';
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
function renderIde(windows, current) {
|
|
1113
|
+
var selected = current || windows[0];
|
|
1114
|
+
el('ide-ws-list').innerHTML = windows.map(function (win) {
|
|
1115
|
+
var label = workspaceLabel(win.cwd, windows);
|
|
1116
|
+
// Non-live entries stay clickable: dsh may still have the session, the iframe
|
|
1117
|
+
// shows the real result (m08268).
|
|
1118
|
+
var suffix = win.live === false ? '(未活跃)' : '';
|
|
1119
|
+
return '<li><button type="button" data-cwd="' + esc(win.cwd || '') + '" title="' + esc((win.cwd || '') + (suffix ? ' ' + suffix : '')) + '">' +
|
|
1120
|
+
esc(label + suffix) + '</button></li>';
|
|
1121
|
+
}).join('');
|
|
1122
|
+
var staleClose = el('ide-stale-close');
|
|
1123
|
+
if (staleClose) {
|
|
1124
|
+
staleClose.onclick = function () {
|
|
1125
|
+
state.staleDismissed[state.cwd || ''] = true;
|
|
1126
|
+
var bar = el('ide-stale');
|
|
1127
|
+
if (bar) bar.hidden = true;
|
|
1128
|
+
};
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
el('ide-ws-list').onclick = function (event) {
|
|
1132
|
+
var button = event.target.closest('[data-cwd]');
|
|
1133
|
+
if (button) selectIdeWindow(windows, button.getAttribute('data-cwd'));
|
|
1134
|
+
};
|
|
1135
|
+
clearIdeMessage();
|
|
1136
|
+
ensureIdeFrame();
|
|
1137
|
+
applySide();
|
|
1138
|
+
selectIdeWindow(windows, selected.cwd || '');
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
function ideUnavailable(body) {
|
|
1142
|
+
el('ide-ws-list').innerHTML = '';
|
|
1143
|
+
var frame = el('ide-frame');
|
|
1144
|
+
if (frame) frame.hidden = true;
|
|
1145
|
+
ideMessage('warn', 'IDE 不可用:' + (body.reason || 'dsh 未运行'),
|
|
1146
|
+
'IDE 界面由 dsh 提供;启动 dsh 并打开一次 RIS-IDE 面板后,这里会自动可用。');
|
|
1147
|
+
applySide();
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
function loadIde() {
|
|
1151
|
+
if (!el('ide-frame') && el('ide-msg').hidden) {
|
|
1152
|
+
el('ide-msg').hidden = false;
|
|
1153
|
+
el('ide-msg').innerHTML = '<p class="empty">检查 dsh 状态…</p>';
|
|
1154
|
+
}
|
|
1155
|
+
return api('/v1/dsh').then(function (body) {
|
|
1156
|
+
state.dsh = body;
|
|
1157
|
+
state.dshSignature = dshWindowSignature(body);
|
|
1158
|
+
var windows = body.windows || [];
|
|
1159
|
+
if (!body.available || windows.length === 0) {
|
|
1160
|
+
ideUnavailable(body);
|
|
1161
|
+
return;
|
|
1162
|
+
}
|
|
1163
|
+
var preferred = state.cwd;
|
|
1164
|
+
var selected = windows.filter(function (win) { return (win.cwd || '') === preferred; })[0] ||
|
|
1165
|
+
windows.filter(function (win) { return win.live !== false; })[0] || windows[0];
|
|
1166
|
+
renderIde(windows, selected);
|
|
1167
|
+
}).catch(function (error) {
|
|
1168
|
+
ideMessage('err', 'IDE 状态读取失败:' + error.message);
|
|
1169
|
+
});
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
/**
|
|
1173
|
+
* Re-read the window list but keep the existing iframe: called when the IDE tab
|
|
1174
|
+
* is re-opened or the dsh window set changed. Rebuilding the element here was
|
|
1175
|
+
* the P0 that silently dropped unsaved edits.
|
|
1176
|
+
*/
|
|
1177
|
+
function refreshIdeWindows() {
|
|
1178
|
+
return api('/v1/dsh').then(function (body) {
|
|
1179
|
+
state.dsh = body;
|
|
1180
|
+
state.dshSignature = dshWindowSignature(body);
|
|
1181
|
+
var windows = body.windows || [];
|
|
1182
|
+
if (!body.available || windows.length === 0) {
|
|
1183
|
+
ideUnavailable(body);
|
|
1184
|
+
return;
|
|
1185
|
+
}
|
|
1186
|
+
var preferred = state.cwd;
|
|
1187
|
+
var selected = windows.filter(function (win) { return (win.cwd || '') === preferred; })[0] ||
|
|
1188
|
+
windows.filter(function (win) { return win.live !== false; })[0] || windows[0];
|
|
1189
|
+
renderIde(windows, selected);
|
|
1190
|
+
}).catch(function (error) {
|
|
1191
|
+
ideMessage('err', 'IDE 状态读取失败:' + error.message);
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
function dshWindowSignature(body) {
|
|
1196
|
+
// Liveness is part of the rendering state: a window going 未活跃 must repaint
|
|
1197
|
+
// the hint bar even though cwd/sessionId did not change.
|
|
1198
|
+
return (body.windows || []).map(function (win) {
|
|
1199
|
+
return (win.cwd || '') + '|' + (win.sessionId || '') + '|' + (win.live === false ? 'stale' : 'live');
|
|
1200
|
+
}).sort().join('\\n');
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
/** Grey the IDE tab when no dsh window is answering; re-check every 5s. */
|
|
1204
|
+
function syncDshTab() {
|
|
1205
|
+
return api('/v1/dsh').then(function (body) {
|
|
1206
|
+
var available = body.available === true && (body.windows || []).length > 0;
|
|
1207
|
+
var changed = state.dshAvailable !== available;
|
|
1208
|
+
state.dsh = body;
|
|
1209
|
+
state.dshAvailable = available;
|
|
1210
|
+
var tab = document.querySelector('nav button[data-tab="ide"]');
|
|
1211
|
+
tab.disabled = !available;
|
|
1212
|
+
tab.title = available
|
|
1213
|
+
? '嵌入 dsh 的 IDE(可按工作区切换)'
|
|
1214
|
+
: ('IDE 不可用:' + (body.reason || 'dsh 未运行'));
|
|
1215
|
+
var signature = dshWindowSignature(body);
|
|
1216
|
+
var windowsChanged = state.dshSignature !== signature;
|
|
1217
|
+
state.dshSignature = signature;
|
|
1218
|
+
if (state.tab === 'ide' && (changed || windowsChanged)) {
|
|
1219
|
+
if (available) refreshIdeWindows();
|
|
1220
|
+
else ideUnavailable(body);
|
|
1221
|
+
}
|
|
1222
|
+
return available;
|
|
1223
|
+
}).catch(function () {
|
|
1224
|
+
state.dshAvailable = false;
|
|
1225
|
+
var tab = document.querySelector('nav button[data-tab="ide"]');
|
|
1226
|
+
tab.disabled = true;
|
|
1227
|
+
tab.title = 'IDE 不可用:无法读取 dsh 状态';
|
|
1228
|
+
return false;
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
// ---- tabs ----
|
|
1233
|
+
function showTab(name) {
|
|
1234
|
+
// Refuse a disabled IDE tab BEFORE touching visibility: the old order made the
|
|
1235
|
+
// pane visible and then returned, so any caller other than a click (deep link,
|
|
1236
|
+
// shortcut) would leave an empty IDE view behind (review C-P2-9).
|
|
1237
|
+
if (name === 'ide') {
|
|
1238
|
+
var ideTab = document.querySelector('nav button[data-tab="ide"]');
|
|
1239
|
+
if (ideTab && ideTab.disabled) return;
|
|
1240
|
+
}
|
|
1241
|
+
state.tab = name;
|
|
1242
|
+
try { localStorage.setItem(TAB_STORAGE, name); } catch { /* storage off */ }
|
|
1243
|
+
document.querySelectorAll('nav button[data-tab]').forEach(function (button) {
|
|
1244
|
+
button.setAttribute('aria-selected', button.getAttribute('data-tab') === name ? 'true' : 'false');
|
|
1245
|
+
});
|
|
1246
|
+
el('view-overview').hidden = name !== 'overview';
|
|
1247
|
+
el('view-proxy').hidden = name !== 'proxy';
|
|
1248
|
+
el('view-ide').hidden = name !== 'ide';
|
|
1249
|
+
if (name === 'overview') refreshOverview();
|
|
1250
|
+
if (name === 'proxy') loadProxyTab();
|
|
1251
|
+
// Re-opening the tab must not reload the embedded IDE: only the window list is
|
|
1252
|
+
// refreshed, the iframe (and its unsaved state) stays (review C-P0-1).
|
|
1253
|
+
if (name === 'ide') { if (el('ide-frame')) refreshIdeWindows(); else loadIde(); }
|
|
1254
|
+
// Leaving the proxy tab must stop the log tail; entering it restarts it only if
|
|
1255
|
+
// the pane was already on (review C-P2-3).
|
|
1256
|
+
syncLogTimer();
|
|
1257
|
+
// Same lifecycle for the reachability chip: never probe while another tab shows.
|
|
1258
|
+
syncPingTimer();
|
|
1259
|
+
// The header toggle only applies to the IDE pane: disable it (with its title
|
|
1260
|
+
// explaining why) while another tab is showing.
|
|
1261
|
+
applySide();
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
// ---- wiring ----
|
|
1265
|
+
document.querySelectorAll('nav button[data-tab]').forEach(function (button) {
|
|
1266
|
+
button.onclick = function () { showTab(button.getAttribute('data-tab')); };
|
|
1267
|
+
});
|
|
1268
|
+
// Stopping the daemon stops everything it supervises, so it gets a real
|
|
1269
|
+
// reminder (not a one-line confirm): list what is about to go away.
|
|
1270
|
+
function askStopDaemon() {
|
|
1271
|
+
return refreshOverview().catch(function () {}).then(function () {
|
|
1272
|
+
var summary = state.summary || {};
|
|
1273
|
+
var programs = summary.programs || 0;
|
|
1274
|
+
var debugs = summary.debugs || 0;
|
|
1275
|
+
var lines = [];
|
|
1276
|
+
lines.push('停止后需要重新启动 dsh,或手跑 <b>risd serve</b>,才能再次使用代理与 IDE。');
|
|
1277
|
+
lines.push('将要停止:');
|
|
1278
|
+
lines.push('· 代理引擎:' + (summary.proxyRunning ? '<b>运行中</b>(会一并停止)' : '未运行'));
|
|
1279
|
+
lines.push('· 系统层拦截:随代理一起拆除(可能弹一次提权确认)');
|
|
1280
|
+
lines.push('· 运行中的项目:<b>' + programs + '</b> 个' + (programs > 0 ? '(会先停止)' : ''));
|
|
1281
|
+
lines.push('· 调试会话:<b>' + debugs + '</b> 个' + (debugs > 0 ? '(会先断开)' : ''));
|
|
1282
|
+
el('confirm-body').innerHTML = lines.join('<br>');
|
|
1283
|
+
el('confirm').hidden = false;
|
|
1284
|
+
return new Promise(function (resolve) {
|
|
1285
|
+
function finish(value) {
|
|
1286
|
+
el('confirm').hidden = true;
|
|
1287
|
+
el('confirm-ok').onclick = null;
|
|
1288
|
+
el('confirm-cancel').onclick = null;
|
|
1289
|
+
document.removeEventListener('keydown', onKey);
|
|
1290
|
+
resolve(value);
|
|
1291
|
+
}
|
|
1292
|
+
function onKey(event) { if (event.key === 'Escape') finish(false); }
|
|
1293
|
+
el('confirm-ok').onclick = function () { finish(true); };
|
|
1294
|
+
el('confirm-cancel').onclick = function () { finish(false); };
|
|
1295
|
+
document.addEventListener('keydown', onKey);
|
|
1296
|
+
});
|
|
1297
|
+
});
|
|
1298
|
+
}
|
|
1299
|
+
el('daemon-stop').onclick = function () {
|
|
1300
|
+
askStopDaemon().then(function (ok) {
|
|
1301
|
+
if (!ok) return;
|
|
1302
|
+
return shutdownDaemon();
|
|
1303
|
+
});
|
|
1304
|
+
};
|
|
1305
|
+
// (overview's 停止 keeps its own path below; the proxy tab's stop reloads the tab.)
|
|
1306
|
+
function shutdownDaemon() {
|
|
1307
|
+
api('/v1/shutdown', { method: 'POST', body: {} }).catch(function () { /* the page dies with it */ });
|
|
1308
|
+
el('daemon-pill').className = 'pill off';
|
|
1309
|
+
el('daemon-pill').textContent = '已停止';
|
|
1310
|
+
el('daemon-sub').textContent = '页面已断开;下次 daemonMode=auto 的 dsh 启动或手跑 risd serve 会重新拉起';
|
|
1311
|
+
};
|
|
1312
|
+
// One button, two states: the label IS the next action (m10585). A stop leaves it
|
|
1313
|
+
// reading 启动, so the same spot brings the tunnel back. Starting can wait seconds
|
|
1314
|
+
// behind sudo/UAC, so both buttons show the in-flight action and ignore clicks
|
|
1315
|
+
// until it settles (user m11279).
|
|
1316
|
+
var engineBusy = false;
|
|
1317
|
+
var engineBusyAction = '';
|
|
1318
|
+
function paintEngineButtons() {
|
|
1319
|
+
var running = state.summary.proxyRunning === true || state.proxyRunning === true;
|
|
1320
|
+
[el('engine-toggle'), el('proxy-engine-toggle')].forEach(function (button) {
|
|
1321
|
+
if (!button) return;
|
|
1322
|
+
if (engineBusy) {
|
|
1323
|
+
button.disabled = true;
|
|
1324
|
+
button.setAttribute('aria-busy', 'true');
|
|
1325
|
+
button.textContent = engineBusyAction === 'stop' ? '停止中…' : '启动中…';
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
button.removeAttribute('aria-busy');
|
|
1329
|
+
button.disabled = false;
|
|
1330
|
+
button.textContent = running ? '停止' : '启动';
|
|
1331
|
+
button.className = running ? 'danger' : '';
|
|
1332
|
+
button.title = running
|
|
1333
|
+
? '停止引擎、系统层拦截与 git 代理'
|
|
1334
|
+
: '按当前规则表启动代理引擎(系统层拦截可能需要提权)';
|
|
1335
|
+
});
|
|
1336
|
+
}
|
|
1337
|
+
function toggleProxyEngine() {
|
|
1338
|
+
if (engineBusy) return;
|
|
1339
|
+
var running = state.summary.proxyRunning === true || state.proxyRunning === true;
|
|
1340
|
+
var action = running ? 'stop' : 'start';
|
|
1341
|
+
var host = state.tab === 'proxy' ? el('proxy-banner') : el('overview-banner');
|
|
1342
|
+
host.innerHTML = '';
|
|
1343
|
+
engineBusy = true;
|
|
1344
|
+
engineBusyAction = action;
|
|
1345
|
+
paintEngineButtons();
|
|
1346
|
+
api('/v1/action', { method: 'POST', body: { action: action, cwd: state.cwd } })
|
|
1347
|
+
.then(function (response) {
|
|
1348
|
+
var result = (response && response.result) || {};
|
|
1349
|
+
var redirector = result.redirector || {};
|
|
1350
|
+
var skipped = Array.isArray(redirector.skipped) ? redirector.skipped : [];
|
|
1351
|
+
if (skipped.length > 0) {
|
|
1352
|
+
host.innerHTML = banner('warn', '本机引擎已启动;系统层拦截需要提权:' + skipped.map(function (item) { return item.side + ':' + item.reason; }).join(';'));
|
|
1353
|
+
}
|
|
1354
|
+
engineBusy = false;
|
|
1355
|
+
paintEngineButtons();
|
|
1356
|
+
return state.tab === 'proxy' ? loadProxyTab() : refreshOverview();
|
|
1357
|
+
})
|
|
1358
|
+
.catch(function (error) {
|
|
1359
|
+
engineBusy = false;
|
|
1360
|
+
paintEngineButtons();
|
|
1361
|
+
host.innerHTML = banner('err', (running ? '停止' : '启动') + '代理引擎失败:' + error.message +
|
|
1362
|
+
(running ? '(可以再点一次重试;仍不行就停掉守护进程,它会一并拆除引擎)' : ''));
|
|
1363
|
+
});
|
|
1364
|
+
}
|
|
1365
|
+
el('engine-toggle').onclick = toggleProxyEngine;
|
|
1366
|
+
el('proxy-engine-toggle').onclick = toggleProxyEngine;
|
|
1367
|
+
el('lifecycle-save').onclick = saveLifecycle;
|
|
1368
|
+
// Any toggle means "this form is mine now": the 4 s refresh stops refilling it
|
|
1369
|
+
// until the save succeeds (review C-P2-3).
|
|
1370
|
+
el('lifecycle-rows').addEventListener('change', function () { state.lifecycleDirty = true; });
|
|
1371
|
+
|
|
1372
|
+
// Re-probe while the user edits host/port: 800 ms after the last keystroke, and
|
|
1373
|
+
// only on the proxy tab (the inputs are hidden elsewhere).
|
|
1374
|
+
var pingDebounce = null;
|
|
1375
|
+
function queuePing() {
|
|
1376
|
+
if (state.tab !== 'proxy') return;
|
|
1377
|
+
if (pingDebounce !== null) clearTimeout(pingDebounce);
|
|
1378
|
+
pingDebounce = setTimeout(function () { pingDebounce = null; refreshPing(); }, 800);
|
|
1379
|
+
}
|
|
1380
|
+
el('proxy-socks-host').addEventListener('input', queuePing);
|
|
1381
|
+
el('proxy-socks-port').addEventListener('input', queuePing);
|
|
1382
|
+
// Dirty tracking: the proxy tab is the only editable form on this page, and a
|
|
1383
|
+
// drift-banner refresh must not silently throw the edit away (review C3 P1-3).
|
|
1384
|
+
// Editing is the save: every change (socks 行/端口/规则表) re-validates and, once
|
|
1385
|
+
// the form is complete, writes the table and starts the engine if it was down.
|
|
1386
|
+
el('proxy-socks-host').addEventListener('input', scheduleProxySave);
|
|
1387
|
+
el('proxy-socks-port').addEventListener('input', scheduleProxySave);
|
|
1388
|
+
el('proxy-listen').addEventListener('input', scheduleProxySave);
|
|
1389
|
+
el('proxy-rules').addEventListener('input', scheduleProxySave);
|
|
1390
|
+
el('proxy-rules').addEventListener('change', scheduleProxySave);
|
|
1391
|
+
el('proxy-add').onclick = function () {
|
|
1392
|
+
var rules = (state.view && state.view.rules ? state.view.rules.slice() : []);
|
|
1393
|
+
rules.push({ id: 'user-' + Math.random().toString(36).slice(2, 9), program: '', hosts: '', ports: '*', enabled: true, source: 'user', shadow: false });
|
|
1394
|
+
renderProxyTab(Object.assign({}, state.view || {}, { rules: rules }));
|
|
1395
|
+
scheduleProxySave();
|
|
1396
|
+
};
|
|
1397
|
+
el('bridge-restart').onclick = function () {
|
|
1398
|
+
api('/v1/action', { method: 'POST', body: { action: 'redirector-restart', cwd: state.cwd } })
|
|
1399
|
+
.then(function (response) {
|
|
1400
|
+
// The action result lives under result: reading response.started always
|
|
1401
|
+
// found nothing and reported "需要提权" even after a successful restart
|
|
1402
|
+
// (review C-P1-3).
|
|
1403
|
+
var result = (response && response.result) || {};
|
|
1404
|
+
var skipped = Array.isArray(result.skipped) ? result.skipped : [];
|
|
1405
|
+
var started = Array.isArray(result.started) ? result.started : [];
|
|
1406
|
+
var text = started.length > 0
|
|
1407
|
+
? '已按新规则重启(' + started.join(',') + ')'
|
|
1408
|
+
: '需要提权:' + JSON.stringify(skipped.length > 0 ? skipped : (result.stopped || {}));
|
|
1409
|
+
el('proxy-banner').innerHTML = banner(started.length > 0 ? 'ok' : 'warn', text);
|
|
1410
|
+
}).catch(function (error) { el('proxy-banner').innerHTML = banner('err', error.message); });
|
|
1411
|
+
};
|
|
1412
|
+
el('bridge-download').onclick = downloadBundle;
|
|
1413
|
+
el('bridge-upload').onclick = function () { el('bridge-file').click(); };
|
|
1414
|
+
el('bridge-file').onchange = function (event) {
|
|
1415
|
+
var file = event.target.files && event.target.files[0];
|
|
1416
|
+
uploadBundle(file);
|
|
1417
|
+
event.target.value = '';
|
|
1418
|
+
};
|
|
1419
|
+
el('proxy-log-toggle').onclick = logToggle;
|
|
1420
|
+
el('proxy-rules').onclick = function (event) {
|
|
1421
|
+
var remove = event.target.getAttribute('data-remove');
|
|
1422
|
+
if (remove === null) return;
|
|
1423
|
+
var rules = rulesFromTable();
|
|
1424
|
+
rules.splice(Number(remove), 1);
|
|
1425
|
+
renderProxyTab(Object.assign({}, state.view || {}, { rules: rules }));
|
|
1426
|
+
// Deleting is an edit like any other: schedule the write, otherwise the table on
|
|
1427
|
+
// screen and the table the engine uses diverge until the next keystroke.
|
|
1428
|
+
scheduleProxySave();
|
|
1429
|
+
};
|
|
1430
|
+
el('projects').onclick = function (event) {
|
|
1431
|
+
var button = event.target.closest('[data-stop-program]');
|
|
1432
|
+
if (!button) return;
|
|
1433
|
+
var cwd = button.getAttribute('data-cwd');
|
|
1434
|
+
var id = button.getAttribute('data-stop-program');
|
|
1435
|
+
api('/v1/programs', { method: 'POST', body: { cwd: cwd, action: 'stop', id: id } })
|
|
1436
|
+
.then(loadProjects).catch(function (error) { alert(error.message); });
|
|
1437
|
+
};
|
|
1438
|
+
el('debugs').onclick = function (event) {
|
|
1439
|
+
var stop = event.target.closest('[data-stop-debug]');
|
|
1440
|
+
if (stop) {
|
|
1441
|
+
api('/v1/debug', { method: 'POST', body: { action: 'stop', cwd: stop.getAttribute('data-cwd'), id: stop.getAttribute('data-stop-debug') } })
|
|
1442
|
+
.then(loadDebugs).catch(function (error) { alert(error.message); });
|
|
1443
|
+
return;
|
|
1444
|
+
}
|
|
1445
|
+
var forget = event.target.closest('[data-forget-debug]');
|
|
1446
|
+
if (forget) {
|
|
1447
|
+
if (!window.confirm('清除这条调试记录?断点集合也会一起丢失。')) return;
|
|
1448
|
+
api('/v1/debug', { method: 'POST', body: { action: 'forget', logPath: forget.getAttribute('data-forget-debug') } })
|
|
1449
|
+
.then(loadDebugs).catch(function (error) { alert(error.message); });
|
|
1450
|
+
return;
|
|
1451
|
+
}
|
|
1452
|
+
var detach = event.target.closest('[data-detach]');
|
|
1453
|
+
if (!detach) return;
|
|
1454
|
+
api('/v1/debug', { method: 'POST', body: { action: 'detach', logPath: detach.getAttribute('data-detach') } })
|
|
1455
|
+
.then(loadDebugs).catch(function (error) { alert(error.message); });
|
|
1456
|
+
};
|
|
1457
|
+
|
|
1458
|
+
// Which tab to open: an explicit ?tab= wins (handy for tests/docs), a plain
|
|
1459
|
+
// entry (the dsh ⧉ deep-links with only ?cwd=, m11345) and a reload both fall
|
|
1460
|
+
// back to the tab the user was last on (m10900). Same for the workspace:
|
|
1461
|
+
// showTab/selectIdeWindow write localStorage, so a shortcut- or toolbar reload
|
|
1462
|
+
// keeps both.
|
|
1463
|
+
var TAB_STORAGE = 'risd.tab';
|
|
1464
|
+
var requestedTab = 'overview';
|
|
1465
|
+
try {
|
|
1466
|
+
var wanted = new URLSearchParams(location.search).get('tab');
|
|
1467
|
+
if (wanted === 'ide' || wanted === 'proxy' || wanted === 'overview') {
|
|
1468
|
+
requestedTab = wanted;
|
|
1469
|
+
} else {
|
|
1470
|
+
var savedTab = localStorage.getItem(TAB_STORAGE);
|
|
1471
|
+
if (savedTab === 'ide' || savedTab === 'proxy' || savedTab === 'overview') requestedTab = savedTab;
|
|
1472
|
+
}
|
|
1473
|
+
} catch { /* no URL API / no storage */ }
|
|
1474
|
+
el('ide-side-toggle').onclick = function () {
|
|
1475
|
+
localStorage.setItem('risd.ideSide', sideVisible() ? 'off' : 'on');
|
|
1476
|
+
applySide();
|
|
1477
|
+
};
|
|
1478
|
+
// Clicking anywhere outside the iframe puts the caret back into the embedded
|
|
1479
|
+
// IDE. A cross-origin iframe swallows clicks inside itself, so any click that
|
|
1480
|
+
// reaches this document means the user left the IDE: refocus it. (Clicks on the
|
|
1481
|
+
// workspace list run first; focus moves after the switch has been applied.)
|
|
1482
|
+
document.addEventListener('click', function (event) {
|
|
1483
|
+
if (state.tab !== 'ide') return;
|
|
1484
|
+
var frame = el('ide-frame');
|
|
1485
|
+
if (!frame || event.target === frame) return;
|
|
1486
|
+
focusIdeFrame();
|
|
1487
|
+
}, true);
|
|
1488
|
+
applySide();
|
|
1489
|
+
// Coming back to the tab must resume the log tail immediately, and hiding the
|
|
1490
|
+
// page must stop it (review C-P2-3).
|
|
1491
|
+
document.addEventListener('visibilitychange', function () { syncLogTimer(); syncPingTimer(); });
|
|
1492
|
+
|
|
1493
|
+
// ---- 页面代码漂移 / 令牌漂移(m09693、review C3-P1-3)----
|
|
1494
|
+
// 以前 loaded 变化直接 location.reload():代理规则表正在编辑时刷新会丢失输入,
|
|
1495
|
+
// 内嵌 IDE iframe 的 beforeunload 在跨源页面里还不弹。现在只提示,刷新由用户点。
|
|
1496
|
+
function showDrift(text, mode) {
|
|
1497
|
+
var box = el('ui-drift');
|
|
1498
|
+
if (!box) return;
|
|
1499
|
+
var message = String(text || '');
|
|
1500
|
+
if (message !== '' && message === driftDismissedFor) return;
|
|
1501
|
+
driftMessage = message;
|
|
1502
|
+
driftMode = mode === 'restart' ? 'restart' : 'reload';
|
|
1503
|
+
var label = el('ui-drift-text');
|
|
1504
|
+
if (label) label.textContent = message;
|
|
1505
|
+
var button = el('ui-drift-reload');
|
|
1506
|
+
if (button) {
|
|
1507
|
+
button.disabled = false;
|
|
1508
|
+
button.textContent = driftMode === 'restart' ? '重启并刷新' : '刷新';
|
|
1509
|
+
button.title = driftMode === 'restart' ? '先重启 daemon,再重新加载本页' : '重新加载本页';
|
|
1510
|
+
}
|
|
1511
|
+
box.hidden = false;
|
|
1512
|
+
}
|
|
1513
|
+
function hideDrift() {
|
|
1514
|
+
var box = el('ui-drift');
|
|
1515
|
+
if (box) box.hidden = true;
|
|
1516
|
+
}
|
|
1517
|
+
// The embedded IDE lives in a cross-origin iframe, so the only way to know
|
|
1518
|
+
// whether a reload would drop unsaved editor buffers is to ask it over
|
|
1519
|
+
// postMessage (m10900). The iframe answers only its own parent (see
|
|
1520
|
+
// lib/client.js), and a frame that predates the bridge simply does not answer
|
|
1521
|
+
// — treated as "unknown", which never triggers an automatic reload.
|
|
1522
|
+
var ideAskState = { seq: 0, waiting: {} };
|
|
1523
|
+
function ideFrameWindow() {
|
|
1524
|
+
var frame = el('ide-frame');
|
|
1525
|
+
if (!frame) return null;
|
|
1526
|
+
try { return frame.contentWindow; } catch { return null; }
|
|
1527
|
+
}
|
|
1528
|
+
function askIdeDirty() {
|
|
1529
|
+
var target = ideFrameWindow();
|
|
1530
|
+
if (target === null) return Promise.resolve(0);
|
|
1531
|
+
var requestId = 'dirty-' + (ideAskState.seq += 1);
|
|
1532
|
+
return new Promise(function (resolve) {
|
|
1533
|
+
var timer = setTimeout(function () {
|
|
1534
|
+
delete ideAskState.waiting[requestId];
|
|
1535
|
+
resolve(null);
|
|
1536
|
+
}, 1500);
|
|
1537
|
+
ideAskState.waiting[requestId] = { resolve: resolve, timer: timer };
|
|
1538
|
+
try {
|
|
1539
|
+
target.postMessage({ type: 'dsh-ris-ide:probe-dirty', requestId: requestId }, '*');
|
|
1540
|
+
} catch {
|
|
1541
|
+
clearTimeout(timer);
|
|
1542
|
+
delete ideAskState.waiting[requestId];
|
|
1543
|
+
resolve(null);
|
|
1544
|
+
}
|
|
1545
|
+
}).then(function (data) {
|
|
1546
|
+
if (data === null || typeof data.count !== 'number') return null;
|
|
1547
|
+
return data.count;
|
|
1548
|
+
});
|
|
1549
|
+
}
|
|
1550
|
+
function askIdeSaveAll() {
|
|
1551
|
+
var target = ideFrameWindow();
|
|
1552
|
+
if (target === null) return Promise.resolve(false);
|
|
1553
|
+
var requestId = 'save-' + (ideAskState.seq += 1);
|
|
1554
|
+
return new Promise(function (resolve) {
|
|
1555
|
+
var timer = setTimeout(function () {
|
|
1556
|
+
delete ideAskState.waiting[requestId];
|
|
1557
|
+
resolve(null);
|
|
1558
|
+
}, 20000);
|
|
1559
|
+
ideAskState.waiting[requestId] = { resolve: resolve, timer: timer };
|
|
1560
|
+
try {
|
|
1561
|
+
target.postMessage({ type: 'dsh-ris-ide:save-all', requestId: requestId }, '*');
|
|
1562
|
+
} catch {
|
|
1563
|
+
clearTimeout(timer);
|
|
1564
|
+
delete ideAskState.waiting[requestId];
|
|
1565
|
+
resolve(null);
|
|
1566
|
+
}
|
|
1567
|
+
}).then(function (data) { return data !== null && data.ok === true; });
|
|
1568
|
+
}
|
|
1569
|
+
window.addEventListener('message', function (event) {
|
|
1570
|
+
var data = event.data;
|
|
1571
|
+
if (data === null || typeof data !== 'object') return;
|
|
1572
|
+
if (data.type !== 'dsh-ris-ide:dirty-report' && data.type !== 'dsh-ris-ide:save-all-done') return;
|
|
1573
|
+
var pending = ideAskState.waiting[data.requestId];
|
|
1574
|
+
if (pending === undefined) return;
|
|
1575
|
+
delete ideAskState.waiting[data.requestId];
|
|
1576
|
+
clearTimeout(pending.timer);
|
|
1577
|
+
pending.resolve(data);
|
|
1578
|
+
});
|
|
1579
|
+
|
|
1580
|
+
// The drift bar can offer a save-all when the embedded IDE holds drafts: a
|
|
1581
|
+
// reload would otherwise silently drop them (the iframe's beforeunload does
|
|
1582
|
+
// not run for a cross-origin reload).
|
|
1583
|
+
function setDriftSave(count) {
|
|
1584
|
+
var button = el('ui-drift-save');
|
|
1585
|
+
if (!button) return;
|
|
1586
|
+
button.hidden = !(typeof count === 'number' && count > 0);
|
|
1587
|
+
button.disabled = false;
|
|
1588
|
+
button.textContent = '保存所有文件';
|
|
1589
|
+
}
|
|
1590
|
+
function driftSaveThenReload() {
|
|
1591
|
+
var button = el('ui-drift-save');
|
|
1592
|
+
if (button) { button.disabled = true; button.textContent = '保存中…'; }
|
|
1593
|
+
askIdeSaveAll().then(function (saved) {
|
|
1594
|
+
if (saved !== true) {
|
|
1595
|
+
if (button) { button.disabled = false; button.textContent = '保存所有文件'; }
|
|
1596
|
+
showDrift('有文件没保存成功:先在开发环境里处理,再点「重启并刷新」', driftMode);
|
|
1597
|
+
return;
|
|
1598
|
+
}
|
|
1599
|
+
if (driftMode === 'restart') { restartDaemon(); return; }
|
|
1600
|
+
location.reload();
|
|
1601
|
+
});
|
|
1602
|
+
}
|
|
1603
|
+
// m11421: 插件版本更新后,旧 daemon 进程里 import 过的模块不会变,光刷新页面
|
|
1604
|
+
// 只会再拿到一份旧代码。这里让 daemon 自己交接重启(保留代理引擎),等新 instance
|
|
1605
|
+
// 出现在免认证的 /v1/health 上,再刷新本页。
|
|
1606
|
+
function restartDaemon() {
|
|
1607
|
+
var button = el('ui-drift-reload');
|
|
1608
|
+
if (button) { button.disabled = true; button.textContent = '重启中…'; }
|
|
1609
|
+
var previous = PAGE_INSTANCE;
|
|
1610
|
+
showDrift('插件版本已更新:正在重启 daemon,完成后本页会自动刷新…', 'restart');
|
|
1611
|
+
api('/v1/restart', { method: 'POST' }).catch(function () { /* 进程退出会切断这个请求,属正常 */ });
|
|
1612
|
+
var startedAt = Date.now();
|
|
1613
|
+
var timer = setInterval(function () {
|
|
1614
|
+
if (Date.now() - startedAt > 30000) {
|
|
1615
|
+
clearInterval(timer);
|
|
1616
|
+
showDrift('插件版本已更新,但 daemon 重启超时:手动重启后刷新本页', 'restart');
|
|
1617
|
+
if (button) { button.disabled = false; button.textContent = '重启并刷新'; }
|
|
1618
|
+
return;
|
|
1619
|
+
}
|
|
1620
|
+
fetch('/v1/health', { headers: { 'x-risd-api-version': '1' } })
|
|
1621
|
+
.then(function (res) { return res.json(); })
|
|
1622
|
+
.then(function (body) {
|
|
1623
|
+
if (body && body.ok === true && body.instance && body.instance !== previous) {
|
|
1624
|
+
clearInterval(timer);
|
|
1625
|
+
location.reload();
|
|
1626
|
+
}
|
|
1627
|
+
})
|
|
1628
|
+
.catch(function () { /* daemon 还没起来,下一轮再看 */ });
|
|
1629
|
+
}, 400);
|
|
1630
|
+
}
|
|
1631
|
+
function restartDaemonThenReload() {
|
|
1632
|
+
if (state.proxyDirty === true && window.confirm('代理规则还没保存,重启并刷新会丢失') !== true) return;
|
|
1633
|
+
askIdeDirty().then(function (count) {
|
|
1634
|
+
if (count === 0) { restartDaemon(); return; }
|
|
1635
|
+
setDriftSave(count);
|
|
1636
|
+
if (count === null) showDrift('无法确认开发环境里有没有未保存的文件:先保存,再点「重启并刷新」', 'restart');
|
|
1637
|
+
else showDrift('开发环境里有 ' + count + ' 个文件未保存:先点「保存所有文件」,再点「重启并刷新」', 'restart');
|
|
1638
|
+
});
|
|
1639
|
+
}
|
|
1640
|
+
function reloadPage() {
|
|
1641
|
+
// Unsaved proxy rules are the only edit on THIS page worth a confirm; the IDE
|
|
1642
|
+
// buffers are discovered through the bridge and turned into the save-all button.
|
|
1643
|
+
if (state.proxyDirty === true && window.confirm('代理规则还没保存,刷新会丢失') !== true) return;
|
|
1644
|
+
askIdeDirty().then(function (count) {
|
|
1645
|
+
if (count === 0) { location.reload(); return; }
|
|
1646
|
+
setDriftSave(count);
|
|
1647
|
+
if (count === null) showDrift('无法确认开发环境里有没有未保存的文件,刷新前请先保存');
|
|
1648
|
+
else showDrift('开发环境里有 ' + count + ' 个文件未保存:先点「保存所有文件」,再刷新');
|
|
1649
|
+
});
|
|
1650
|
+
}
|
|
1651
|
+
if (el('ui-drift-reload')) el('ui-drift-reload').addEventListener('click', function () {
|
|
1652
|
+
if (driftMode === 'restart') restartDaemonThenReload();
|
|
1653
|
+
else reloadPage();
|
|
1654
|
+
});
|
|
1655
|
+
if (el('ui-drift-save')) el('ui-drift-save').addEventListener('click', driftSaveThenReload);
|
|
1656
|
+
if (el('ui-drift-close')) {
|
|
1657
|
+
el('ui-drift-close').addEventListener('click', function () { driftDismissedFor = driftMessage; hideDrift(); });
|
|
1658
|
+
}
|
|
1659
|
+
function checkBuild() {
|
|
1660
|
+
return api('/v1/health').then(function (body) {
|
|
1661
|
+
if (!body) return;
|
|
1662
|
+
// A different instance = a restarted daemon: every token on this page is dead
|
|
1663
|
+
// and the HTML is stale. Reloading is the only cure — automatically when that
|
|
1664
|
+
// cannot lose work, otherwise with the button (the old page is unusable either
|
|
1665
|
+
// way: the daemon rejects its token).
|
|
1666
|
+
var stale = (PAGE_INSTANCE && body.instance && body.instance !== PAGE_INSTANCE)
|
|
1667
|
+
|| (PAGE_BUILD && body.uiBuild && body.uiBuild.loaded && body.uiBuild.loaded !== PAGE_BUILD);
|
|
1668
|
+
if (stale) {
|
|
1669
|
+
return askIdeDirty().then(function (count) {
|
|
1670
|
+
var safe = count === 0 && state.proxyDirty !== true && state.lifecycleDirty !== true;
|
|
1671
|
+
if (safe) { location.reload(); return; }
|
|
1672
|
+
setDriftSave(count);
|
|
1673
|
+
if (count === null) showDrift('daemon 已重启:本页令牌已失效;无法确认开发环境里有没有未保存的文件,刷新前请先保存');
|
|
1674
|
+
else if (count > 0) showDrift('daemon 已重启:本页令牌已失效;开发环境里有 ' + count + ' 个文件未保存,先点「保存所有文件」');
|
|
1675
|
+
else showDrift('daemon 已重启:本页令牌已失效,点右侧「刷新」');
|
|
1676
|
+
});
|
|
1677
|
+
}
|
|
1678
|
+
var build = body.uiBuild ? body.uiBuild : null;
|
|
1679
|
+
if (build === null) { hideDrift(); return; }
|
|
1680
|
+
// disk≠loaded = 源码比进程新:刷新拿不到新代码,只能提示重启 daemon。
|
|
1681
|
+
if (build.disk && build.loaded && build.disk !== build.loaded) {
|
|
1682
|
+
showDrift('插件版本已更新,点「重启并刷新」后生效', 'restart');
|
|
1683
|
+
return;
|
|
1684
|
+
}
|
|
1685
|
+
hideDrift();
|
|
1686
|
+
}).catch(function () { /* daemon 忙/重启中:下一轮再看 */ });
|
|
1687
|
+
}
|
|
1688
|
+
state.buildTimer = setInterval(function () { if (!document.hidden) checkBuild(); }, 5000);
|
|
1689
|
+
void checkBuild();
|
|
1690
|
+
syncDshTab().then(function (available) {
|
|
1691
|
+
// A dsh click asks for the IDE tab; without a live dsh fall back to 概览.
|
|
1692
|
+
showTab(requestedTab === 'ide' && available !== true ? 'overview' : requestedTab);
|
|
1693
|
+
});
|
|
1694
|
+
state.pollTimer = setInterval(function () {
|
|
1695
|
+
if (document.hidden) return;
|
|
1696
|
+
// The window list feeds the IDE tab and the overview's daemon row; reading it
|
|
1697
|
+
// from the proxy tab was wasted work (review C-P2-3).
|
|
1698
|
+
if (state.tab === 'ide' || state.tab === 'overview') syncDshTab();
|
|
1699
|
+
if (state.tab === 'overview') refreshOverview();
|
|
1700
|
+
}, 4000);
|
|
1701
|
+
})();
|
|
1702
|
+
</script>
|
|
1703
|
+
</body>
|
|
1704
|
+
</html>`
|
|
1705
|
+
}
|