This repository has been archived by the owner on Oct 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpia-patcher.py
executable file
·155 lines (134 loc) · 4.89 KB
/
pia-patcher.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
#!/usr/bin/env python3
# Author: Franccesco Orozco
# Version: 1.0.0
#
# For installation make a symbolic link:
# ~ sudo ln -s $PWD/pia_patcher.py /usr/bin/pia-patcher
# Execute:
# ~ pia-patcher
"Replace resolv.conf with Private Internet Access DNS's"
import shutil
import os
from sys import exit
import argparse
def check_current_dir():
"""Check current directory."""
current_dir = os.path.dirname(os.path.realpath(__file__)) + '/'
return current_dir
def check_resolv_exist():
"""Check if resolv.conf exists."""
return os.path.isfile('/etc/resolv.conf')
def backup_resolv():
"""Back up resolv.conf... just in case :P."""
current_dir = check_current_dir()
try:
shutil.copy('/etc/resolv.conf', current_dir + 'resolv.bak')
except PermissionError as denied:
message = "\033[1m" + "ERR! ROOT is needed." + "\033[0;0m"
print(message)
exit()
else:
print("Successfully backup resolv.conf")
return True
def restore_backup():
"""Restores resolv.conf to its original state."""
current_dir = check_current_dir()
try:
shutil.copy(current_dir + 'resolv.bak', '/etc/resolv.conf')
except PermissionError as denied:
denied_message = denied
print(denied_message)
print("\033[1m" + "Err. Run me as ROOT." + "\033[0;0m")
exit()
else:
print("Restore complete.")
return True
def append_to_resolv():
"""Append template to resolv.conf instead of replacing it."""
current_dir = check_current_dir()
try:
with open(current_dir + 'pia_dns_template.txt', mode='r') as temp_obj:
pia_template = temp_obj.read()
with open('/etc/resolv.conf', mode='a') as resolv_obj:
# double return so it doesn't get cluttered
resolv_obj.writelines("\n")
resolv_obj.write(pia_template)
except PermissionError as denied:
denied_message = denied
print(denied_message)
print("\033[1m" + "Waddup, run me as ROOT okay?" + "\033[0;0m")
print("Breaking...")
exit()
else:
print("DNS successfully patched (Appended).")
return True
def replace_resolv():
"""Replace resolv.conf with PIA template."""
current_dir = check_current_dir()
try:
shutil.copy(current_dir + 'pia_dns_template.txt', '/etc/resolv.conf')
except PermissionError as denied:
denied_message = str(denied)
print(denied_message)
print("\033[1m" + "Whoops! Run it as root buddy." + "\033[0;0m")
else:
print("DNS successfully patched.")
return True
if __name__ == '__main__':
"""Run if script called directly."""
# Case scenario: if resolv exists, overwrite, append or break.
# but make a backup first!
options = argparse.ArgumentParser()
group = options.add_mutually_exclusive_group()
group.add_argument("-o", "--overwrite",
help="Overwrites the entire resolv.conf file.",
action="store_true")
options.add_argument("-f", "--force",
help="Forces overwrite. Use with -o",
action="store_true")
group.add_argument("-a", "--append",
help="Append resolv.conf instead of overwriting.",
action="store_true")
group.add_argument("-b", "--backup",
help="Only performs a backup of current resolv.conf",
action="store_true")
group.add_argument("-r", "--restore",
help="Restores previous backup.",
action="store_true")
args = options.parse_args()
if args.overwrite and args.force:
print("Warning: resolf.conf exists. Force overwriting.")
print("Safety first: Making a backup.")
backup_resolv()
print("Proceeding to overwrite resolv.conf")
replace_resolv()
elif args.overwrite:
if check_resolv_exist():
print("Warning: resolv.conf exist.")
proceed = input("Proceed? [y/N]: ")
proceed.lower()
if proceed is 'y':
print("Warning: resolf.conf exists. Force overwriting.")
print("Safety first: Making a backup.")
backup_resolv()
print("Proceeding to overwrite resolv.conf")
replace_resolv()
else:
print("Exiting...")
exit()
else:
print("Proceeding to overwrite resolv.conf")
replace_resolv()
elif args.append:
print("\nMaking a backup first.")
backup_resolv()
print("Appending to resolv.conf")
append_to_resolv()
elif args.backup:
if check_resolv_exist():
backup_resolv()
elif args.restore:
restore_backup()
else:
print("You need to provide an options.\n")
options.print_help()