forked from HuJK/DN42-AutoPeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DN42AP_regenerate_config.py
executable file
·73 lines (62 loc) · 2.88 KB
/
DN42AP_regenerate_config.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
#!/usr/bin/python3
import os
import yaml
import time
import json
import asyncio
import pathlib
from distutils.dir_util import copy_tree
import DN42AutoPeer
conf_dir = DN42AutoPeer.wgconfpath + "/peerinfo"
bkfdr = os.path.expanduser(f"~/dn42ap_{str(int(time.time()))}")
backup = input(f"This script will clear all old files in {DN42AutoPeer.wgconfpath} and {DN42AutoPeer.bdconfpath}, backup it into {bkfdr} ? (Y/N)")
allowed_myip = []
if "DN42_IPV4" in os.environ:
allowed_myip += [os.environ["DN42_IPV4"]]
if "DN42_IPV6" in os.environ:
allowed_myip += [os.environ["DN42_IPV6"]]
if "DN42AP_ALLOWED_MYIP" in os.environ:
allowed_myip += json.loads(os.environ["DN42AP_ALLOWED_MYIP"])
if backup == "y" or backup == "Y":
print(bkfdr)
print(os.path.basename(DN42AutoPeer.wgconfpath))
print(os.path.basename(DN42AutoPeer.bdconfpath))
os.mkdir(bkfdr)
os.mkdir(os.path.join(bkfdr, os.path.basename(DN42AutoPeer.wgconfpath)))
os.mkdir(os.path.join(bkfdr, os.path.basename(DN42AutoPeer.bdconfpath)))
copy_tree(DN42AutoPeer.wgconfpath, os.path.join(bkfdr, os.path.basename(DN42AutoPeer.wgconfpath)))
copy_tree(DN42AutoPeer.bdconfpath, os.path.join(bkfdr, os.path.basename(DN42AutoPeer.bdconfpath)))
def saveConfig(new_config):
for path,content in new_config["config"].items():
# print("================================")
# print(path)
# print(content)
fileparent = pathlib.Path(path).parent.absolute()
if not os.path.isdir(fileparent):
os.makedirs(fileparent, mode=0o700 , exist_ok=True)
with open(path,"w") as conffd:
conffd.write(content)
if content.startswith("#!"):
os.chmod(path, 0o755)
# print("================================")
for f in os.listdir(DN42AutoPeer.bdconfpath):
if f.endswith(".conf"):
os.remove(os.path.join(DN42AutoPeer.bdconfpath,f))
for f in os.listdir(DN42AutoPeer.wgconfpath):
if f.endswith(".conf") or f.endswith(".sh") :
os.remove(os.path.join(DN42AutoPeer.wgconfpath,f))
async def main():
for old_conf_file in os.listdir(conf_dir):
if old_conf_file.endswith(".yaml") and os.path.isfile(f"{conf_dir}/{old_conf_file}"):
try:
print(old_conf_file)
old_conf = yaml.load(open(f"{conf_dir}/{old_conf_file}").read(),Loader=yaml.SafeLoader)
action , paramaters = DN42AutoPeer.get_paramaters(old_conf,isAdmin=True)
paramaters = await DN42AutoPeer.check_reg_paramater(paramaters,skip_check=old_conf_file[:-5],git_pull=False,allow_invalid_as=True,allowed_custom_myip=allowed_myip)
new_config = DN42AutoPeer.newConfig(paramaters,overwrite=True)
except Exception as e:
raise e
saveConfig(new_config)
loop = asyncio.get_event_loop()
coroutine = main()
loop.run_until_complete(coroutine)