Skip to content

Instantly share code, notes, and snippets.

@SomajitDey
Last active January 1, 2024 18:00
Show Gist options
  • Save SomajitDey/fa8c951007b90f7fb2b3a25c25b0e5cc to your computer and use it in GitHub Desktop.
Save SomajitDey/fa8c951007b90f7fb2b3a25c25b0e5cc to your computer and use it in GitHub Desktop.
Chat using cURL and netcat or ncat. Demo with localhost.run.

Server-side

Create free ephemeral http relay using localhost.run:
port=<port> # Choose your local port that your server will listen to
file=<filename> # Choose file where <subdomain.localhost.run> will be stored

# Create tunnel in background
ssh -n -R 80:localhost:$port [email protected] -- --output json --no-inject-http-proxy-headers --no-inject-proxy-protocol-header 2>/dev/null | jq --unbuffered -r '.address' > ${file} &

# Pass the contents of $file to client
Run server:
cat <(echo -e "HTTP/1.1 200 OK\r\n") - | nc -l $port
Start texting...
For reverse pubsub / unidirectional data transfer:

Pubsub is when authenticated/priviledged user(s) publish(es) and unauthenticated users subscribe. In reverse pubsub, unautheticated users publish and authenticated/priviledged user subscribes.

echo -e "HTTP/1.1 200 OK\r\n" | nc -l $port

Client-side

Bidirectional connection with server:
hostname=<as sent by server> # Server is supposed to send client subdomain.localhost.run

curl -sf -o - -T . $hostname

# Or, you may also use nc to the same effect as follows. Note the use of CRLF instead of LF

cat <(echo -e "GET / HTTP/1.1\r\nHost:$hostname\r\n") - | nc -N $hostname 80
Start texting...

Note: Free port-forwarding at localhost.run times out at 1 hour. Then, both curl and nc will terminate. In case curl can't connect to subdomain.localhost.run because the server's tunnel has timed-out, curl will exit with non-zero exit-code (22) by virtue of the -f option.

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