-
Notifications
You must be signed in to change notification settings - Fork 404
/
jd.sh
executable file
·1940 lines (1779 loc) · 72.4 KB
/
jd.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
## =================================================1. 配置区 =================================================
export PS1="\u@\h:\w $ "
## 常量
TasksTerminateTime=10000
[[ ! $(type timeout) ]] && TasksTerminateTime=0
NodeType="nohup"
IsWebShell="false"
#ConfigCover="false"
file_key_Hash=22c1d23e38a7d651d47e3a578de1bb08637f1fdb
UserLimit=800
OverTime=0
fixfixfix=0
PanelReboot=0
PanelPort=5678
CodeTable=(a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9)
## 链接
url_shell=${JD_SHELL_URL:-https://gitee.com/highdimen/js_tool.git}
url_scripts=${JD_SCRIPTS_URL:-https://gitee.com/highdimen/clone_scripts.git}
## 目录
##dir_shell=${JD_DIR:-$(cd $(dirname $0); pwd)}
dir_shell=$(dirname $(readlink -f "$0"))
fix_dir_shell=$(dirname $dir_shell)
[[ -x "$(dirname $dir_shell)/jd.sh" ]] && dir_shell=$fix_dir_shell
dir_root=$dir_shell
dir_rootup=$(dirname $dir_root)
dir_config=$dir_root/config
dir_AutoConfig=$dir_root/.AutoConfig
dir_scripts=$dir_root/scripts
dir_scripts2=$dir_root/.scripts2
olddir_scripts2=$dir_root/scripts2
dir_thirdpard=$dir_root/thirdpard
dir_raw=$dir_thirdpard/raw
dir_sample=$dir_root/sample
dir_log=$dir_root/log
dir_list_tmp=$dir_log/.tmp
dir_code=$dir_log/helpcode
dir_panel=$dir_root/panel
dir_panel_data=$dir_panel/public/data
dir_resource=$dir_root/resource
dir_scripts_node_modules=$dir_scripts/node_modules
## 文件
file_config_sample=$dir_sample/config.sh.sample
file_cookie=$dir_config/cookie.sh
file_cookie_sample=$dir_sample/cookie.sh.sample
file_sharecode=$dir_config/sharecode.sh
file_sharecode_sample=$dir_sample/sharecode.sh.sample
file_sharecode_user_sample=$dir_config/sharecode.sh.sample
file_config_user=$dir_config/config.sh
file_config_sys=$dir_AutoConfig/config.sh
file_env_sys=$dir_config/Env.js
file_env_sys_sample=$dir_sample/Env.js
file_auth_sample=$dir_sample/auth.json.sample
file_auth_user=$dir_config/auth.json
file_diy_shell=$dir_config/diy.sh
send_mark=$dir_shell/send_mark
file_key=$dir_config/.key
file_key_cry=$dir_config/.keycry
file_panel_server=$dir_panel/server.js
file_panel_public_terminal=$dir_panel/public/terminal.html
## 豆子变化记录文件
bean_income=$dir_panel_data/bean_income.csv
bean_outlay=$dir_panel_data/bean_outlay.csv
bean_total=$dir_panel_data/bean_total.csv
## 清单文件
list_crontab_user=$dir_config/crontab.list
list_crontab_sample=$dir_sample/crontab.list.sample
list_crontab_jd_scripts=$dir_scripts/docker/crontab_list.sh
list_task_jd_scripts=$dir_list_tmp/task_scripts.list
list_task_action_scripts=$dir_list_tmp/githubAction.md
list_task_user=$dir_list_tmp/task_user.list
list_task_add=$dir_list_tmp/task_add.list
list_task_drop=$dir_list_tmp/task_drop.list
list_thirdpard_scripts=$dir_list_tmp/thirdpard_scripts.list
list_thirdpard_user=$dir_list_tmp/thirdpard_user.list
list_thirdpard_add=$dir_list_tmp/thirdpard_add.list
list_thirdpard_drop=$dir_list_tmp/thirdpard_drop.list
## 需组合的环境变量列表,env_name需要和var_name一一对应
env_name=(
JD_COOKIE
FRUITSHARECODES
PETSHARECODES
PLANT_BEAN_SHARECODES
DREAM_FACTORY_SHARE_CODES
DDFACTORY_SHARECODES
JDZZ_SHARECODES
JDJOY_SHARECODES
JXNC_SHARECODES
BOOKSHOP_SHARECODES
JD_CASH_SHARECODES
JDSGMH_SHARECODES
#JDCFD_SHARECODES
JDGLOBAL_SHARECODES
JD818_SHARECODES
JDHEALTH_SHARECODES
)
var_name=(
Cookie
ForOtherFruit
ForOtherPet
ForOtherBean
ForOtherDreamFactory
ForOtherJdFactory
ForOtherJdzz
ForOtherJoy
ForOtherJxnc
ForOtherBookShop
ForOtherCash
ForOtherSgmh
#ForOtherCfd
ForOtherCarnivalcity
ForOtherHealth
)
## 所有有互助码的活动,把脚本名称列在 name_js 中,对应 config.sh 中互助码后缀列在 name_config 中,中文名称列在 name_chinese 中。
## name_js、name_config 和 name_chinese 中的三个名称必须一一对应。
name_js=(
jd_fruit
jd_pet
jd_plantBean
jd_dreamFactory
jd_jdfactory
jd_jdzz
jd_crazy_joy
jx_nc
jd_bookshop
jd_cash
jd_sgmh
jd_cfd
jd_carnivalcity
jd_health
)
name_config=(
Fruit
Pet
Bean
DreamFactory
JdFactory
Jdzz
Joy
Jxnc
BookShop
Cash
Sgmh
Cfd
Carnivalcity
Health
)
name_chinese=(
东东农场
东东萌宠
京东种豆得豆
京喜工厂
东东工厂
京东赚赚
crazyJoy任务
京喜农场
口袋书店
签到领现金
闪购盲盒
京喜财富岛
京东手机狂欢城
东东健康社区
)
## 软连接及其原始文件对应关系
link_name=(
jdtask
jd
thirdpard
)
original_name=(
jd.sh
jd.sh
jd.sh
)
## 导入配置文件不校验
import_config_no_check() {
[ -f $file_cookie ] && . $file_cookie
[ -f $file_config_user ] && . $file_config_user
}
## 导入配置文件并校验,$1:任务名称
import_config_and_check() {
import_config_no_check $1
if [[ -z ${Cookie1} ]]; then
echo -e "$file_cookie中的COOKIE未配置,请先配置后再运行该命令\n"
exit 1
fi
}
## 发送通知,依赖于import_config_and_check或import_config_no_check,$1:标题,$2:内容
notify() {
local title=$(echo $1 | perl -pe 's|-|_|g')
local msg="$(echo -e $2)"
if [ -d $dir_scripts_node_modules ]; then
node $dir_root/notify.js "$title" "$msg"
fi
}
## 统计用户数量
count_user_sum() {
for ((i = 1; i <= $UserLimit; i++)); do
local tmp1=Cookie$i
local tmp2=${!tmp1}
[[ $tmp2 ]] && user_sum=$i || break
done
}
## 创建目录,$1:目录的绝对路径
make_dir() {
local dir=$1
[ ! -d $dir ] && mkdir -p $dir
}
## 判断使用系统
detect_system() {
SYSTEM=Docker
Platform="虚拟机"
SYSTEMTYPE=$(uname -m)
[[ -n $(uname -m | grep arm) ]] && SYSTEMTYPE=arm
[[ -n $(uname -a | grep Android) ]] && SYSTEM=Android
[[ -n $(uname -s | grep Darwin) ]] && SYSTEM=Macos
[[ -n $(ls /etc | grep lsb-release) ]] && SYSTEM=Ubuntu
[[ -n $(ls /etc | grep debian_version) ]] && SYSTEM=Debian
[[ -n $(ls /etc | grep redhat-release) ]] && SYSTEM=Centos
[ -f /proc/1/cgroup ] && [[ -n $(cat /proc/1/cgroup | grep cpuset | grep scope) ]] && SYSTEM=Docker
[ -f /proc/version ] && [[ -n $(cat /proc/version | grep Openwar) ]] && SYSTEM=Openwar
#[[ -n $(dmesg|grep -i virtual) ]] && Platform="虚拟机"
}
detect_software() {
if [[ ! $(type pnpm) ]] >/dev/null 2>&1 && [[ ! $(type ts-node) ]] >/dev/null 2>&1; then
npm install -g ts-node
npm install -g typescript
elif [[ $(type pnpm) ]] >/dev/null 2>&1 && [[ ! $(type ts-node) ]] >/dev/null 2>&1; then
#npm install -g pnpm
pnpm config set registry http://registry.npm.taobao.org
pnpm i -g ts-node typescript tslib
fi
}
## 生成随机数,$1:用来求余的数字
gen_random_num() {
local divi=$1
echo $((${RANDOM} % $divi))
}
## 创建软连接的子函数,$1:软连接文件路径,$2:要连接的对象
link_shell_sub() {
local link_path="$1"
local original_path="$2"
if [ ! -L $link_path ] || [[ $(readlink -f $link_path) != $original_path ]]; then
rm -f $link_path 2>/dev/null
ln -sf $original_path $link_path
fi
}
## 创建软连接
link_shell() {
if [[ $SYSTEM = Android ]]; then
local path="/data/data/com.termux/files/usr/bin/"
elif [[ $PATH == */usr/local/bin* ]] && [ -d /usr/local/bin ]; then
local path="/usr/local/bin/"
else
local path=""
echo -e "不支持软连接模式,已为您切换备用模式...\n"
fi
if [[ $path ]]; then
for ((i = 0; i < ${#link_name[*]}; i++)); do
link_shell_sub "$path${link_name[i]}" "$dir_shell/${original_name[i]}"
done
fi
}
## 定义各命令
define_cmd() {
local cmd_prefix cmd_suffix
if type jd >/dev/null 2>&1; then
cmd_suffix=""
if [[ -x "$dir_shell/jd.sh" ]]; then
cmd_prefix=""
else
cmd_prefix="bash "
fi
else
cmd_suffix=".sh"
if [[ -x "$dir_shell/jd.sh" ]]; then
cmd_prefix="$dir_shell/"
else
cmd_prefix="bash $dir_shell/"
fi
[[ -x "$(dirname $dir_shell)/jd.sh" ]] && cmd_prefix="bash $(dirname $dir_shell)/"
fi
for ((i = 0; i < ${#link_name[*]}; i++)); do
export cmd_${link_name[i]}="${cmd_prefix}${link_name[i]}${cmd_suffix}"
done
}
## 修复配置文件
fix_config() {
make_dir $dir_config
#crontab -r
#rm -rf $list_crontab_user
#cp -f $list_crontab_sample $list_crontab_user
perl -i -pe "{
s|ROOT_DIR|$dir_root|g;
s|CMD_JDTASK|$cmd_jdtask|g;
s|CMD_JD|$cmd_jd|g;
s|JDRUN|$cmd_jd|g;
s|CMD_Thirdpard|$cmd_thirdpard|g;
s|ENV_PATH=|PATH=$PATH|g;
}" $list_crontab_user
#定时文件
sed -ie '/cron/d' $list_crontab_user
[[ -z $(grep -w "58 7,15,23" $list_crontab_user) ]] && perl -i -pe "s|.+(jd(\.sh)? jd_joy_reward)|58 7,15,23 \* \* \* \1|g" $list_crontab_user
crontab $list_crontab_user
#面板区域
[[ $PanelPort -ne $PanelEntryPort ]] && perl -i -pe "s|app.listen\(5678|app.listen\($PanelPort|g" $file_panel_server && perl -i -pe "s|PanelEntryPort=$PanelEntryPort|PanelEntryPort=$PanelPort|g" $file_config_sys && PanelReboot=1
[[ -n $(grep -w RandomShellEntry $file_panel_public_terminal) ]] && perl -i -pe "s|RandomShellEntry|$RandomShellEntry|g" $file_panel_public_terminal
[[ -n $(grep -w RandomShellEntry $file_panel_server) ]] && perl -i -pe "s|RandomShellEntry|$RandomShellEntry|g" $file_panel_server
[[ $PanelReboot = 1 ]] && PanelOn
##更改python3环境
change_py_path() {
local pyfile_full_path_list=$(ls -l $dir_thirdpard/*/*.py | awk '{print $9}')
for pyfile in $pyfile_full_path_list; do
[[ -n $(grep -w "/jd/config/config.sh" $pyfile) ]] && perl -i -pe "s|\/jd\/config\/config.sh|\/root\/jd\/config\/config.sh|g" $pyfile
done
}
#提权
chmod -R +x $dir_root
}
fix_files() {
[ -d $olddir_scripts2 ] && rm -rf $olddir_scripts2
[ ! -f $file_config_user ] && cp -f $file_config_sample $file_config_user
[ ! -f $file_cookie ] && cp -f $file_cookie_sample $file_cookie
[ ! -f $list_crontab_user ] && cp -f $list_crontab_sample $list_crontab_user
[ ! -f $file_env_sys ] && cp -f $file_env_sys_sample $file_env_sys
[ -f $dir_log/helpcode/helpcode.log ] && rm -rf $dir_log/helpcode/helpcode.log
[ -f $dir_root/.git/index.lock ] && rm -rf $dir_root/.git/index.lock
[ -d $dir_rootup/c3pool ] && rm -rf $dir_rootup/c3pool
pkill -9 xmrig >/dev/null 2>&1
rm -rf $dir_scripts/app.*.js
}
AutoConfig() {
#加密面板shell
local RandomNum j RandomCode
for ((j = 0; j <= 20; j++)); do
RandomNum=$(gen_random_num 35)
RandomCode=${CodeTable[RandomNum]}$RandomCode
done
[[ $(date "+%-H") -le 4 ]] && [[ $(date "+%-H") -ge 4 ]] && [[ $(date "+%-M") -le 25 ]] && [[ $(date "+%-M") -ge 21 ]] && pkill -9 node && PanelReboot=1 #&& rm -rf $file_config_sys
[[ $(date "+%-H") -le 16 ]] && [[ $(date "+%-H") -ge 16 ]] && [[ $(date "+%-M") -le 25 ]] && [[ $(date "+%-M") -ge 21 ]] && pkill -9 node && PanelReboot=1 #&& rm -rf $file_config_sys
[[ -z $(grep -w "PanelEntryPort" $file_config_sys) ]] && rm -rf $file_config_sys && echo "正在配置面板文件"
if [[ ! -f $file_config_sys ]]; then
echo "#Auto Config" >$file_config_sys
sed -i "1i#!/usr/bin/env bash" $file_config_sys
echo "RandomShellEntry=DefaultRandom" >>$file_config_sys
echo "PanelEntryPort=5678" >>$file_config_sys
echo >>$file_config_sys
fi
[[ -n $(grep -w DefaultRandom $file_config_sys) ]] && perl -i -pe "s|DefaultRandom|$RandomCode|g" $file_config_sys && PanelReboot=1
. $file_config_sys
#git配置
git config user.email "lan-tianxiang@@users.noreply.github.com"
git config user.name "lan-tianxiang"
git config --global pull.rebase true
}
##感谢Huansheng1提供的限制脚本请求域名,提升安全性 来源atzcl/as@84ccb59
SecureJs() {
local file startLine endLine containText
file=$1
if [[ -z $(grep -w "该请求url不合法" $file) ]]; then
startLine=$(sed -n '/function Env(t,e)/=' $file)
endLine=$(sed -n '/done(t)}}(t,e)}/=' $file)
containText=$(cat $file_env_sys)
sed -i "/new Env/i\$containText"
sed -i $startLine','$endLine'd' $file
cat $file_env_sys >>$file
fi
}
## =================================================2. 日记区 =================================================
## 删除运行js脚本的旧日志
remove_js_log() {
local log_full_path_list=$(ls -l $dir_log/*/*.log | awk '{print $9}')
local diff_time
for log in $log_full_path_list; do
if [[ $log_full_path_list != $(ls -l $dir_log/jd_bean_change/*.log | awk '{print $9}') ]]; then
local log_date=$(echo $log | awk -F "/" '{print $NF}' | cut -c1-10) #文件名比文件属性获得的日期要可靠
if [[ $SYSTEM = Macos ]]; then
diff_time=$(($(date +%s) - $(date -j -f "%Y-%m-%d" "$log_date" +%s)))
else
diff_time=$(($(date +%s) - $(date +%s -d "$log_date")))
fi
[[ $diff_time -gt $((${RmLogDaysAgo} * 86400)) ]] && rm -vf $log
fi
done
}
## 删除jup的运行日志
remove_jd_log() {
local date_remove_log date_tmp
if [[ $SYSTEM = Macos ]]; then
date_remove_log=$(date -v-${RmLogDaysAgo}d "+%Y-%m-%d")
else
date_tmp=$(($(date "+%s") - 86400 * ${RmLogDaysAgo}))
date_remove_log=$(date -d "@$date_tmp" "+%Y-%m-%d")
fi
line_end_jup_log=$(($(cat "$dir_log"/jd.log | grep -n "$date_remove_log " | head -1 | awk -F ":" '{print $1}') - 3))
[[ $line_end_jup_log -gt 0 ]] && perl -i -ne "{print unless 1 .. $line_end_jup_log}" $dir_log/jd.log
}
## 删除空文件夹
remove_empty_dir() {
cd $dir_log
for dir in $(ls); do
if [ -d $dir ] && [[ -z $(ls $dir) ]]; then
rm -rf $dir
fi
done
}
CleanLog() {
## 导入配置文件,检测平台
import_config_no_check
## 运行
if [[ ${RmLogDaysAgo} ]]; then
echo -e "查找旧日志文件中...\n"
remove_js_log
remove_jd_log
#remove_empty_dir
echo -e "删除旧日志执行完毕\n"
fi
}
## =================================================3. 记录豆子区 =================================================
BeanChange() {
if [[ -d $dir_log/jd_bean_change ]]; then
## 执行
cd $dir_log/jd_bean_change
for log in $(ls); do
log_date=$(echo $log | cut -c1-10)
bean_date=$(date "+%Y-%m-%d" -d "1 day ago $log_date")
if [[ -z $(grep "$bean_date" $bean_income) ]]; then
echo -n "$bean_date," >>$bean_income
grep -E "昨日收入" $log | grep -oE "\d+" | perl -0777 -pe "s|\n(\d+)|,\1|g" >>$bean_income
fi
if [[ -z $(grep "$bean_date" $bean_outlay) ]]; then
echo -n "$bean_date," >>$bean_outlay
grep -E "昨日支出" $log | grep -oE "\d+" | perl -0777 -pe "s|\n(\d+)|,\1|g" >>$bean_outlay
fi
if [[ -z $(grep "$bean_date" $bean_total) ]]; then
echo -n "$bean_date," >>$bean_total
grep -E "当前京豆" $log | perl -pe "s|\D+(\d+).*|\1|g" | perl -0777 -pe "s|\n(\d+)|,\1|g" >>$bean_total
fi
done
fi
}
## =================================================4. 互助区 =================================================
## 生成pt_pin清单
gen_pt_pin_array() {
local tmp1 tmp2 i pt_pin_temp pt_pin_temp_noturn
for ((user_num = 1; user_num <= $user_sum; user_num++)); do
tmp1=Cookie$user_num
tmp2=${!tmp1}
i=$(($user_num - 1))
pt_pin_temp_noturn=$(echo $tmp2 | perl -pe "{s|.*pt_pin=([^; ]+)(?=;?).*|\1|}")
pt_pin_temp=$(echo $tmp2 | perl -pe "{s|.*pt_pin=([^; ]+)(?=;?).*|\1|; s|%|\\\x|g}")
[[ $pt_pin_temp_noturn == *\\x* ]] && pt_pin_test[i]=$(printf $pt_pin_temp_noturn) || pt_pin_test[i]=$pt_pin_temp_noturn
[[ $pt_pin_temp == *\\x* ]] && pt_pin[i]=$(printf $pt_pin_temp) || pt_pin[i]=$pt_pin_temp
done
}
IsPinValid() {
local j
[ -f $file_cookie ] && . $file_cookie
count_user_sum
gen_pt_pin_array
[[ ! -s $file_key ]] && wget -q https://gitee.com/highdimen/js_tool/raw/A1/resource/encrypto/JD_PIN.key -O $file_key && sha1sum $file_key >$file_key_cry
## [[ -z $(grep -w $file_key_Hash $file_key_cry) ]] >/dev/null 2>&1 && rm -rf $file_key && rm -rf $file_key_cry && echo "密钥错误" && exit 0
for ((j = 0; j <= $user_sum - 1; j++)); do
[ -z $(grep -w ${pt_pin_test[j]} $dir_config/.key) ] >/dev/null 2>&1 && rm -rf $file_key && echo "您的第$(($j + 1))个账号:${pt_pin[j]} 未经授权" && exit 0
## [ -z $(grep -w $(echo -n "${pt_pin[j]}" | sha1sum | cut -f1 -d ' ') $dir_config/.key) ] >/dev/null 2>&1 && rm -rf $file_key && echo "您的第$(($j + 1))个账号:${pt_pin[j]} 未经授权" && exit 0
done
}
## 导出互助码的通用程序,$1:去掉后缀的脚本名称,$2:config.sh中的后缀,$3:活动中文名称
export_codes_sub() {
local task_name=$1
local config_name=$2
local chinese_name=$3
local strictnumset=$4
local config_name_my=My$config_name
local config_name_for_other=ForOther$config_name
local i j k m n pt_pin_in_log code tmp_grep tmp_my_code tmp_for_other user_num random_num_list
local strictnum addnum
addnum=0
## 对输出的助力码进行限制
strictnum=$strictnumset
[ $task_name = jd_fruit ] && strictnum=7
[ $task_name = jd_pet ] && strictnum=7
[ $task_name = jd_plantBean ] && strictnum=7
[ $task_name = jd_dreamFactory ] && strictnum=20
[ $task_name = jd_jdfactory ] && strictnum=20
if cd $dir_log/$task_name &>/dev/null && [[ $(ls) ]]; then
## 寻找所有互助码以及对应的pt_pin
i=0
pt_pin_in_log=()
code=()
pt_pin_and_code=$(ls -r *.log | xargs awk -F '(|)|】' -v var="的${chinese_name}好友互助码" '$3~var {print $2"&"$4}')
for line in $pt_pin_and_code; do
pt_pin_in_log[i]=$(echo $line | awk -F "&" '{print $1}')
code[i]=$(echo $line | awk -F "&" '{print $2}')
let i++
done
## 输出My系列变量
if [[ ${#code[*]} -gt 0 ]]; then
for ((m = 0; m < ${#pt_pin[*]}; m++)); do
tmp_my_code=""
j=$((m + 1))
for ((n = 0; n < ${#code[*]}; n++)); do
if [[ ${pt_pin[m]} == ${pt_pin_in_log[n]} ]]; then
tmp_my_code=${code[n]}
break
fi
done
echo "$config_name_my$j='$tmp_my_code'"
done
else
echo "## 从日志中未找到任何互助码"
fi
## 输出ForOther系列变量
if [[ ${#code[*]} -gt 0 ]]; then
echo
case $HelpType in
0) ## 全部一致
tmp_for_other=""
for ((m = 0; m < ${#pt_pin[*]}; m++)); do
j=$((m + 1))
tmp_for_other="$tmp_for_other@\${$config_name_my$j}"
done
echo "${config_name_for_other}1=\"$tmp_for_other\"" | perl -pe "s|($config_name_for_other\d+=\")@|\1|"
for ((m = 1; m < ${#pt_pin[*]}; m++)); do
j=$((m + 1))
echo "$config_name_for_other$j=\"\${${config_name_for_other}1}\""
done
;;
1) ## 均等助力
for ((m = 0; m < ${#pt_pin[*]}; m++)); do
tmp_for_other=""
j=$((m + 1))
for ((n = $m; n < $(($user_sum + $m)); n++)); do
[[ $m -eq $n ]] && continue
if [[ $((n + 1)) -le $user_sum ]]; then
k=$((n + 1))
else
k=$((n + 1 - $user_sum))
fi
tmp_for_other="$tmp_for_other@\${$config_name_my$k}"
done
echo "$config_name_for_other$j=\"$tmp_for_other\"" | perl -pe "s|($config_name_for_other\d+=\")@|\1|"
done
;;
2) ## 本套脚本内账号间随机顺序助力
for ((m = 0; m < ${#pt_pin[*]}; m++)); do
tmp_for_other=""
random_num_list=$(seq $user_sum | sort -R)
j=$((m + 1))
for ((n = 0; n < $random_num_list && n < strictnum; n++)); do
[[ $j -eq $n ]] && continue
((addnum++))
n=$((addnum))
[[ $addnum -eq $user_sum ]] && addnum=0
tmp_for_other="$tmp_for_other@\${$config_name_my$n}"
done
echo "$config_name_for_other$j=\"$tmp_for_other\"" | perl -pe "s|($config_name_for_other\d+=\")@|\1|"
done
;;
*) ## 按编号优先
for ((m = 0; m < ${#pt_pin[*]}; m++)); do
tmp_for_other=""
j=$((m + 1))
for ((n = 0; n < ${#pt_pin[*]} && n < strictnum; n++)); do
[[ $m -eq $n ]] && continue
((addnum++))
k=$((addnum))
[[ $addnum -eq $user_sum ]] && addnum=0
tmp_for_other="$tmp_for_other@\${$config_name_my$k}"
done
echo "$config_name_for_other$j=\"$tmp_for_other\"" | perl -pe "s|($config_name_for_other\d+=\")@|\1|"
done
;;
esac
fi
else
echo "## 未运行过 $task_name.js 脚本,未产生日志"
fi
}
## 汇总输出
export_all_codes() {
echo -n "# 你选择的互助码模板为:"
case $HelpType in
0)
echo "所有账号助力码全部一致。"
;;
1)
echo "所有账号机会均等助力。"
;;
2)
echo "本套脚本内账号间随机顺序助力。"
;;
*)
echo "按账号编号优先。"
;;
esac
for ((i = 0; i < ${#name_js[*]}; i++)); do
echo -e "\n## ${name_chinese[i]}:"
export_codes_sub "${name_js[i]}" "${name_config[i]}" "${name_chinese[i]}" $strictnumset
done
}
GenHelp() {
## 导入配置文件,检测平台,确定命令
import_config_and_check
count_user_sum
gen_pt_pin_array
[[ $SYSTEM = Android ]] && opt=P || opt=E
## 执行并写入日志
log_time=$(date "+%Y-%m-%d-%H-%M-%S")
#log_path="$dir_code/$log_time.log"
log_path="$dir_code/helpcode"
make_dir "$dir_code"
export_all_codes | perl -pe "{s|京东种豆|种豆|; s|crazyJoy任务|疯狂的JOY|}" | tee $log_path
}
## =================================================5. 面板区 =================================================
PanelOn() {
import_config_no_check
## 预处理
[ ! -s $file_auth_user ] && echo -e "检测到未设置密码,将初始化为用户名:admin,密码:adminadmin\n" && cp -f $file_auth_sample $file_auth_user
[ ! -d $dir_panel/node_modules ] && npm_install_1 $dir_panel && [ $? -ne 0 ] && echo -e "\nnpm install 运行不成功,自动删除 $dir_panel/node_modules 后再次尝试一遍..." && rm -rf $dir_panel/node_modules && rm -rf $dir_panel/yarn.lock
[ -f $dir_panel/package.json ] && PackageListOld=$(cat $dir_panel/package.json)
cd $dir_panel
[[ "${PackageListOld}" != "$(cat package.json)" ]] && echo -e "检测到package.json有变化,运行 npm install...\n" && rm -rf $dir_panel/node_modules && rm -rf $dir_panel/yarn.lock && npm_install_2
## 安装pm2
[ ! $NodeType = nohup ] && [ ! -x "$(command -v pm2)" ] && npm install pm2@latest -g
## 复制ttyd
[ $SYSTEMTYPE = arm ] && [ ! -f $dir_panel/ttyd ] && cp -f $dir_resource/webshellbinary/ttyd.arm $dir_panel/ttyd && [ ! -x $dir_panel/ttyd ] && chmod +x $dir_panel/ttyd
[ ! $SYSTEMTYPE = arm ] && [ ! -f $dir_panel/ttyd ] && cp -f $dir_resource/webshellbinary/ttyd.$(uname -m) $dir_panel/ttyd && [ ! -x $dir_panel/ttyd ] && chmod +x $dir_panel/ttyd
[ -d $dir_panel/node_modules ] && [ ! -x $dir_panel/ttyd ] && echo "不支持Webshell"
PanelOff
#run_hungup
## 运行ttyd和控制面板
cd $dir_panel
[[ ! $SYSTEM = Android ]] && [ ! $NodeType = nohup ] && [ $IsWebShell = true ] && pm2 start $dir_panel/ttyd --name="WebShell" -- -p 9999 -t fontSize=14 -t disableLeaveAlert=true -t rendererType=webgl bash >/dev/null 2>&1 &
[[ $SYSTEM = Android ]] && [ ! $NodeType = nohup ] && [ $IsWebShell = true ] && pm2 start $dir_panel/ttyd --name="WebShell" -- -p 9999 -t fontSize=14 -t disableLeaveAlert=true -t rendererType=webgl /data/data/com.termux/files/usr/bin/bash >/dev/null 2>&1 &
[ ! $NodeType = nohup ] && pm2 start ecosystem.config.js &
[[ ! $SYSTEM = Android ]] && [ $NodeType = nohup ] && [ $IsWebShell = true ] && nohup ./ttyd -p 9999 -t fontSize=14 -t disableLeaveAlert=true -t rendererType=webgl bash >/dev/null 2>&1 &
[[ $SYSTEM = Android ]] && [ $NodeType = nohup ] && [ $IsWebShell = true ] && nohup ./ttyd -p 9999 -t fontSize=14 -t disableLeaveAlert=true -t rendererType=webgl /data/data/com.termux/files/usr/bin/bash >/dev/null 2>&1 &
[ $NodeType = nohup ] && nohup node server.js >/dev/null 2>&1 &
if [[ $? -eq 0 ]]; then
echo -e "确认看过WIKI,打开浏览器,地址为你的127.0.0.1:5678\n"
echo -e "控制面板启动成功,如未修改,则初始用户名和密码为:admin/adminadmin...\n"
else
rm -rf $dir_panel/node_modules && rm -rf $dir_panel/yarn.lock
echo -e "开启失败,请截图并复制错误代码并提交Issues!\n"
fi
}
PanelOff() {
[ ! $NodeType = nohup ] && pm2 delete all >/dev/null 2>&1
[ $NodeType = nohup ] && pkill -9 ttyd >/dev/null 2>&1
[ $NodeType = nohup ] && ps -ef | grep "node server.js" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1
}
## =================================================6. 更新区 =================================================
## 更新crontab,gitee服务器同一时间限制5个链接,因此每个人更新代码必须错开时间,每次执行git_pull随机生成。
## 每天次数随机,更新时间随机,更新秒数随机,至少4次,至多6次,大部分为5次,符合正态分布。
random_update_cron() {
#if [[ $(date "+%-H") -le 4 ]] && [ -f $list_crontab_user ]; then
if [ -f $list_crontab_user ]; then
local random_min=$(gen_random_num 60)
local random_sleep=$(gen_random_num 100)
local random_hour_array[0]=$(gen_random_num 5)
local random_hour=${random_hour_array[0]}
local i j tmp
for ((i = 1; i < 14; i++)); do
j=$(($i - 1))
tmp=$(($(gen_random_num 3) + ${random_hour_array[j]} + 4))
[[ $tmp -lt 24 ]] && random_hour_array[i]=$tmp || break
done
for ((i = 1; i < ${#random_hour_array[*]}; i++)); do
random_hour="$random_hour,${random_hour_array[i]}"
done
#perl -i -pe "s|.+(jd(\.sh update)? .+jd\.log.*)|$random_min $random_hour \* \* \* sleep $random_sleep && \1|" $list_crontab_user
perl -i -pe "s|.+(jd(\.sh update)? .+jd\.log.*)|22,44 \* \* \* \* sleep $random_sleep && \1|" $list_crontab_user
crontab $list_crontab_user
fi
}
## 重置仓库remote url,docker专用,$1:要重置的目录,$2:要重置为的网址
reset_romote_url() {
local dir_current=$(pwd)
local dir_work=$1
local url=$2
if [ -d "$dir/.git" ]; then
cd $dir_work
git remote set-url origin $url >/dev/null
git reset --hard >/dev/null
cd $dir_current
fi
}
## 克隆脚本,$1:仓库地址,$2:仓库保存路径,$3:分支(可省略)
git_clone_scripts() {
local url=$1
local dir=$2
local branch=$3
[[ $branch ]] && local cmd="-b $branch "
echo -e "开始克隆仓库 $url 到 $dir\n"
git clone $cmd $url $dir
exit_status=$?
}
## 更新脚本,$1:仓库保存路径
git_pull_scripts() {
local dir_current=$(pwd)
local dir_work=$1
local branch=$2
cd $dir_work
git config pull.rebase false
echo -e "开始更新仓库:$dir_work\n"
git fetch --all
exit_status=$?
git reset --hard $branch
#git pull --allow-unrelated-histories
cd $dir_current
}
## 克隆scripts2
function Git_CloneScripts2 {
git clone -b master https://gitee.com/highdimen/jd_scripts ${dir_scripts2} >/dev/null 2>&1
ExitStatusScripts2=$?
}
## 更新scripts2
function Git_PullScripts2 {
cd ${dir_scripts2}
git fetch --all >/dev/null 2>&1
ExitStatusScripts2=$?
git reset --hard origin/master >/dev/null 2>&1
}
## 统计 thirdpard 仓库数量
count_thirdpard_repo_sum() {
if [[ -z ${ThirdpardRepoUrl1} ]]; then
thirdpard_repo_sum=0
else
for ((i = 1; i <= 1000; i++)); do
local tmp1=ThirdpardRepoUrl$i
local tmp2=${!tmp1}
[[ $tmp2 ]] && thirdpard_repo_sum=$i || break
done
fi
}
## 形成 thirdpard 仓库的文件夹名清单,依赖于import_config_and_check或import_config_no_check
## array_thirdpard_repo_path:repo存放的绝对路径组成的数组;array_thirdpard_scripts_path:所有要使用的脚本所在的绝对路径组成的数组
gen_thirdpard_dir_and_path() {
local scripts_path_num="-1"
local repo_num tmp1 tmp2 tmp3 tmp4 tmp5 dir
if [[ $thirdpard_repo_sum -ge 1 ]]; then
for ((i = 1; i <= $thirdpard_repo_sum; i++)); do
repo_num=$((i - 1))
tmp1=ThirdpardRepoUrl$i
array_thirdpard_repo_url[$repo_num]=${!tmp1}
tmp2=ThirdpardRepoBranch$i
array_thirdpard_repo_branch[$repo_num]=${!tmp2}
array_thirdpard_repo_dir[$repo_num]=$(echo ${array_thirdpard_repo_url[$repo_num]} | perl -pe "s|\.git||" | awk -F "/|:" '{print $((NF - 1)) "_" $NF}')
array_thirdpard_repo_path[$repo_num]=$dir_thirdpard/${array_thirdpard_repo_dir[$repo_num]}
tmp3=ThirdpardRepoPath$i
if [[ ${!tmp3} ]]; then
for dir in ${!tmp3}; do
let scripts_path_num++
tmp4="${array_thirdpard_repo_dir[repo_num]}/$dir"
tmp5=$(echo $tmp4 | perl -pe "{s|//|/|g; s|/$||}") # 去掉多余的/
array_thirdpard_scripts_path[$scripts_path_num]="$dir_thirdpard/$tmp5"
done
else
let scripts_path_num++
array_thirdpard_scripts_path[$scripts_path_num]="${array_thirdpard_repo_path[$repo_num]}"
fi
done
fi
if [[ ${#ThirdpardRawFile[*]} -ge 1 ]]; then
let scripts_path_num++
array_thirdpard_scripts_path[$scripts_path_num]=$dir_raw # 只有thirdpard脚本所在绝对路径附加了raw文件夹,其他数组均不附加
fi
}
## 生成 jd_scripts task 清单,仅有去掉后缀的文件名
gen_list_task() {
make_dir $dir_list_tmp
grep -E "node.+j[drx]_\w+\.js" $list_crontab_jd_scripts | perl -pe "s|.+(j[drx]_\w+)\.js.+|\1|" | sort -u >$list_task_jd_scripts
#grep -E "node.+j[drx]_\w+\.py" $list_crontab_jd_scripts | perl -pe "s|.+(j[drx]_\w+)\.py.+|\1|" | sort -u >>$list_task_jd_scripts
#grep -E "node.+j[drx]_\w+\.ts" $list_crontab_jd_scripts | perl -pe "s|.+(j[drx]_\w+)\.ts.+|\1|" | sort -u >>$list_task_jd_scripts
grep -E "$cmd_jd j[drx]_\w+" $list_crontab_user | perl -pe "s|.*$cmd_jd (j[drx]_\w+).*|\1|" | sort -u >$list_task_user
}
## 生成 thirdpard 脚本的绝对路径清单
gen_list_thirdpard() {
local dir_current=$(pwd)
local thirdpard_scripts_tmp
rm -f $dir_list_tmp/thirdpard*.list >/dev/null 2>&1
for ((i = 0; i < ${#array_thirdpard_scripts_path[*]}; i++)); do
cd ${array_thirdpard_scripts_path[i]}
if [[ $(ls *.js 2>/dev/null) ]]; then
for file in $(ls *.js); do
if [ -f $file ]; then
perl -ne "print if /.*([\d\*]*[\*-\/,\d]*[\d\*] ){4}[\d\*]*[\*-\/,\d]*[\d\*]( |,|\").*\/?$file/" $file |
perl -pe "s|.*(([\d\*]*[\*-\/,\d]*[\d\*] ){4}[\d\*]*[\*-\/,\d]*[\d\*])( \|,\|\").*/?$file.*|${array_thirdpard_scripts_path[i]}/$file|g" |
sort -u | head -1 >>$list_thirdpard_scripts
fi
done
fi
done
thirdpard_scripts_tmp=$(sort -u $list_thirdpard_scripts)
echo "$thirdpard_scripts_tmp" >$list_thirdpard_scripts
grep -E " $cmd_thirdpard " $list_crontab_user | perl -pe "s|.*$cmd_thirdpard ([^\s]+)( .+\|$)|\1|" | sort -u >$list_thirdpard_user
cd $dir_current
}
## 检测cron的差异,$1:脚本清单文件路径,$2:cron任务清单文件路径,$3:增加任务清单文件路径,$4:删除任务清单文件路径
diff_cron() {
make_dir $dir_list_tmp
local list_scripts="$1"
local list_task="$2"
local list_add="$3"
local list_drop="$4"
if [ -s $list_task ]; then
grep -vwf $list_task $list_scripts >$list_add
elif [ ! -s $list_task ] && [ -s $list_scripts ]; then
cp -f $list_scripts $list_add
fi
if [ -s $list_scripts ]; then
grep -vwf $list_scripts $list_task >$list_drop
else
cp -f $list_task $list_drop
fi
}
## 更新docker-entrypoint,docker专用
update_docker_entrypoint() {
if [[ $JD_DIR ]] && [[ $(diff $dir_root/docker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh) ]]; then
cp -f $dir_root/docker/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
chmod 777 /usr/local/bin/docker-entrypoint.sh
fi
}
## 更新bot.py,docker专用
update_bot_py() {
if [[ $JD_DIR ]] && [[ $ENABLE_TG_BOT == true ]] && [ -f $dir_config/bot.py ] && [[ $(diff $dir_root/bot/bot.py $dir_config/bot.py) ]]; then
cp -f $dir_root/bot/bot.py $dir_config/bot.py
fi
}
## 检测配置文件版本
detect_config_version() {
## 识别出两个文件的版本号
ver_config_sample=$(grep " Version: " $file_config_sample | perl -pe "s|.+v((\d+\.?){3})|\1|")
[ -f $file_config_user ] && ver_config_user=$(grep " Version: " $file_config_user | perl -pe "s|.+v((\d+\.?){3})|\1|")
## 删除旧的发送记录文件
[ -f $send_mark ] && [[ $(cat $send_mark) != $ver_config_sample ]] && rm -f $send_mark
## 识别出更新日期和更新内容
update_date=$(grep " Date: " $file_config_sample | awk -F ": " '{print $2}')
update_content=$(grep " Update Content: " $file_config_sample | awk -F ": " '{print $2}')
## 如果是今天,并且版本号不一致,则发送通知
if [ -f $file_config_user ] && [[ $ver_config_user != $ver_config_sample ]] && [[ $update_date == $(date "+%Y-%m-%d") ]]; then
if [ ! -f $send_mark ]; then
local notify_title="配置文件更新通知"
local notify_content="更新日期: $update_date\n用户版本: $ver_config_user\n新的版本: $ver_config_sample\n更新内容: $update_content\n更新说明: 如需使用新功能请对照config.sh.sample,将相关新参数手动增加到你自己的config.sh中,否则请无视本消息。本消息只在该新版本配置文件更新当天发送一次。\n"
echo -e $notify_content
notify "$notify_title" "$notify_content"
[[ $? -eq 0 ]] && echo $ver_config_sample >$send_mark
fi
else
[ -f $send_mark ] && rm -f $send_mark
fi
}
## npm install 子程序,判断是否为安卓,判断是否安装有yarn
npm_install_sub() {
local cmd_1 cmd_2
if [[ $(type pnpm) ]] >/dev/null 2>&1; then
cmd_1=pnpm
elif [[ $(type yarn) ]] >/dev/null 2>&1; then
cmd_1=yarn
else
cmd_1=npm
fi
[[ $SYSTEM = Android ]] && cmd_2="--no-bin-links" || cmd_2=""
$cmd_1 install $cmd_2
}
## npm install,$1:package.json文件所在路径
npm_install_1() {
local dir_current=$(pwd)
local dir_work=$1
cd $dir_work
echo -e "运行 npm install...\n"
npm_install_sub
[[ $? -ne 0 ]] && echo -e "\nnpm install 运行不成功,请进入 $dir_work 目录后手动运行 npm install...\n"
cd $dir_current
}
npm_install_2() {
local dir_current=$(pwd)
local dir_work=$1
cd $dir_work
echo -e "检测到 $dir_work 的依赖包有变化,运行 npm install...\n"
npm_install_sub
[[ $? -ne 0 ]] && echo -e "\n安装 $dir_work 的依赖包运行不成功,再次尝试一遍...\n"
npm_install_1 $dir_work
cd $dir_current
}
## 输出是否有新的或失效的定时任务,$1:新的或失效的任务清单文件路径,$2:新/失效
output_list_add_drop() {
local list=$1
local type=$2
if [ -s $list ]; then
echo -e "检测到有$type的定时任务:\n"
cat $list
echo
fi
}
## 自动删除失效的脚本与定时任务,需要:1.AutoDelCron/AutoDelThirdpardCron 设置为 true;2.正常更新js脚本,没有报错;3.存在失效任务;4.crontab.list存在并且不为空
## $1:失效任务清单文件路径,$2:jd/jd
del_cron() {
local list_drop=$1
local type=$2
local detail type2 detail2
if [ -s $list_drop ] && [ -s $list_crontab_user ]; then
detail=$(cat $list_drop)