embed.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. package static
  2. // IndexHTML is the embedded admin panel HTML
  3. var IndexHTML = `<!DOCTYPE html>
  4. <html lang="zh-CN">
  5. <head>
  6. <meta charset="UTF-8">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>FTP Server 管理面板</title>
  9. <style>
  10. * { margin: 0; padding: 0; box-sizing: border-box; }
  11. body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f0f2f5; color: #333; }
  12. /* Login Page */
  13. .login-container { display: flex; justify-content: center; align-items: center; min-height: 100vh; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
  14. .login-card { background: white; border-radius: 12px; padding: 40px; width: 400px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
  15. .login-card h1 { text-align: center; margin-bottom: 30px; color: #333; font-size: 24px; }
  16. .login-card h1 .icon { font-size: 48px; display: block; margin-bottom: 10px; }
  17. .form-group { margin-bottom: 20px; }
  18. .form-group label { display: block; margin-bottom: 6px; font-weight: 600; color: #555; font-size: 14px; }
  19. .form-group input { width: 100%; padding: 12px 16px; border: 1px solid #ddd; border-radius: 8px; font-size: 14px; transition: border-color 0.3s; }
  20. .form-group input:focus { outline: none; border-color: #667eea; box-shadow: 0 0 0 3px rgba(102,126,234,0.1); }
  21. .btn { padding: 12px 24px; border: none; border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 600; transition: all 0.3s; }
  22. .btn-primary { background: linear-gradient(135deg, #667eea, #764ba2); color: white; width: 100%; }
  23. .btn-primary:hover { transform: translateY(-1px); box-shadow: 0 4px 15px rgba(102,126,234,0.4); }
  24. .btn-danger { background: #dc3545; color: white; }
  25. .btn-danger:hover { background: #c82333; }
  26. .btn-success { background: #28a745; color: white; }
  27. .btn-success:hover { background: #218838; }
  28. .btn-sm { padding: 6px 14px; font-size: 12px; }
  29. .error-msg { color: #dc3545; text-align: center; margin-top: 12px; font-size: 14px; min-height: 20px; }
  30. /* Dashboard */
  31. .app-container { display: none; }
  32. .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 16px 30px; display: flex; justify-content: space-between; align-items: center; box-shadow: 0 2px 10px rgba(0,0,0,0.2); }
  33. .header h1 { font-size: 20px; font-weight: 600; }
  34. .header .user-info { display: flex; align-items: center; gap: 16px; }
  35. .header .user-info span { font-size: 14px; opacity: 0.9; }
  36. .sidebar { position: fixed; left: 0; top: 60px; bottom: 0; width: 220px; background: #fff; box-shadow: 2px 0 10px rgba(0,0,0,0.05); padding-top: 20px; }
  37. .sidebar a { display: block; padding: 14px 24px; color: #666; text-decoration: none; font-size: 14px; transition: all 0.3s; border-left: 3px solid transparent; }
  38. .sidebar a:hover, .sidebar a.active { background: #f8f9ff; color: #667eea; border-left-color: #667eea; }
  39. .main-content { margin-left: 220px; padding: 30px; margin-top: 60px; }
  40. .stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 20px; margin-bottom: 30px; }
  41. .stat-card { background: white; border-radius: 12px; padding: 24px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
  42. .stat-card .label { color: #888; font-size: 13px; margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.5px; }
  43. .stat-card .value { font-size: 28px; font-weight: 700; color: #333; }
  44. .stat-card .value.purple { color: #667eea; }
  45. .stat-card .value.green { color: #28a745; }
  46. .stat-card .value.blue { color: #007bff; }
  47. .card { background: white; border-radius: 12px; padding: 24px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); margin-bottom: 20px; }
  48. .card h2 { font-size: 18px; margin-bottom: 20px; padding-bottom: 12px; border-bottom: 1px solid #eee; color: #333; }
  49. .card h3 { font-size: 15px; margin-bottom: 16px; color: #555; }
  50. table { width: 100%; border-collapse: collapse; }
  51. th, td { padding: 12px 16px; text-align: left; border-bottom: 1px solid #f0f0f0; font-size: 14px; }
  52. th { background: #fafafa; font-weight: 600; color: #555; }
  53. tr:hover { background: #f8f9ff; }
  54. .badge { display: inline-block; padding: 3px 10px; border-radius: 12px; font-size: 12px; font-weight: 600; }
  55. .badge-success { background: #e8f5e9; color: #28a745; }
  56. .badge-secondary { background: #f5f5f5; color: #888; }
  57. .section { display: none; }
  58. .section.active { display: block; }
  59. .config-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
  60. .config-grid .form-group input, .config-grid .form-group select { width: 100%; padding: 10px 14px; border: 1px solid #ddd; border-radius: 6px; font-size: 14px; }
  61. .config-grid .form-group input:focus { outline: none; border-color: #667eea; }
  62. .modal-overlay { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); z-index: 1000; justify-content: center; align-items: center; }
  63. .modal-overlay.show { display: flex; }
  64. .modal { background: white; border-radius: 12px; padding: 30px; width: 480px; max-width: 90%; }
  65. .modal h2 { margin-bottom: 24px; }
  66. .modal-actions { display: flex; justify-content: flex-end; gap: 12px; margin-top: 24px; }
  67. .toast { position: fixed; top: 80px; right: 30px; padding: 14px 24px; border-radius: 8px; color: white; font-size: 14px; font-weight: 500; z-index: 2000; animation: slideIn 0.3s ease; }
  68. .toast-success { background: #28a745; }
  69. .toast-error { background: #dc3545; }
  70. @keyframes slideIn { from { transform: translateX(100px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
  71. .actions-cell { display: flex; gap: 8px; }
  72. </style>
  73. </head>
  74. <body>
  75. <!-- Login Page -->
  76. <div class="login-container" id="loginPage">
  77. <div class="login-card">
  78. <h1><span class="icon">&#128193;</span>FTP Server 管理面板</h1>
  79. <div class="form-group">
  80. <label>用户名</label>
  81. <input type="text" id="loginUser" placeholder="请输入管理员用户名" value="admin">
  82. </div>
  83. <div class="form-group">
  84. <label>密码</label>
  85. <input type="password" id="loginPass" placeholder="请输入密码" value="">
  86. </div>
  87. <button class="btn btn-primary" onclick="doLogin()">登 录</button>
  88. <div class="error-msg" id="loginError"></div>
  89. </div>
  90. </div>
  91. <!-- Dashboard -->
  92. <div class="app-container" id="appContainer">
  93. <div class="header">
  94. <h1>&#128193; FTP Server 管理面板</h1>
  95. <div class="user-info">
  96. <span id="welcomeText">欢迎, Admin</span>
  97. <button class="btn btn-sm btn-danger" onclick="doLogout()">退出登录</button>
  98. </div>
  99. </div>
  100. <div class="sidebar">
  101. <a href="#" class="active" onclick="showSection('dashboard', this)">&#128202; 仪表盘</a>
  102. <a href="#" onclick="showSection('users', this)">&#128100; 用户管理</a>
  103. <a href="#" onclick="showSection('settings', this)">&#9881; 系统设置</a>
  104. </div>
  105. <div class="main-content">
  106. <!-- Dashboard Section -->
  107. <div class="section active" id="section-dashboard">
  108. <div class="stats-grid">
  109. <div class="stat-card">
  110. <div class="label">服务状态</div>
  111. <div class="value green">运行中</div>
  112. </div>
  113. <div class="stat-card">
  114. <div class="label">FTP 端口</div>
  115. <div class="value purple" id="statFtpPort">-</div>
  116. </div>
  117. <div class="stat-card">
  118. <div class="label">Web 端口</div>
  119. <div class="value blue" id="statWebPort">-</div>
  120. </div>
  121. <div class="stat-card">
  122. <div class="label">HTTP 文件服务</div>
  123. <div class="value" id="statHttpFilePort">-</div>
  124. </div>
  125. <div class="stat-card">
  126. <div class="label">FTP 用户数</div>
  127. <div class="value" id="statUserCount">-</div>
  128. </div>
  129. </div>
  130. <div class="card">
  131. <h2>服务器信息</h2>
  132. <table>
  133. <tr><td style="width:200px;font-weight:600;">FTP 根目录</td><td id="infoRootDir">-</td></tr>
  134. <tr><td style="font-weight:600;">被动模式端口范围</td><td id="infoPassiveRange">-</td></tr>
  135. <tr><td style="font-weight:600;">HTTP 文件服务地址</td><td id="infoHttpFileAddr">-</td></tr>
  136. <tr><td style="font-weight:600;">管理面板地址</td><td id="infoWebAddr">-</td></tr>
  137. </table>
  138. </div>
  139. </div>
  140. <!-- Users Section -->
  141. <div class="section" id="section-users">
  142. <div class="card">
  143. <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px;">
  144. <h2 style="margin:0;border:none;padding:0;">FTP 用户管理</h2>
  145. <button class="btn btn-primary" style="width:auto;background:linear-gradient(135deg,#667eea,#764ba2);" onclick="showAddUserModal()">+ 添加用户</button>
  146. </div>
  147. <table>
  148. <thead>
  149. <tr><th>用户名</th><th>主目录</th><th>写入权限</th><th>操作</th></tr>
  150. </thead>
  151. <tbody id="userTableBody">
  152. </tbody>
  153. </table>
  154. </div>
  155. </div>
  156. <!-- Settings Section -->
  157. <div class="section" id="section-settings">
  158. <div class="card">
  159. <h2>FTP 服务设置</h2>
  160. <div class="config-grid">
  161. <div class="form-group">
  162. <label>FTP 监听地址</label>
  163. <input type="text" id="cfgFtpHost">
  164. </div>
  165. <div class="form-group">
  166. <label>FTP 端口</label>
  167. <input type="number" id="cfgFtpPort">
  168. </div>
  169. <div class="form-group">
  170. <label>被动模式起始端口</label>
  171. <input type="number" id="cfgPassiveMin">
  172. </div>
  173. <div class="form-group">
  174. <label>被动模式结束端口</label>
  175. <input type="number" id="cfgPassiveMax">
  176. </div>
  177. <div class="form-group">
  178. <label>FTP 根目录</label>
  179. <input type="text" id="cfgRootDir">
  180. </div>
  181. </div>
  182. </div>
  183. <div class="card">
  184. <h2>Web 管理面板设置</h2>
  185. <div class="config-grid">
  186. <div class="form-group">
  187. <label>Web 监听地址</label>
  188. <input type="text" id="cfgWebHost">
  189. </div>
  190. <div class="form-group">
  191. <label>Web 端口</label>
  192. <input type="number" id="cfgWebPort">
  193. </div>
  194. </div>
  195. </div>
  196. <div class="card">
  197. <h2>管理员密码</h2>
  198. <div class="config-grid">
  199. <div class="form-group">
  200. <label>新密码</label>
  201. <input type="password" id="cfgAdminPass" placeholder="输入新密码">
  202. </div>
  203. <div class="form-group">
  204. <label>确认密码</label>
  205. <input type="password" id="cfgAdminPassConfirm" placeholder="再次输入新密码">
  206. </div>
  207. </div>
  208. </div>
  209. <button class="btn btn-primary" style="width:auto;padding:12px 40px;" onclick="saveSettings()">保存设置</button>
  210. </div>
  211. </div>
  212. </div>
  213. <!-- Add/Edit User Modal -->
  214. <div class="modal-overlay" id="userModal">
  215. <div class="modal">
  216. <h2 id="userModalTitle">添加 FTP 用户</h2>
  217. <input type="hidden" id="editUserName">
  218. <div class="form-group">
  219. <label>用户名</label>
  220. <input type="text" id="modalUsername" placeholder="输入用户名">
  221. </div>
  222. <div class="form-group">
  223. <label>密码</label>
  224. <input type="password" id="modalPassword" placeholder="输入密码">
  225. </div>
  226. <div class="form-group">
  227. <label>主目录</label>
  228. <input type="text" id="modalHomeDir" placeholder="例如: ./ftp_root/user1">
  229. </div>
  230. <div class="form-group">
  231. <label><input type="checkbox" id="modalWrite" checked> 允许写入</label>
  232. </div>
  233. <div class="modal-actions">
  234. <button class="btn" style="background:#eee;" onclick="closeUserModal()">取消</button>
  235. <button class="btn btn-primary" style="width:auto;" onclick="saveUser()">保存</button>
  236. </div>
  237. </div>
  238. </div>
  239. <script>
  240. let authToken = localStorage.getItem('ftp_admin_token') || '';
  241. // Check auth on load
  242. window.onload = function() {
  243. if (authToken) {
  244. checkAuth();
  245. }
  246. };
  247. function checkAuth() {
  248. fetch('/api/status', {
  249. headers: { 'Authorization': 'Bearer ' + authToken }
  250. }).then(r => {
  251. if (r.ok) {
  252. showDashboard();
  253. } else {
  254. showLogin();
  255. }
  256. }).catch(() => showLogin());
  257. }
  258. function showLogin() {
  259. document.getElementById('loginPage').style.display = 'flex';
  260. document.getElementById('appContainer').style.display = 'none';
  261. }
  262. function showDashboard() {
  263. document.getElementById('loginPage').style.display = 'none';
  264. document.getElementById('appContainer').style.display = 'block';
  265. loadStatus();
  266. loadUsers();
  267. loadConfig();
  268. }
  269. function doLogin() {
  270. const username = document.getElementById('loginUser').value;
  271. const password = document.getElementById('loginPass').value;
  272. const errorEl = document.getElementById('loginError');
  273. if (!username || !password) {
  274. errorEl.textContent = '请输入用户名和密码';
  275. return;
  276. }
  277. fetch('/api/login', {
  278. method: 'POST',
  279. headers: { 'Content-Type': 'application/json' },
  280. body: JSON.stringify({ username, password })
  281. }).then(r => r.json()).then(data => {
  282. if (data.token) {
  283. authToken = data.token;
  284. localStorage.setItem('ftp_admin_token', authToken);
  285. showDashboard();
  286. } else {
  287. errorEl.textContent = data.error || '登录失败';
  288. }
  289. }).catch(() => {
  290. errorEl.textContent = '网络错误';
  291. });
  292. }
  293. function doLogout() {
  294. fetch('/api/logout', {
  295. headers: { 'Authorization': 'Bearer ' + authToken }
  296. }).finally(() => {
  297. authToken = '';
  298. localStorage.removeItem('ftp_admin_token');
  299. showLogin();
  300. });
  301. }
  302. function loadStatus() {
  303. fetch('/api/status', {
  304. headers: { 'Authorization': 'Bearer ' + authToken }
  305. }).then(r => r.json()).then(data => {
  306. document.getElementById('statFtpPort').textContent = data.ftpPort;
  307. document.getElementById('statWebPort').textContent = data.webPort;
  308. document.getElementById('statUserCount').textContent = data.userCount;
  309. document.getElementById('infoRootDir').textContent = data.rootDir;
  310. if (data.httpFilePort) {
  311. document.getElementById('statHttpFilePort').textContent = data.httpFilePort;
  312. document.getElementById('statHttpFilePort').style.color = '#28a745';
  313. document.getElementById('infoHttpFileAddr').textContent = 'http://' + location.hostname + ':' + data.httpFilePort;
  314. } else {
  315. document.getElementById('statHttpFilePort').textContent = '未启用';
  316. document.getElementById('statHttpFilePort').style.color = '#999';
  317. document.getElementById('infoHttpFileAddr').textContent = '未启用';
  318. }
  319. });
  320. }
  321. function loadConfig() {
  322. fetch('/api/config', {
  323. headers: { 'Authorization': 'Bearer ' + authToken }
  324. }).then(r => r.json()).then(data => {
  325. document.getElementById('cfgFtpHost').value = data.ftp.host;
  326. document.getElementById('cfgFtpPort').value = data.ftp.port;
  327. document.getElementById('cfgPassiveMin').value = data.ftp.passivePortMin;
  328. document.getElementById('cfgPassiveMax').value = data.ftp.passivePortMax;
  329. document.getElementById('cfgRootDir').value = data.ftp.rootDir;
  330. document.getElementById('cfgWebHost').value = data.web.host;
  331. document.getElementById('cfgWebPort').value = data.web.port;
  332. document.getElementById('infoPassiveRange').textContent = data.ftp.passivePortMin + ' - ' + data.ftp.passivePortMax;
  333. document.getElementById('infoWebAddr').textContent = 'http://' + data.web.host + ':' + data.web.port;
  334. });
  335. }
  336. function loadUsers() {
  337. fetch('/api/users', {
  338. headers: { 'Authorization': 'Bearer ' + authToken }
  339. }).then(r => r.json()).then(users => {
  340. const tbody = document.getElementById('userTableBody');
  341. tbody.innerHTML = '';
  342. if (users && users.length > 0) {
  343. users.forEach(u => {
  344. const tr = document.createElement('tr');
  345. const writeBadge = u.write ? '<span class="badge badge-success">可写</span>' : '<span class="badge badge-secondary">只读</span>';
  346. tr.innerHTML =
  347. '<td>' + u.username + '</td>' +
  348. '<td>' + u.homeDir + '</td>' +
  349. '<td>' + writeBadge + '</td>' +
  350. '<td class="actions-cell">' +
  351. '<button class="btn btn-sm" style="background:#667eea;color:white;" onclick="editUser(\'' + u.username + '\', \'' + u.homeDir + '\', ' + u.write + ')">编辑</button>' +
  352. '<button class="btn btn-sm btn-danger" onclick="deleteUser(\'' + u.username + '\')">删除</button>' +
  353. '</td>';
  354. tbody.appendChild(tr);
  355. });
  356. } else {
  357. tbody.innerHTML = '<tr><td colspan="4" style="text-align:center;color:#888;">暂无用户</td></tr>';
  358. }
  359. });
  360. }
  361. function showSection(name, el) {
  362. document.querySelectorAll('.section').forEach(s => s.classList.remove('active'));
  363. document.getElementById('section-' + name).classList.add('active');
  364. document.querySelectorAll('.sidebar a').forEach(a => a.classList.remove('active'));
  365. el.classList.add('active');
  366. if (name === 'dashboard') { loadStatus(); loadConfig(); }
  367. if (name === 'users') { loadUsers(); }
  368. if (name === 'settings') { loadConfig(); }
  369. }
  370. function showAddUserModal() {
  371. document.getElementById('userModalTitle').textContent = '添加 FTP 用户';
  372. document.getElementById('editUserName').value = '';
  373. document.getElementById('modalUsername').value = '';
  374. document.getElementById('modalUsername').disabled = false;
  375. document.getElementById('modalPassword').value = '';
  376. document.getElementById('modalHomeDir').value = '';
  377. document.getElementById('modalWrite').checked = true;
  378. document.getElementById('userModal').classList.add('show');
  379. }
  380. function editUser(username, homeDir, write) {
  381. document.getElementById('userModalTitle').textContent = '编辑 FTP 用户';
  382. document.getElementById('editUserName').value = username;
  383. document.getElementById('modalUsername').value = username;
  384. document.getElementById('modalUsername').disabled = true;
  385. document.getElementById('modalPassword').value = '';
  386. document.getElementById('modalPassword').placeholder = '留空则不修改密码';
  387. document.getElementById('modalHomeDir').value = homeDir;
  388. document.getElementById('modalWrite').checked = write;
  389. document.getElementById('userModal').classList.add('show');
  390. }
  391. function closeUserModal() {
  392. document.getElementById('userModal').classList.remove('show');
  393. document.getElementById('modalPassword').placeholder = '输入密码';
  394. }
  395. function saveUser() {
  396. const editUser = document.getElementById('editUserName').value;
  397. const username = document.getElementById('modalUsername').value;
  398. const password = document.getElementById('modalPassword').value;
  399. const homeDir = document.getElementById('modalHomeDir').value;
  400. const write = document.getElementById('modalWrite').checked;
  401. if (!username) { showToast('请输入用户名', 'error'); return; }
  402. if (editUser) {
  403. // Update existing user
  404. const user = { username, homeDir, write };
  405. if (password) user.password = password;
  406. else {
  407. // Keep existing password - send a flag
  408. user.keepPassword = true;
  409. }
  410. fetch('/api/users/update', {
  411. method: 'POST',
  412. headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + authToken },
  413. body: JSON.stringify({ username: editUser, user })
  414. }).then(r => r.json()).then(data => {
  415. if (data.status === 'ok') {
  416. showToast('用户已更新');
  417. loadUsers();
  418. closeUserModal();
  419. } else {
  420. showToast(data.error || '更新失败', 'error');
  421. }
  422. });
  423. } else {
  424. // Add new user
  425. if (!password) { showToast('请输入密码', 'error'); return; }
  426. fetch('/api/users/add', {
  427. method: 'POST',
  428. headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + authToken },
  429. body: JSON.stringify({ username, password, homeDir, write })
  430. }).then(r => r.json()).then(data => {
  431. if (data.status === 'ok') {
  432. showToast('用户已添加');
  433. loadUsers();
  434. closeUserModal();
  435. } else {
  436. showToast(data.error || '添加失败', 'error');
  437. }
  438. });
  439. }
  440. }
  441. function deleteUser(username) {
  442. if (!confirm('确定要删除用户 "' + username + '" 吗?')) return;
  443. fetch('/api/users/delete', {
  444. method: 'POST',
  445. headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + authToken },
  446. body: JSON.stringify({ username })
  447. }).then(r => r.json()).then(data => {
  448. if (data.status === 'ok') {
  449. showToast('用户已删除');
  450. loadUsers();
  451. } else {
  452. showToast(data.error || '删除失败', 'error');
  453. }
  454. });
  455. }
  456. function saveSettings() {
  457. const adminPass = document.getElementById('cfgAdminPass').value;
  458. const adminPassConfirm = document.getElementById('cfgAdminPassConfirm').value;
  459. if (adminPass && adminPass !== adminPassConfirm) {
  460. showToast('两次密码输入不一致', 'error');
  461. return;
  462. }
  463. const update = {
  464. ftp: {
  465. host: document.getElementById('cfgFtpHost').value,
  466. port: parseInt(document.getElementById('cfgFtpPort').value),
  467. passivePortMin: parseInt(document.getElementById('cfgPassiveMin').value),
  468. passivePortMax: parseInt(document.getElementById('cfgPassiveMax').value),
  469. rootDir: document.getElementById('cfgRootDir').value
  470. },
  471. web: {
  472. host: document.getElementById('cfgWebHost').value,
  473. port: parseInt(document.getElementById('cfgWebPort').value)
  474. }
  475. };
  476. if (adminPass) {
  477. update.adminPassword = adminPass;
  478. }
  479. fetch('/api/config/update', {
  480. method: 'POST',
  481. headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + authToken },
  482. body: JSON.stringify(update)
  483. }).then(r => r.json()).then(data => {
  484. if (data.status === 'ok') {
  485. showToast('设置已保存,部分设置需重启服务生效');
  486. document.getElementById('cfgAdminPass').value = '';
  487. document.getElementById('cfgAdminPassConfirm').value = '';
  488. loadConfig();
  489. loadStatus();
  490. } else {
  491. showToast(data.error || '保存失败', 'error');
  492. }
  493. });
  494. }
  495. function showToast(msg, type) {
  496. const toast = document.createElement('div');
  497. toast.className = 'toast ' + (type === 'error' ? 'toast-error' : 'toast-success');
  498. toast.textContent = msg;
  499. document.body.appendChild(toast);
  500. setTimeout(() => toast.remove(), 3000);
  501. }
  502. // Handle Enter key on login
  503. document.getElementById('loginPass').addEventListener('keypress', function(e) {
  504. if (e.key === 'Enter') doLogin();
  505. });
  506. </script>
  507. </body>
  508. </html>
  509. `