This repository has been archived by the owner on Feb 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 338
/
export-configure.py
executable file
·100 lines (76 loc) · 2.75 KB
/
export-configure.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import json
import os.path
import sys
from argparse import ArgumentParser
from collections import namedtuple
from datetime import datetime
from math import isnan
from operator import attrgetter
from io import open
formats = {
'hosts': '{ip:<15} {domain}',
'surge': '{domain} = {ip}',
'dnsmasq': 'address=/{domain}/{ip}',
'ros': 'add name {domain} address={ip}',
'unbound': '{domain} IN A {ip}'
}
def check_requirements():
def check_python_version():
if 0x2000000 <= sys.hexversion <= 0x2070000:
print('your "python" lower than 2.7.0 upgrade.')
return False
if 0x3000000 <= sys.hexversion <= 0x3040000:
print('your "python" lower than 3.4.0 upgrade.')
return False
return True
return check_python_version()
def find_fast_ip(ipset):
Item = namedtuple('Item', ['tag', 'ip', 'avg_rtt'])
def handle_delta(items):
tag, delta_map = items
def handle(item):
ip, delta = item
delta = list(item for item in delta if item != None)
if delta:
return Item(tag, ip, sum(delta) / float(len(delta)))
return Item(tag, ip, float('NaN'))
return list(map(handle, delta_map.items()))
def handle_sorted():
data = sum(list(map(handle_delta, ipset.items())), [])
return sorted(filter(lambda x: x.avg_rtt>0, data), key=attrgetter('avg_rtt'))
iptable = handle_sorted()
return iptable[0] if iptable else None
def export(payload, target):
if not payload:
return
print('# Build Date: %s (UTC)' % datetime.utcnow().isoformat())
for service in sorted(payload, key=lambda item: item['title']):
tag, ip, avg_rtt = find_fast_ip(service['ips'])
if isnan(avg_rtt):
continue
print('# %s [%s] (Avg RTT: %.3fms)' % (service['title'], tag, avg_rtt))
for domain in sorted(service['domains'], key=len):
template = '%s' if ip else '# %s'
print(template % formats[target].format(domain=domain, ip=ip))
def load_payload():
target_filename = 'apple-cdn-speed.report'
if os.path.exists(target_filename):
return json.load(open(target_filename, encoding='UTF-8'))
print('please run "fetch-timeout.py" build "%s".' % target_filename)
def main():
parser = ArgumentParser()
parser.add_argument(
'target',
help='output target',
choices=sorted(formats.keys(), key=len)
)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
export(load_payload(), args.target)
if __name__ == '__main__' and check_requirements():
main()