forked from FunctionClub/SSR-Bash-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timelimit.sh
227 lines (218 loc) · 7.16 KB
/
timelimit.sh
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
#!/bin/bash
#Check Root
[ $(id -u) != "0" ] && { echo "Error: You must be root to run this script"; exit 1; }
#Initialization
userlimit="/usr/local/SSR-Bash-Python/timelimit.db"
nowdate=$(date +%Y%m%d%H%M)
declare -a param=($1 $2 $3)
unset edit
#Set function
checkonly(){
if [[ -e ${userlimit} ]];then
for line in $(cat ${userlimit})
do
if [[ ! -z ${line} ]];then
port=$(echo ${line} | awk -F':' '{ print $1 }')
limitdate=$(echo ${line} | awk -F':' '{ print $2 }')
if [[ ${nowdate} -ge ${limitdate} ]];then
cd /usr/local/shadowsocksr/
python mujson_mgr.py -d -p ${port} 1>/dev/null 2>&1
sed -i '/'"${line}"'/d' ${userlimit}
fi
fi
done
fi
}
Add(){
if [[ ! -e ${userlimit} ]];then
touch ${userlimit}
fi
checkuser=$(grep -i "${param[1]}:" ${userlimit})
if [[ ${edit} == "yes" || -z ${checkuser} ]];then
if [[ ${param[2]} == *d ]];then
timing=$(echo ${param[2]} | sed 's\d\\g')
dating=$(date +%Y%m%d%H%M --date="+${timing}day")
elif [[ ${param[2]} == *m ]];then
timing=$(echo ${param[2]} | sed 's\m\\g')
dating=$(date +%Y%m%d%H%M --date="+${timing}month")
elif [[ ${param[2]} == *h ]];then
timing=$(echo ${param[2]} | sed 's\h\\g')
dating=$(date +%Y%m%d%H%M --date="+${timing}hour")
elif [[ ${param[2]} == "a" ]];then
exit 0
else
echo "错误的参数属性值"
exit 1
fi
echo "${param[1]}:${dating}" >> ${userlimit}
else
Edit
fi
}
Edit(){
checkuser=$(grep -i "${param[1]}:" ${userlimit})
if [[ -z ${checkuser} ]];then
Add
else
limitdate=$(echo ${checkuser} | awk -F':' '{ print $2 }')
edit="yes"
sed -i '/'"${checkuser}"'/d' ${userlimit}
Add
fi
}
EasyAdd(){
echo "1.使用用户名"
echo "2.使用端口"
echo ""
while :; do echo
read -p "请选择: " lsid
if [[ ! $lsid =~ ^[1-2]$ ]]; then
echo "输入错误! 请输入正确的数字!"
else
break
fi
done
if [[ ${lsid} == 1 ]];then
read -p "输入用户名: " uid
cd /usr/local/shadowsocksr
checkuid=$(python mujson_mgr.py -l -u ${uid})
if [[ -z ${checkuid} ]];then
echo "用户名不存在!"
EasyAdd
else
read -p "请输入有效期(单位:月[m]日[d]小时[h],例如:1个月就输入1m){默认:一个月[1m]}: " limit
if [[ -z ${limit} ]];then
limit="1m"
fi
port=$(python mujson_mgr.py -l -u ${uid} | grep "port :" | awk -F" : " '{ print $2 }')
bash /usr/local/SSR-Bash-Python/timelimit.sh a ${port} ${limit} || EasyAdd
datelimit=$(cat ${userlimit} | grep "${port}:" | awk -F":" '{ print $2 }' | sed 's/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9}\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1年\2月\3日 \4:/')
if [[ -z ${datelimit} ]];then
datelimit="永久"
fi
echo -e "添加成功!当前用户端口号:${port},有效期至:${datelimit}\n"
fi
fi
if [[ ${lsid} == 2 ]];then
read -p "输入端口号: " port
cd /usr/local/shadowsocksr
checkuid=$(python mujson_mgr.py -l -p ${port})
if [[ -z ${checkuid} ]];then
echo "用户不存在!"
EasyAdd
else
read -p "请输入有效期(单位:月[m]日[d]小时[h],例如:1个月就输入1m){默认:一个月[1m]}: " limit
if [[ -z ${limit} ]];then
limit="1m"
fi
bash /usr/local/SSR-Bash-Python/timelimit.sh a ${port} ${limit} || EasyAdd
datelimit=$(cat ${userlimit} | grep "${port}:" | awk -F":" '{ print $2 }' | sed 's/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9}\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1年\2月\3日 \4:/')
if [[ -z ${datelimit} ]];then
datelimit="永久"
fi
echo -e "添加成功!当前用户端口号:${port},有效期至:${datelimit}\n"
fi
fi
}
EasyEdit(){
echo "1.使用用户名"
echo "2.使用端口"
echo ""
while :; do echo
read -p "请选择: " lsid
if [[ ! $lsid =~ ^[1-2]$ ]]; then
echo "输入错误! 请输入正确的数字!"
else
break
fi
done
if [[ ${lsid} == 1 ]];then
read -p "输入用户名: " uid
cd /usr/local/shadowsocksr
checkuid=$(python mujson_mgr.py -l -u ${uid})
if [[ -z ${checkuid} ]];then
echo "用户名不存在!"
EasyEdit
else
port=$(python mujson_mgr.py -l -u ${uid} | grep "port :" | awk -F" : " '{ print $2 }')
datelimit=$(cat ${userlimit} | grep "${port}:" | awk -F":" '{ print $2 }' | sed 's/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9}\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1年\2月\3日 \4:/')
if [[ -z ${datelimit} ]];then
datelimit="永久"
fi
echo -e "当前用户端口号:${port},有效期至:${datelimit}\n"
read -p "请输入新的有效期(单位:月[m]日[d]小时[h],例如:1个月就输入1m){默认:永久[a]}: " limit
if [[ -z ${limit} ]];then
limit="a"
fi
fi
fi
if [[ ${lsid} == 2 ]];then
read -p "输入端口号: " port
cd /usr/local/shadowsocksr
checkuid=$(python mujson_mgr.py -l -p ${port} 2>/dev/null)
if [[ -z ${checkuid} ]];then
echo "用户不存在!"
EasyEdit
else
datelimit=$(cat ${userlimit} | grep "${port}:" | awk -F":" '{ print $2 }' | sed 's/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9}\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1年\2月\3日 \4:/')
if [[ -z ${datelimit} ]];then
datelimit="永久"
fi
echo -e "当前用户端口号:${port},有效期至:${datelimit}\n"
read -p "请输入新的有效期(单位:月[m]日[d]小时[h],例如:1个月就输入1m){默认:永久[a]}: " limit
if [[ -z ${limit} ]];then
limit="a"
fi
fi
fi
bash /usr/local/SSR-Bash-Python/timelimit.sh e ${port} ${limit} || EasyEdit
datelimit=$(cat ${userlimit} | grep "${port}:" | awk -F":" '{ print $2 }' | sed 's/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9}\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1年\2月\3日 \4:/')
if [[ -z ${datelimit} ]];then
datelimit="永久"
fi
echo -e "修改成功!当前用户端口号:${port},新的有效期至:${datelimit}\n"
}
readme(){
echo "Usage: $0 params [port] [expiration time]"
echo "params can be one or more of the following :"
echo " a | A : Add a time limit for a user."
echo " e | E : Modify a user's time limit."
echo "If you do not add any parameters after the first parameter,you will enter a simple interface to operate."
echo ""
echo 'About the second parameter "port" :'
echo " As the unique identifier of a user,the port number is unique and the script determines the user's basis.So when you add an account with the same port number,the script will overwrite the original record without any hint."
echo ""
echo 'About the third parameter "expiration time" :'
echo ' Account expiration date refers to the period from the current date.This is true whether it is added or modified.The format is "number+unit".For example,one month is "1m",one day is "1d" and one hour is "1h".'
echo ""
echo "Note: This script does not interact with other scripts.When you add traffic to a user, the script will still be deleted as before."
echo ""
echo "e.g.: "
echo "bash ./timelimit.sh a 443 1m #You will add a month's validity to a user with a port number of 443."
echo ""
echo "If you find a bug, send it to 'stackzhao@gmail.com'"
}
#Main
case ${param[0]} in
a|A)
if [[ -z ${param[1]} && -z ${param[2]} ]];then
EasyAdd
else
Add
fi
;;
e|E)
if [[ -z ${param[1]} && -z ${param[2]} ]];then
EasyEdit
else
Edit
fi
;;
c|C)
checkonly
;;
*)
readme
;;
esac
exit 0