Skip to content

Reading an existing config #2

@house-of-vanity

Description

@house-of-vanity

Hi, could you implement reading an existing config rather than rewriting it? I know that you cannot obtain servers pub and clients secrets from it. Anyway it will be convenient to migrate seamlessly to this tool. I moved my config with simple Python script.

import wgconfig # default iniparser cannot read WG configs.
import json

cfg_path = "/etc/wireguard/wg0.conf"
server_public_key = "IfhwVCps7GEtplKh44wGEHhj892leqpnDXeAKkAmAWg="
public_ip = "10.10.10.1"
dns = "8.8.8.8"

wg = wgconfig.WGConfig(cfg_path)
wg.read_file()

cfg = dict()

_id = 1
peers = list()
for pk, data in wg.peers.items():
    p = dict()
    p["Active"] = False
    p["ID"] = _id
    p["Name"] = data["PublicKey"][0:7]
    p["PrivateKey"] = None
    p["PublicKey"] = data["PublicKey"]
    p["VirtualIP"] = data["AllowedIPs"].split("/")[0]
    p["KeepAlive"] = 0
    peers.append(p)
    _id += 1


cfg["PublicIP"] = public_ip
cfg["PublicKey"] = server_public_key
cfg["DNS"] = dns
cfg["WgConfigPath"] = cfg_path
cfg["Port"] = str(wg.interface["ListenPort"])
cfg["VirtualIP"] = wg.interface["Address"].split("/")[0]
cfg["CIDR"] = wg.interface["Address"].split("/")[1]
cfg["PrivateKey"] = wg.interface["PrivateKey"]
cfg["Active"] = True
cfg["Peers"] = peers

print(json.dumps(cfg, indent=4))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions