2024-07-18 16:26:00 +00:00
|
|
|
import subprocess
|
|
|
|
import re
|
|
|
|
import tempfile
|
|
|
|
import os
|
2024-07-18 17:07:02 +00:00
|
|
|
import sys
|
2024-07-18 16:26:00 +00:00
|
|
|
import argparse
|
|
|
|
import pathlib
|
|
|
|
|
|
|
|
SCRIPT_PATH = pathlib.Path(__file__).parent / "worker.py"
|
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:26:00 +00:00
|
|
|
def execute_script(provider, location, server_type):
|
|
|
|
command = [
|
2024-07-18 17:07:02 +00:00
|
|
|
os.path.realpath(sys.executable),
|
|
|
|
SCRIPT_PATH,
|
|
|
|
"--provider",
|
|
|
|
provider,
|
|
|
|
"--location",
|
|
|
|
location,
|
|
|
|
"--server_type",
|
|
|
|
server_type,
|
2024-07-18 16:26:00 +00:00
|
|
|
]
|
|
|
|
result = subprocess.run(command, capture_output=True, text=True)
|
|
|
|
if result.returncode != 0:
|
|
|
|
raise Exception(f"Error executing script: {result.stderr}")
|
|
|
|
return result.stdout.strip()
|
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:26:00 +00:00
|
|
|
def replace_peer_section(config_content, new_peer_section):
|
|
|
|
# Regular expression to match the [Peer] section
|
2024-07-18 17:07:02 +00:00
|
|
|
peer_section_pattern = re.compile(r"\[Peer\](?:\n(?!\[)[^\n]*)*")
|
|
|
|
|
2024-07-18 16:26:00 +00:00
|
|
|
# Find the existing [Peer] section
|
|
|
|
match = peer_section_pattern.search(config_content)
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:26:00 +00:00
|
|
|
if match:
|
|
|
|
# Replace the existing [Peer] section with the new one
|
2024-07-18 17:07:02 +00:00
|
|
|
updated_content = (
|
|
|
|
config_content[: match.start()]
|
|
|
|
+ new_peer_section
|
|
|
|
+ config_content[match.end() :]
|
|
|
|
)
|
2024-07-18 16:26:00 +00:00
|
|
|
else:
|
|
|
|
# If no [Peer] section exists, append the new one
|
|
|
|
updated_content = config_content + "\n" + new_peer_section
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:26:00 +00:00
|
|
|
return updated_content
|
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:26:00 +00:00
|
|
|
def main():
|
2024-07-18 17:07:02 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Update local Wireguard configuration with a new peer."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--provider",
|
|
|
|
type=str,
|
|
|
|
choices=["hetzner", "aws", "digitalocean", "azure"],
|
|
|
|
required=True,
|
|
|
|
help="Cloud provider",
|
|
|
|
)
|
2024-07-18 16:26:00 +00:00
|
|
|
parser.add_argument("--location", type=str, required=True, help="Server location")
|
|
|
|
parser.add_argument("--server_type", type=str, required=True, help="Server type")
|
2024-07-18 17:07:02 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--interface", type=str, required=True, help="Wireguard interface (e.g., wg0)"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--config_path", type=str, help="Path to the Wireguard configuration file"
|
|
|
|
)
|
2024-07-18 16:26:00 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
provider = args.provider
|
|
|
|
location = args.location
|
|
|
|
server_type = args.server_type
|
|
|
|
interface = args.interface
|
2024-07-18 16:37:56 +00:00
|
|
|
config_path = args.config_path or f"/etc/wireguard/{interface}.conf"
|
2024-07-18 16:26:00 +00:00
|
|
|
|
|
|
|
# Step 1: Execute the existing script to set up the remote VPN server
|
|
|
|
new_peer_section = execute_script(provider, location, server_type)
|
|
|
|
|
|
|
|
# Step 2: Read the local Wireguard configuration file
|
2024-07-18 17:07:02 +00:00
|
|
|
with open(config_path, "r") as file:
|
2024-07-18 16:26:00 +00:00
|
|
|
config_content = file.read()
|
|
|
|
|
|
|
|
# Step 3: Replace the existing [Peer] section with the new one
|
|
|
|
updated_content = replace_peer_section(config_content, new_peer_section)
|
|
|
|
|
|
|
|
# Step 4: Save the updated configuration file
|
2024-07-18 16:50:58 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tempdir:
|
2024-07-18 16:46:05 +00:00
|
|
|
temp_file = pathlib.Path(tempdir) / f"{interface}.conf"
|
|
|
|
temp_file_path = str(temp_file)
|
2024-07-18 16:26:00 +00:00
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
with open(temp_file_path, "w") as file:
|
2024-07-18 16:46:05 +00:00
|
|
|
file.write(updated_content)
|
2024-07-18 16:26:00 +00:00
|
|
|
|
2024-07-18 16:46:05 +00:00
|
|
|
# Step 5: Apply the updated Wireguard configuration
|
|
|
|
subprocess.run(["wg-quick", "down", interface], stderr=subprocess.DEVNULL)
|
|
|
|
subprocess.run(["wg-quick", "up", temp_file_path])
|
|
|
|
|
|
|
|
# Overwrite the original config file with the updated content
|
|
|
|
os.replace(temp_file_path, config_path)
|
2024-07-18 16:26:00 +00:00
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
print(
|
|
|
|
f"Local Wireguard configuration for {interface} updated and applied successfully."
|
|
|
|
)
|
|
|
|
|
2024-07-18 16:26:00 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-07-18 17:07:02 +00:00
|
|
|
main()
|