async validateX11Environment()

in src/core/swa-cli-persistence-plugin/impl/credentials-store.ts [224:282]


  async validateX11Environment() {
    if (!isWSL()) {
      // we assume that if we're not on WSL, we're on a sane environment
      // that has a valid X11 environment.
      return;
    }

    const { DISPLAY, WAYLAND_DISPLAY, MIR_SOCKET, WAYLAND_SOCKET } = process.env;
    let x11Host = DISPLAY || WAYLAND_DISPLAY || MIR_SOCKET || WAYLAND_SOCKET;

    const printX11ErrorAndExit = () =>
      logger.error(
        `An X11 server is required when keychain is enabled. You can disable keychain using --no-use-keychain or try a different login method.`,
        true
      );

    if (!x11Host) {
      logger.error(`Environment variable DISPLAY is not set.`);
      printX11ErrorAndExit();
    } else {
      logger.silly("X11 is set: " + x11Host);

      // An X11 address can be one of the following:
      //   - hostname:D.S means screen S on display D of host hostname; the X server for this display is listening at TCP port 6000+D.
      //   - host/unix:D.S means screen S on display D of host host; the X server for this display is listening at UNIX domain socket /tmp/.X11-unix/XD
      //     (so it's only reachable from host).
      //   - :D.S is equivalent to host/unix:D.S, where host is the local hostname.

      let [x11Hostname, x11Display] = x11Host.split(":");

      let x11Port = 6000;
      if (x11Display) {
        let [display, _screen] = x11Display.split(".");
        x11Port += parseInt(display, 10);
      }

      logger.silly("X11 hostname: " + x11Hostname);

      if (isValidIpAddress(x11Hostname)) {
        logger.silly("X11 has a valid IP address");
      } else {
        x11Hostname = os.hostname();
        logger.silly("X11 value is not a valid hostname. Forcing X11 host name to " + x11Hostname);
      }

      logger.silly(`checking if X11 host ${x11Hostname}:${x11Port} is reachable. This may take a few seconds...`);

      try {
        await waitOn({
          resources: ["tcp:" + x11Hostname + ":" + x11Port],
          delay: 5000, // 5 seconds
          timeout: 10000, // 10 seconds
        });
      } catch (error) {
        logger.error(`X11 host ${x11Hostname}:${x11Port} is not reachable.`);
        printX11ErrorAndExit();
      }
    }
  }