|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +#======================================== |
| 3 | + |
| 4 | +# Home: https://www.netkiller.cn |
| 5 | +# Callsign: BG7NYT |
| 6 | +# Data: 2025-10-30 |
| 7 | +#======================================== |
| 8 | +import argparse |
| 9 | +import subprocess |
| 10 | + |
| 11 | +class Wireguard: |
| 12 | + def __init__(self): |
| 13 | + self.parser = argparse.ArgumentParser(description='Wireguard config tools', |
| 14 | + epilog='Author: netkiller - https://www.netkiller.cn') |
| 15 | + |
| 16 | + self.parser.add_argument('-c', '--cidr', type=str, default='10.0.0.0/24', metavar="10.0.0.0/24", help='子网') |
| 17 | + self.parser.add_argument('-e', '--endpoint', type=str, default=None, metavar="[服务器公网IP]:50814", help='服务器端IP地址及端口号') |
| 18 | + # self.parser.add_argument('-s','--server', action="store_true", default=False, help='生成服务端配置') |
| 19 | + # self.parser.add_argument('-p','--peer', action="store_true", default=False, help='生成客户端配置') |
| 20 | + self.parser.add_argument('-n','--node', type=int, default=5, metavar="2", help='指定节点数量并自动创建服务端和客户端配置') |
| 21 | + |
| 22 | + pass |
| 23 | + def subnet(self, cidr): |
| 24 | + # cidr = "10.660.0.0/24" |
| 25 | + ip_part = cidr.split('/')[0] # 先取 IP 部分 "10.0.0.0" |
| 26 | + # 按 '.' 分割为列表 ["10", "0", "0", "0"],取前 3 段拼接并加 '.' |
| 27 | + subnet = '.'.join(ip_part.split('.')[:3]) + '.' |
| 28 | + return (subnet) |
| 29 | + |
| 30 | + |
| 31 | + def server(self,cidr:str, keys:dict): |
| 32 | + subnet =self.subnet(cidr) |
| 33 | + address = subnet+'1/24' |
| 34 | + |
| 35 | + privateKey = keys[0]['private'] |
| 36 | + publicKey = keys[0]['public'] |
| 37 | + |
| 38 | + interface = f"""[Interface] |
| 39 | +Address = {address} |
| 40 | +ListenPort = 51820 |
| 41 | +PrivateKey = {privateKey} |
| 42 | +
|
| 43 | +""" |
| 44 | + |
| 45 | + peers =[] |
| 46 | + n = 2 |
| 47 | + for key in keys[1:]: |
| 48 | + # print(key) |
| 49 | + peerPrivateKey = key['private'] |
| 50 | + peerPublicKey = key['public'] |
| 51 | + peerAddress = f"{subnet}{n}/32" |
| 52 | + peers.append(f"""[Peer] |
| 53 | +PublicKey = {peerPublicKey} |
| 54 | +AllowedIPs = {peerAddress} |
| 55 | + """) |
| 56 | + n+=1 |
| 57 | + conf = interface + "\n".join(peers) |
| 58 | + |
| 59 | + with open('wg0.conf','w') as file: |
| 60 | + file.write(conf) |
| 61 | + # print(conf) |
| 62 | + |
| 63 | + def peer(self,cidr:str, endpoint:str, keys:dict): |
| 64 | + subnet = self.subnet(cidr) |
| 65 | + address = subnet + '1/24' |
| 66 | + |
| 67 | + privateKey = keys[0]['private'] |
| 68 | + publicKey = keys[0]['public'] |
| 69 | + peers = [] |
| 70 | + n = 2 |
| 71 | + for key in keys[1:]: |
| 72 | + # print(key) |
| 73 | + peerPrivateKey = key['private'] |
| 74 | + peerPublicKey = key['public'] |
| 75 | + peerAddress = f"{subnet}{n}/32" |
| 76 | + conf=f"""[Interface] |
| 77 | +PrivateKey = {peerPrivateKey} |
| 78 | +Address = {peerAddress} |
| 79 | +DNS = 8.8.8.8 |
| 80 | +
|
| 81 | +[Peer] |
| 82 | +PublicKey = {publicKey} |
| 83 | +Endpoint = {endpoint} |
| 84 | +AllowedIPs = 0.0.0.0/0 |
| 85 | +PersistentKeepalive = 25 |
| 86 | + """ |
| 87 | + |
| 88 | + with open(f'client{n}.conf', 'w') as file: |
| 89 | + file.write(conf) |
| 90 | + n += 1 |
| 91 | + def genkey(self, number): |
| 92 | + keys = [] |
| 93 | + for n in range(): |
| 94 | + privateKey = subprocess.check_output(["wg", "genkey"], encoding="utf-8").strip() |
| 95 | + # print(privateKey) |
| 96 | + |
| 97 | + publicKey = subprocess.check_output(f"echo '{privateKey}' | wg pubkey", shell=True, encoding="utf-8").strip() |
| 98 | + # print(publicKey) |
| 99 | + keys.append({'private': privateKey, 'public': publicKey}) |
| 100 | + |
| 101 | + # print(keys) |
| 102 | + return keys |
| 103 | + def main(self): |
| 104 | + # (options, args) = self.parser.parse_args() |
| 105 | + args = self.parser.parse_args() |
| 106 | + # print(args) |
| 107 | + # self.parser.print_help() |
| 108 | + # |
| 109 | + if args.cidr and args.endpoint: |
| 110 | + keys = self.genkey(args.node) |
| 111 | + self.server(args.cidr, keys) |
| 112 | + self.peer(args.cidr, args.endpoint, keys) |
| 113 | + else: |
| 114 | + self.parser.print_usage() |
| 115 | + exit() |
| 116 | + |
0 commit comments