From 23bf91d771dc044029805f8edca3dddd1205c02e Mon Sep 17 00:00:00 2001 From: 233boy <31657650+233boy@users.noreply.github.com> Date: Wed, 25 Jun 2025 11:02:59 +0800 Subject: [PATCH] support set log level --- install.sh | 2 +- sing-box.sh | 2 +- src/core.sh | 26 +++++++++++--------------- src/download.sh | 2 +- src/import.sh | 6 +++--- src/init.sh | 4 ++-- src/log.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 src/log.sh diff --git a/install.sh b/install.sh index f8150dc..1c7af1c 100644 --- a/install.sh +++ b/install.sh @@ -159,7 +159,7 @@ install_pkg() { download() { case $1 in core) - [[ ! $is_core_ver ]] && is_core_ver=$(_wget -qO- "https://api.github.com/repos/${is_core_repo}/releases/latest?v=$RANDOM" | grep tag_name | egrep -o 'v([0-9.]+)') + [[ ! $is_core_ver ]] && is_core_ver=$(_wget -qO- "https://api.github.com/repos/${is_core_repo}/releases/latest?v=$RANDOM" | grep tag_name | grep -E -o 'v([0-9.]+)') [[ $is_core_ver ]] && link="https://github.com/${is_core_repo}/releases/download/${is_core_ver}/${is_core}-${is_core_ver:1}-linux-${is_arch}.tar.gz" name=$is_core_name tmpfile=$tmpcore diff --git a/sing-box.sh b/sing-box.sh index 81bd8e6..8161ba6 100644 --- a/sing-box.sh +++ b/sing-box.sh @@ -1,6 +1,6 @@ #!/bin/bash args=$@ -is_sh_ver=v1.08 +is_sh_ver=v1.09 . /etc/sing-box/sh/src/init.sh \ No newline at end of file diff --git a/src/core.sh b/src/core.sh index 7706a20..b754c3e 100644 --- a/src/core.sh +++ b/src/core.sh @@ -159,7 +159,7 @@ show_list() { is_test() { case $1 in number) - echo $2 | egrep '^[1-9][0-9]?+$' + echo $2 | grep -E '^[1-9][0-9]?+$' ;; port) if [[ $(is_test number $2) ]]; then @@ -170,13 +170,13 @@ is_test() { [[ $(is_port_used $2) && ! $is_cant_test_port ]] && echo ok ;; domain) - echo $2 | egrep -i '^\w(\w|\-|\.)?+\.\w+$' + echo $2 | grep -E -i '^\w(\w|\-|\.)?+\.\w+$' ;; path) - echo $2 | egrep -i '^\/\w(\w|\-|\/)?+\w$' + echo $2 | grep -E -i '^\/\w(\w|\-|\/)?+\w$' ;; uuid) - echo $2 | egrep -i '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' + echo $2 | grep -E -i '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' ;; esac @@ -781,7 +781,7 @@ add() { ;; *) for v in ${protocol_list[@]}; do - [[ $(egrep -i "^$is_lower$" <<<$v) ]] && is_new_protocol=$v && break + [[ $(grep -E -i "^$is_lower$" <<<$v) ]] && is_new_protocol=$v && break done [[ ! $is_new_protocol ]] && err "无法识别 ($1), 请使用: $is_core add [protocol] [args... | auto]" @@ -905,7 +905,7 @@ add() { is_tmp_use_name=加密方式 is_tmp_list=${ss_method_list[@]} for v in ${is_tmp_list[@]}; do - [[ $(egrep -i "^${is_use_method}$" <<<$v) ]] && is_tmp_use_type=$v && break + [[ $(grep -E -i "^${is_use_method}$" <<<$v) ]] && is_tmp_use_type=$v && break done [[ ! ${is_tmp_use_type} ]] && { warn "(${is_use_method}) 不是一个可用的${is_tmp_use_name}." @@ -1033,8 +1033,8 @@ get() { file) is_file_str=$2 [[ ! $is_file_str ]] && is_file_str='.json$' - # is_all_json=("$(ls $is_conf_dir | egrep $is_file_str)") - readarray -t is_all_json <<<"$(ls $is_conf_dir | egrep -i "$is_file_str" | sed '/dynamic-port-.*-link/d' | head -233)" # limit max 233 lines for show. + # is_all_json=("$(ls $is_conf_dir | grep -E $is_file_str)") + readarray -t is_all_json <<<"$(ls $is_conf_dir | grep -E -i "$is_file_str" | sed '/dynamic-port-.*-link/d' | head -233)" # limit max 233 lines for show. [[ ! $is_all_json ]] && err "无法找到相关的配置文件: $2" [[ ${#is_all_json[@]} -eq 1 ]] && is_config_file=$is_all_json && is_auto_get_config=1 [[ ! $is_config_file ]] && { @@ -1071,7 +1071,7 @@ get() { is_config_name=$is_config_file if [[ $is_caddy && $host && -f $is_caddy_conf/$host.conf ]]; then - is_tmp_https_port=$(egrep -o "$host:[1-9][0-9]?+" $is_caddy_conf/$host.conf | sed s/.*://) + is_tmp_https_port=$(grep -E -o "$host:[1-9][0-9]?+" $is_caddy_conf/$host.conf | sed s/.*://) fi [[ $is_tmp_https_port ]] && is_https_port=$is_tmp_https_port [[ $is_client && $host ]] && port=$is_https_port @@ -1206,11 +1206,6 @@ get() { [[ $(grep ":" <<<$ip) ]] && is_dns_type="aaaa" is_host_dns=$(_wget -qO- --header="accept: application/dns-json" "https://one.one.one.one/dns-query?name=$host&type=$is_dns_type") ;; - log | logerr) - msg "\n 提醒: 按 $(_green Ctrl + C) 退出\n" - [[ $1 == 'log' ]] && tail -f $is_log_dir/access.log - [[ $1 == 'logerr' ]] && tail -f $is_log_dir/error.log - ;; install-caddy) _green "\n安装 Caddy 实现自动配置 TLS.\n" load download.sh @@ -1652,7 +1647,8 @@ main() { load import.sh ;; log) - get $@ + load log.sh + log_set $2 ;; url | qr) url_qr $@ diff --git a/src/download.sh b/src/download.sh index 0278d2b..9657d38 100644 --- a/src/download.sh +++ b/src/download.sh @@ -13,7 +13,7 @@ get_latest_version() { url="https://api.github.com/repos/$is_caddy_repo/releases/latest?v=$RANDOM" ;; esac - latest_ver=$(_wget -qO- $url | grep tag_name | egrep -o 'v([0-9.]+)') + latest_ver=$(_wget -qO- $url | grep tag_name | grep -E -o 'v([0-9.]+)') [[ ! $latest_ver ]] && { err "获取 ${name} 最新版本失败." } diff --git a/src/import.sh b/src/import.sh index b4aade4..d93d8e3 100644 --- a/src/import.sh +++ b/src/import.sh @@ -19,7 +19,7 @@ in_conf() { [[ ! $uuid ]] && uuid=$trojan_password if [[ $host ]]; then if [[ $is_caddy && -f $is_caddy_conf/$host.conf ]]; then - tmp_tlsport=$(egrep -o "$host:[1-9][0-9]?+" $is_caddy_conf/$host.conf | sed s/.*://) + tmp_tlsport=$(grep -E -o "$host:[1-9][0-9]?+" $is_caddy_conf/$host.conf | sed s/.*://) fi [[ $tmp_tlsport ]] && https_port=$tmp_tlsport add $is_protocol-$net-tls @@ -53,10 +53,10 @@ is_change=1 is_dont_auto_exit=1 is_dont_test_host=1 if [[ -f $is_xray_sh && -d $is_xray_conf ]]; then - is_list=($(ls $is_xray_conf | grep .json | egrep -iv 'kcp|grpc|dynamic|quic' | sed "s#^#$is_xray_conf/#")) + is_list=($(ls $is_xray_conf | grep .json | grep -E -iv 'kcp|grpc|dynamic|quic' | sed "s#^#$is_xray_conf/#")) fi if [[ -f $is_v2ray_sh && -d $is_v2ray_conf ]]; then - is_list+=($(ls $is_v2ray_conf | grep .json | egrep -iv 'kcp|grpc|dynamic|quic' | sed "s#^#$is_v2ray_conf/#")) + is_list+=($(ls $is_v2ray_conf | grep .json | grep -E -iv 'kcp|grpc|dynamic|quic' | sed "s#^#$is_v2ray_conf/#")) fi [[ ${is_list[@]} =~ "xray" ]] && is_xray_in=1 [[ ${is_list[@]} =~ "v2ray" ]] && is_v2ray_in=1 diff --git a/src/init.sh b/src/init.sh index 9111e98..b290b1c 100644 --- a/src/init.sh +++ b/src/init.sh @@ -124,8 +124,8 @@ if [[ -f $is_caddy_bin && -d $is_caddy_dir && $is_caddy_service ]]; then systemctl restart caddy & } is_caddy_ver=$($is_caddy_bin version | head -n1 | cut -d " " -f1) - is_tmp_http_port=$(egrep '^ {2,}http_port|^http_port' $is_caddyfile | egrep -o [0-9]+) - is_tmp_https_port=$(egrep '^ {2,}https_port|^https_port' $is_caddyfile | egrep -o [0-9]+) + is_tmp_http_port=$(grep -E '^ {2,}http_port|^http_port' $is_caddyfile | grep -E -o [0-9]+) + is_tmp_https_port=$(grep -E '^ {2,}https_port|^https_port' $is_caddyfile | grep -E -o [0-9]+) [[ $is_tmp_http_port ]] && is_http_port=$is_tmp_http_port [[ $is_tmp_https_port ]] && is_https_port=$is_tmp_https_port if [[ $(pgrep -f $is_caddy_bin) ]]; then diff --git a/src/log.sh b/src/log.sh new file mode 100644 index 0000000..cc604cf --- /dev/null +++ b/src/log.sh @@ -0,0 +1,44 @@ +is_log_level_list=( + trace + debug + info + warn + error + fatal + panic + none + del +) +log_set() { + if [[ $1 ]]; then + for v in ${is_log_level_list[@]}; do + [[ $(grep -E -i "^${1,,}$" <<<$v) ]] && is_log_level_use=$v && break + done + [[ ! $is_log_level_use ]] && { + err "无法识别 log 参数: $@ \n请使用 $is_core log [${is_log_level_list[@]}] 进行相关设定.\n备注: del 参数仅临时删除 log 文件; none 参数将不会生成 log 文件." + } + case $is_log_level_use in + del) + rm -rf $is_log_dir/*.log + msg "\n $(_green 已临时删除 log 文件, 如果你想要完全禁止生成 log 文件请使用: $is_core log none)\n" + ;; + none) + rm -rf $is_log_dir/*.log + cat <<<$(jq '.log={"disabled":true}' $is_config_json) >$is_config_json + ;; + *) + cat <<<$(jq '.log={output:"/var/log/'$is_core'/access.log",level:"'$is_log_level_use'","timestamp":true}' $is_config_json) >$is_config_json + ;; + esac + + manage restart & + [[ $1 != 'del' ]] && msg "\n已更新 Log 设定为: $(_green $is_log_level_use)\n" + else + if [[ -f $is_log_dir/access.log ]]; then + msg "\n 提醒: 按 $(_green Ctrl + C) 退出\n" + tail -f $is_log_dir/access.log + else + err "无法找到 log 文件." + fi + fi +} \ No newline at end of file