GithubHelp home page GithubHelp logo

wireguard-tools's Introduction

WireGuard-tools

Pure Python reimplementation of wireguard-tools with an aim to provide easily reusable library functions to handle reading and writing of WireGuard® 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/Usage

    pipx install wireguard-tools
    wg-py --help

Implemented wg command line functionality,

  • show - Show configuration and device information
  • showconf - Dump current device configuration
  • set - Change current configuration, add/remove/change peers
  • setconf - Apply configuration to device
  • addconf - Append configuration to device
  • syncconf - Synchronizes configuration with device
  • genkey, genpsk, pubkey - Key generation

Also includes some wg-quick functions,

  • up, down - Create and configure WireGuard device and interface
  • save - Dump device and interface configuration
  • strip - Filter wg-quick settings from configuration

Needs 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 use python3 -m wireguard_tools

    sudo `which wg-py` showconf <interface>
    sudo /path/to/venv/python3 -m wireguard_tools showconf <interface>

Library usage

Parsing WireGuard keys

The 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.

from wireguard_tools import WireguardKey

private_key = WireguardKey.generate()
public_key = private_key.public_key()

# print base64 encoded key
print(public_key)

# print urlsafe encoded key
print(public_key.urlsafe)

# print hexadecimal encoded key
print(public_key.hex())

Working with WireGuard configuration files

The 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'.

from wireguard_tools import WireguardConfig

with open("wg0.conf") as fh:
    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 additional friendly_name and friendly_json attributes 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.

from wireguard_tools import WireguardConfig
from pprint import pprint

dict_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 a to_qrcode function 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.

from wireguard_tools import WireguardConfig
from pprint import pprint

dict_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 devices

from wireguard_tools import WireguardDevice

ifnames = [device.interface for device in WireguardDevice.list()]

device = WireguardDevice.get("wg0")

wgconfig = device.get_config()

device.set_config(wgconfig)

Bugs

The setconf/syncconf implementation is not quite correct. They currently use the same underlying set of operations but netlink-api's set_config implementation 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.

Licenses

wireguard-tools is MIT licensed

Copyright (c) 2022-2024 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.py was released in the public domain

Copyright 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.

wireguard-tools's People

Contributors

fbct avatar jaharkes avatar pzverr avatar shizacat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wireguard-tools's Issues

Add missing wg-quick config fields for Table and SaveConfig

There are two wg-quick specific config fields that are missing from this library currently, which are Table and SaveConfig. These can be found in the man page for wg-quick here. The valid values for Table are Off and Auto (the default if left blank), and SaveConfig can be true or false (the default if left blank).

[bug] Setting AllowedIPs results in invalid configuration

@jaharkes Found an interesting bug that's not covered in the unit-tests. Updating the AllowedIPs results in a config file with 2 entries for "AllowedIPs". Something is not right with the conversion between a list and the string fields.

from wireguard_tools import WireguardConfig

config = None

with open("wg0.conf") as fh:
    config = WireguardConfig.from_wgconfig(fh)

for _, peer in config.peers.items():
    peer.allowed_ips = [IPv4Interface("192.168.0.0/16"), IPv4Interface("10.168.0.0/16")]

print(config.to_wgconfig(wgquick_format=True))

The printed config will have 2 entries for AllowedIPs:

AllowedIPs = "192.168.0.0/16"
AllowedIPs = "10.168.0.0/16"

Instead it should be:

AllowedIPs = "192.168.0.0/16,10.168.0.0/16"

Up / Down

Hi,

Is it planned to be able to mount a Wireguard interface or unmount it ? (Up/Down)

Thanks

[FR] Add support for friendly_name (and optionally friendly_json)

Some wireguard monitoring tools (notably prometheus-wireguard-exporter) support a friendly_name comment in the peer section of a wireguard configuration file that is used name the corresponding peer in exported metrics. In addition to that there's the even more non standard friendly_json comment that allows for arbitrary json tags.

A "generic" implementation that allows for setting one or more generic comments under a [Peer] section would also work fine for me personally.

My use case is using wireguard-tools in a wg config generator I'm writing for a Capture The Flag platform I'm working on :)

Example config file (taken from here):

[Peer]
# friendly_name = frcognowin10
PublicKey = lqYcojJMsIZXMUw1heAFbQHBoKjCEaeo7M1WXDh/KWc=
AllowedIPs = 10.70.0.40/32

Example with friendly_json (taken from here):

[Peer]
# friendly_json={"id":482217555,"username":"DrProxyMeCoordinator", "first_name": "Coordinator", "last_name": "DrProxy.me" ,"auth_date":1614869789}
PublicKey = L2UoJZN7RmEKsMmqaJgKG0m1S2Zs2wd2ptAf+kb3008=
AllowedIPs = 10.70.0.4/32

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.