Skip to content

Commit 7bc34a2

Browse files
committed
wireguard
1 parent 8d6d1a3 commit 7bc34a2

File tree

7 files changed

+176
-1
lines changed

7 files changed

+176
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ netkiller-gantt/netkiller/netkiller_gantt.egg-info
5353
netkiller-gantt/test
5454
*.xlsx
5555
/.idea
56+
/netkiller/network/wg0.conf
57+
/netkiller/network/*.conf

bin/wireguard

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /usr/bin/env python3
2+
# -*- coding: UTF-8 -*-
3+
##############################################
4+
# Home : https://www.netkiller.cn
5+
# Author: Neo <[email protected]>
6+
##############################################
7+
import os,sys
8+
9+
from netkiller.network.wireguard import Wireguard
10+
11+
def main():
12+
try:
13+
run = Wireguard()
14+
run.main()
15+
except KeyboardInterrupt as e:
16+
print(e)
17+
18+
if __name__ == "__main__":
19+
main()

netkiller/network/wireguard.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+

pyproject.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[build-system]
2+
requires = ["setuptools"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "netkiller-devops"
7+
version = "0.7.12"
8+
authors = [
9+
{ name = "Neo", email = "[email protected]" },
10+
]
11+
description = "DevOps of useful deployment and automation"
12+
readme = "README.md"
13+
requires-python = ">=3.12"
14+
classifiers = [
15+
"Programming Language :: Python :: 3",
16+
"Operating System :: OS Independent",
17+
]
18+
license = "MIT"
19+
license-files = ["LICEN[CS]E*"]
20+
21+
dependencies = [
22+
"ruamel.yaml",
23+
"requests",
24+
"redis",
25+
"pyttsx3",
26+
"simple_term_menu",
27+
"prompt_toolkit",
28+
]
29+
30+
[project.urls]
31+
Homepage = "https://www.netkiller.cn"
32+
Issues = "https://github.com/netkiller/netkiller-devops/issues"
33+
34+
35+
36+
#[project.scripts]
37+
#gantt = "netkiller.gantt:main"
38+
#mindmap = "netkiller.mindmap:main"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"bin/logviewer.kubectl",
5555
"bin/merge",
5656
"bin/dingtalk",
57-
# 'bin/gantt',
57+
'bin/wireguard',
5858
"bin/nacos",
5959
"bin/exceldiff",
6060
"bin/cicd",

src/__init__.py

Whitespace-only changes.

src/netkiller/network/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)