Skip to main content

A python wrapper for controlling Wireguard

Project description

Python Wireguard interface

Library for controlling Wireguard using python.

security badge test badge linter badge package badge

Installation

To install this package, use pip:

pip install python_wireguard

Security remark

Changing network interface settings, and interacting with Wireguard, is only possible as the root user by default.

Usage

This package was designed with a client/server infrastructure in mind. This differs from the 'default' usage of Wireguard, which is peer to peer. Because of this, there is a different set of functions required depending on whether you are writing client-side code or server-side code. We will now discuss an example workflow for setting up a client-server connection to Wireguard.

Generate key pair

Both the client and the server need a key pair.

from python_wireguard import Key
private, public = Key.key_pair()
public
# <python_wireguard.Key object: 5TxYUa403l9A9yEVMyIsSZwae4C7497IT8uaMYEdLHQ=>
print(public)
# 5TxYUa403l9A9yEVMyIsSZwae4C7497IT8uaMYEdLHQ=

Creating a key from a base64 string is also possible, which is useful for creating one for the other device's public key:

from python_wireguard import Key
srv_public = Key("some string containing a base64 key")

Server

This section explains setting up the connection on the server machine.

from python_wireguard import Server, ClientConnection
server = Server("wg-srv", private, "10.0.0.1/24", 12345)
server.enable()

You should now be able to see connection on your machine using the 'normal' wireguard cli:

sudo wg

Example output:

interface: wg-srv
  public key: Z9mHJ0apfgTvULpV3t9jpzyjmABSts1weE2jPiee8w8=
  private key: (hidden)
  listening port: 12345

Add a client

For adding a client connection, you first need to create a ClientConnection object:

from python_wireguard import ClientConnection, Key

client_key = Key("base64 string received from client (public key)")
client_ip = "10.0.0.2" # The 'local' ip address that the client will be assigned.
conn = ClientConnection(client_key, client_ip)

You can now add this client to the server:

server.add_client(conn)

Client

This section explains setting up the connection on a client machine. This needs to be a different machine than the server machine.

from python_wireguard import Client, ServerConnection, Key

local_ip = "10.0.0.2/24" # CIDR block received from server.

client = Client('wg-client', private, local_ip)

srv_key = Key("base64 string received from the server (public key)")
endpoint = "public ip address of the server"
port = 12345 # The port on which the server has been set up to listen

server_conn = ServerConnection(srv_key, endpoint, port)

client.set_server(server_conn)
client.connect()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_wireguard-0.2.2.tar.gz (72.3 kB view details)

Uploaded Source

Built Distribution

python_wireguard-0.2.2-py3-none-any.whl (72.3 kB view details)

Uploaded Python 3

File details

Details for the file python_wireguard-0.2.2.tar.gz.

File metadata

  • Download URL: python_wireguard-0.2.2.tar.gz
  • Upload date:
  • Size: 72.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for python_wireguard-0.2.2.tar.gz
Algorithm Hash digest
SHA256 08e27dacf4224a7f30080e7e03f37c8c038d5891ce9e107f706c0ff364e68f88
MD5 62f200068f12e9eb784e12c238cedcfe
BLAKE2b-256 04b9e8e9ed73b6e7510844f50c5f825547666aad90be4ac87350e0d47da15b17

See more details on using hashes here.

File details

Details for the file python_wireguard-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for python_wireguard-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1c86b95198a9b2b7040decf054f219bf2899aae1d6eb2d2ae9db2dad7fada6f2
MD5 fddf301095d3ff69bd0d3a4c1825c8af
BLAKE2b-256 2e9ce3adb2409f06023cc8101864bd7df88b4e0bc3f885b8f249b40acd4b9e03

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page