Wireguard: friendly peernames on cli

hi,

this is a long pending request on the kernel mailing list here and with patches here but not landed yet.

since we already have userfriendly peernames in the config, it would be nice to have a small script that brings that together with wg-quick. similar to what FlyveHest has done here. on call something like this is put out:

peer: 0123456789abcdef0123456789abcdef0123456789a=
friendly name: A friendly peer name
endpoint: 127.0.0.1:12345
allowed ips: 127.0.0.2/32
latest handshake: 2 hours, 4 minutes, 15 seconds ago
transfer: 84.60 MiB received, 94.05 MiB sent

any thoughs on integration?

This works for me:

import { cursor } from 'uci';
import { fdopen } from 'fs';

function get_key_name() {
  let ctx = cursor();
  let d = {};
  ctx.foreach('network', 'interface',
    function (intf) {
      if (intf["proto"] == 'wireguard') {
        ctx.foreach('network', `wireguard_${intf[".name"]}`,
          function (peer) {
            d[peer["public_key"]] = peer["description"] || "(absent)";
          }
        )
      }
    }
  );
  return d;
}

const stdin = fdopen(0, 'r');
let buf = stdin.read(65536);
d = get_key_name();
for (key in d) {
  buf = replace(buf, key, `${key} / ${d[key]}`);
};
print(buf);

hi

this was nice input!

i took your idea and enhanced it to the point that it imitates wg fully and prints the friendly names of the links.

i created a github project for it here: OpenWRT-Wireguard-friendly-cli-peernames

i hope this is ok for you.

maybe there should be a openwrt package with it? also a pr to the packages would be nice?

It's ok. Your popen() and the coloring are very nice.