-
Notifications
You must be signed in to change notification settings - Fork 296
/
core
307 lines (281 loc) · 6.8 KB
/
core
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
Println()
{
printf '\n%b\n' "$1"
}
Trim()
{
local text="${!1}"
text="${text#"${text%%[![:space:]]*}"}"
text="${text%"${text##*[![:space:]]}"}"
read -r ${1} <<< "$text"
}
JoinByChar()
{
local IFS="$1"
shift
echo "$*"
}
JoinByString()
{
local separator="$1"
shift
local first="$1"
shift
printf "%s" "$first" "${@/#/$separator}"
}
ToJsonArray()
{
local var=("$1"[@])
if [[ -z "${!var:-}" ]]
then
echo "[]"
return 0
fi
local arr=("${!var}")
local json_array=$(printf '%s\n' "${arr[@]}" | $JQ_FILE -R . | $JQ_FILE -s .)
echo $json_array
}
shopt -s extglob
EleInArray()
{
local ele="$1"
shift
local var=("$1"[@])
if [[ -z "${!var:-}" ]]
then
return 1
fi
local arr=("${!var}")
[[ "$ele" == @($(JoinByChar '|' "${arr[@]//|/\\|}")) ]]
}
printf()
{
if [ "${BASH_VERSINFO[0]}" -lt 4 ]
then
if [[ "${3:-}" =~ \%\((.+)\)T ]]
then
if [ "${4:--1}" == "-1" ]
then
read -r $2 < <(date +"${BASH_REMATCH[1]}")
elif [ "$dist" == "mac" ] && ! type -p gdate &>/dev/null
then
read -r $2 < <(date -r $4 +"${BASH_REMATCH[1]}")
else
read -r $2 < <(date +"${BASH_REMATCH[1]}" -d @$4)
fi
return 0
elif [[ "$1" =~ \%\((.+)\)T ]]
then
if [ "${2:--1}" == "-1" ]
then
echo $(date +"${BASH_REMATCH[1]}")
elif [ "$dist" == "mac" ] && ! type -p gdate &>/dev/null
then
echo $(date -r $2 +"${BASH_REMATCH[1]}")
else
echo $(date +"${BASH_REMATCH[1]}" -d @$2)
fi
return 0
fi
fi
builtin printf "$@"
}
CurlFake()
{
CurlImpersonateInstall
local service_name="$1"
if [[ -x "${CURL_IMPERSONATE_FILE}-$service_name" ]]
then
shift
"${CURL_IMPERSONATE_FILE}-$service_name" "$@"
return 0
elif [ "$service_name" == "xtream_codes" ]
then
rm -f "${CURL_IMPERSONATE_FILE}-$service_name"
awk '!x{x=sub(/dir=.*/,"dir='"$DEPENDS_ROOT/curl-impersonate"'")}1' "$DEPENDS_ROOT/curl-impersonate/curl_chrome99_android" > "${CURL_IMPERSONATE_FILE}-$service_name"
chmod +x "${CURL_IMPERSONATE_FILE}-$service_name"
echo "$(awk '!x{x=sub(/-H '\''User-Agent: .*'\'' \\/,"-H '\''User-Agent: '"$USER_AGENT_TV"''\'' \\")}1' "${CURL_IMPERSONATE_FILE}-$service_name")" > "${CURL_IMPERSONATE_FILE}-$service_name"
shift
"${CURL_IMPERSONATE_FILE}-$service_name" "$@"
return 0
fi
$CURL_IMPERSONATE_FILE "$@"
}
SetDelimiters()
{
[ -n "${delimiters:-}" ] && return 0
if [ "${BASH_VERSINFO[0]}" -lt 4 ]
then
delimiters=( $'μ' $'\002' $'\003' $'\004' $'\005' $'\006' )
else
delimiters=( $'\001' $'\002' $'\003' $'\004' $'\005' $'\006' )
fi
}
RandStr()
{
local str_size=${1:-8}
local str_array=(
q w e r t y u i o p a s d f g h j k l z x c v b n m
Q W E R T Y U I O P A S D F G H J K L Z X C V B N M
)
local str_array_size=${#str_array[*]}
local str_len=0
local rand_str="" str_index
while [[ $str_len -lt $str_size ]]
do
str_index=$((RANDOM%str_array_size))
rand_str="$rand_str${str_array[str_index]}"
str_len=$((str_len+1))
done
echo "$rand_str"
}
# printf %s "$1" | jq -s -R -r @uri
Urlencode()
{
local LC_ALL='' LANG=C i c e=''
for ((i=0;i<${#1};i++))
do
c=${1:$i:1}
[[ $c =~ [a-zA-Z0-9\.\~\_\-] ]] || printf -v c '%%%02x' "'$c"
e+="$c"
done
echo "$e"
}
UrlencodeUpper()
{
local LC_ALL='' LANG=C i c e=''
for ((i=0;i<${#1};i++))
do
c=${1:$i:1}
[[ $c =~ [a-zA-Z0-9\.\~\_\-] ]] || printf -v c '%%%02X' "'$c"
e+="$c"
done
echo "$e"
}
Urldecode()
{
local i="${*//+/ }"
echo -e "${i//%/\\x}"
}
GetServerIp()
{
local ip
ip=$(dig +short myip.opendns.com @resolver1.opendns.com) || true
if [ -z "$ip" ] || [ "${#ip}" -gt 15 ]
then
ip=$(curl -s whatismyip.akamai.com)
fi
[ -z "$ip" ] && ip=$(curl -s ipv4.icanhazip.com)
[ -z "$ip" ] && ip=$(curl -s api.ip.sb/ip)
[ -z "$ip" ] && ip=$(curl -s ipinfo.io/ip)
echo "$ip"
}
GetFreePort()
{
local lport uport
if [ -n "${1:-}" ] && [ -n "${2:-}" ]
then
lport=$1
uport=$2
elif [ "$dist" == "mac" ]
then
lport=1024
uport=49151
else
read lport uport < /proc/sys/net/ipv4/ip_local_port_range
fi
if [ "$dist" == "mac" ]
then
DepInstall lsof
while true
do
candidate=$((lport+RANDOM%(uport-lport)))
if ! lsof -Pi :"$candidate" -sTCP:LISTEN -t &>/dev/null
then
echo "$candidate"
break
fi
done
else
while true
do
candidate=$((lport+RANDOM%(uport-lport)))
if ! ( echo -n "" >/dev/tcp/127.0.0.1/"$candidate" ) >/dev/null 2>&1
then
echo "$candidate"
break
fi
done
fi
}
GetRandomMac()
{
echo $RANDOM|md5sum|sed 's/../&:/g'|cut -c 1-17
}
RemoveQuotes()
{
local text="${!1}"
if [[ "$text" =~ ^\"(.*)\"$ ]] || [[ "$text" =~ ^\'(.*)\'$ ]]
then
text="${BASH_REMATCH[1]}"
read -r ${1} <<< "$text"
fi
}
PrepTerm()
{
unset term_child_pid
unset term_kill_needed
unset term_signal
trap 'HandleTerm' TERM
}
HandleTerm()
{
if [ -n "${term_child_pid:-}" ]
then
if [ "${pkill:-0}" -eq 1 ]
then
pkill -TERM -P "$term_child_pid" 2> /dev/null || true
elif [ "${force_exit:-0}" -eq 1 ]
then
kill -9 "$term_child_pid" 2> /dev/null || true
else
kill -TERM "$term_child_pid" 2> /dev/null || true
fi
else
term_kill_needed=1
fi
}
WaitTerm()
{
term_child_pid=$!
if [ "${term_kill_needed:-0}" -eq 1 ]
then
if [ "${pkill:-0}" -eq 1 ]
then
pkill -TERM -P "$term_child_pid" 2> /dev/null || true
elif [ "${force_exit:-0}" -eq 1 ]
then
kill -9 "$term_child_pid" 2> /dev/null || true
else
kill -TERM "$term_child_pid" 2> /dev/null || true
fi
fi
wait $term_child_pid 2> /dev/null || term_signal=1
trap - TERM
wait $term_child_pid 2> /dev/null || true
if [ "${term_signal:-0}" -eq 1 ]
then
rm -rf "${delete_on_term:-notfound}"
exit 1
fi
}
Include utils/system "$@"
Include utils/build "$@"
Include utils/inquirer "$@"
Include utils/spinner "$@"
Include utils/progress "$@"
Include utils/shfile "$@"
Include utils/jq "$@"
Include utils/git "$@"
Include utils/curl "$@"
Include utils/log