Skip to content

Instantly share code, notes, and snippets.

@9seconds
Created October 13, 2022 09:06
Show Gist options
  • Save 9seconds/d8d46d84a57ff05674e1284b1f1697c2 to your computer and use it in GitHub Desktop.
Save 9seconds/d8d46d84a57ff05674e1284b1f1697c2 to your computer and use it in GitHub Desktop.
Migrate from https://www.linuxserver.io/ WireGuard to WGEasy
#!/usr/bin/env python
import argparse
import datetime
import ipaddress
import json
import pathlib
import sys
import uuid
import wgconfig
import wgconfig.wgexec
def main():
options = parse_options()
directory = pathlib.Path(options.dir).absolute()
i_config = wgconfig.WGConfig(str(directory.joinpath("wg0.conf")))
i_config.read_file()
wgeasy_config = {
"server": {
"privateKey": i_config.interface["PrivateKey"],
"publicKey": wgconfig.wgexec.get_publickey(
i_config.interface["PrivateKey"]),
"address": i_config.interface["Address"]
},
"clients": {}
}
now = datetime.datetime.utcnow().isoformat()
for peer in i_config.peers.values():
name = peer["_rawdata"][1].lstrip("# ")
conf = wgeasy_config["clients"].setdefault(
str(uuid.uuid4()),
{
"publicKey": peer["PublicKey"],
"createdAt": now,
"updatedAt": now,
"enabled": True,
"address": str(ipaddress.ip_interface(peer["AllowedIPs"]).ip),
"name": name.removeprefix("peer_"),
}
)
if "PresharedKey" in peer:
conf["preSharedKey"] = peer["PresharedKey"]
private_key_path = directory.joinpath(name).joinpath(f"privatekey-{name}")
if private_key_path.exists():
conf["privateKey"] = private_key_path.read_text().strip()
print(json.dumps(wgeasy_config, sort_keys=True, indent=2))
def parse_options():
parser = argparse.ArgumentParser(
description='Migrate configuration from linuxserver/wireguard to wgeasy')
parser.add_argument("dir", help="Docker runtime directory")
return parser.parse_args()
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment