From 0257274732a3c5cc1c86e385d4ccfe10995b6d19 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 22 Feb 2017 21:53:55 +0100 Subject: tools: add wg show [interface] dump --- src/tools/completion/wg.bash-completion | 2 +- src/tools/show.c | 44 +++++++++++++++++++++++++++++++-- src/tools/wg.8 | 10 ++++++-- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/tools/completion/wg.bash-completion b/src/tools/completion/wg.bash-completion index 8822d01..1a2e059 100644 --- a/src/tools/completion/wg.bash-completion +++ b/src/tools/completion/wg.bash-completion @@ -19,7 +19,7 @@ _wg_completion() { fi if [[ $COMP_CWORD -eq 3 && ${COMP_WORDS[1]} == show && ${COMP_WORDS[2]} != interfaces ]]; then - COMPREPLY+=( $(compgen -W "public-key private-key preshared-key listen-port peers endpoints allowed-ips fwmark latest-handshakes persistent-keepalive transfer" -- "${COMP_WORDS[3]}") ) + COMPREPLY+=( $(compgen -W "public-key private-key preshared-key listen-port peers endpoints allowed-ips fwmark latest-handshakes persistent-keepalive transfer dump" -- "${COMP_WORDS[3]}") ) return fi diff --git a/src/tools/show.c b/src/tools/show.c index 6cf2b23..7b057cf 100644 --- a/src/tools/show.c +++ b/src/tools/show.c @@ -203,7 +203,7 @@ static char *bytes(uint64_t b) static const char *COMMAND_NAME = NULL; static void show_usage(void) { - fprintf(stderr, "Usage: %s %s { | all | interfaces } [public-key | private-key | preshared-key | listen-port | fwmark | peers | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive]\n", PROG_NAME, COMMAND_NAME); + fprintf(stderr, "Usage: %s %s { | all | interfaces } [public-key | private-key | preshared-key | listen-port | fwmark | peers | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive | dump]\n", PROG_NAME, COMMAND_NAME); } static void pretty_print(struct wgdevice *device) @@ -252,6 +252,44 @@ static void pretty_print(struct wgdevice *device) } } +static void dump_print(struct wgdevice *device, bool with_interface) +{ + size_t i, j; + struct wgpeer *peer; + struct wgipmask *ipmask; + + if (with_interface) + printf("%s\t", device->interface); + printf("%s\t", key(device->private_key)); + printf("%s\t", key(device->public_key)); + printf("%s\t", key(device->preshared_key)); + printf("%u\t", device->port); + if (device->fwmark) + printf("0x%x\n", device->fwmark); + else + printf("off\n"); + for_each_wgpeer(device, peer, i) { + if (with_interface) + printf("%s\t", device->interface); + printf("%s\t", key(peer->public_key)); + if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6) + printf("%s\t", endpoint(&peer->endpoint.addr)); + else + printf("(none)\t"); + if (peer->num_ipmasks) { + for_each_wgipmask(peer, ipmask, j) + printf("%s/%u%c", ip(ipmask), ipmask->cidr, j == (size_t)peer->num_ipmasks - 1 ? '\t' : ','); + } else + printf("(none)\t"); + printf("%llu\t", (unsigned long long)peer->last_handshake_time.tv_sec); + printf("%" PRIu64 "\t%" PRIu64 "\t", (uint64_t)peer->rx_bytes, (uint64_t)peer->tx_bytes); + if (peer->persistent_keepalive_interval) + printf("%u\n", peer->persistent_keepalive_interval); + else + printf("off\n"); + } +} + static bool ugly_print(struct wgdevice *device, const char *param, bool with_interface) { size_t i, j; @@ -328,7 +366,9 @@ static bool ugly_print(struct wgdevice *device, const char *param, bool with_int printf("%s\t", device->interface); printf("%s\n", key(peer->public_key)); } - } else { + } else if (!strcmp(param, "dump")) + dump_print(device, with_interface); + else { fprintf(stderr, "Invalid parameter: `%s`\n", param); show_usage(); return false; diff --git a/src/tools/wg.8 b/src/tools/wg.8 index 2933761..b9c1f79 100644 --- a/src/tools/wg.8 +++ b/src/tools/wg.8 @@ -36,14 +36,20 @@ Sub-commands that take an INTERFACE must be passed a WireGuard interface. .SH COMMANDS .TP -\fBshow\fP { \fI\fP | \fIall\fP | \fIinterfaces\fP } [\fIpublic-key\fP | \fIprivate-key\fP | \fIpreshared-key\fP | \fIlisten-port\fP | \fIfwmark\fP | \fIpeers\fP | \fIendpoints\fP | \fIallowed-ips\fP | \fIlatest-handshakes\fP | \fIpersistent-keepalive\fP | \fItransfer\fP] +\fBshow\fP { \fI\fP | \fIall\fP | \fIinterfaces\fP } [\fIpublic-key\fP | \fIprivate-key\fP | \fIpreshared-key\fP | \fIlisten-port\fP | \fIfwmark\fP | \fIpeers\fP | \fIendpoints\fP | \fIallowed-ips\fP | \fIlatest-handshakes\fP | \fIpersistent-keepalive\fP | \fItransfer\fP | \fIdump\fP] Shows current WireGuard configuration of specified \fI\fP. If no \fI\fP is specified, \fI\fP defaults to \fIall\fP. If \fIinterfaces\fP is specified, prints a list of all WireGuard interfaces, one per line, and quit. If no options are given after the interface specification, then prints a list of all attributes in a visually pleasing way meant for the terminal. Otherwise, prints specified information grouped by -newlines and tabs, meant to be used in scripts. +newlines and tabs, meant to be used in scripts. For this script-friendly display, +if \fIall\fP is specified, then the first field for all categories of information +is the interface name. If \fPdump\fP is specified, then several lines are printed; +the first contains in order separated by tab: private-key, public-key, preshared-key, +listen-port, fwmark. Subsequent lines are printed for each peer and contain in order +separated by tab: public-key, endpoint, allowed-ips, latest-handshake, transfer-rx, +transfer-tx, persistent-keepalive. .TP \fBshowconf\fP \fI\fP Shows the current configuration of \fI\fP in the format described -- cgit v1.2.3-59-g8ed1b