Skip to main content

ENTER_JS

Constant ENTER_JS 

Source
const ENTER_JS: &str = r#"(() => {
  const msg = document.getElementById("msg");
  const phrase = document.getElementById("phrase");
  const hint = document.getElementById("hint");
  const paste = document.getElementById("paste");
  const field = document.getElementById("code");
  // Each attempt to open is a run; a new code in the fragment (a hash-only navigation in this tab) starts a new one and ends the last.
  let run = 0;
  const begin = async () => {
    const mine = ++run;
    const current = () => mine === run;
    const fail = () => {
      if (!current()) return;
      msg.textContent = "not accepted";
      phrase.hidden = true;
      // The same words for everyone, whatever went wrong: how to try again.
      hint.textContent = "Run agentmux open in the terminal again for a new link.";
      hint.hidden = false;
    };
    msg.textContent = "Opening...";
    phrase.hidden = true;
    hint.hidden = true;
    paste.hidden = true;
    const code = location.hash.slice(1);
    history.replaceState(null, "", location.pathname);
    if (!code) {
      // Nobody tried to open it: a visitor with no session, or a browser that has just been disconnected.
      msg.textContent = "This browser is not open to AgentMux.";
      hint.textContent = "To open AgentMux here, run agentmux open (or agentmux-srv open) in a terminal on the machine where AgentMux runs, as the user that started it, and paste the code it prints below if its link will not copy.";
      hint.hidden = false;
      paste.hidden = false;
      return;
    }
    const post = (path, body) =>
      fetch(path, {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify(body),
        cache: "no-store",
        credentials: "same-origin",
      });
    let entered;
    try {
      const r = await post("/auth/enter", { code });
      if (!current()) return;
      if (!r.ok) return fail();
      entered = await r.json();
    } catch {
      return fail();
    }
    if (!current()) return;
    msg.textContent = "Waiting for you to confirm in the terminal.";
    phrase.textContent = entered.phrase;
    phrase.hidden = false;
    hint.textContent = "Compare this with the terminal where you ran agentmux open. Answer y there only if the two match: that keeps another user of this computer from opening your AgentMux.";
    hint.hidden = false;
    for (;;) {
      let w;
      try {
        w = await post("/auth/wait", { handle: entered.handle });
      } catch {
        return fail();
      }
      if (!current()) return;
      if (w.status === 202) continue;
      if (!w.ok) return fail();
      const { token } = await w.json();
      if (!current()) return;
      sessionStorage.setItem("amx-session", token);
      location.replace("/");
      return;
    }
  };
  paste.addEventListener("submit", (e) => {
    e.preventDefault();
    const typed = field.value.trim();
    field.value = "";
    if (typed) location.hash = typed;
  });
  window.addEventListener("hashchange", begin);
  begin();
})();
"#;
Expand description

The code is in the fragment. The history entry loses it before anything is sent; the page then waits for the person’s answer in the terminal and keeps the token in sessionStorage only. A page opened with no code (a visitor with no session, or a browser that was disconnected) says how to open AgentMux and is no refusal: it is the same words for everyone and tells nothing about the server.