Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ffbs-wireguard-respondd #141

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ffbs-wireguard-respondd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=ffbs-wireguard-respondd
PKG_VERSION:=1

PKG_MAINTAINER:=Chris Fiege <chris@tinyhost.de>
PKG_LICENSE:=MIT

include $(TOPDIR)/../package/gluon.mk

define Package/ffbs-wireguard-respondd
TITLE:=Respondd support for wireguard.
DEPENDS:=+respondd +kmod-wireguard
endef

define Package/ffbs-nodeconfig-respndd/description
Respondd support for wireguard.
endef

$(eval $(call BuildPackageGluon,ffbs-wireguard-respondd))
69 changes: 69 additions & 0 deletions ffbs-wireguard-respondd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
ffbs-wireguard-respondd
=======================

A respondd module to report basic statistics for WireGuard.

How it is used
--------------

This module can be added as a dependency in your `image-customization.lua`.
Afterwards you should be able to query `wireguard` from your `respondd`.

For a node with the following WireGuard interfaces:

```shell
root@Mhostname:~# wg
interface: wg_c1
public key: K71ZNs4FurKnHc7mrUcuBfqLIi6IacRkPjAmq1bQ0A8=
private key: (hidden)
listening port: 35239
fwmark: 0x1

peer: JQZCuxU/6RF7xbQ+hulFJ+RtyleAQ9JP5VP6S3vszlM=
endpoint: [2a01:4f8:a0:6381::2]:10000
allowed ips: 0.0.0.0/0, ::/0
latest handshake: 1 minute, 42 seconds ago
transfer: 176.93 GiB received, 12.07 GiB sent
persistent keepalive: every 15 seconds

interface: wg_c3
public key: K71ZNs4FurKnHc7mrUcuBfqLIi6IacRkPjAmq1bQ0A8=
private key: (hidden)
listening port: 32998
fwmark: 0x1

peer: Gnfc/z9FDGiBC1eAi4TcnR2p5EmpE3zJTsA7KcNOnyo=
endpoint: [2a0e:1580:1000::2dff:fe0e:e982]:10000
allowed ips: 0.0.0.0/0, ::/0
latest handshake: 32 seconds ago
transfer: 12.19 GiB received, 1.27 GiB sent
persistent keepalive: every 15 seconds
```

The response may look like this:

```shell
root@hostname:~# gluon-neighbour-info -r wireguard
{
"interfaces": {
"wg_c1": {
"peers": {
"JQZCuxU/6RF7xbQ+hulFJ+RtyleAQ9JP5VP6S3vszlM=": {
"handshake": 92,
"transfer_rx": 189973610414,
"transfer_tx": 12955824977
}
}
},
"wg_c3": {
"peers": {
"Gnfc/z9FDGiBC1eAi4TcnR2p5EmpE3zJTsA7KcNOnyo=": {
"handshake": 23,
"transfer_rx": 13097904167,
"transfer_tx": 1360334934
}
}
}
}
}
```
6 changes: 6 additions & 0 deletions ffbs-wireguard-respondd/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
all: respondd.so

CFLAGS += -Wall -fPIC

respondd.so: respondd.c wireguard.o
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -D_GNU_SOURCE -o $@ $^ $(LDLIBS)
120 changes: 120 additions & 0 deletions ffbs-wireguard-respondd/src/respondd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
on_ Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include <respondd.h>

#include <json-c/json.h>

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <sys/socket.h>
#include <sys/un.h>

#include "wireguard.h"


struct json_object * get_wireguard_handshakes()
{
char *device_names, *device_name;
size_t len;
json_object *json_val;
json_object *json_device;
json_object *json_ptr;
json_object *json_interface;
json_object *ret = json_object_new_object();

json_interface = json_object_new_object();
json_object_object_add(ret, "interfaces", json_interface);

device_names = wg_list_device_names();

if (!device_names) {
perror("Unable to get device names");
exit(1);
}

wg_for_each_device_name(device_names, device_name, len) {
json_object *json_peers;
wg_device *device;
wg_peer *peer;

if (wg_get_device(&device, device_name) < 0) {
perror("Unable to get device");
continue;
}

json_device = json_object_new_object();
json_object_object_add(json_interface, device_name, json_device);

json_peers = json_object_new_object();
json_object_object_add(json_device, "peers",
json_peers);

wg_for_each_peer(device, peer) {
wg_key_b64_string pubkey;
json_ptr = json_object_new_object();

int64_t age = (int64_t)time(NULL) - peer->last_handshake_time.tv_sec;
if (age <= 3600) {
json_val = json_object_new_int64(time(NULL) - peer->last_handshake_time.tv_sec);
json_object_object_add(json_ptr, "handshake", json_val);
}

/* Yes, these values are uint64_t, we just take the risk
* off an overflow here since JSON can only handle
* 53-bit integers according to spec anyway
*/
json_val = json_object_new_int64(peer->rx_bytes);
json_object_object_add(json_ptr, "transfer_rx", json_val);
json_val = json_object_new_int64(peer->tx_bytes);
json_object_object_add(json_ptr, "transfer_tx", json_val);

wg_key_to_base64(pubkey, peer->public_key);
json_object_object_add(json_peers, pubkey, json_ptr);
}

wg_free_device(device);
}
free(device_names);
return ret;
}

static struct json_object * respondd_wireguard_handshake(void) {
struct json_object *ret = get_wireguard_handshakes();
if (ret)
return ret;
return NULL;
}


const struct respondd_provider_info respondd_providers[] = {
{"wireguard", respondd_wireguard_handshake},
{}
};
Loading