login.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. max-height: 10rem;
  69. overflow-y: auto;
  70. }
  71. .auth-container .warn {
  72. color: orange;
  73. }
  74. .auth-container .error {
  75. color: red;
  76. }
  77. </style>
  78. </head>
  79. <body>
  80. <form id="auth" class="loading">
  81. <div class="auth-container">
  82. <h1>PageOS 登录</h1>
  83. <span class="load">正加载</span>
  84. <div class="auth-interface">
  85. <label class="prompt">
  86. <div>用户名:</div>
  87. </label>
  88. <input
  89. id="input"
  90. type="text"
  91. placeholder="请输入用户名"
  92. autocomplete="username"
  93. />
  94. <button type="submit" class="submit-btn">提交</button>
  95. </div>
  96. <details>
  97. <summary>高级</summary>
  98. <p>环境变量:</p>
  99. <input
  100. id="session-env"
  101. type="text"
  102. placeholder="LANG=zh_CN.UTF-8"
  103. autocomplete="session-env"
  104. />
  105. <p>启动命令:</p>
  106. <input
  107. id="session-cmd"
  108. type="text"
  109. value="%SESSION_COMMAND%"
  110. placeholder="pageos-core --command 'cage -s -- firefox --kiosk --no-remote http://127.0.0.1:12800'"
  111. autocomplete="session-cmd"
  112. />
  113. <p>日志:</p>
  114. <div class="log"></div>
  115. </details>
  116. </div>
  117. </form>
  118. <script>
  119. (function () {
  120. // 获取表单元素
  121. const auth = document.getElementById("auth");
  122. const loadElm = auth.querySelector(".auth-container span.load");
  123. const ai = auth.querySelector(".auth-container .auth-interface");
  124. const promptElm = auth.querySelector(".auth-container label.prompt");
  125. const inputElm = auth.querySelector(".auth-container input#input");
  126. const submitBtn = auth.querySelector(".auth-container .submit-btn");
  127. const statusLog = auth.querySelector(".auth-container .log");
  128. // 显示加载状态
  129. function showLoading(message = "正加载") {
  130. loadElm.textContent = message;
  131. auth.classList.add("loading");
  132. }
  133. // 隐藏加载状态
  134. function hideLoading() {
  135. auth.classList.remove("loading");
  136. inputElm.focus();
  137. }
  138. // 接口和状态
  139. const ws = new WebSocket(`ws://${window.location.host}/ws`);
  140. var currentState = "username"; // username: 未创建 greetd 会话, auth: 在会话中验证
  141. hideLoading();
  142. // 显示状态信息
  143. function logMessage(msg, type = "info") {
  144. const time = new Date().toLocaleTimeString();
  145. const div = document.createElement("div");
  146. div.textContent = `${time}:${msg}`;
  147. if (type === "error") div.className = "error";
  148. statusLog.appendChild(div);
  149. statusLog.scrollTop = statusLog.scrollHeight;
  150. }
  151. // 新增一条提示信息
  152. function addPrompt(text, type = "info") {
  153. const message = document.createElement("div");
  154. message.textContent = text;
  155. message.className = type;
  156. promptElm.appendChild(message);
  157. promptElm.scrollTop = promptElm.scrollHeight;
  158. }
  159. // 清空提示信息
  160. function clearPrompt() {
  161. promptElm.innerHTML = "";
  162. }
  163. // 设定页面文字
  164. function inerface_text(
  165. prompt = "用户名:",
  166. type = "info",
  167. input_type = "text",
  168. placeholder = "请输入用户名"
  169. ) {
  170. addPrompt(prompt, type);
  171. inputElm.type = input_type;
  172. inputElm.placeholder = placeholder;
  173. inputElm.value = "";
  174. hideLoading();
  175. }
  176. // 重置表单
  177. function resetForm() {
  178. clearPrompt();
  179. inerface_text();
  180. hideLoading();
  181. currentState = "username";
  182. }
  183. // 连接
  184. ws.onopen = () => {
  185. logMessage("已连接到服务器");
  186. };
  187. // 断开链接
  188. ws.onclose = () => {
  189. logMessage("连接已关闭", "warn");
  190. resetForm();
  191. };
  192. // 连接出错
  193. ws.onerror = (err) => {
  194. logMessage(`连接错误:${err}`, "error");
  195. };
  196. // 接收消息
  197. ws.onmessage = (event) => {
  198. try {
  199. const msg = JSON.parse(event.data);
  200. logMessage(`收到:${JSON.stringify(msg)}`);
  201. switch (msg.type) {
  202. case "AUTH_PROMPT":
  203. currentState = "auth";
  204. if (msg.message_type === "SECRET") {
  205. inerface_text(msg.message, "info", "password", "请输入密钥");
  206. } else if (msg.message_type === "VISIBLE") {
  207. currentState = "auth";
  208. inerface_text(msg.message, "info", "text", "请输入文本");
  209. } else if (msg.message_type === "INFO") {
  210. currentState = "auth";
  211. addPrompt(msg.message);
  212. } else if (msg.message_type === "ERROR") {
  213. logMessage(`✖ greetd 错误:${msg.message}`, "error");
  214. addPrompt(msg.message, "error");
  215. }
  216. break;
  217. case "AUTH_SUCCESS":
  218. logMessage("✔ 登录成功! 正在启动会话……");
  219. showLoading("登录成功,正启动会话");
  220. const envStr = document.getElementById("session-env").value;
  221. const cmdStr = document.getElementById("session-cmd").value;
  222. // 将命令字符串分割为参数数组,处理带引号的参数
  223. const cmd =
  224. cmdStr
  225. .match(/[^\s"']+|"([^"]*)"|'([^']*)'/g)
  226. ?.map((arg) => arg.replace(/^["']|["']$/g, "")) || [];
  227. // 将环境变量字符串分割为键值对数组
  228. const env =
  229. envStr
  230. .match(/[^\s"']+|"([^"]*)"|'([^']*)'/g)
  231. ?.map((arg) => arg.replace(/^["']|["']$/g, "")) || [];
  232. ws.send(JSON.stringify({ type: "START_SESSION", cmd, env }));
  233. break;
  234. case "AUTH_ERROR":
  235. logMessage(`✖ 错误:${msg.reason}`, "error");
  236. resetForm();
  237. break;
  238. }
  239. hideLoading();
  240. } catch (e) {
  241. logMessage(`消息解析错误:${e}`, "error");
  242. addPrompt(`消息解析错误:${e}`, "error");
  243. }
  244. };
  245. document.getElementById("auth").addEventListener("submit", (e) => {
  246. e.preventDefault();
  247. console.log("表单提交已触发");
  248. if (currentState === "username") {
  249. const username = inputElm.value.trim();
  250. if (!username) {
  251. logMessage("请输入用户名", "warn");
  252. addPrompt("请输入用户名", "warn");
  253. return;
  254. }
  255. showLoading();
  256. ws.send(JSON.stringify({ type: "AUTH_REQUEST", username }));
  257. } else {
  258. const response = inputElm.value;
  259. if (!response) {
  260. logMessage("请输入信息", "warn");
  261. addPrompt("请输入信息", "warn");
  262. return;
  263. }
  264. showLoading();
  265. ws.send(JSON.stringify({ type: "AUTH_RESPONSE", response }));
  266. }
  267. });
  268. })();
  269. </script>
  270. </body>
  271. </html>