Lint code

This commit is contained in:
Arash Hatami 2023-03-03 21:44:21 +03:30
parent a5c0502106
commit 1cc69bd516
No known key found for this signature in database
GPG Key ID: D3D9E8CB2E49731F

45
main.py
View File

@ -2,9 +2,9 @@
Generate WireGuard tunnel configuration files from your data Generate WireGuard tunnel configuration files from your data
""" """
from typing import List
import json import json
import environ import environ
from typing import List
# Load environment variables from .env file # Load environment variables from .env file
env = environ.Env() env = environ.Env()
@ -13,30 +13,31 @@ environ.Env.read_env()
def get_ips() -> List[str]: def get_ips() -> List[str]:
""" """
Load the list of IP addresses from the `ip-list.json` file and return a list of unique IP addresses. Load the list of IP addresses from the `ip-list.json` file
and return a list of unique IP addresses.
Returns: Returns:
A list of unique IP addresses. A list of unique IP addresses.
""" """
with open('ip-list.json', 'r', encoding='UTF-8') as f: with open('ip-list.json', 'r', encoding='UTF-8') as ip_list:
ips = [] ips = []
# Load IP addresses from JSON file # Load IP addresses from JSON file
lists = json.load(f) lists = json.load(ip_list)
# Iterate over groups of IP addresses # Iterate over groups of IP addresses
for group in lists: for group in lists:
# Iterate over individual IP addresses # Iterate over individual IP addresses
for ip in lists[group]: for endpoint_ip in lists[group]:
ips.append(ip) ips.append(endpoint_ip)
# Return a list of unique IP addresses # Return a list of unique IP addresses
return list(set(ips)) return list(set(ips))
def generate_config(endpoint: dict) -> None: def generate_config(profile: dict) -> None:
""" """
Generate a WireGuard configuration file for the specified endpoint. Generate a WireGuard configuration file for the specified endpoint.
Args: Args:
endpoint: A dictionary containing the endpoint name and address. profile: A dictionary containing the endpoint name and address.
Returns: Returns:
None None
@ -44,29 +45,29 @@ def generate_config(endpoint: dict) -> None:
# Get the list of unique IP addresses # Get the list of unique IP addresses
ips = ", ".join(map(str, get_ips())) ips = ", ".join(map(str, get_ips()))
# Define the filename for the configuration file # Define the filename for the configuration file
filename = endpoint['name'] + '.conf' filename = profile['name'] + '.conf'
# Get environment variables with default values # Get environment variables with default values
address = env('ADDRESS', default='10.0.0.1/24') address = env('ADDRESS', default='10.0.0.1/24')
mtu = env('MTU', default='1420') mtu = env('MTU', default='1420')
persistent_keepalive = env('PERSISTENT_KEEPALIVE', default='25') keepalive = env('PERSISTENT_KEEPALIVE', default='25')
# Write the configuration file # Write the configuration file
with open(filename, 'w', encoding='UTF-8') as f: with open(filename, 'w', encoding='UTF-8') as profile_file:
# Write the [Interface] section # Write the [Interface] section
f.write('[Interface]\n') profile_file.write("[Interface]\n")
f.write('PrivateKey = {}\n'.format(env('PRIVATE_KEY'))) profile_file.write(f"PrivateKey = {env('PRIVATE_KEY')}\n")
f.write('Address = {}\n'.format(address)) profile_file.write(f"Address = {address}\n")
f.write('MTU = {}\n\n'.format(mtu)) profile_file.write(f"MTU = {mtu}\n\n")
# Write the [Peer] section # Write the [Peer] section
f.write('[Peer]\n') profile_file.write("[Peer]\n")
f.write('PublicKey = {}\n'.format(env('PUBLIC_KEY'))) profile_file.write(f"PublicKey = {env('PUBLIC_KEY')}\n")
f.write('AllowedIPs = {}\n'.format(ips)) profile_file.write(f"AllowedIPs = {ips}\n")
f.write('Endpoint = {}\n'.format(endpoint['address'])) profile_file.write(f"Endpoint = {endpoint['address']}\n")
f.write('PersistentKeepalive = {}\n'.format(persistent_keepalive)) profile_file.write(f"PersistentKeepalive = {keepalive}\n")
# Load endpoint data from JSON file # Load endpoint data from JSON file
with open('endpoints.json', 'r', encoding='UTF-8') as f: with open('endpoints.json', 'r', encoding='UTF-8') as endpoints_file:
endpoints = json.load(f) endpoints = json.load(endpoints_file)
# Generate a configuration file for each endpoint # Generate a configuration file for each endpoint
for endpoint in endpoints: for endpoint in endpoints:
generate_config(endpoint) generate_config(endpoint)