forked from tengzy/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.sh
More file actions
executable file
·113 lines (96 loc) · 2.14 KB
/
proxy.sh
File metadata and controls
executable file
·113 lines (96 loc) · 2.14 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
#!/bin/bash
## (C) Daniel Teng, 2012
##
## Proxy service configuration script for OSX
## tested on MacOSX Lion 10.7
##
## Adapted from George Goulas's script
## USAGE
##
# $ proxy on
# $ proxy off
# $ proxy status
## SETTINGS
##
# SOCKS PROXY PORT
PORT=7777
# SSH OPTIONS TO CREATE PROXY
SSH_OPTS="-C2qTnNfD"
# user@host
SSH_HOST="username@yourhost.com"
# SSH PORT
SSH_PORT=22
# Pac file
PAC_File="proxy.pac"
# OSX network service to configure proxy for
WIFI_SERVICE="wi-fi"
ETHERNET_SERVICE="ethernet"
# Verbose, if not empty, it prints diagnosing messages
VERBOSE=1
##
## END OF SETTINGS, DO NOT MODIFY PAST THIS POINT
##
SSH_CMD="ssh ${SSH_OPTS} ${PORT} ${SSH_HOST}"
function enableProxy {
enableProxyForService ${WIFI_SERVICE}
enableProxyForService ${ETHERNET_SERVICE}
ssh_connect
}
function ssh_connect {
${SSH_CMD}
}
function enableProxyForService {
networksetup -setautoproxyurl $1 ${PAC_File}
networksetup -setsocksfirewallproxy $1 localhost ${PORT}
networksetup -setsocksfirewallproxystate $1 on
}
function disableProxy {
killSSHCommand
disableProxyOfService ${WIFI_SERVICE}
disableProxyOfService ${ETHERNET_SERVICE}
}
function killSSHCommand {
ps -ax | grep "${SSH_CMD}" | grep -v grep | awk '{print $1}'| xargs kill
}
function disableProxyOfService {
networksetup -setsocksfirewallproxystate $1 off
}
function showStatus {
showSSHStatus
showStatusOfService ${WIFI_SERVICE}
showStatusOfService ${ETHERNET_SERVICE}
}
function showSSHStatus {
ps -ax | grep "ssh*" | grep -v grep > /dev/null
if [ $? -eq 0 ]; then
echo SSH SOCKS Proxy status: ON
else
echo SSH SOCKS Proxy status: OFF
fi
}
function showStatusOfService {
networksetup -getsocksfirewallproxy $1 | grep Enabled | grep Yes > /dev/null
if [ $? -eq 0 ]; then
echo Proxy setting in network setup for $1: ON
else
echo Proxy setting in network setup for $1: OFF
fi
}
function report {
MSG=$1
if [ -n "${VERBOSE}" ]; then
echo $MSG
fi
}
case "$1" in
on) report "Enabling Proxy"
enableProxy
;;
off) report "Disabling Proxy"
disableProxy
;;
status) echo status
showStatus
;;
*) echo Options: on to enable proxy, off to disable, status to see status.
esac