-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathkcp_ss_lib
executable file
·226 lines (161 loc) · 5.22 KB
/
kcp_ss_lib
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
urlencode() {
# urlencode <string>
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~\_\-\/]) printf "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE=$old_lc_collate
}
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
get_docker_container_id(){
local CONTAINER_ID=$(basename "$(cat /proc/1/cpuset)")
if [ -z ${CONTAINER_ID} -o ${CONTAINER_ID} = "/" ]; then
CONTAINER_ID=$(cat /proc/self/mountinfo | grep "docker/containers/.*/hostname" | cut -d "/" -f 6)
fi
echo ${CONTAINER_ID}
}
get_docker_image_name(){
docker inspect "$1" -f {{.Config.Image}}
}
get_default_network_interface(){
route | grep '^default' | grep -o '[^ ]*$'
}
get_default_network_ip(){
local INTERFACE=$(get_default_network_interface)
ifconfig $INTERFACE | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1
}
random_int(){
shuf -i "$1-$2" -n 1
}
port_used(){
local PROTOCOL="$1"
local PORT="$2"
netstat -lnp --$PROTOCOL|grep ":$PORT " >/dev/null 2>&1
}
find_available_port(){
local PROTOCOL="$1"
local MIN=$2
local MAX=$3
local TRIES="${4:-10}"
for ((i=1;i<=$TRIES;i+=1))
do
# echo $i
local PORT=$(random_int $MIN $MAX)
# echo $PORT
local DUMP
DUMP=$(port_used $PROTOCOL $PORT)
if [ $? -ne 0 ]; then
echo $PORT
return 0
fi
done
return 1
}
container_name(){
local SS_PORT=$1
local KCPTUN_PORT=$2
echo "ss-${SS_PORT}-kcp-${KCPTUN_PORT}"
}
setup_bootstrap_envs(){
SS_PORT=${SS_PORT:-$(find_available_port tcp 5000 20000)}
KCPTUN_PORT=${KCPTUN_PORT:-$(find_available_port udp 5000 20000)}
if [ $SS_PORT -eq $KCPTUN_PORT ]; then
echo "ERROR: SS_PORT $SS_PORT is equal to KCPTUN_PORT $KCPTUN_PORT." >&2
exit 1
fi
PASSWORD="${PASSWORD:-$(pwgen 8 1)}"
SS_PASSWORD="${SS_PASSWORD:-${PASSWORD}}"
KCPTUN_PASSWORD="${KCPTUN_PASSWORD:-${PASSWORD}}"
# SS_METHOD=${SS_METHOD:-aes-256-cfb}
# KCPTUN_CRYPT=${KCPTUN_CRYPT:-aes}
# KCPTUN_MODE=${KCPTUN_MODE:-normal}
SS_METHOD="${SS_METHOD:-aes-256-gcm}"
KCPTUN_CRYPT="${KCPTUN_CRYPT:-aes}"
KCPTUN_MODE="${KCPTUN_MODE:-normal}"
KCPTUN_MTU="${KCPTUN_MTU:-1350}"
KCPTUN_SNDWND="${KCPTUN_SNDWND:-256}"
KCPTUN_CLIENT_SNDWND="${KCPTUN_CLIENT_SNDWND:-${KCPTUN_SNDWND}}"
KCPTUN_RCVWND="${KCPTUN_RCVWND:-256}"
KCPTUN_DATASHARD="${KCPTUN_DATASHARD:-10}"
KCPTUN_PARITYSHARD="${KCPTUN_PARITYSHARD:-3}"
set|grep -e '^SS_'
set|grep -e '^KCPTUN_'
}
generate_dood_command(){
local IMAGE_ID=$1
local HOST=$2
local SS_LINK="$(base64_ss_link $HOST)#SS:$HOST:$SS_PORT"
local SIP002_LINK=$(tag_link $(sip002_link $HOST $SS_PORT) "SS:$HOST:$SS_PORT")
local KCPTUN_SS_LINK=$(tag_link $(kcp_ss_link $HOST) "KCPSS:$HOST:$KCPTUN_PORT")
echo "docker run -d --restart=always \
--name $(container_name $SS_PORT $KCPTUN_PORT) \
-e SS_PASSWORD=$SS_PASSWORD -e SS_METHOD=$SS_METHOD \
-e KCPTUN_CRYPT=$KCPTUN_CRYPT \
-e KCPTUN_MODE=$KCPTUN_MODE -e KCPTUN_PASSWORD=$KCPTUN_PASSWORD -e KCPTUN_MTU=$KCPTUN_MTU \
-e KCPTUN_SNDWND=$KCPTUN_SNDWND -e KCPTUN_RCVWND=$KCPTUN_RCVWND \
-e KCPTUN_DATASHARD=$KCPTUN_DATASHARD -e KCPTUN_PARITYSHARD=$KCPTUN_PARITYSHARD \
-e SS_LINK=$SS_LINK -e SIP002_LINK=$SIP002_LINK -e KCPTUN_SS_LINK=$KCPTUN_SS_LINK \
--log-opt max-size=200k --log-opt max-file=1 \
-p $SS_PORT:8338/tcp -p $SS_PORT:8338/udp -p $KCPTUN_PORT:41111/udp $IMAGE_ID"
}
ss_link(){
local HOST=$1
local USER_INFO=`echo -n $SS_METHOD:$SS_PASSWORD|base64`
echo "ss://$USER_INFO@$HOST:$SS_PORT"
}
base64_ss_link(){
# according to https://shadowsocks.org/en/config/quick-guide.html
# compatible with Shadowsocks QT5 and Shadowsocks Android
local HOST=$1
local ENCODED=`echo -n $SS_METHOD:$SS_PASSWORD@$HOST:$SS_PORT|base64|tr -d "\n"`
echo "ss://$ENCODED"
}
sip002_link(){
local HOST=$1
local PORT=$2
PORT=${PORT:-$SS_PORT}
local PLUGIN=$3
local USER_INFO=`echo -n $SS_METHOD:$SS_PASSWORD|base64`
local SIP002_LINK="ss://$USER_INFO@$HOST:$PORT"
if [ ! -z "$PLUGIN" ]; then
SIP002_LINK="$SIP002_LINK/?$PLUGIN"
fi
echo "$SIP002_LINK"
}
kcp_ss_link(){
# according to https://shadowsocks.org/en/spec/SIP002-URI-Scheme.html
# compatible with Shadowsocks Android
local HOST=$1
local USER_INFO=`echo -n $SS_METHOD:$SS_PASSWORD|base64`
local RAW_KCPTUN_PLUGIN_STR="kcptun;crypt=$KCPTUN_CRYPT;mode=$KCPTUN_MODE"
RAW_KCPTUN_PLUGIN_STR+=";rcvwnd=$KCPTUN_RCVWND;sndwnd=$KCPTUN_CLIENT_SNDWND;key=$KCPTUN_PASSWORD;mtu=$KCPTUN_MTU"
RAW_KCPTUN_PLUGIN_STR+=";datashard=$KCPTUN_DATASHARD;parityshard=$KCPTUN_PARITYSHARD"
# echo "ss://$USER_INFO@$HOST:$KCPTUN_PORT?plugin=$(urlencode $RAW_KCPTUN_PLUGIN_STR)"
sip002_link $HOST $KCPTUN_PORT "plugin=$(urlencode $RAW_KCPTUN_PLUGIN_STR)"
}
tag_link(){
local LINK=$1
local TAG=$2
echo "$LINK#$(urlencode $TAG)"
}
qr_link(){
local LINK=$1
echo "https://api.qrserver.com/v1/create-qr-code/?size=300x300&data=$(urlencode $LINK)"
}
shorten_link(){
local LINK=$1
local ISGD_URL="https://is.gd/create.php?format=simple&url=$(urlencode $LINK)"
# echo $ISGD_URL
curl -s "$ISGD_URL"
}