diff --git a/scripts/init.sh b/scripts/init.sh index 24efc405..6393d512 100644 --- a/scripts/init.sh +++ b/scripts/init.sh @@ -128,11 +128,18 @@ fi #镜像化OpenWrt(snapshot)额外设置 if [ "$systype" = "mi_snapshot" -o "$systype" = "ng_snapshot" ]; then chmod 755 "$CRASHDIR"/starts/snapshot_init.sh - uci delete firewall.ShellClash 2>/dev/null + if [ "$systype" = "mi_snapshot" ];then + path="/data/shellcrash_init.sh" + setconfig CRASHDIR "$CRASHDIR" "$CRASHDIR"/starts/snapshot_init.sh + mv -f "$CRASHDIR"/starts/snapshot_init.sh "$path" + else + path="$CRASHDIR"/starts/snapshot_init.sh + fi + uci delete firewall.auto_ssh 2>/dev/null uci delete firewall.ShellCrash 2>/dev/null uci set firewall.ShellCrash=include uci set firewall.ShellCrash.type='script' - uci set firewall.ShellCrash.path="$CRASHDIR/starts/snapshot_init.sh" + uci set firewall.ShellCrash.path="$path" uci set firewall.ShellCrash.enabled='1' uci commit firewall else @@ -205,7 +212,7 @@ sed -i '/shellclash/d' /etc/group rm -rf /etc/init.d/clash rm -rf "$CRASHDIR"/rules [ "$systype" = "mi_snapshot" -a "$CRASHDIR" != '/data/clash' ] && rm -rf /data/clash -for file in CrashCore clash.sh getdate.sh core.new clashservice log mark? mark.bak; do +for file in tools webget.sh misnap_init.sh core.new; do rm -rf "$CRASHDIR/$file" done #旧版变量改名 diff --git a/scripts/menus/7_gateway.sh b/scripts/menus/7_gateway.sh index cb7ae627..7ca00e7e 100644 --- a/scripts/menus/7_gateway.sh +++ b/scripts/menus/7_gateway.sh @@ -81,7 +81,12 @@ set_fw_wan() { read -p "请输入对应数字 > " num case $num in 1) - [ "$fw_wan" = ON ] && fw_wan=OFF || fw_wan=ON + if [ "$fw_wan" = ON ];then + read -p "确认关闭防火墙?这会带来极大的安全隐患!(1/0) > " res + [ "$res" = 1 ] && fw_wan=OFF || fw_wan=ON + else + fw_wan=ON + fi setconfig fw_wan "$fw_wan" set_fw_wan ;; @@ -156,13 +161,10 @@ set_bot_tg_init(){ set_bot_tg_service(){ if [ "$bot_tg_service" = ON ];then bot_tg_service=OFF - [ -f "$TMPDIR/bot_tg.pid" ] && kill -TERM -"$(cat "$TMPDIR/bot_tg.pid")" && rm -f "$TMPDIR/bot_tg.pid" + . "$CRASHDIR"/menus/bot_tg_service.sh && bot_tg_stop else bot_tg_service=ON - [ -n "$(pidof CrashCore)" ] && { - setsid sh "$CRASHDIR/menus/bot_tg.sh" & - echo $! > "$TMPDIR/bot_tg.pid" - } + [ -n "$(pidof CrashCore)" ] && . "$CRASHDIR"/menus/bot_tg_service.sh && bot_tg_start fi setconfig bot_tg_service "$bot_tg_service" } diff --git a/scripts/menus/bot_tg_service.sh b/scripts/menus/bot_tg_service.sh new file mode 100644 index 00000000..6ed6239b --- /dev/null +++ b/scripts/menus/bot_tg_service.sh @@ -0,0 +1,13 @@ + +. "$CRASHDIR"/libs/set_cron.sh + +bot_tg_start(){ + . "$CRASHDIR"/starts/start_legacy.sh + start_legacy "$CRASHDIR/menus/bot_tg.sh" 'bot_tg' + cronset 'TG_BOT守护进程' "*/10 * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh bot_tg #ShellCrash-TG_BOT守护进程" +} +bot_tg_stop(){ + cronset 'TG_BOT守护进程' + [ -f "$TMPDIR/bot_tg.pid" ] && kill -TERM "$(cat "$TMPDIR/bot_tg.pid")" + rm -f "$TMPDIR/bot_tg.pid" +} \ No newline at end of file diff --git a/scripts/menus/set_crashdir.sh b/scripts/menus/set_crashdir.sh index e6ed36e5..2f1be6c9 100644 --- a/scripts/menus/set_crashdir.sh +++ b/scripts/menus/set_crashdir.sh @@ -2,6 +2,7 @@ # Copyright (C) Juewuy . /tmp/SC_tmp/libs/check_dir_avail.sh +[ -n "$(echo -e | grep e)" ] && echo=echo || echo='echo -e' set_crashdir() { set_usb_dir() { diff --git a/scripts/menus/uninstall.sh b/scripts/menus/uninstall.sh index fcd5d128..06392a7d 100644 --- a/scripts/menus/uninstall.sh +++ b/scripts/menus/uninstall.sh @@ -54,8 +54,6 @@ uninstall() { userdel -r shellcrash 2>/dev/null nvram set script_usbmount="" 2>/dev/null nvram commit 2>/dev/null - uci delete firewall.ShellCrash 2>/dev/null - uci commit firewall 2>/dev/null echo "-----------------------------------------------" echo -e "\033[36m已卸载ShellCrash相关文件!有缘再会!\033[0m" echo -e "\033[33m请手动关闭当前窗口以重置环境变量!\033[0m" diff --git a/scripts/start.sh b/scripts/start.sh index 3e35f3ec..8a6ba343 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -25,15 +25,11 @@ stop_firewall(){ "$CRASHDIR"/starts/fw_stop.sh } #保守模式启动 -start_old(){ - #使用传统后台执行二进制文件的方式执行 - if ckcmd su && [ -n "$(grep 'shellcrash:x:0:7890' /etc/passwd)" ]; then - su shellcrash -c "$COMMAND >/dev/null 2>&1" & - else - ckcmd nohup && local nohup=nohup - $nohup $COMMAND >/dev/null 2>&1 & - fi - afstart & +start_l(){ + bfstart && { + . "$CRASHDIR"/starts/start_legacy.sh + start_legacy "$COMMAND" 'shellcrash' + } && afstart & } case "$1" in @@ -45,10 +41,10 @@ start) if [ "$firewall_area" = "5" ]; then #主旁转发 . "$CRASHDIR"/starts/fw_start.sh elif [ "$start_old" = "已开启" ]; then - bfstart && start_old - elif [ -f /etc/rc.common -a "$(cat /proc/1/comm)" = "procd" ]; then + start_l + elif [ -f /etc/rc.common ] && grep -q 'procd' /proc/1/comm; then /etc/init.d/shellcrash start - elif [ "$USER" = "root" -a "$(cat /proc/1/comm)" = "systemd" ]; then + elif [ "$USER" = "root" ] && grep -q 'systemd' /proc/1/comm; then FragmentPath=$(systemctl show -p FragmentPath shellcrash | sed 's/FragmentPath=//') [ -f $FragmentPath ] && { setconfig ExecStart "$COMMAND >/dev/null" "$FragmentPath" @@ -64,7 +60,7 @@ start) rc-service shellcrash stop >/dev/null 2>&1 rc-service shellcrash start else - bfstart && start_old + start_l fi ;; stop) @@ -75,11 +71,14 @@ stop) cronset '运行时每' cronset '流媒体预解析' #停止tg机器人 - [ -f "$TMPDIR/bot_tg.pid" ] && kill -TERM -"$(cat "$TMPDIR/bot_tg.pid")" && rm -f "$TMPDIR/bot_tg.pid" + . "$CRASHDIR"/menus/bot_tg_service.sh && bot_tg_stop #多种方式结束进程 - if [ "$start_old" != "已开启" -a "$USER" = "root" -a "$(cat /proc/1/comm)" = "systemd" ]; then + if [ -f "$TMPDIR/shellcrash.pid" ];then + kill -TERM "$(cat "$TMPDIR/shellcrash.pid")" + rm -f "$TMPDIR/shellcrash.pid" + elif [ "$USER" = "root" ] && grep -q 'systemd' /proc/1/comm; then systemctl stop shellcrash.service >/dev/null 2>&1 - elif [ -f /etc/rc.common -a "$(cat /proc/1/comm)" = "procd" ]; then + elif [ -f /etc/rc.common ] && grep -q 'procd' /proc/1/comm; then /etc/init.d/shellcrash stop >/dev/null 2>&1 elif grep -q 's6' /proc/1/comm; then /command/s6-svc -d /run/service/shellcrash @@ -89,7 +88,7 @@ stop) else stop_firewall #清理路由策略 fi - PID=$(pidof CrashCore) && [ -n "$PID" ] && kill -9 $PID >/dev/null 2>&1 + PID=$(pidof CrashCore) && [ -n "$PID" ] && ckcmd killall && killall CrashCore >/dev/null #清理缓存目录 rm -rf "$TMPDIR"/CrashCore ;; diff --git a/scripts/starts/afstart.sh b/scripts/starts/afstart.sh index aab89dfd..d2d566ce 100644 --- a/scripts/starts/afstart.sh +++ b/scripts/starts/afstart.sh @@ -40,7 +40,7 @@ if [ -n "$test" -o -n "$(pidof CrashCore)" ]; then cronset '2fjdi124dd12s' "$line" done <"$CRASHDIR"/task/running } - [ "$start_old" = "已开启" ] && cronset '保守模式守护进程' "* * * * * test -z \"\$(pidof CrashCore)\" && $CRASHDIR/start.sh daemon #ShellCrash保守模式守护进程" + [ "$start_old" = "已开启" ] && cronset '保守模式守护进程' "* * * * * /bin/sh $CRASHDIR/starts/start_legacy_wd.sh shellcrash #ShellCrash保守模式守护进程" #加载条件任务 [ -s "$CRASHDIR"/task/afstart ] && { . "$CRASHDIR"/task/afstart; } & [ -s "$CRASHDIR"/task/affirewall -a -s /etc/init.d/firewall -a ! -f /etc/init.d/firewall.bak ] && { @@ -51,10 +51,7 @@ if [ -n "$test" -o -n "$(pidof CrashCore)" ]; then sed -i "${line}a\\. $CRASHDIR/task/affirewall" /etc/init.d/firewall } & #启动TG机器人 - [ "$bot_tg_service" = ON ] && { - setsid sh "$CRASHDIR/menus/bot_tg.sh" & - echo $! > "$TMPDIR/bot_tg.pid" - } + [ "$bot_tg_service" = ON ] && . "$CRASHDIR"/menus/bot_tg_service.sh && bot_tg_start exit 0 else . "$CRASHDIR"/starts/start_error.sh diff --git a/scripts/starts/snapshot_init.sh b/scripts/starts/snapshot_init.sh index 8a1a69d4..f6a6688e 100644 --- a/scripts/starts/snapshot_init.sh +++ b/scripts/starts/snapshot_init.sh @@ -1,9 +1,9 @@ #!/bin/sh # Copyright (C) Juewuy -CRASHDIR="$(uci get firewall.ShellCrash.path | sed 's/\/misnap_init.sh//')" -profile=/etc/profile -. "$CRASHDIR"/configs/ShellCrash.cfg +CRASHDIR="$(uci get firewall.ShellCrash.path | sed 's/\/starts\/snapshot_init.sh//')" + +[ -f "$CRASHDIR"/configs/ShellCrash.cfg ] && . "$CRASHDIR"/configs/ShellCrash.cfg autoSSH(){ #自动开启SSH @@ -14,7 +14,6 @@ autoSSH(){ [ -z "$(pidof dropbear)" -o -z "$(netstat -ntul | grep :22)" ] && { sed -i 's/channel=.*/channel="debug"/g' /etc/init.d/dropbear /etc/init.d/dropbear restart - mi_autoSSH_pwd=$(grep 'mi_autoSSH_pwd=' $CRASHDIR/configs/ShellCrash.cfg | awk -F "=" '{print $2}') [ -n "$mi_autoSSH_pwd" ] && echo -e "$mi_autoSSH_pwd\n$mi_autoSSH_pwd" | passwd root } #配置nvram @@ -22,8 +21,8 @@ autoSSH(){ [ "$(nvram get telnet_en)" = 0 ] && nvram set telnet_en=1 nvram commit &> /dev/null #备份还原SSH秘钥 - [ -f $CRASHDIR/configs/dropbear_rsa_host_key ] && ln -sf $CRASHDIR/configs/dropbear_rsa_host_key /etc/dropbear/dropbear_rsa_host_key - [ -f $CRASHDIR/configs/authorized_keys ] && ln -sf $CRASHDIR/configs/authorized_keys /etc/dropbear/authorized_keys + [ -f "$CRASHDIR"/configs/dropbear_rsa_host_key ] && ln -sf "$CRASHDIR"/configs/dropbear_rsa_host_key /etc/dropbear/dropbear_rsa_host_key + [ -f "$CRASHDIR"/configs/authorized_keys ] && ln -sf "$CRASHDIR"/configs/authorized_keys /etc/dropbear/authorized_keys } tunfix(){ ko_dir=$(modinfo ip_tables | grep -Eo '/lib/modules.*/ip_tables.ko' | sed 's|/ip_tables.ko||' ) @@ -33,7 +32,7 @@ tunfix(){ mkdir -p /tmp/overlay/work mount -o noatime,lowerdir=${ko_dir},upperdir=/tmp/overlay/upper,workdir=/tmp/overlay/work -t overlay "overlay_mods_only" ${ko_dir} #将tun.ko链接到lib - ln -sf $CRASHDIR/tools/tun.ko ${ko_dir}/tun.ko || $CRASHDIR/start.sh loggger "小米Tun模块修复失败!" + ln -sf "$CRASHDIR"/tools/tun.ko ${ko_dir}/tun.ko } tproxyfix(){ sed -i 's/sysctl -w net.bridge.bridge-nf-call-ip/#sysctl -w net.bridge.bridge-nf-call-ip/g' /etc/init.d/qca-nss-ecm @@ -47,7 +46,28 @@ auto_clean(){ /etc/init.d/stat_points stop 2>/dev/null /etc/init.d/stat_points disable 2>/dev/null sed -i '\#/logrotate#{ /^[[:space:]]*#/!s/^/#ShellCrash自动注释 / }' /etc/crontabs/root - rm -rf /data/usr/log + sed -i '\#/sec_cfg_bak#{ /^[[:space:]]*#/!s/^/#ShellCrash自动注释 / }' /etc/crontabs/root + rm -rf /data/usr/log /data/usr/sec_cfg + +} +auto_start(){ + #设置init.d服务 + cp -f "$CRASHDIR"/shellcrash.procd /etc/init.d/shellcrash + chmod 755 /etc/init.d/shellcrash + #初始化环境变量 + . "$CRASHDIR"/libs/set_profile.sh && set_profile '/etc/profile' + #启动服务 + if [ ! -f "$CRASHDIR"/.dis_startup ]; then + #AX6S/AX6000修复tun功能 + [ -s "$CRASHDIR"/tools/tun.ko ] && tunfix + #小米7000/小米万兆修复tproxy + [ -f /etc/init.d/qca-nss-ecm ] && [ "$redir_mod" = 'Tproxy' ] && tproxyfix + #自动覆盖根证书文件 + [ -s "$CRASHDIR"/tools/ca-certificates.crt ] && cp -f "$CRASHDIR"/tools/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt + #启动服务 + "$CRASHDIR"/start.sh start + /etc/init.d/shellcrash enable + fi } init(){ #等待启动完成 @@ -55,29 +75,9 @@ init(){ sleep 10 done sleep 20 - #初始化环境变量 - [ -z "$my_alias" ] && my_alias=crash - sed -i "/ShellCrash\/menu.sh/"d "$profile" - echo "alias ${my_alias}=\"sh $CRASHDIR/menu.sh\"" >>"$profile" - sed -i "/export CRASHDIR/d" "$profile" - echo "export CRASHDIR=\"$CRASHDIR\"" >>"$profile" autoSSH #软固化功能 auto_clean #自动清理 - #设置init.d服务 - cp -f $CRASHDIR/shellcrash.procd /etc/init.d/shellcrash - chmod 755 /etc/init.d/shellcrash - #启动服务 - if [ ! -f $CRASHDIR/.dis_startup ]; then - #AX6S/AX6000修复tun功能 - [ -s $CRASHDIR/tools/tun.ko ] && tunfix - #小米7000/小米万兆修复tproxy - [ -f /etc/init.d/qca-nss-ecm ] && [ -n "$(grep 'redir_mod=Tproxy' $CRASHDIR/configs/ShellCrash.cfg )" ] && tproxyfix - #自动覆盖根证书文件 - [ -s $CRASHDIR/tools/ca-certificates.crt ] && cp -f $CRASHDIR/tools/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt - #启动服务 - $CRASHDIR/start.sh start - /etc/init.d/shellcrash enable - fi + [ -s "$CRASHDIR"/start.sh ] && auto_start } case "$1" in diff --git a/scripts/starts/start_legacy.sh b/scripts/starts/start_legacy.sh new file mode 100644 index 00000000..b4147622 --- /dev/null +++ b/scripts/starts/start_legacy.sh @@ -0,0 +1,14 @@ + +. "$CRASHDIR"/libs/check_cmd.sh + +start_legacy(){ + if ckcmd su && grep -q 'shellcrash:x:0:7890' /etc/passwd; then + su shellcrash -c "$1 >/dev/null 2>&1 & echo \$! > /tmp/ShellCrash/$2.pid" + elif ckcmd nohup; then + nohup $1 >/dev/null 2>&1 & + echo $! > "/tmp/ShellCrash/$2.pid" + else + $1 >/dev/null 2>&1 & + echo $! > "/tmp/ShellCrash/$2.pid" + fi +} diff --git a/scripts/starts/start_legacy_wd.sh b/scripts/starts/start_legacy_wd.sh new file mode 100644 index 00000000..b81252eb --- /dev/null +++ b/scripts/starts/start_legacy_wd.sh @@ -0,0 +1,16 @@ + +PIDFILE="/tmp/ShellCrash/$1.pid" + +if [ -f "$PIDFILE" ]; then + PID="$(cat "$PIDFILE")" + if [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; then + return 0 + else + if [ "$1" = shellcrash ];then + "$CRASHDIR"/start.sh start + else + . "$CRASHDIR"/starts/start_legacy.sh + start_legacy "$CRASHDIR/menus/bot_tg.sh" "$1" + fi + fi +fi