diff --git a/scripts/init.sh b/scripts/init.sh index 0e2c3cf..9c74b69 100644 --- a/scripts/init.sh +++ b/scripts/init.sh @@ -177,53 +177,38 @@ mv -f /tmp/SC_tmp/* ${CRASHDIR} 2>/dev/null mkdir -p ${CRASHDIR}/configs [ -f "${CRASHDIR}/configs/ShellCrash.cfg" ] || echo '#ShellCrash配置文件,不明勿动!' >${CRASHDIR}/configs/ShellCrash.cfg #判断系统类型写入不同的启动文件 +[ -w /usr/lib/systemd/system ] && sysdir=/usr/lib/systemd/system +[ -w /etc/systemd/system ] && sysdir=/etc/systemd/system if [ -f /etc/rc.common -a "$(cat /proc/1/comm)" = "procd" ]; then #设为init.d方式启动 cp -f ${CRASHDIR}/shellcrash.procd /etc/init.d/shellcrash chmod 755 /etc/init.d/shellcrash -else - [ -w /usr/lib/systemd/system ] && sysdir=/usr/lib/systemd/system - [ -w /etc/systemd/system ] && sysdir=/etc/systemd/system - if [ -n "$sysdir" -a "$USER" = "root" -a "$(cat /proc/1/comm)" = "systemd" ]; then - #创建shellcrash用户 - userdel shellcrash 2>/dev/null - sed -i '/0:7890/d' /etc/passwd - sed -i '/x:7890/d' /etc/group - if useradd -h >/dev/null 2>&1; then - useradd shellcrash -u 7890 2>/dev/null - sed -Ei s/7890:7890/0:7890/g /etc/passwd - else - echo "shellcrash:x:0:7890::/home/shellcrash:/bin/sh" >>/etc/passwd - fi - #配置systemd - mv -f ${CRASHDIR}/shellcrash.service $sysdir/shellcrash.service 2>/dev/null - sed -i "s%/etc/ShellCrash%$CRASHDIR%g" $sysdir/shellcrash.service - rm -rf $sysdir/clash.service #旧版文件清理 - systemctl daemon-reload + rm -rf ${CRASHDIR}/shellcrash.openrc +elif [ -n "$sysdir" -a "$USER" = "root" -a "$(cat /proc/1/comm)" = "systemd" ]; then + #创建shellcrash用户 + userdel shellcrash 2>/dev/null + sed -i '/0:7890/d' /etc/passwd + sed -i '/x:7890/d' /etc/group + if useradd -h >/dev/null 2>&1; then + useradd shellcrash -u 7890 2>/dev/null + sed -Ei s/7890:7890/0:7890/g /etc/passwd else - #设为保守模式启动 - systemctl disable shellcrash 2>/dev/null - setconfig start_old 已开启 + echo "shellcrash:x:0:7890::/home/shellcrash:/bin/sh" >>/etc/passwd fi + #配置systemd + mv -f ${CRASHDIR}/shellcrash.service $sysdir/shellcrash.service 2>/dev/null + sed -i "s%/etc/ShellCrash%$CRASHDIR%g" $sysdir/shellcrash.service + systemctl daemon-reload +elif rc-status -r >/dev/null 2>&1; then + #设为openrc方式启动 + cp -f ${CRASHDIR}/shellcrash.openrc /etc/init.d/shellcrash + chmod 755 /etc/init.d/shellcrash + rm -rf ${CRASHDIR}/shellcrash.procd +else + #设为保守模式启动 + setconfig start_old 已开启 fi -# 在此处添加Alpine OpenRC配置(约第220-230行附近) -# Alpine Linux (OpenRC) 自启动配置 -if [ -f "/sbin/openrc" ] && [ "$(cat /proc/1/comm)" = "init" ]; then - # 创建启动脚本 - if [ ! -f "/etc/local.d/shellcrash.start" ]; then - echo "#!/bin/sh" > /etc/local.d/shellcrash.start - echo "${CRASHDIR}/start.sh start &" >> /etc/local.d/shellcrash.start - chmod +x /etc/local.d/shellcrash.start - fi - # 添加到默认运行级别 - rc-update add local default >/dev/null 2>&1 - setconfig autostart "enable" - logger "已配置Alpine系统自启动" 32 -fi - - - #修饰文件及版本号 command -v bash >/dev/null 2>&1 && shtype=bash [ -x /bin/ash ] && shtype=ash @@ -232,7 +217,7 @@ for file in start.sh task.sh menu.sh; do chmod 755 ${CRASHDIR}/${file} done setconfig versionsh_l $version -#生成用于执行systemd及procd服务的变量文件 +#生成用于执行启动服务的变量文件 [ ! -f ${CRASHDIR}/configs/command.env ] && { TMPDIR='/tmp/ShellCrash' BINDIR=${CRASHDIR} @@ -351,7 +336,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 shellcrash.rc core.new clashservice log shellcrash.service mark? mark.bak; do +for file in CrashCore clash.sh getdate.sh core.new clashservice log shellcrash.service mark? mark.bak; do rm -rf ${CRASHDIR}/$file done #旧版变量改名 diff --git a/scripts/menu.sh b/scripts/menu.sh index 6e4953c..ed2ecda 100644 --- a/scripts/menu.sh +++ b/scripts/menu.sh @@ -63,6 +63,8 @@ ckstatus() { [ -n "$(find /etc/rc.d -name '*shellcrash')" ] && autostart=enable || autostart=disable elif ckcmd systemctl; then [ "$(systemctl is-enabled shellcrash.service 2>&1)" = enabled ] && autostart=enable || autostart=disable + elif rc-status -r >/dev/null 2>&1; then + rc-update show default | grep -q "shellcrash" && autostart=enable || autostart=disable else [ -f ${CRASHDIR}/.dis_startup ] && autostart=disable || autostart=enable fi @@ -1169,7 +1171,7 @@ setboot() { #启动相关设置 echo ----------------------------------------------- echo -e "\033[30;47m欢迎使用启动设置菜单:\033[0m" echo ----------------------------------------------- - echo -e " 1 ${auto_set}\033[0mShellCrash开机启动(alpine需先禁用再启用)" + echo -e " 1 ${auto_set}\033[0mShellCrash开机启动" echo -e " 2 使用保守模式: \033[36m$start_old\033[0m ————基于定时任务(每分钟检测)" echo -e " 3 设置自启延时: \033[36m$delay\033[0m ————用于解决自启后服务受限" echo -e " 4 启用小闪存模式: \033[36m$mini_clash\033[0m ————用于闪存空间不足的设备" @@ -1186,8 +1188,7 @@ setboot() { #启动相关设置 # 禁止自启动:删除各系统的启动项 [ -d /etc/rc.d ] && cd /etc/rc.d && rm -rf *shellcrash >/dev/null 2>&1 && cd - >/dev/null ckcmd systemctl && systemctl disable shellcrash.service >/dev/null 2>&1 - # 新增:删除Alpine的启动脚本 - [ -f "/sbin/openrc" ] && rm -f /etc/local.d/shellcrash.start + [rc-status -r >/dev/null 2>&1 && rc-update del shellcrash default >/dev/null touch ${CRASHDIR}/.dis_startup autostart=disable echo -e "\033[33m已禁止ShellCrash开机启动!\033[0m" @@ -1195,15 +1196,7 @@ setboot() { #启动相关设置 # 允许自启动:配置各系统的启动项 [ -f /etc/rc.common -a "$(cat /proc/1/comm)" = "procd" ] && /etc/init.d/shellcrash enable ckcmd systemctl && systemctl enable shellcrash.service >/dev/null 2>&1 - # 新增:配置Alpine的启动脚本 - if [ -f "/sbin/openrc" ]; then - [ ! -f "/etc/local.d/shellcrash.start" ] && { - echo "#!/bin/sh" > /etc/local.d/shellcrash.start - echo "${CRASHDIR}/start.sh start &" >> /etc/local.d/shellcrash.start - chmod +x /etc/local.d/shellcrash.start - } - rc-update add local default >/dev/null 2>&1 - fi + rc-status -r >/dev/null 2>&1 && rc-update add shellcrash default >/dev/null rm -rf ${CRASHDIR}/.dis_startup autostart=enable echo -e "\033[32m已设置ShellCrash开机启动!\033[0m" @@ -1219,7 +1212,7 @@ setboot() { #启动相关设置 setconfig start_old $start_old ${CRASHDIR}/start.sh stop else - if [ "$(cat /proc/1/comm)" = "procd" -o "$(cat /proc/1/comm)" = "systemd" ]; then + if grep -qE 'procd|systemd' /proc/1/comm || rc-status -r >/dev/null 2>&1; then echo -e "\033[32m改为使用系统守护进程启动服务!!\033[0m" ${CRASHDIR}/start.sh cronset "ShellCrash初始化" start_old=未开启 @@ -2060,7 +2053,7 @@ tools() { [ -f /etc/firewall.user ] && echo -e " 4 \033[32m配置\033[0m外网访问SSH" [ -x /usr/sbin/otapredownload ] && echo -e " 5 \033[33m$mi_update\033[0m小米系统自动更新" [ "$systype" = "mi_snapshot" ] && echo -e " 6 小米设备软固化SSH ———— \033[$mi_autoSSH_type \033[0m" - [ -d /etc/ddns ] && echo -e " 7 配置\033[32mDDNS服务\033[0m(需下载相关脚本)" + [ -f /etc/config/ddns ] && echo -e " 7 配置\033[32mDDNS服务\033[0m(需下载相关脚本)" [ "$systype" = "mi_snapshot" ] && echo -e " 8 小米设备Tun模块修复 ———— \033[$mi_tunfix \033[0m" echo ----------------------------------------------- echo -e " 0 返回上级菜单" @@ -2093,12 +2086,12 @@ tools() { ${CRASHDIR}/start.sh get_bin ${TMPDIR}/ShellDDNS.sh tools/ShellDDNS.sh if [ "$?" = "0" ]; then mv -f ${TMPDIR}/ShellDDNS.sh ${CRASHDIR}/tools/ShellDDNS.sh - source ${CRASHDIR}/tools/ShellDDNS.sh + . ${CRASHDIR}/tools/ShellDDNS.sh else echo -e "\033[31m文件下载失败!\033[0m" fi else - source ${CRASHDIR}/tools/ShellDDNS.sh + . ${CRASHDIR}/tools/ShellDDNS.sh fi sleep 1 tools diff --git a/scripts/shellcrash.openrc b/scripts/shellcrash.openrc new file mode 100644 index 0000000..36de2bd --- /dev/null +++ b/scripts/shellcrash.openrc @@ -0,0 +1,74 @@ +#!/sbin/openrc-run + +# 服务名 +name="ShellCrash" +description="Custom proxy service for ShellCrash" +#获取目录 +CRASHDIR=$(cat /etc/profile | grep CRASHDIR | awk -F "\"" '{print $2}') +[ -z "$CRASHDIR" ] && CRASHDIR=$(cat ~/.bashrc | grep CRASHDIR | awk -F "\"" '{print $2}') +source ${CRASHDIR}/configs/command.env #加载启动命令和启动目录 + +# PID 文件 +pidfile="/run/shellcrash.pid" + +depend() { + #need net + after firewall +} + +start() { + ebegin "Starting ShellCrash service" + + # 如果 firewal_area=5 则运行主旁转发脚本 + if grep -q 'firewall_area=5' "$CRASHDIR/configs/ShellCrash.cfg" 2>/dev/null; then + "$CRASHDIR/start.sh" start_firewall + eend $? "Firewall start failed" + return + fi + + # 确定运行用户:shellcrash 或 root + if grep -q 'shellcrash:x:0:7890' /etc/passwd; then + runuser="shellcrash" + else + runuser="root" + fi + + # 必要文件检测 + "$CRASHDIR/start.sh" bfstart + if [ "$?" != "0" ]; then + eend 1 "bfstart check failed" + return + fi + + # 启动主程序(后台进程) + start-stop-daemon --start \ + --background \ + --make-pidfile \ + --pidfile "${pidfile}" \ + --user "${runuser}" \ + --exec ${COMMAND%% *} -- ${COMMAND#* } + + ret=$? + eend $ret + [ $ret -ne 0 ] && return + + # 启动后操作 + "$CRASHDIR/start.sh" afstart & +} + +stop() { + ebegin "Stopping ShellCrash service" + + # 停止后台进程 + start-stop-daemon --stop \ + --pidfile "${pidfile}" \ + --retry 5 + + rm -f "${pidfile}" + + # 清理 firewall、proxy + "$CRASHDIR/start.sh" stop_firewall + "$CRASHDIR/start.sh" unset_proxy + + eend $? +} diff --git a/scripts/start.sh b/scripts/start.sh index 58dc988..b155ae9 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -2032,6 +2032,8 @@ start) systemctl daemon-reload systemctl start shellcrash.service || start_error } + elif rc-status -r >/dev/null 2>&1; then + rc-service shellcrash start >/dev/null 2>&1 else bfstart && start_old fi @@ -2052,6 +2054,8 @@ stop) systemctl stop shellcrash.service >/dev/null 2>&1 elif [ -f /etc/rc.common -a "$(cat /proc/1/comm)" = "procd" ]; then /etc/init.d/shellcrash stop >/dev/null 2>&1 + elif rc-status -r >/dev/null 2>&1; then + rc-service shellcrash stop >/dev/null 2>&1 else stop_firewall #清理路由策略 unset_proxy #禁用本机代理