-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathansible-wg.yaml
392 lines (328 loc) · 11 KB
/
ansible-wg.yaml
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
---
######################################
# ANSIBLE PLAYBOOK FOR WG INSTALLING #
######################################
- name: "Install Wireguard"
hosts: all
remote_user: root
vars_prompt:
- name: "interface"
prompt: "What interface should I use?"
default: "eth0"
private: no
- name: "uidir"
prompt: "Set directory to store UI files or use default (recommended)"
default: "/opt/wireguard-ui"
private: no
tasks:
- name: "Install wireguard"
ansible.builtin.apt:
name: wireguard
state: latest
update_cache: yes
- name: "Install docker-compose"
ansible.builtin.apt:
name: docker-compose
state: latest
update_cache: yes
- name: "Install docker"
ansible.builtin.apt:
name: docker.io
state: latest
- name: "Create server keys"
shell: wg genkey | tee /etc/wireguard/server.priv | wg pubkey > /etc/wireguard/server.pub
args:
executable: /bin/bash
- name: "Register server public key"
shell: cat /etc/wireguard/server.pub
register: server_pub
- name: "Register server private key"
shell: cat /etc/wireguard/server.priv
register: server_priv
- name: "Get public IP address"
get_url:
url: http://ifconfig.me
http_agent: curl
dest: /tmp/public_ip
- name: "Register server public IP"
shell: cat /tmp/public_ip
register: public_ip
- name: "Create server config file"
copy:
dest: "/etc/wireguard/wg0.conf"
content: |
[Interface]
PrivateKey = {{ server_priv.stdout }}
Address = 10.0.0.1/24
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o {{ interface }} -j MASQUERADE; echo 1 > /proc/sys/net/ipv4/ip_forward
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o {{ interface }} -j MASQUERADE; echo 0 > /proc/sys/net/ipv4/ip_forward
ListenPort = 51820
- name: "Start WireGuard"
shell:
cmd: systemctl start wg-quick@wg0
args:
executable: /bin/bash
- name: "Enable WireGuard"
shell:
cmd: systemctl enable wg-quick@wg0
args:
executable: /bin/bash
- name: "Disable ufw"
shell:
cmd: ufw disable
args:
executable: /bin/bash
- name: "Get github files"
ansible.builtin.git:
repo: "https://github.com/ngoduykhanh/wireguard-ui.git"
dest: "{{ uidir }}/"
- name: "First UI start for ctreating UI config files"
shell: docker-compose up -d
args:
chdir: "{{ uidir }}/"
- name: "Change server private key in the UI config file"
replace:
path: "{{ uidir }}/db/server/keypair.json"
regexp: 'private_key(.*)$'
replace: "private_key\": \"{{ server_priv.stdout }}\","
- name: "Change server public key in the UI config file"
replace:
path: "{{ uidir }}/db/server/keypair.json"
regexp: 'public_key(.*)$'
replace: "public_key\": \"{{ server_pub.stdout }}\","
- name: "Change postup script in server's config"
replace:
path: "{{ uidir }}/db/server/interfaces.json"
regexp: 'post_up(.*)$'
replace: "post_up\": \"iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o {{ interface }} -j MASQUERADE; echo 1 > /proc/sys/net/ipv4/ip_forward\","
- name: "Change postdown script in server's config"
replace:
path: "{{ uidir }}/db/server/interfaces.json"
regexp: 'post_down(.*)$'
replace: "post_down\": \"iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o {{ interface }} -j MASQUERADE; echo 0 > /proc/sys/net/ipv4/ip_forward\""
- name: "Change IP in server's config"
replace:
path: "{{ uidir }}/db/server/interfaces.json"
regexp: '([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)(.*)$'
replace: "10.0.0.1/24\""
- name: "Add cronjob which restarts wg after changing wg0.conf"
ansible.builtin.cron:
name: "restart wg"
minute: "*/1"
job: "find /etc/wireguard/ -name wg0.conf -mmin -1 -exec systemctl restart wg-quick@wg0 \\; 2>&1"
- name: "Install nginx"
ansible.builtin.apt:
name: nginx
state: latest
update_cache: yes
- name: Generate an OpenSSL private key with the default values (4096 bits, RSA)
openssl_privatekey:
path: /etc/ssl/wg.com.pem
type: RSA
- name: Generate an OpenSSL Certificate Signing Request
openssl_csr:
path: /etc/ssl/wg.com.csr
privatekey_path: /etc/ssl/wg.com.pem
country_name: UA
organization_name: yukonet.xyz
email_address: support@yukonet.xyz
common_name: wg.yukonet.xyz
subject_alt_name: 'DNS:wg.yukonet.xyz'
- name: Generate a Self Signed OpenSSL certificate
openssl_certificate:
path: /etc/ssl/wg.com.crt
privatekey_path: /etc/ssl/wg.com.pem
csr_path: /etc/ssl/wg.com.csr
provider: selfsigned
- set_fact:
nginx_pass: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
- name: "Create htpasswd for admin-ui"
copy:
dest: /etc/nginx/passwdfile
content: |
admin:{{ nginx_pass | password_hash }}
- name: "Change admin-ui login"
replace:
path: "{{ uidir }}/db/server/users.json"
regexp: 'username(.*)$'
replace: "username\": \"admin\","
- name: "Change admin-ui password"
replace:
path: "{{ uidir }}/db/server/users.json"
regexp: 'password(.*)$'
replace: "password\": \"{{ nginx_pass }}\""
- name: "Create proxy config file"
copy:
dest: /etc/nginx/sites-available/proxy.conf
content: |
upstream backend {
server 127.0.0.1:5000;
}
server {
listen 80 default_server;
server_name _;
location nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
# listen 80;
listen 443 ssl;
ssl_certificate /etc/ssl/wg.com.crt;
ssl_certificate_key /etc/ssl/wg.com.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
access_log /var/log/nginx/admin-gui.access.log;
error_log /var/log/nginx/admin-gui.error.log;
server_name _;
location / {
auth_basic "Admin area";
auth_basic_user_file /etc/nginx/passwdfile;
proxy_pass http://backend;
}
}
- name: "Unlink default nginx config"
shell: unlink /etc/nginx/sites-enabled/default
args:
executable: /bin/bash
ignore_errors: yes
- name: "Link proxy nginx config"
shell: ln -s /etc/nginx/sites-available/proxy.conf /etc/nginx/sites-enabled/proxy
args:
executable: /bin/bash
ignore_errors: yes
- name: "Start nginx"
shell:
cmd: systemctl start nginx
args:
executable: /bin/bash
- name: "Enable nginx"
shell:
cmd: systemctl enable nginx
args:
executable: /bin/bash
- name: "Restart nginx"
shell:
cmd: systemctl restart nginx
args:
executable: /bin/bash
- name: Allow all connections from localhost
shell:
cmd: iptables -A INPUT -s 127.0.0.1/32 -j ACCEPT
args:
executable: /bin/bash
- name: Drop all connections to 5000 port
shell:
cmd: iptables -A INPUT -p tcp -m tcp --dport 5000 -j DROP
args:
executable: /bin/bash
- name: Save iptables rules to persistent file
shell:
cmd: iptables-save > /etc/iptables_rules_v4
args:
executable: /bin/bash
- name: "Add cronjob which load iptables-rules after restart"
ansible.builtin.cron:
name: "load iptables-rules after restart"
special_time: reboot
job: "/sbin/iptables-restore < /etc/iptables_rules_v4"
- name: "Update docker for autostart"
shell:
cmd: docker update --restart=always wgui
args:
executable: /bin/bash
- name: "Install monitorix"
ansible.builtin.apt:
name: monitorix
state: latest
- name: "Change monitorix config file (configuring graphs)"
replace:
path: "/etc/monitorix/monitorix.conf"
regexp: '(<graph_enable>[\s\S]*)</graph_enable>'
replace: |
<graph_enable>
system = y
kern = y
proc = y
hptemp = n
lmsens = n
gensens = n
ipmi = n
ambsens = n
nvidia = n
disk = n
fs = y
zfs = n
du = y
net = y
netstat = y
tc = n
libvirt = n
process = n
serv = y
mail = n
port = y
user = y
ftp = n
apache = n
nginx = y
lighttpd = n
mysql = n
mongodb = n
varnish = n
pagespeed = n
squid = n
nfss = n
nfsc = n
bind = n
unbound = n
ntp = n
chrony = n
fail2ban = y
icecast = n
raspberrypi = n
phpapc = n
memcached = n
phpfpm = n
apcupsd = n
nut = n
wowza = n
int = y
verlihub = n
</graph_enable>
- name: "Change monitorix config file (Change hostname)"
replace:
path: "/etc/monitorix/monitorix.conf"
regexp: '^title(.*)$'
replace: "title = Monitoring\nenable_hourly_view = y"
- name: "Change monitorix config file (enable htpasswd)"
replace:
path: "/etc/monitorix/monitorix.conf"
regexp: '(<auth>[\s\S]*)</auth>'
replace: |
<auth>
enabled = y
msg = Monitorix: Restricted access
htpasswd = /etc/nginx/passwdfile
</auth>
- name: "Restart monitorix"
shell:
cmd: systemctl restart monitorix
args:
executable: /bin/bash
- set_fact:
description: |
Your VPN server ready!
URL: https://{{ public_ip.stdout }}/
Monitoring: http://{{ public_ip.stdout }}:8080/monitorix
User: admin
Password: {{ nginx_pass }}
- pause:
seconds: 1
prompt: '{{ description }}'