-
Notifications
You must be signed in to change notification settings - Fork 1
/
trace.sh
executable file
·158 lines (127 loc) · 3.41 KB
/
trace.sh
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
#!/bin/bash
# Source the workflow library
. workflowHandler.sh
# Config file
PROFILE_CONF='proxy.conf'
# Icons folder
ICONS_DIR="./icons"
# Current device
DEVICE=''
# ======== private ========
function get_curr_proxy_url()
{
networksetup -getautoproxyurl $DEVICE | grep URL | awk '{print $2}'
}
function get_curr_socks_server()
{
networksetup -getsocksfirewallproxy $DEVICE | grep Server | awk '{print $2}'
}
function get_curr_socks_port()
{
networksetup -getsocksfirewallproxy $DEVICE | grep Port | awk '{print $2}'
}
function get_curr_proxy_state()
{
networksetup -getautoproxyurl $DEVICE | grep Enabled | awk '{print $2}'
}
function get_curr_socks_state()
{
networksetup -getsocksfirewallproxy $DEVICE | grep -E '^Enabled' | awk '{print $2}'
}
function get_curr_proxy_setting()
{
if [[ $(get_curr_proxy_state) = 'Yes' ]]; then
proxy_setting=$(get_curr_proxy_url)
elif [[ $(get_curr_socks_state) = 'Yes' ]]; then
proxy_setting="$(get_curr_socks_server):$(get_curr_socks_port)"
else
proxy_setting=''
fi
echo $proxy_setting
}
# ======== private ========
# Generate feedback results
function list_proxy()
{
local cnt=0
local curr_setting=$(get_curr_proxy_setting)
local title subtitle arg icon
while IFS=' ' read proxy_name proxy_setting; do
uid="trace-$cnt"
let cnt+=1
icon="$ICONS_DIR/$proxy_name.png"
# Use default workflow icon if not found
if [ ! -f "$icon" ]; then
icon="icon.png"
fi
if [[ $curr_setting = $proxy_setting ]]; then
title="Now: $proxy_name"
else
title="$proxy_name"
fi
if [ ${proxy_setting:0:4} = 'http' ]; then
title="$title [AutoProxy]"
else
title="$title"
fi
subtitle="$proxy_setting"
arg="$proxy_name $proxy_setting"
addResult "$uid" "$arg" "$title" "$subtitle" "$icon" "yes"
done <<EOF
`grep -vE '^#.*' $PROFILE_CONF`
off Close all proxy
EOF
# Show feedback results
getXMLResults
}
# Turn on the choice proxy
function on()
{
if [ ${2:0:4} = 'http' ]; then
sudo networksetup -setsocksfirewallproxystate $DEVICE off
sudo networksetup -setautoproxyurl $DEVICE $2
echo "$1 AutoProxy, TRACE ON"
else
domain=$(echo "$2" | awk -F: '{print $1}')
port=$(echo "$2" | awk -F: '{print $2}')
if [[ $domain != '' ]]&&[[ $port != '' ]]; then
sudo networksetup -setautoproxystate $DEVICE off
sudo networksetup -setsocksfirewallproxy $DEVICE $domain $port
echo "$1, TRACE ON!"
else
echo "TRACE FAILED"
fi
fi
}
# Turn off all proxy
function off()
{
sudo networksetup -setautoproxystate $DEVICE off
sudo networksetup -setsocksfirewallproxystate $DEVICE off
echo "TRACE END"
}
# The main entry
function main()
{
local device=$(networksetup -listnetworkserviceorder | awk '
BEGIN {
"netstat -rn | grep default " | getline var
split(var, ARRAY, " ")
}
{ if ($5 ~ ARRAY[6]) { NAME=$3 } }
END { print NAME }
')
DEVICE=${device%,*}
case $1 in
list_proxy)
list_proxy $2
;;
off)
off
;;
*)
on "$1" "$2"
;;
esac
}
main "$@" # Run from here