Package: wireguard-tools
WireGuard-toolsPure Python reimplementation of wireguard-tools with an aim to provide easily
reusable library functions to handle reading and writing ofWireGuard®configuration files as well as
interacting with WireGuard devices, both in-kernel through the Netlink API and
userspace implementations through the cross-platform UAPI API.Installation/Usagepipxinstallwireguard-toolswg-py--helpImplementedwgcommand line functionality,show - Show configuration and device informationshowconf - Dump current device configurationset - Change current configuration, add/remove/change peerssetconf - Apply configuration to deviceaddconf - Append configuration to devicesyncconf - Synchronizes configuration with devicegenkey, genpsk, pubkey - Key generationAlso includes somewg-quickfunctions,up, down - Create and configure WireGuard device and interfacesave - Dump device and interface configurationstrip - Filter wg-quick settings from configurationNeeds root (sudo) access to query and configure the WireGuard devices through
netlink. But root doesn't know about the currently active virtualenv, you may
have to pass the full path to the script in the virtualenv, or usepython3 -m wireguard_toolssudo`whichwg-py`showconf<interface>sudo/path/to/venv/python3-mwireguard_toolsshowconf<interface>Library usageParsing WireGuard keysThe WireguardKey class will parse base64-encoded keys, the default base64
encoded string, but also an urlsafe base64 encoded variant. It also exposes
both private key generating and public key deriving functions. Be sure to pass
any base64 or hex encoded keys as 'str' and not 'bytes', otherwise it will
assume the key was already decoded to its raw form.fromwireguard_toolsimportWireguardKeyprivate_key=WireguardKey.generate()public_key=private_key.public_key()# print base64 encoded keyprint(public_key)# print urlsafe encoded keyprint(public_key.urlsafe)# print hexadecimal encoded keyprint(public_key.hex())Working with WireGuard configuration filesThe WireGuard configuration file is similar to, but not quite, the INI format
because it has duplicate keys for both section names (i.e. [Peer]) as well as
configuration keys within a section. According to the format description,
AllowedIPs, Address, and DNS configuration keys 'may be specified multiple
times'.fromwireguard_toolsimportWireguardConfigwithopen("wg0.conf")asfh:config=WireguardConfig.from_wgconfig(fh)Also supported are the "Friendly Tags" comments as introduced by
prometheus-wireguard-exporter, where a[Peer]section can contain
comments which add a user friendly description and/or additional attributes.[Peer]
# friendly_name = Peer description for end users
# friendly_json = {"flat"="json", "dictionary"=1, "attribute"=2}
...These will show up as additionalfriendly_nameandfriendly_jsonattributes
on the WireguardPeer object.We can also serialize and deserialize from a simple dict-based format which
uses only basic JSON datatypes and, as such, can be used to convert to various
formats (i.e. json, yaml, toml, pickle) either to disk or to pass over a
network.fromwireguard_toolsimportWireguardConfigfrompprintimportpprintdict_config=dict(private_key="...",peers=[dict(public_key="...",preshared_key=None,endpoint_host="remote_host",endpoint_port=5120,persistent_keepalive=30,allowed_ips=["0.0.0.0/0"],friendly_name="Awesome Peer",),],)config=WireguardConfig.from_dict(dict_config)dict_config=config.asdict()pprint(dict_config)Finally, there is ato_qrcodefunction that returns a segno.QRCode object
which contains the configuration. This can be printed and scanned with the
wireguard-android application. Careful with these because the QRcode exposes
an easily captured copy of the private key as part of the configuration file.
It is convenient, but definitely not secure.fromwireguard_toolsimportWireguardConfigfrompprintimportpprintdict_config=dict(private_key="...",peers=[dict(public_key="...",preshared_key=None,endpoint_host="remote_host",endpoint_port=5120,persistent_keepalive=30,allowed_ips=["0.0.0.0/0"],),],)config=WireguardConfig.from_dict(dict_config)qr=config.to_qrcode()qr.save("wgconfig.png")qr.terminal(compact=True)Working with WireGuard devicesfromwireguard_toolsimportWireguardDeviceifnames=[device.interfacefordeviceinWireguardDevice.list()]device=WireguardDevice.get("wg0")wgconfig=device.get_config()device.set_config(wgconfig)BugsThe setconf/syncconf implementation is not quite correct. They currently use
the same underlying set of operations but netlink-api'sset_configimplementation actually does something closer to syncconf, while the uapi-api
implementation matches setconf.This implementation has only been tested on Linux where we've only actively
used a subset of the available functionality, i.e. the common scenario is
configuring an interface only once with just a single peer.Licenseswireguard-tools is MIT licensedCopyright (c) 2022-2023 Carnegie Mellon University

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.wireguard_tools/curve25519.pywas released in the public domainCopyright Nicko van Someren, 2021. This code is released into the public domain.
https://gist.github.com/nickovs/cc3c22d15f239a2640c185035c06f8a3"WireGuard" is a registered trademark of Jason A. Donenfeld.