Skip to content

Instantly share code, notes, and snippets.

@realyukii
Last active April 11, 2024 09:57
Show Gist options
  • Save realyukii/e0feea6d8fbf1b0963bab570e5828d57 to your computer and use it in GitHub Desktop.
Save realyukii/e0feea6d8fbf1b0963bab570e5828d57 to your computer and use it in GitHub Desktop.
gwrok keep-alive
const net = require('node:net');
function keepAlive(ephPort, intervalId) {
const socket = new net.Socket();
socket.setTimeout(1000);
socket.connect(port, process.env.GWROK_IP, () => socket.destroy());
// stop the interval, indicate either the gwrok server no longer accept request
// or just bad internet connection and should retry if this is the case.
// for now we not perform retry, just stop the interval.
socket.on('timeout', () => {
clearInterval(intervalId);
});
}
const intervalId = setInterval(
() => keepAlive(ephPort, intervalId),
1000 * process.env.KEEPALIVE_TIMEOUT
);
#!/bin/sh
# The script will prevent the gwrok client from closing the connection after 5 minutes of inactivity.
# usage:
# - start gwrok client first:
# setsid ./gwrok client <client parameter> &>/tmp/gwrok-http.log </dev/null
# - start the keep-alive:
# setsid ./keep-alive.sh <ip> <port> &>/tmp/gwrok-http.log </dev/null
while :;do printf 'GET / HTTP/1.1\r\nHost: $1\r\n\r\n' | nc $1 $2;sleep 4m;done
@realyukii
Copy link
Author

realyukii commented Mar 22, 2024

references:

  • the clean version of >file 2&>1 - man bash and locate the &> string.
  • setting keep-alive in both side, apache and telnet - serverfault
  • run script as daemon, supress stdin, stderr, and stdout in order to completely detach process from the shell - stackoverflow
  • nohup, setsid, disown % - stackoverflow, related to persisting running background program (daemon)
  • superuser

@realyukii
Copy link
Author

realyukii commented Mar 23, 2024

for keep-alive ssh service: while :;do nc -d -N <ip> <port>;sleep 5;done

-d: disable stdin
-N: immediately exit after connect

@realyukii
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment