-
Notifications
You must be signed in to change notification settings - Fork 3
/
bypass.py
129 lines (111 loc) · 3.84 KB
/
bypass.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Eric Wu
# @Date: 2013-12-04 15:24:56
# @Email: [email protected]
# @Last modified by: Eric Wu
# @Last Modified time: 2013-12-27 17:18:14
import sys
import alfred
import subprocess
def confirm(msg):
applescript = """
set output to button returned of (display dialog ¬
"%s" with title ¬
"Are you fucking sure to add domains below to bypass?" buttons {"Yes", "No"} ¬
default button 2 ¬
with icon caution)
do shell script "echo " & quoted form of output"""
cmd = "osascript -e '{}'".format(applescript % msg)
return subprocess.check_output(cmd, shell=True).strip() == "Yes"
def parse(uid, action, title, subtitle):
return alfred.Item(
attributes={
'uid': alfred.uid(uid),
'arg': action,
},
title=title.replace("+", ", "),
subtitle=subtitle,
icon="icon.png",
)
class bypass:
def __init__(self):
self.deviceName = "Wi-Fi"
self.cmdList = {
"search": self.bypassSearch,
"add": self.bypassAddAll,
"rm": self.bypassRemove,
}
self.bypassHellOn = False
self.bypassRead()
def bypassRead(self):
self.bypassList = []
cmd = ["networksetup", "-getproxybypassdomains", self.deviceName]
output = subprocess.check_output(cmd)
for item in output.split():
self.bypassList.append(item)
def bypassSet(self):
cmd = ["networksetup", "-setproxybypassdomains", self.deviceName]
subprocess.check_output(cmd+self.bypassList)
def bypassAdd(self, rule):
if rule not in self.bypassList:
self.bypassList.append(rule)
def bypassRemove(self, rule):
if rule in self.bypassList:
self.bypassList.remove(rule)
return "Remove '%s' from list." % rule
def bypassAddAll(self, rule):
if self.bypassHellOn:
return self.bypassHell(rule)
else:
for item in rule.split("+"):
self.bypassAdd(item)
return "Add '%s' to list." % rule.replace("+", "', '")
def bypassHell(self, domain):
cmd = ["sh", "bypass.sh", domain]
output = subprocess.check_output(cmd)
if confirm(output):
for item in output.split():
if item == "*.":
continue
self.bypassAdd(item)
return "Add all items in '%s' to list." % domain
def bypassShow(self, inList, items=None):
if items is None:
items = []
for index, item in enumerate(inList):
items.append(parse(index+1, "rm %s" % item, item, "REMOVE RULE"))
alfred.write(alfred.xml(items))
def bypassSearch(self, rule):
items = self.verifyDomain(rule)
inList = [item for item in self.bypassList if rule in item]
self.bypassShow(inList, items)
def verifyDomain(self, domain):
if self.bypassHellOn:
title = domain
subtitle = "ADD ALL ITEMS"
action = "add %s -a" % title
else:
title = ""
subtitle = "ADD RULE"
for item in domain.split("+"):
if item not in self.bypassList:
title += "+%s" % item
if len(item.split(".")) == 2:
title += "+*.%s" % item
title = title[1:]
action = "add %s" % title
return ([parse(0, action, title, subtitle)] if title else [])
if __name__ == '__main__':
bp = bypass()
if len(sys.argv) == 2:
bp.bypassShow(reversed(bp.bypassList[-9:]))
sys.exit()
config = sys.argv[1:]
if "-a" in config:
bp.bypassHellOn = True
config.remove("-a")
callback = bp.cmdList[config[0]](config[1])
if callback:
bp.bypassSet()
sys.stdout.write(callback)