-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_vars.py
executable file
·86 lines (80 loc) · 3.41 KB
/
create_vars.py
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
#!/usr/bin/env python3
import yaml
import socket
import requests
import sys
with open('./vars/main.yaml') as f:
domains = yaml.load(f, Loader=yaml.FullLoader)
output = dict()
output['apache_remove_default_vhost'] = True
output['apache_ssl_protocol'] = '-all +TLSv1.3 +TLSv1.2'
output['apache_ssl_cipher_suite'] = 'HIGH:!aNULL:!MD5'
output['apache_vhosts'] = []
output['apache_vhosts_ssl'] = []
for domain in domains:
try:
ipaddress = socket.gethostbyname(domain['domain'])
except:
sys.stderr.write(f"Cannot resolve ip for domain {domain['domain']}. Skipping...\n")
continue
if ipaddress != '77.87.50.10':
sys.stderr.write(f"Domain {domain['domain']} doesn't point to our redirects server, but {ipaddress}. Skipping...\n")
continue
try:
r = requests.head(domain['target'], timeout=10)
except:
sys.stderr.write(f"Exception when trying to reach target {domain['target']} for domain {domain['domain']}. Skipping...\n")
continue
if r.status_code >= 400:
sys.stderr.write(f"Target {domain['target']} gives status {r.status_code} for domain {domain['domain']}. FIY...\n")
vhost = dict()
vhost['servername'] = domain['domain']
vhost['redirect_to_https'] = True
vhost_ssl = dict()
vhost_ssl['servername'] = domain['domain']
vhost_ssl['certificate_file'] = f"/etc/letsencrypt/live/{domain['domain']}/fullchain.pem"
vhost_ssl['certificate_key_file'] = f"/etc/letsencrypt/live/{domain['domain']}/privkey.pem"
conditions = dict()
conditions['test_string'] = "%{HTTP_HOST}"
conditions['pattern'] = domain['pattern']
conditions['flags'] = "[NC]"
pattern = dict()
pattern['pattern'] = "^(.*)$"
pattern['substitution'] = domain['target']
pattern['flags'] = "[R,L]"
pattern['conditions'] = []
pattern['conditions'].append(conditions)
vhost_ssl['custom_rewrites'] = []
vhost_ssl['custom_rewrites'].append(pattern)
output['apache_vhosts'].append(vhost)
output['apache_vhosts_ssl'].append(vhost_ssl)
fallbackVhost = dict()
fallbackVhost['servername'] = "zzz.freifunk.net"
fallbackVhost['serveralias'] = []
fallbackVhost['serveralias'].append("*.freifunk.net")
fallbackVhost['serveralias'].append("www.freifunk.net")
fallbackVhost['redirect_to_servername'] = False
fallbackCustomError = dict()
fallbackCustomError['code'] = 410
fallbackCustomError['action'] = "/410.html"
fallbackVhost['custom_errors'] = []
fallbackVhost['custom_errors'].append(fallbackCustomError)
fallbackVhost['custom_rewrites'] = []
fallbackCondition1 = dict()
fallbackCondition1['test_string'] = "%{HTTP_HOST}"
fallbackCondition1['pattern'] = "!^freifunk\\.net"
fallbackCondition1['flags'] = "[NC]"
fallbackCondition2 = dict()
fallbackCondition2['test_string'] = "%{REQUEST_URI}"
fallbackCondition2['pattern'] = "!^/410\.html"
fallbackCondition2['flags'] = "[NC]"
fallbackPattern = dict()
fallbackPattern['conditions'] = []
fallbackPattern['conditions'].append(fallbackCondition1)
fallbackPattern['conditions'].append(fallbackCondition2)
fallbackPattern['substitution'] = "-"
fallbackPattern['flags'] = "[G]"
fallbackPattern['pattern'] = "^.*$"
fallbackVhost['custom_rewrites'].append(fallbackPattern)
output['apache_vhosts'].append(fallbackVhost)
print(yaml.dump(output))