diff --git a/bin/set_domain.sh b/bin/set_domain.sh index e2632e5..5ad21de 100644 --- a/bin/set_domain.sh +++ b/bin/set_domain.sh @@ -1,8 +1,13 @@ #!/bin/bash +# 当前脚本所在目录的绝对路径 +script_dir="$(dirname "$(realpath "$0")")" + +# 导入utils文件夹中的jq_util.sh脚本 +source "$script_dir/utils/jq_util.sh" + gen_cert() { local domain="$1" - local script_dir="$(dirname "$(realpath "$0")")" # 当前脚本所在目录的绝对路径 local key_dir="$script_dir/../key" # key目录绝对路径 # 使用openssl生成自签名证书,设置密钥和有效期,并指定域名信息 openssl req -x509 -nodes -newkey ec:<(openssl ecparam -name prime256v1) -keyout "$key_dir/server.key" -out "$key_dir/server.crt" -subj "/CN=$domain" -days 36500 @@ -10,11 +15,9 @@ gen_cert() { update_config() { local domain="$1" - local script_dir="$(dirname "$(realpath "$0")")" # 当前脚本所在目录的绝对路径 local config_file="$script_dir/../config.json" # config.json文件路径 - # 使用jq命令修改文件conifg.json中的masquerade.proxy.url - new_config=$(jq --arg new_url "https://$domain" '.masquerade.proxy.url = $new_url' "$config_file") - echo "$new_config" > "$config_file" + # 使用jq_util.sh脚本中的modify_json_file函数修改config.json文件中的域名信息 + modify_json_file $config_file "masquerade.proxy.url" "https://$domain" } domain="${1:-bing.com}" # 域名, 默认为"bing.com" diff --git a/bin/update_password.sh b/bin/update_password.sh index 1989016..e32a1ff 100644 --- a/bin/update_password.sh +++ b/bin/update_password.sh @@ -1,5 +1,17 @@ #!/bin/bash +# 检查是否安装了jq命令 +if ! command -v jq &> /dev/null; then + echo "Error: jq command not found. Please install jq first." + exit 1 +fi + +# 当前脚本所在目录的绝对路径 +script_dir="$(dirname "$(realpath "$0")")" + +# 加载jq_util.sh脚本 +source $script_dir/utils/jq_util.sh + # 函数生成随机密码 gen_random_pass() { local pass_length=$1 # 密码长度作为第一个参数 @@ -11,24 +23,14 @@ gen_random_pass() { echo "$rand" } -# 检查是否安装了jq命令 -if ! command -v jq &> /dev/null; then - echo "Error: jq command not found. Please install jq first." - exit 1 -fi - # 生成一个长度为16的随机密码 new_password=$(gen_random_pass 16) # 更新config.json文件中的auth.password字段 -script_dir="$(dirname "$(realpath "$0")")" # 当前脚本所在目录的绝对路径 config_file="$script_dir/../config.json" -# 使用jq命令修改auth.password字段的值 -new_config=$(jq --arg new_password "$new_password" '.auth.password = $new_password' "$config_file") - -# 将修改后的JSON写回config.json文件 -echo "$new_config" > "$config_file" +# 使用jq_util.sh中的modify_json_file函数修改config.json文件中的auth.password字段 +modify_json_file "$config_file" '.auth.password' "$new_password" echo "New password: $new_password" echo "config.json file updated with new password." \ No newline at end of file diff --git a/bin/utils/jq_util.sh b/bin/utils/jq_util.sh new file mode 100644 index 0000000..b5dab98 --- /dev/null +++ b/bin/utils/jq_util.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# 修改json文件的属性值 +function modify_json_file() { + local json_file=$1 # json文件路径 + local key=$2 # 要修改的key + local value=$3 # 要修改的value + + jq ".${key} = \"${value}\"" ${json_file} > ${json_file}.tmp + mv ${json_file}.tmp ${json_file} +} diff --git a/config.json b/config.json index c2598d8..0f80626 100644 --- a/config.json +++ b/config.json @@ -6,7 +6,7 @@ }, "auth": { "type": "password", - "password": "设置你的密码" + "password": "La4AdWOoE57OdtwY" }, "masquerade": { "type": "proxy", @@ -15,4 +15,4 @@ "rewriteHost": true } } -} \ No newline at end of file +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..e69de29 diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..06b5429 --- /dev/null +++ b/test.sh @@ -0,0 +1 @@ +echo $(dirname "$0") \ No newline at end of file