-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy
More file actions
executable file
·167 lines (127 loc) · 4.26 KB
/
proxy
File metadata and controls
executable file
·167 lines (127 loc) · 4.26 KB
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
#!/usr/bin/env python
import subprocess
import os
import time
from os.path import expanduser
# Utility constant
home = expanduser("~")
# Proxy dependent constants
proxy_name = "BBC {} Network"
on = "On"
off = "Off"
reload_command = "source ~/.profile"
# Dictionary of files to change
files = {
'spotify': '{}/Library/Application Support/Spotify/prefs'.format(home),
'maven': '{}/.m2/settings.xml'.format(home),
'ssh': '{}/.ssh/config'.format(home)
}
# List of programs to reload on proxy change
processes = ["Spotify"]
# AppleScript to reload profile in iTerm2
reload_iterm2 = """tell application "iTerm"
repeat with aWindow in windows
tell aWindow
repeat with aTab in tabs
tell aTab
repeat with aSession in sessions
tell aSession
write text "{}"
end tell
end repeat
end tell
end repeat
end tell
end repeat
end tell""".format(reload_command)
# AppleScript to reload profile in the Mac Terminal
reload_mac_term = """tell application "Terminal"
local allWindows
set allWindows to id of every window
repeat with windowID in allWindows
do shell script "{}"
end repeat
end tell""".format(reload_command)
# Dictionary of all terminals to reload on proxy change
terminals = dict(iTerm2=reload_iterm2, Terminal=reload_mac_term)
# Terminal style
style = {
'cyan' : '\033[36m',
'green' : '\033[92m',
'red' : '\033[91m',
'purple' : '\033[95m',
'yellow' : '\033[33m',
'blue' : '\033[34m',
'bold' : '\033[01m',
'ul' : '\033[4m',
'blink' : '\033[5m',
'invert' : '\033[7m',
'reset' : '\033[0m'
}
def get_location():
network_loc = "networksetup -getcurrentlocation"
loc = subprocess.Popen(network_loc, stdout=subprocess.PIPE, shell=True).communicate()[0].decode().strip()
if on in loc:
loc_text = on
else:
loc_text = off
return loc, loc_text
def change_proxy():
before = proxy['before_text'].strip()
if before is on:
network = proxy_name.format(off)
else:
network = proxy_name.format(on)
change = "networksetup -switchtolocation '{}'".format(network)
bin_it = subprocess.Popen(change, stdout=subprocess.PIPE, shell=True).communicate()[0].decode()
after_reith = get_location()
message = "{red}{ul}Proxy is {bold}{}{reset}".format(get_location()[0], **style)
print(message)
def update_file(process):
location = files[process]
new_location = files[process] + ".old"
temp_location = files[process] + ".temp"
print("{bold}Changing the proxy settings for {}{reset}".format(process.title(), **style))
# print("{green}Moving {} to {}".format(new_location, temp_location, **style))
os.rename(new_location, temp_location)
# print("Moving {} to {}".format(location, new_location, **style))
os.rename(location, new_location)
# print("Moving {} to {}{reset}".format(temp_location, location, **style))
os.rename(temp_location, location)
def reload():
for terminal in terminals:
reload_terminal(terminal)
map(reload_process, processes)
def reload_process(process):
pid = find_pid(process)
if pid:
print("{yellow}Reloading the process {bold}{}{reset}{yellow} at pid {bold}{}{reset}".format(process, pid, **style))
subprocess.Popen("kill {}".format(pid), shell=True).communicate()
subprocess.Popen("open -g -a {}".format(process), shell=True).communicate()
# Checks whether a terminal is running and then reloads if thats the case
def reload_terminal(terminal):
pid = find_pid(terminal)
if pid:
print("{yellow}Reloading the terminal {bold}{}{reset}".format(terminal, **style))
command = "osascript -e '{}'".format(terminals[terminal])
bin_it = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()
def find_pid(process):
grep = "grep -w {}".format(process)
process_bash = '''ps -A | {}'''.format(grep)
output = subprocess.Popen(process_bash, stdout=subprocess.PIPE, shell=True).communicate()[0]
for line in output.splitlines():
if grep not in line:
return line[:5]
return False
def main():
before_proxy = get_location()
global proxy
proxy = {}
proxy['before'] = before_proxy[0]
proxy['before_text'] = before_proxy[1]
change_proxy()
for file in files:
update_file(file)
reload()
if __name__ == "__main__":
main()