butlerx / wetty

Terminal in browser over http/https. (Ajaxterm/Anyterm alternative, but much better)
https://butlerx.github.io/wetty
MIT License
4.36k stars 673 forks source link

Reconnect does not work #447

Closed erycson closed 11 months ago

erycson commented 1 year ago

Describe the bug Wetty does not reconnect and continues session when losing connection and reconnecting.

To Reproduce

  1. Open a new session
  2. Remove the network cable
  3. Wait a few seconds and reconnect the cable

Expected behavior I expected the session to pick up where it left off

Versions

Additional context When I log in, unplug the cable and plug in, the terminal closes instead of reconnecting and picking up where it left off

When I use chrome dev console and select option to simulate connection loss, it reconnects and works, but when I remove the network cable (or turn off wifi) it doesn't work

src/client/wetty/socket.ts

import io from 'socket.io-client';

const userRegex = new RegExp('ssh/[^/]+$');
export const trim = (str: string): string => str.replace(/\/*$/, '');

const socketBase = trim(window.location.pathname).replace(userRegex, '');
export const socket = io(window.location.origin, {
  path: `${trim(socketBase)}/socket.io`,
  timeout: 180000,
  reconnection: true,
  reconnectionDelay: 5000,
  reconnectionAttempts: 20
});

src/server/socketServer/socket.ts

import type express from 'express';
import socket from 'socket.io';
import http from 'http';
import https from 'https';
import isUndefined from 'lodash/isUndefined.js';

import { logger } from '../../shared/logger.js';
import type { SSLBuffer } from '../../shared/interfaces.js';

export const listen = (
  app: express.Express,
  host: string,
  port: number,
  path: string,
  { key, cert }: SSLBuffer,
): SocketIO.Server => {
  const server = !isUndefined(key) && !isUndefined(cert)
    ? https.createServer({ key, cert }, app).listen(port, host, () => {
      logger.info('Server started', {
        port,
        connection: 'https',
      });
    })
    : http.createServer(app).listen(port, host, () => {
      logger.info('Server started', {
        port,
        connection: 'http',
      });
    });
  server.setTimeout(180000);

  return socket(
    server,
    {
      path: `${path}/socket.io`,
      pingInterval: 190000, // 3min10s
      pingTimeout: 180000, // 3min
    },
  );
};

haproxy

defaults
    log global
    mode http
    option httplog
    timeout client 4h
    timeout connect 4h
    timeout server 4h
    timeout tunnel 4h

frontend http
    bind *:81
    option forwardfor

    acl do-wetty-ws hdr(Upgrade) -i WebSocket
    acl do-wetty-ws hdr_beg(Host) -i ws
    acl do-wetty-ws path_beg -i /wetty/socket.io
    use_backend webssh_ws if do-wetty-ws

    acl do-wetty-http path_beg -i /wetty
    use_backend webssh_http if do-wetty-http

backend webssh_http
    mode http
    server webssh_http_server webssh:3000

backend webssh_ws
    option http-server-close
    option logasap
    server webssh_ws_server webssh:3000
github-actions[bot] commented 11 months ago

Stale issue message