login.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <!DOCTYPE html>
  2. <html lang="zh-Hans-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>PageOS 登录管理器</title>
  7. <link
  8. rel="icon"
  9. type="image/svg+xml"
  10. href="data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI1MCIgaGVpZ2h0PSI1MCI+PGNpcmNsZSBjeD0iMjUiIGN5PSIyNSIgcj0iMjAiIGZpbGw9ImdyZXkiIC8+PC9zdmc+"
  11. />
  12. <style>
  13. :root,
  14. body {
  15. width: 100%;
  16. height: 100%;
  17. background-color: gray;
  18. display: flex;
  19. align-items: center;
  20. justify-content: center;
  21. }
  22. h1 {
  23. font-size: xx-large;
  24. }
  25. summary {
  26. text-decoration: underline;
  27. }
  28. button,
  29. summary {
  30. cursor: pointer;
  31. }
  32. .auth-container {
  33. margin: 1rem;
  34. color: black;
  35. background-color: white;
  36. display: flex;
  37. flex-direction: column;
  38. gap: 0.5rem;
  39. border: 0.1rem solid black;
  40. padding: 1rem;
  41. }
  42. .auth-container .auth-interface {
  43. display: flex;
  44. flex-direction: column;
  45. gap: 0.5rem;
  46. }
  47. .loading .auth-interface {
  48. display: none;
  49. }
  50. .auth-container input {
  51. box-sizing: border-box;
  52. width: 100%;
  53. }
  54. .auth-container .load {
  55. display: none;
  56. color: white;
  57. text-align: center;
  58. padding: 0.2rem;
  59. background-color: grey;
  60. }
  61. .loading .load {
  62. display: block;
  63. }
  64. .auth-container .log {
  65. border: 0.1rem solid black;
  66. padding: 0.5rem;
  67. font-size: smaller;
  68. }
  69. .auth-container .warn {
  70. color: yellow;
  71. }
  72. .auth-container .error {
  73. color: red;
  74. }
  75. </style>
  76. </head>
  77. <body>
  78. <form id="auth" class="loading">
  79. <div class="auth-container">
  80. <h1>PageOS 登录</h1>
  81. <span class="load">正加载</span>
  82. <div class="auth-interface">
  83. <label class="prompt">
  84. <div>用户名:</div>
  85. </label>
  86. <input
  87. id="input"
  88. type="text"
  89. placeholder="请输入用户名"
  90. autocomplete="username"
  91. />
  92. <button type="submit" class="submit-btn">提交</button>
  93. </div>
  94. <details>
  95. <summary>高级</summary>
  96. <p>环境变量:</p>
  97. <input
  98. id="session-env"
  99. type="text"
  100. placeholder="LANG=zh_CN.UTF-8"
  101. autocomplete="session-env"
  102. />
  103. <p>启动命令:</p>
  104. <input
  105. id="session-cmd"
  106. type="text"
  107. value="%SESSION_COMMAND%"
  108. placeholder="pageos-core --command 'cage -s -- firefox --kiosk --no-remote http://127.0.0.1:12800'"
  109. autocomplete="session-cmd"
  110. />
  111. <p>日志:</p>
  112. <div class="log"></div>
  113. </details>
  114. </div>
  115. </form>
  116. <script>
  117. (function () {
  118. // 获取表单元素
  119. const auth = document.getElementById("auth");
  120. const loadElm = auth.querySelector(".auth-container span.load");
  121. const ai = auth.querySelector(".auth-container .auth-interface");
  122. const promptElm = auth.querySelector(".auth-container label.prompt");
  123. const inputElm = auth.querySelector(".auth-container input#input");
  124. const submitBtn = auth.querySelector(".auth-container .submit-btn");
  125. const statusLog = auth.querySelector(".auth-container .log");
  126. // 显示加载状态
  127. function showLoading(message = "正加载") {
  128. loadElm.textContent = message;
  129. auth.classList.add("loading");
  130. }
  131. // 隐藏加载状态
  132. function hideLoading() {
  133. auth.classList.remove("loading");
  134. inputElm.focus();
  135. }
  136. // 接口和状态
  137. const ws = new WebSocket(`ws://${window.location.host}/ws`);
  138. var currentState = "username"; // username: 未创建 greetd 会话, auth: 在会话中验证
  139. hideLoading();
  140. // 显示状态信息
  141. function logMessage(msg, type = "info") {
  142. const time = new Date().toLocaleTimeString();
  143. const div = document.createElement("div");
  144. div.textContent = `${time}:${msg}`;
  145. if (type === "error") div.className = "error";
  146. statusLog.appendChild(div);
  147. statusLog.scrollTop = statusLog.scrollHeight;
  148. }
  149. // 新增一条提示信息
  150. function addPrompt(text, type = "info") {
  151. const message = document.createElement("div");
  152. message.textContent = text;
  153. message.className = type;
  154. promptElm.appendChild(message);
  155. promptElm.scrollTop = promptElm.scrollHeight;
  156. }
  157. // 清空提示信息
  158. function clearPrompt() {
  159. promptElm.innerHTML = "";
  160. }
  161. // 设定页面文字
  162. function inerface_text(
  163. prompt = "用户名:",
  164. type = "info",
  165. input_type = "text",
  166. placeholder = "请输入用户名"
  167. ) {
  168. addPrompt(prompt, type);
  169. inputElm.type = input_type;
  170. inputElm.placeholder = placeholder;
  171. inputElm.value = "";
  172. hideLoading();
  173. }
  174. // 重置表单
  175. function resetForm() {
  176. clearPrompt();
  177. inerface_text();
  178. hideLoading();
  179. currentState = "username";
  180. }
  181. // 连接
  182. ws.onopen = () => {
  183. logMessage("已连接到服务器");
  184. };
  185. // 断开链接
  186. ws.onclose = () => {
  187. logMessage("连接已关闭", "warn");
  188. resetForm();
  189. };
  190. // 连接出错
  191. ws.onerror = (err) => {
  192. logMessage(`连接错误:${err}`, "error");
  193. };
  194. // 接收消息
  195. ws.onmessage = (event) => {
  196. try {
  197. const msg = JSON.parse(event.data);
  198. logMessage(`收到:${JSON.stringify(msg)}`);
  199. switch (msg.type) {
  200. case "AUTH_PROMPT":
  201. currentState = "auth";
  202. if (msg.message_type === "SECRET") {
  203. inerface_text(msg.message, "info", "password", "请输入密钥");
  204. } else if (msg.message_type === "VISIBLE") {
  205. currentState = "auth";
  206. inerface_text(msg.message, "info", "text", "请输入文本");
  207. } else if (msg.message_type === "INFO") {
  208. currentState = "auth";
  209. addPrompt(msg.message);
  210. } else if (msg.message_type === "ERROR") {
  211. logMessage(`✖ greetd 错误:${msg.message}`, "error");
  212. addPrompt(msg.message, "error");
  213. }
  214. break;
  215. case "AUTH_SUCCESS":
  216. logMessage("✔ 登录成功! 正在启动会话……");
  217. showLoading("登录成功,正启动会话");
  218. const envStr = document.getElementById("session-env").value;
  219. const cmdStr = document.getElementById("session-cmd").value;
  220. // 将命令字符串分割为参数数组,处理带引号的参数
  221. const cmd =
  222. cmdStr
  223. .match(/[^\s"']+|"([^"]*)"|'([^']*)'/g)
  224. ?.map((arg) => arg.replace(/^["']|["']$/g, "")) || [];
  225. // 将环境变量字符串分割为键值对数组
  226. const env =
  227. envStr
  228. .match(/[^\s"']+|"([^"]*)"|'([^']*)'/g)
  229. ?.map((arg) => arg.replace(/^["']|["']$/g, "")) || [];
  230. ws.send(JSON.stringify({ type: "START_SESSION", cmd, env }));
  231. break;
  232. case "AUTH_ERROR":
  233. logMessage(`✖ 错误:${msg.reason}`, "error");
  234. resetForm();
  235. break;
  236. }
  237. hideLoading();
  238. } catch (e) {
  239. logMessage(`消息解析错误:${e}`, "error");
  240. addPrompt(`消息解析错误:${e}`, "error");
  241. }
  242. };
  243. document.getElementById("auth").addEventListener("submit", (e) => {
  244. e.preventDefault();
  245. console.log("表单提交已触发");
  246. if (currentState === "username") {
  247. const username = inputElm.value.trim();
  248. if (!username) {
  249. logMessage("请输入用户名", "warn");
  250. addPrompt("请输入用户名", "warn");
  251. return;
  252. }
  253. showLoading();
  254. ws.send(JSON.stringify({ type: "AUTH_REQUEST", username }));
  255. } else {
  256. const response = inputElm.value;
  257. if (!response) {
  258. logMessage("请输入信息", "warn");
  259. addPrompt("请输入信息", "warn");
  260. return;
  261. }
  262. showLoading();
  263. ws.send(JSON.stringify({ type: "AUTH_RESPONSE", response }));
  264. }
  265. });
  266. })();
  267. </script>
  268. </body>
  269. </html>