-
Notifications
You must be signed in to change notification settings - Fork 7
/
kubectl-socks5-proxy
executable file
·169 lines (146 loc) · 4.18 KB
/
kubectl-socks5-proxy
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
#!/usr/bin/env bash
#
# A kubectl plugin that creates a local SOCKS5 proxy through which you can access to pods/services in a Kubernetes cluster
#
[[ -n $DEBUG ]] && set -x -e
MAX_POD_CREATION_TIME=10 # unit: second
DEFAULT_NAME=$(echo ${USER}-proxy|sed -e 's/[^[:alnum:]|-]//g'|tr '[:upper:]' '[:lower:]')
DEFAULT_PORT=1080
DEFAULT_PROXY_NAMESPACE=default
DEFAULT_PROXY_IMAGE=serjs/go-socks5-proxy
help(){
echo "Usage: "
echo " kubectl socks5-proxy"
echo ""
options
}
options(){
cat << EOF
Options:
-n, --namespace <namespace> Namespace to create SOCKS5 proxy server in
Default: ${DEFAULT_PROXY_NAMESPACE}
-p, --port <local port> Local port to be forwarded to the SOCKS5 proxy server (Pod)
A client connects to this port, then the connection is forwarded to the
SOCKS5 proxy server, which is then forwareded to the destination server
Default: ${DEFAULT_PORT}
-N, --name <pod name> Name to give the pod in the cluster
Default: ${DEFAULT_NAME}
-i, --image <socks5 image> Set the image used as socks5 proxy server
Default: ${DEFAULT_PROXY_IMAGE}
--skip-cleanup-proxy Skip cearning up SOCKS5 proxy pod
Default: Cleaning up SOCKS5 proxy pod at the end
-h, --help Show this message
EOF
}
cleanup_proxy(){
local namespace="$1"
echo "Clearning up SOCKS5 proxy server (Pod)..."
kubectl delete pod ${podname} -n ${namespace}
}
run_proxy(){
local namespace="$1"
local port="$2"
local image="$3"
# Install an SOCKS5 proxy server if not yet installed
r=$(kubectl get pod ${podname} -n ${namespace} 2>/dev/null | tail -1 | awk '{print $1}') #
if [ "${r}" != "${podname}" ];then
echo "Creating SOCKS5 Proxy (Pod)..."
cat <<EOF | kubectl apply -n ${namespace} -f -
apiVersion: v1
kind: Pod
metadata:
name: ${podname}
labels:
env: test
spec:
containers:
- name: ${podname}
image: ${image}
ports:
- containerPort: 1080
nodeSelector:
"kubernetes.io/os": linux
EOF
# Wait until SOCKS5 proxy server gets ready
c=1
while [[ ${c} -le ${MAX_POD_CREATION_TIME} ]];
do
pod_status=$(kubectl get pod ${podname} -n ${namespace} 2>/dev/null | tail -1 | awk '{print $3}')
if [ "${pod_status}" = "Running" ]; then
break
fi
(( c++ ))
sleep 1
done
fi
echo "Starting a local SOCKS5 Proxy (localhost:${port})..."
echo ""
echo "The proxy Pod can be cleaned up with ^C (if not in background),"
echo "or with a command \"kubectl delete pod ${podname} -n ${namespace}\""
echo ""
# Setup portforward
kubectl port-forward -n ${namespace} ${podname} $port:1080
}
plugin_main() {
skip_cleanup_proxy=no
while [ $# -gt 0 ] ; do
nSkip=1
case $1 in
"-h" | "--help")
help
exit 0
;;
"--skip-cleanup-proxy")
skip_cleanup_proxy=yes
;;
"-n" | "--namespace" )
namespace=$2
nSkip=2
;;
"-p" | "--port" )
port=$2
nSkip=2
;;
"-N" | "--name" )
podname=$2
nSkip=2
;;
"-i" | "--image" )
image=$2
nSkip=2
;;
*)
help >&2
exit 1
;;
esac
shift $nSkip
done
if [[ "$(type kubectl &>/dev/null; echo $?)" -eq 1 ]]; then
echo "Error: missing kubectl command" >&2
echo "Please install kubectl (https://kubernetes.io/docs/tasks/tools/install-kubectl/)" >&2
exit 1
fi
if [ ! -n "${namespace}" ]; then
namespace=${DEFAULT_PROXY_NAMESPACE}
fi
echo "using: namespace=${namespace}"
if [ ! -n "${port}" ]; then
port=${DEFAULT_PORT}
fi
echo "using: port=${port}"
if [ ! -n "${podname}" ]; then
podname=${DEFAULT_NAME}
fi
echo "using: name=${podname}"
if [ ! -n "${image}" ]; then
image=${DEFAULT_PROXY_IMAGE}
fi
echo "using: image=${image}"
run_proxy "${namespace}" "${port}" "${image}"
# Cleaning up SOCKS5 proxy server if needed
if [ "${skip_cleanup_proxy}" = "no" ]; then
cleanup_proxy "${namespace}"
fi
}
plugin_main "$@"