-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwireMaker.py
More file actions
executable file
·275 lines (232 loc) · 7.84 KB
/
Copy pathwireMaker.py
File metadata and controls
executable file
·275 lines (232 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/python3
import argparse
import os
def addClient(template, svrConf='svrWire.conf', keepalive = None):
srv = svrParse(svrConf)
cli = cliParse(template)
nxt = nextIp(svrConf)
prefix = srv['prefix']
## Generate keys
os.system('umask 077 && wg genkey > t_cprv')
os.system('cat t_cprv | wg pubkey > t_cpub')
with open('t_cprv') as f:
cliPriv = f.read().strip()
with open('t_cpub') as f:
cliPub = f.read().strip()
os.remove('t_cprv')
os.remove('t_cpub')
os.system('umask 077 && wg genpsk > t_psk')
with open('t_psk') as f:
psk = f.read().strip()
os.remove('t_psk')
## Write new client config
newCliFile = f'cliWire-{nxt}.conf'
cliText = f"""[Interface]
PrivateKey = {cliPriv}
Address = {prefix}.{nxt}/24
[Peer]
PublicKey = {cli['svr_pub']}
Endpoint = {cli['endpoint']}
AllowedIPs = {prefix}.0/24
PresharedKey = {psk}
{keepAlive('server', keepalive)}
"""
with open(newCliFile, 'w') as f:
f.write(cliText)
## Append to server config
peerBlock = f"""[Peer]
PublicKey = {cliPub}
AllowedIPs = {prefix}.{nxt}/32
PresharedKey = {psk}
{keepAlive('server', keepalive)}
"""
with open(svrConf, 'a') as f:
f.write(peerBlock)
print(f'Added new client: {newCliFile} (IP {prefix}.{nxt})')
def cliParse(path):
data = {}
with open(path) as f:
for line in f:
if line.startswith('PublicKey'):
data['svr_pub'] = line.split('=', 1)[1].strip()
elif line.startswith('Endpoint'):
data['endpoint'] = line.split('=', 1)[1].strip()
elif line.startswith('AllowedIPs'):
p = line.split('=', 1)[1].strip()
data['prefix'] = '.'.join(p.split('.')[:3])
return data
def keepAlive(side, keepalive):
if keepalive in (side, 'both'):
return 'PersistentKeepalive = 25\n'
return ''
def nextIp(svrConf='svrWire.conf'):
used = []
with open(svrConf) as f:
for line in f:
if line.startswith('AllowedIPs'):
ip = line.split('=')[1].strip()
octet = int(ip.split('.')[3].split('/')[0])
used.append(octet)
nextOct = max(used) + 1 if used else 2
if nextOct > 254:
raise Exception('Subnet full: cannot allocate beyond .254')
return nextOct
def peerGen(endIp, endPt, svrPub, cliOct, prefix, keepalive = None):
"""Generate and return peer information"""
os.system('umask 077 && wg genkey > cliPrvkey')
os.system('cat cliPrvkey | wg pubkey > cliPubkey')
os.system('umask 077 && wg genpsk > psk 2>/dev/null')
with open('cliPrvkey') as iFile:
cliPriv = iFile.read().splitlines()[0]
with open('cliPubkey') as iFile:
cliPub = iFile.read().splitlines()[0]
with open('psk') as iFile:
psk = iFile.read().splitlines()[0]
cliIp = f'Address = {prefix}.{cliOct}/24'
cliQck = f"""[Interface]
PrivateKey = {cliPriv}
{cliIp}
[Peer]
PublicKey = {svrPub}
Endpoint = {endIp}:{endPt}
AllowedIPs = {prefix}.0/24
PresharedKey = {psk}
{keepAlive('client', keepalive)}
"""
return cliQck, cliPub, psk, cliOct
def templateFind():
"""Select highest-numbered cliWire-N.conf, otherwise cliWire.conf"""
numbered = []
for f in os.listdir('.'):
if f.startswith('cliWire-') and f.endswith('.conf'):
try:
num = int(f.replace('cliWire-', '').replace('.conf', ''))
numbered.append((num, f))
except:
continue
if numbered:
numbered.sort()
return numbered[-1][1]
if os.path.exists('cliWire.conf'):
return 'cliWire.conf'
raise Exception('No client config files found.')
def secureConfigs():
"""Ensure all WireGuard-related files have 0600 permissions."""
files = []
# Standard single-client and server configs
files.append('cliWire.conf')
files.append('svrWire.conf')
# Any numbered client configs
for f in os.listdir('.'):
if f.startswith('cliWire-') and f.endswith('.conf'):
files.append(f)
for f in files:
if os.path.exists(f):
try:
os.chmod(f, 0o600)
except Exception as e:
print(f'Warning: could not chmod 0600 on {f}: {e}')
def svrParse(path = 'svrWire.conf'):
data = {}
with open(path) as f:
for line in f:
if line.startswith('PrivateKey'):
data['svr_priv'] = line.split('=', 1)[1].strip()
elif line.startswith('ListenPort'):
data['port'] = line.split('=', 1)[1].strip()
elif line.startswith('Address'):
addr = line.split('=', 1)[1].strip()
data['prefix'] = '.'.join(addr.split('.')[:3])
return data
def main(endIp, endPt, multiClient = False, prefix = '10.249.177', keepalive = None):
os.system('umask 077 && wg genkey > svrPrvkey')
os.system('cat svrPrvkey | wg pubkey > svrPubkey')
with open('svrPrvkey') as iFile:
svrPriv = iFile.read().splitlines()[0]
with open('svrPubkey') as iFile:
svrPub = iFile.read().splitlines()[0]
## Create environment for one client
if multiClient is False:
cliQck = peerGen(endIp, endPt, svrPub, 2, prefix, keepalive)
svrQck = f"""[Interface]
PrivateKey = {svrPriv}
ListenPort = {endPt}
Address = {prefix}.1
[Peer]
PublicKey = {cliQck[1]}
AllowedIPs = {prefix}.2/32
PresharedKey = {cliQck[2]}
{keepAlive('server', keepalive)}
"""
with open('svrWire.conf', 'w') as oFile:
oFile.write(f'{svrQck}')
with open('cliWire.conf', 'w') as oFile:
oFile.write(f'{cliQck[0]}')
## Create environment for multiple clients
else:
pList = []
for i in range (2, multiClient + 1):
pList.append(peerGen(endIp, endPt, svrPub, i, prefix, keepalive))
fList = []
for p in pList:
cStr = f"""[Peer]
PublicKey = {p[1]}
AllowedIPs = {prefix}.{p[3]}/32
PresharedKey = {p[2]}
{keepAlive('server', keepalive)}
"""
fList.append(cStr)
cliStr = '\n'.join(fList)
svrQck = f"""[Interface]
PrivateKey = {svrPriv}
ListenPort = {endPt}
Address = {prefix}.1
{cliStr}
"""
with open('svrWire.conf', 'w') as oFile:
oFile.write(f'{svrQck}')
## Generate client configs
for i in range(len(pList)):
with open(f'cliWire-{i + 2}.conf', 'w') as oFile:
oFile.write(f'{pList[i][0]}')
if __name__ == '__main__':
## Inputs
psr = argparse.ArgumentParser(description = 'A wrapper for creating WireGuard configurations')
psr.add_argument('-k', help = 'Add a keepalive', choices = ['client', 'server', 'both'])
psr.add_argument('-m', help = 'Multi client mode')
psr.add_argument('-s', help = 'WireGuard server endpoint IP address')
psr.add_argument('-p', help = '51820 is the default port')
psr.add_argument('--add', action='store_true', help='Add client to existing environment')
psr.add_argument('--prefix', help = 'Specifies first three octets [Default is 10.249.177]')
args = psr.parse_args()
## Handle keepalives
keepalive = args.k
## Parsing
if args.add:
template = templateFind()
addClient(template, keepalive = keepalive)
secureConfigs()
exit(0)
endIp = args.s
if endIp is None:
print('-s is required')
exit(1)
if args.p is None:
endPt = 51820
else:
endPt = args.p
if args.prefix is None:
prefix = '10.249.177'
else:
prefix = args.prefix
if args.m is not None:
x = main(endIp, endPt, int(args.m), prefix, keepalive = keepalive)
else:
main(endIp, endPt, prefix = prefix, keepalive = keepalive)
## Cleanup
secureConfigs()
os.remove('cliPrvkey')
os.remove('cliPubkey')
os.remove('svrPrvkey')
os.remove('svrPubkey')
os.remove('psk')