-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathactivate
191 lines (165 loc) · 5.32 KB
/
activate
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
# The shebang is just a hint for editors.
function _logerror() {
local RED='\033[0;31m'
local NC='\033[0m'
echo "$(echo -en "${RED}")$*$(echo -en "${NC}")"
}
# Determine the directory containing this script
if [[ -n $BASH_VERSION ]]; then
_SCRIPT_LOCATION=${BASH_SOURCE[0]}
_SHELL="bash"
elif [[ -n $ZSH_VERSION ]]; then
_SCRIPT_LOCATION=${funcstack[1]}
_SHELL="zsh"
else
_logerror "Only bash and zsh are supported"
return 1
fi
SOCKS_CLI_DIR="$(dirname "${_SCRIPT_LOCATION}")"
# Ensure that this script is sourced, not executed
# Also note that errors are ignored as `activate foo` doesn't generate a bad
# value for $0 which would cause errors.
if [[ -n $BASH_VERSION ]] && [[ "$(basename "$0" 2> /dev/null)" = "activate" ]]; then
_logerror "Error: activate must be sourced. Run 'source "$0"' instead."
exit 1
fi
# Reload
if [ "${_socks_cli}" = "1" ]; then
source "${SOCKS_CLI_DIR}/deactivate"
fi
# Convert to absolute path
SOCKS_CLI_DIR="$("${SOCKS_CLI_DIR}/py/py_wrapper" -c "import os,sys;print(os.path.abspath(sys.argv[1]))" "${SOCKS_CLI_DIR}")"
declare -a _socks_cli_saved_vars=()
declare -a _socks_cli_export_vars=()
declare -a _socks_cli_supports=()
function _expand() {
eval "(echo \"\${$1}\") 2>/dev/null"
}
function _defined() {
[ -n "$(eval "(echo \"\${$1+x}\") 2>/dev/null")" ]
return $?
}
function _save_var() {
if [ -n "$1" ]; then
_socks_cli_saved_vars+=("$1")
if _defined "$1"; then
eval "_socks_cli_saved_$1=\"$(_expand "$1")\""
fi
fi
}
function _is_function_exist() {
declare -f "$1" > /dev/null
return $?
}
function _contains() {
for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done; return 1
}
function _clean_invalid_shadows() {
local tmp_path="${SOCKS_CLI_DIR}/tmp/shadows/"
[ ! -d "${tmp_path}" ] && return 0
# First pass: remove command shadows if the owning shell has been dead.
find "${tmp_path}" -name "_shell_pid" -print0 |
while IFS= read -r -d '' file; do
pid="$(tr -d '\n\r' < "${file}")"
if [ "${pid}" = "$$" ]; then
# Ignore the current shell
continue
fi
if ! pgrep -F "${file}" > /dev/null; then
find "$(dirname "${file}")" -type f -delete
fi
done
# Second pass: remove empty directories recursively.
find "${tmp_path}" -empty -type d -delete
}
function LOAD_SUPPORT() {
if [ $# -lt 1 ]; then
_logerror "Error: LOAD_SUPPORT() requires one parameter."
return 1
fi
local script="${SOCKS_CLI_DIR}/supports/$1"
if [ ! -f "${script}" ]; then
_logerror "Error: \"${script}\" doesn't exist."
return 1
fi
_socks_cli_supports+=("$1")
source "${script}"
# Execute the activation hook
local hook="socks_cli_activate_$1"
_is_function_exist "$hook" && "$hook"
}
function EXPORT_ENV() {
if [ $# -lt 2 ]; then
_logerror "Error: EXPORT_ENV() requires two parameters."
return 1
fi
if _contains "$1" "${_socks_cli_export_vars[@]}" ; then
_logerror "Warning: ignoring duplicated env var $1=\"$2\""
return 1
fi
_socks_cli_export_vars+=("$1")
_save_var "$1"
eval "export $1=\"$2\""
}
function SHADOW() {
if [ $# -lt 2 ]; then
_logerror "Error: SHADOW() requires at least two parameters."
return 1
fi
local shell_pid=$$
local tmp_path="${SOCKS_CLI_DIR}/tmp/shadows/${shell_pid}"
local path_preappend="${tmp_path}"
if [ -n "${PATH}" ]; then
path_preappend="${path_preappend}:"
fi
if [ "${_socks_cli_shadow_inited}" = "" ]; then
_clean_invalid_shadows
mkdir -p "${tmp_path}"
# Remove existing command shadows
[ -n "${tmp_path}" ] && find "${tmp_path}/" -type f -delete
echo "${shell_pid}" > "${tmp_path}/_shell_pid"
EXPORT_ENV PATH "${path_preappend}${PATH}"
_socks_cli_shadow_inited=1
fi
local shadow_name="$1"
local start_command="${@:2}"
local cmd_shadow="${tmp_path}/${shadow_name}"
cat <<-EOF > "${cmd_shadow}"
#!/bin/bash
function _sed_escape() {
echo "\$1" | sed -e 's/\\\\/\\\\\\\\/g; s/\\//\\\\\\//g; s/&/\\\\\\&/g'
}
function _replace() {
local search=\$(_sed_escape "\$2")
local replace=\$(_sed_escape "\$3")
echo "\$1" | sed "s/\${search}/\${replace}/g"
}
# remove the current folder from the PATH,
# to avoid recursively invoking this script.
export PATH="\$(_replace "\$PATH" "${path_preappend}" "")"
${start_command} "\$@"
EOF
chmod +x "${cmd_shadow}"
}
source "${SOCKS_CLI_DIR}/socksproxyenv"
# Rehash executables if PATH has been changed
if [[ " ${_socks_cli_export_vars[*]} " =~ " PATH " ]]; then
hash -r
fi
# Prompt that socks-cli is activated
_save_var "PS1"
PS1_PREFIX=$'\e[0;32msocks-cli\e[0m '
if [ "$_SHELL" = "bash" ]; then
# Bash needs \[ and \] to surround non-printing characters:
# http://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html
PS1_PREFIX='\[\033[0;32m\]socks-cli\[\033[0m\] '
elif [ "$_SHELL" = "zsh" ]; then
PS1_PREFIX="%{%F{green}%}socks-cli%{%f%} "
fi
export PS1=$PS1_PREFIX$PS1
_socks_cli=1
echo "Done! Environment variables have been changed to:"
for var in "${_socks_cli_export_vars[@]}"; do
echo " $var=$(_expand "$var")"
done