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,
  33. .auth-container {
  34. margin: 1rem;
  35. color: black;
  36. background-color: white;
  37. display: flex;
  38. flex-direction: column;
  39. gap: 0.5rem;
  40. border: 0.1rem solid black;
  41. padding: 1rem;
  42. }
  43. .auth-container .auth-interface {
  44. display: flex;
  45. flex-direction: column;
  46. gap: 0.5rem;
  47. }
  48. .loading .auth-interface {
  49. display: none;
  50. }
  51. .auth-container input {
  52. box-sizing: border-box;
  53. width: 100%;
  54. }
  55. .auth-container .load {
  56. display: none;
  57. color: white;
  58. text-align: center;
  59. padding: 0.2rem;
  60. background-color: grey;
  61. }
  62. .loading .load {
  63. display: block;
  64. }
  65. .auth-container .log {
  66. border: 0.1rem solid black;
  67. padding: 0.5rem;
  68. font-size: smaller;
  69. max-height: 10rem;
  70. overflow-y: auto;
  71. }
  72. .auth-container .warn {
  73. color: orange;
  74. }
  75. .auth-container .error {
  76. color: red;
  77. }
  78. </style>
  79. </head>
  80. <body>
  81. <form id="auth" class="auth-container loading">
  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. </form>
  117. <script>
  118. (function () {
  119. // 获取表单元素
  120. const auth = document.getElementById("auth");
  121. const loadElm = auth.querySelector(".auth-container span.load");
  122. const ai = auth.querySelector(".auth-container .auth-interface");
  123. const promptElm = auth.querySelector(".auth-container label.prompt");
  124. const inputElm = auth.querySelector(".auth-container input#input");
  125. const submitBtn = auth.querySelector(".auth-container .submit-btn");
  126. const statusLog = auth.querySelector(".auth-container .log");
  127. // 显示加载状态
  128. function showLoading(message = "正加载") {
  129. loadElm.textContent = message;
  130. auth.classList.add("loading");
  131. }
  132. // 隐藏加载状态
  133. function hideLoading() {
  134. auth.classList.remove("loading");
  135. inputElm.focus();
  136. }
  137. // 接口和状态
  138. const ws = new WebSocket(`ws://${window.location.host}/ws`);
  139. var currentState = "username"; // username: 未创建 greetd 会话, auth: 在会话中验证
  140. hideLoading();
  141. // 显示状态信息
  142. function logMessage(msg, type = "info") {
  143. const time = new Date().toLocaleTimeString();
  144. const div = document.createElement("div");
  145. div.textContent = `${time}:${msg}`;
  146. if (type === "error") div.className = "error";
  147. statusLog.appendChild(div);
  148. statusLog.scrollTop = statusLog.scrollHeight;
  149. }
  150. // 新增一条提示信息
  151. function addPrompt(text, type = "info") {
  152. const message = document.createElement("div");
  153. message.textContent = text;
  154. message.className = type;
  155. promptElm.appendChild(message);
  156. promptElm.scrollTop = promptElm.scrollHeight;
  157. }
  158. // 清空提示信息
  159. function clearPrompt() {
  160. promptElm.innerHTML = "";
  161. }
  162. // 设定页面文字
  163. function interface_text(
  164. prompt = "用户名:",
  165. type = "info",
  166. input_type = "text",
  167. placeholder = "请输入用户名"
  168. ) {
  169. addPrompt(prompt, type);
  170. inputElm.type = input_type;
  171. inputElm.placeholder = placeholder;
  172. inputElm.value = "";
  173. hideLoading();
  174. }
  175. // 重置表单
  176. function resetForm() {
  177. clearPrompt();
  178. interface_text();
  179. hideLoading();
  180. currentState = "username";
  181. }
  182. // 连接
  183. ws.onopen = () => {
  184. logMessage("已连接到服务器");
  185. };
  186. // 断开链接
  187. ws.onclose = () => {
  188. logMessage("连接已关闭", "warn");
  189. resetForm();
  190. };
  191. // 连接出错
  192. ws.onerror = (err) => {
  193. logMessage(`连接错误:${err}`, "error");
  194. };
  195. // 接收消息
  196. ws.onmessage = (event) => {
  197. try {
  198. const msg = JSON.parse(event.data);
  199. logMessage(`收到:${JSON.stringify(msg)}`);
  200. switch (msg.type) {
  201. case "AUTH_PROMPT":
  202. currentState = "auth";
  203. if (msg.message_type === "SECRET") {
  204. interface_text(msg.message, "info", "password", "请输入密钥");
  205. } else if (msg.message_type === "VISIBLE") {
  206. currentState = "auth";
  207. interface_text(msg.message, "info", "text", "请输入文本");
  208. } else if (msg.message_type === "INFO") {
  209. currentState = "auth";
  210. addPrompt(msg.message);
  211. } else if (msg.message_type === "ERROR") {
  212. logMessage(`✖ greetd 错误:${msg.message}`, "error");
  213. addPrompt(msg.message, "error");
  214. }
  215. break;
  216. case "AUTH_SUCCESS":
  217. logMessage("✔ 登录成功! 正在启动会话……");
  218. showLoading("登录成功,正启动会话");
  219. const envStr = document.getElementById("session-env").value;
  220. const cmdStr = document.getElementById("session-cmd").value;
  221. // 将命令字符串分割为参数数组,处理带引号的参数
  222. const cmd =
  223. cmdStr
  224. .match(/[^\s"']+|"([^"]*)"|'([^']*)'/g)
  225. ?.map((arg) => arg.replace(/^["']|["']$/g, "")) || [];
  226. // 将环境变量字符串分割为键值对数组
  227. const env =
  228. envStr
  229. .match(/[^\s"']+|"([^"]*)"|'([^']*)'/g)
  230. ?.map((arg) => arg.replace(/^["']|["']$/g, "")) || [];
  231. ws.send(JSON.stringify({ type: "START_SESSION", cmd, env }));
  232. break;
  233. case "AUTH_ERROR":
  234. logMessage(`✖ 错误:${msg.reason}`, "error");
  235. resetForm();
  236. break;
  237. }
  238. hideLoading();
  239. } catch (e) {
  240. logMessage(`消息解析错误:${e}`, "error");
  241. addPrompt(`消息解析错误:${e}`, "error");
  242. }
  243. };
  244. document.getElementById("auth").addEventListener("submit", (e) => {
  245. e.preventDefault();
  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>