Created
April 24, 2023 19:03
-
-
Save Brramble/a202178de54bb7fb1c883e94bd663b21 to your computer and use it in GitHub Desktop.
wireguard python API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import argparse | |
# Change this to your domain name or IP address of server running wg-easy | |
base_url = 'http://localhost:51821' | |
# Make sure to update the password to the password you set for your webgui | |
def get_session_id(base_url=base_url): | |
path = base_url + '/api/session' | |
headers = {'Content-Type': 'application/json'} | |
data = '{"password": ""}' | |
# Make initial request to obtain session ID | |
response = requests.post(path, headers=headers, data=data) | |
# Extract session ID from Set-Cookie header | |
session_id = response.cookies.get('connect.sid') | |
return session_id | |
def get_client_data(base_url=base_url, session_id=get_session_id()): | |
# Make second request with session ID in Cookie header | |
path = base_url + '/api/wireguard/client' | |
headers = {'Cookie': f'connect.sid={session_id}'} | |
response = requests.get(path, headers=headers) | |
# Check if the request was successful and print client data | |
if response.status_code == 200: | |
client_data = response.json() | |
print(f'Number of clients: {len(client_data)}') | |
for client in client_data: | |
print(f'Client name: {client["name"]}') | |
else: | |
print(f'Error: {response.status_code} - {response.text}') | |
def create_new_client(client_name, base_url=base_url, session_id=get_session_id()): | |
# Make third request with session ID in Cookie header and provide a name for the new client to be created | |
path = base_url + '/api/wireguard/client' | |
headers = {'Content-Type': 'application/json', 'Cookie': f'connect.sid={session_id}'} | |
data = '{"name":"'+client_name+'"}' | |
response = requests.post(path, headers=headers, data=data) | |
# Check if the request was successful and print new client data | |
if response.status_code == 200: | |
new_client_data = response.json() | |
print('New client created:') | |
print(f'Client name: {new_client_data["name"]}') | |
else: | |
print(f'Error: {response.status_code} - {response.text}') | |
if __name__ == "__main__": | |
# Use argparse to accept user arguments | |
parser = argparse.ArgumentParser(description='Show Wireguard clients or create a new client') | |
parser.add_argument('action', metavar='ACTION', type=str, choices=['status', 'create'], help='Action to perform') | |
parser.add_argument('name', metavar='NAME', type=str, nargs='?', help='Name for new client (required for "create" action)') | |
args = parser.parse_args() | |
if args.action == 'status': | |
get_client_data() | |
elif args.action == 'create': | |
if not args.name: | |
parser.error('Name is required for "create" action') | |
create_new_client(args.name) |
i mean to get config file and save near the python file
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's really work, but i try to write a function which create config file for any client. You now how i can do it?