Sending secrets on-line

Tuesday, November 26, 2019; Reading time 2 minutes.

Yesterday evening I found a cool little service called Patchbay. The main idea is to allow a simple way to transport data point to point using HTTP. Among awesome features implemented on the page I found a really cool feature.

At work we use a homebrew app for sharing secrets online but I always wanted one for my private use and quick terminal usage. Following in this post are two functions, one for fish shell and the other for bash/zsh. This will allow you to send a simple string to a connecting user.

Every secret has a UUID generated as a URL.

Fish shell:

function send_secret
  set -l id (uuidgen)
  set -l url "https://patchbay.pub/$id"

  echo $url
  curl $url -d "$argv"
  echo "Done!"
end

Bash shell:

function send_secret() {
  local id=$(uuidgen)
  local url="https://patchbay.pub/$id"

  echo $url
  curl $url -d "$1"
  echo "Done!"
}

Usage

After you either put this in .basrc, .zshrc or your config.fish you can do cool stuff like:

$ send_secret "Passw0rd"
https://patchbay.pub/506BE069-D76E-4412-A4B8-2685D5D07975

And on the other side your firend can open the link:

$ curl https://patchbay.pub/506BE069-D76E-4412-A4B8-2685D5D07975
Passw0rd

Once the secret has been transfered your function will end.

That’s it!

My implementation

I wanted to see if I could implement it in node. Here is a working example without any dependencies!

const http = require('http');

const pubsub = {};

http
  .createServer((req, res) => {
    const publisher = pubsub[req.url];

    if (req.method === 'POST') {
      const data = [];
      req.on('data', (chunk) => data.push(chunk));
      req.on(
        'end',
        () =>
          (pubsub[req.url] = {
            body: data.toString(),
            res,
          }),
      );
      return;
    }

    if (publisher) {
      res.writeHead(200);
      res.write(publisher.body);
      publisher.res.writeHead(200);
      publisher.res.end();

      return;
    }

    res.writeHead(404);
    res.end();
  })
  .listen(1234);