migrate to toml
This commit is contained in:
parent
301b51cee6
commit
7c2261239e
35
main.py
35
main.py
|
@ -3,8 +3,8 @@ Generate WireGuard tunnel configuration files from your data
|
|||
"""
|
||||
|
||||
from typing import List
|
||||
import json
|
||||
import environ
|
||||
import toml
|
||||
|
||||
# Load environment variables from .env file
|
||||
env = environ.Env()
|
||||
|
@ -13,31 +13,34 @@ environ.Env.read_env()
|
|||
|
||||
def get_ips() -> List[str]:
|
||||
"""
|
||||
Load the list of IP addresses from the `ip-list.json` file
|
||||
Load the list of IP addresses from the `ip-list.toml` file
|
||||
and return a list of unique IP addresses.
|
||||
|
||||
Returns:
|
||||
A list of unique IP addresses.
|
||||
"""
|
||||
with open('ip-list.json', 'r', encoding='UTF-8') as ip_list:
|
||||
with open('ip-list.toml', 'r', encoding='UTF-8') as ip_list:
|
||||
ips = []
|
||||
# Load IP addresses from JSON file
|
||||
lists = json.load(ip_list)
|
||||
# Load IP addresses from TOML file
|
||||
lists = toml.load(ip_list)
|
||||
# Iterate over groups of IP addresses
|
||||
for group in lists:
|
||||
for section in lists:
|
||||
# Iterate over individual IP addresses
|
||||
for endpoint_ip in lists[group]:
|
||||
ips.append(endpoint_ip)
|
||||
for endpoint_ip in lists[section]:
|
||||
ips.extend([
|
||||
value for value in lists[section][endpoint_ip].values()
|
||||
])
|
||||
# Return a list of unique IP addresses
|
||||
return list(set(ips))
|
||||
|
||||
|
||||
def generate_config(profile: dict) -> None:
|
||||
def generate_config(endpoint: str, address: str) -> None:
|
||||
"""
|
||||
Generate a WireGuard configuration file for the specified endpoint.
|
||||
|
||||
Args:
|
||||
profile: A dictionary containing the endpoint name and address.
|
||||
endpoint: The endpoint's name.
|
||||
address: The endpoint's address.
|
||||
|
||||
Returns:
|
||||
None
|
||||
|
@ -45,7 +48,7 @@ def generate_config(profile: dict) -> None:
|
|||
# Get the list of unique IP addresses
|
||||
ips = ", ".join(map(str, get_ips()))
|
||||
# Define the filename for the configuration file
|
||||
filename = profile['name'] + '.conf'
|
||||
filename = endpoint + '.conf'
|
||||
# Get environment variables with default values
|
||||
address = env('ADDRESS', default='10.0.0.1/24')
|
||||
mtu = env('MTU', default='1420')
|
||||
|
@ -61,19 +64,19 @@ def generate_config(profile: dict) -> None:
|
|||
profile_file.write("[Peer]\n")
|
||||
profile_file.write(f"PublicKey = {env('PUBLIC_KEY')}\n")
|
||||
profile_file.write(f"AllowedIPs = {ips}\n")
|
||||
profile_file.write(f"Endpoint = {profile['address']}\n")
|
||||
profile_file.write(f"Endpoint = {address}\n")
|
||||
profile_file.write(f"PersistentKeepalive = {keepalive}\n")
|
||||
|
||||
|
||||
def main():
|
||||
"""The main function"""
|
||||
|
||||
# Load endpoint data from JSON file
|
||||
with open('endpoints.json', 'r', encoding='UTF-8') as endpoints_file:
|
||||
endpoints = json.load(endpoints_file)
|
||||
# Load endpoint data from TOML file
|
||||
with open('endpoints.toml', 'r', encoding='UTF-8') as endpoints_file:
|
||||
endpoints = toml.load(endpoints_file)
|
||||
# Generate a configuration file for each endpoint
|
||||
for endpoint in endpoints:
|
||||
generate_config(endpoint)
|
||||
generate_config(endpoint, endpoints[endpoint])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue