-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsmod.py
executable file
·377 lines (327 loc) · 9.49 KB
/
dnsmod.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
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
#!/usr/bin/env python3
import argparse
import os
import platform
import re
import sys
from abc import ABC, abstractmethod
import requests
VERSION = "1.0.5"
CONNECTION_TEST_URL = "https://google.com"
PING_TEST_IP = "1.1.1.1"
def dns_providers():
dns = {
"Shecan": ["178.22.122.100", "185.51.200.2"],
"Cloudflare": ["1.1.1.1", "1.0.0.1"],
"Google": ["8.8.8.8", "8.8.4.4"],
"OpenDNS": ["208.67.222.222", "208.67.220.220"],
"AdGuard": ["94.140.14.14", "94.140.15.15"],
"403": ["10.202.10.202", "10.202.10.102"],
"RadarGame": ["10.202.10.10", "10.202.10.11"],
}
return dns
# DNSMod abstract class
class DNSModAbs(ABC):
"""
Abstract DNSMod class.
"""
# Init
def __init__(self, args) -> None:
self.args = args
# Backup DNS
@abstractmethod
def backup_dns(self) -> None:
"""
Backup the current DNS config.
"""
pass
# Restore DNS
@abstractmethod
def restore_dns(self) -> None:
"""
Restore the DNS config.
"""
pass
# Test connection
def test_connection(self) -> None:
"""
Test the connection.
"""
try:
requests.get(CONNECTION_TEST_URL, allow_redirects=False, timeout=5)
print("Connection test passed ...")
print("Good luck have fun! :)")
except Exception as e:
print("Connection test failed ...")
print("Try another DNS provider. T_T")
# Test connection via ping
def test_connection_ping(self) -> None:
"""
Test the connection via ping.
"""
os.system("ping -c 4 " + PING_TEST_IP)
# Check provider
def check_provider(self, provider) -> None:
"""
Check if the provider is secure.
"""
if provider == "403":
print("WARNING: This provider is government based and probably not secure!")
# Check permissions
def check_permissions(self) -> None:
"""
Check if the user has the required permissions.
"""
if os.geteuid() != 0:
print("You need to have root privileges to run this.")
print("Try again, this time using 'sudo'. May we meet again!")
sys.exit(1)
# Check current DNS
@abstractmethod
def check_current_dns(self) -> None:
"""
Check the current DNS config.
"""
pass
# Set DNS
@abstractmethod
def set_dns(self) -> None:
"""
Set the DNS config.
"""
pass
# Update
@abstractmethod
def update(self) -> None:
"""
Update DNSMod.
"""
pass
# Do the magic :)
def do_magic(self) -> None:
"""
Do the magic.
"""
self.set_dns()
# DNSMod Linux
class DNSModLinux(DNSModAbs):
"""
This is the main class to manage DNS.
"""
def __init__(self, args) -> None:
super().__init__(args)
self.dns_path = "/etc/resolv.conf"
self.dns_bak_path = "/etc/resolv.conf.dnsmod.bak"
self.check_permissions()
self.backup_dns()
# Backup DNS
def backup_dns(self) -> None:
"""
Backup the current DNS config.
"""
os.system(f"cp {self.dns_path} {self.dns_bak_path}")
# Restore DNS
def restore_dns(self) -> None:
"""
Restore the DNS config.
"""
if not os.path.isfile(self.dns_bak_path):
print("No previous DNS config found!")
return
os.rename(self.dns_bak_path, self.dns_path)
print("Previous DNS config has been restored!")
self.test_connection()
# Check current DNS
def check_current_dns(self) -> None:
"""
Check the current DNS config.
"""
print("Current DNS config:")
print("====" * 6)
with open(self.dns_path, "r") as f:
print(f.read())
# Set DNS
def set_dns(self) -> None:
"""
Set the DNS config.
"""
dns = dns_providers()
if self.args.provider != None:
dns = dns[self.args.provider]
elif self.args.set != None:
dns = self.args.set
self.check_provider(self.args.provider)
with open(self.dns_path, "w") as f:
f.write(
f"# Overwritten by DNSMod v{VERSION} - Provider: {self.args.provider} \n"
)
f.write(f"# Previous DNS config at {self.dns_bak_path} \n \n")
f.write(f"nameserver {dns[0]} \n")
f.write(f"nameserver {dns[1]} \n")
print("DNS config has been set!")
self.test_connection()
# Update DNSMod
def update(self) -> None:
"""
Update DNSMod.
"""
# TODO: add update feature
pass
# DNSMod Darwin
class DNSModDarwin(DNSModAbs):
"""
This is the main class to manage DNS.
"""
def __init__(self, args) -> None:
super().__init__(args)
self.dns_bak_path = "/var/root/.dnsmod"
self.interface = "Wi-Fi"
self.check_permissions()
self.backup_dns()
# Backup DNS
def backup_dns(self) -> None:
"""
Backup the current DNS config.
"""
current = (
os.popen(f"networksetup -getdnsservers {self.interface}")
.read()
.replace("\n", " ")
)
with open(self.dns_bak_path, "w") as f:
f.write(current)
# Restore DNS
def restore_dns(self) -> None:
"""
Restore the DNS config.
"""
if not os.path.isfile(self.dns_bak_path):
print("No previous DNS config found!")
return
with open(self.dns_bak_path, "r") as f:
dns = f.read().split(" ")
os.system(f"networksetup -setdnsservers {self.interface} {dns[0]} {dns[1]}")
print("Previous DNS config has been restored!")
self.test_connection()
# Check current DNS
def check_current_dns(self) -> None:
"""
Check the current DNS config.
"""
current = os.popen(f"networksetup -getdnsservers {self.interface}").read()
print("Current DNS config:")
print("====" * 6)
print(current)
# Set DNS
def set_dns(self) -> None:
"""
Set the DNS config.
"""
dns = dns_providers()
if self.args.provider != None:
dns = dns[self.args.provider]
elif self.args.set != None:
dns = self.args.set
self.check_provider(self.args.provider)
os.system(f"networksetup -setdnsservers {self.interface} {dns[0]} {dns[1]}")
print("DNS config has been set!")
self.test_connection()
# Update DNSMod
def update(self) -> None:
"""
Update DNSMod.
"""
# TODO: add update feature
pass
# handler
def handler(args):
system = platform.system()
if system == "Linux":
app = DNSModLinux(args)
elif system == "Darwin":
app = DNSModDarwin(args)
elif system == "Windows":
return
if args.version:
print(f"DNSMod v{VERSION}")
if args.check:
app.check_current_dns()
elif args.restore:
app.restore_dns()
elif args.test:
app.test_connection_ping()
app.test_connection()
elif args.update:
app.update()
else:
app.do_magic()
def main():
dns = dns_providers()
parser = argparse.ArgumentParser(description="DNSMod")
# providers
parser.add_argument(
"-p",
"--provider",
help=f"Choose a DNS provider from {', '.join(list(dns.keys()))}",
type=str,
choices=list(dns.keys()),
action="store",
default=None,
)
# set (2 ip addresses)
parser.add_argument(
"-s",
"--set",
help="Set custom DNS",
nargs=2,
type=str,
action="store",
metavar=("DNS1", "DNS2"),
default=None,
)
parser.add_argument(
"-c",
"--check",
help="Check current DNS config",
action="store_true",
default=False,
)
parser.add_argument(
"-t",
"--test",
help="Test connection",
action="store_true",
default=False,
)
parser.add_argument(
"-r", "--restore", help="Restore DNS", action="store_true", default=False
)
parser.add_argument(
"-u", "--update", help="Update DNSMod", action="store_true", default=False
)
parser.add_argument(
"-v", "--version", help="Show version", action="store_true", default=False
)
args = parser.parse_args()
# if there is nor provider or custom
if not (args.check or args.test or args.restore or args.update or args.version):
if args.provider == None and args.set == None:
print(
f"Choose a provider from {', '.join(list(dns.keys()))} or use custom DNS!"
)
exit()
# if there is a provider and custom
if args.provider != None and args.set != None:
print("Either choose a provider or use custom DNS!")
exit()
# check if the custom DNS patterns are valid using regex
if args.set != None:
if not re.match(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", args.set[0]):
print("Invalid DNS1!")
exit()
if not re.match(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", args.set[1]):
print("Invalid DNS2!")
exit()
handler(args)
if __name__ == "__main__":
main()