-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsmanager.py
executable file
·242 lines (178 loc) · 6.68 KB
/
dnsmanager.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
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
#!/bin/python3
import textwrap
import sys
import ipaddress
import logging
import argparse
import enum
import json
import os
logger = None
ARE_SERVERS_LOADED : bool = False
SERVERS_JSON_PATH = "/etc/dnsmanager/servers.json"
PRIMARY_DNS_SET = "setting primary dns server"
PRIMARY_DNS_SET_ERROR = "error during setting server as primary dns"
DNS_ADD = "adding dns"
DNS_ADD_ERROR = "error during adding dns"
SERVERS : dict ={}
class ACTION:
SET_DNS = "set_dns"
ADD_DNS = "add_dns"
def dns_action(ip : str, action : str):
command = f"sudo dnsjumper --server {ip} --action {action}"
logger.debug(command)
error_code = os.system(command)
return error_code
def delete_server(name : str)->bool:
if load_server(name) is None:
logger.error(f"{name} doesn't exist")
return False
SERVERS.pop(name)
json_object = json.dumps(SERVERS)
open(SERVERS_JSON_PATH, "w").write(json_object)
return True
def init_conf_files():
error_code = os.system("sudo mkdir /etc/dnsmanager")
error_code = os.system(f"sudo touch {SERVERS_JSON_PATH}")
if error_code:
logger.error(f"error touching {SERVERS_JSON_PATH}")
exit(error_code)
def save_server(ips : list[ipaddress.IPv4Address], name : str)->bool:
if load_server(name) is not None:
logger.error(f"{name} exists")
return False
SERVERS[name] = ips
for ip in ips:
if is_valid_ip(ip) == False:
logger.error(f"{ip} is invalid ip")
return False
json_object = json.dumps(SERVERS)
open(SERVERS_JSON_PATH, "w").write(json_object)
return True
def load_all_servers():
#load the whole json
try:
with open(SERVERS_JSON_PATH, "r") as file_to_read:
try:
global SERVERS
SERVERS = json.load(file_to_read)
logger.debug(f"loaded json {SERVERS} from file {SERVERS_JSON_PATH}")
global ARE_SERVERS_LOADED
ARE_SERVERS_LOADED = True
except:
logger.debug(f"no servers loaded from {SERVERS_JSON_PATH}")
return None
except:
init_conf_files()
def is_valid_ip(ip : str)->bool:
try:
ipaddress.ip_address(ip)
return True
except Exception as e:
logger.debug(e)
return False
def load_server(name : str)->list[str]:
if ARE_SERVERS_LOADED == False or (name not in SERVERS):
logger.debug(f"loading no server cuz no server is loaded")
return None
return SERVERS[name]
def switch_to_server(name : str)->bool:
ips : list[str] = load_server(name)
if ips is None:
logger.error(f"{name} not found")
return False
for ip in ips:
if is_valid_ip(ip) == False:
logger.error(f"{ip} is invalid")
return False
logger.debug(f"loaded from server {name} : {ips}")
if ips is None:
logger.error(f"{name} not found")
return False
logger.debug(f"{PRIMARY_DNS_SET} {ips[0]}")
error_code = dns_action(ips[0], ACTION.SET_DNS)
if (error_code):
logger.error(f"{PRIMARY_DNS_SET_ERROR} {ips[0]} error code : {error_code}")
for ip in ips[1::]:
error_code = dns_action(ip, ACTION.ADD_DNS)
logger.debug(f"{DNS_ADD} {ip}")
if error_code:
logger.error(f"{DNS_ADD_ERROR} {ip} error_code : {error_code}")
return False
return True
def resolve_action()->bool:
global CONFIG
logger.debug(f"config inside resolv_action : {CONFIG}")
if is_valid_ip(CONFIG.server[0]) == False:
return switch_to_server(CONFIG.server[0])
error_code = dns_action(CONFIG.server[0], action=ACTION.SET_DNS)
logger.debug(f"{PRIMARY_DNS_SET} {CONFIG.server[0]}")
if error_code:
logger.debug(f"{PRIMARY_DNS_SET_ERROR} {CONFIG.server[0]}")
return False
for ip in CONFIG.server[1::]:
error_code = dns_action(ip, ACTION.ADD_DNS)
logger.debug(f"{DNS_ADD} {ip}")
if error_code:
logger.debug(f"{DNS_ADD_ERROR} {ip}")
return False
return True
def handle_args():
usage_help="""\
examples :
dnsmanager -s 8.8.8.8 : set the current dns server to 8.8.8.8
dnsmanager -s 8.8.8.8,8.4.4.8 : set the primary dns server to 8.8.8.8 and the secondary to 8.4.4.8
note : you can as many secondary dns servers as you want
dnsmanager -a -s 8.8.8.8,8.4.4.8 -n google.com : store a dns server with name google.com and ips of 8.8.8.8 and 8.4.4.8
dnsmanager -s google.com : try to set a stored dns server named google.com"""
parser = argparse.ArgumentParser(
description="simple dns changer",
epilog=usage_help,
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument("-d","--debug", help="debug logging", action="store_true", default=False)
parser.add_argument("-s", "--server", help="ip of a server of name of one previously stored if is a list of ips intended for -a/--add_server switch ips need to be separated by commas", type=str)
parser.add_argument("-a", "--add_server", help="store the server", action="store_true")
parser.add_argument("-l", "--list", help="print all servers", action="store_true")
parser.add_argument("-n", "--name", help="name of the server for the -a/--add_server switch")
parser.add_argument("-r","--remove", help="remove a stored server", action="store_true")
if len(sys.argv) == 1:
parser.print_help()
exit(1)
args = parser.parse_args()
if args.server is not None:
args.server = args.server.split(',')
return args
def list_servers():
for key in SERVERS.keys():
print(key)
print(SERVERS[key])
if __name__ == "__main__":
CONFIG = handle_args()
logger = logging.getLogger(__name__)
if CONFIG.debug:
logging.basicConfig(level=10)
load_all_servers()
logger.debug(CONFIG)
if CONFIG.list:
if ARE_SERVERS_LOADED:
list_servers()
exit(0)
else:
logger.error("no stored servers")
exit(1)
if CONFIG.add_server:
if CONFIG.server is None:
logger.error("no name provided")
exit(1)
logger.debug(f"called save_server args : {CONFIG.server}, {CONFIG.name}")
save_server(CONFIG.server, CONFIG.name)
exit(0)
if CONFIG.remove:
if CONFIG.server is None:
logger.error("no name provided")
exit(1)
delete_server(CONFIG.server[0])
exit(0)
logger.debug(f"resolv_action with {CONFIG}")
resolve_action()