Skip to content

Commit

Permalink
HY2一键换ip
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejun committed Dec 1, 2024
1 parent 923e9d8 commit e8529f3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
38 changes: 37 additions & 1 deletion start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2317,6 +2317,39 @@ checkInstalled(){
return 1
}

changeHy2IP(){
read -p "是否让程序为HY2选择可用的IP?[y/n] [y]:" input
input=${input:-y}

if [[ "$input" == "y" ]]; then
cd ${installpath}/serv00-play/singbox
if [[ ! -e "singbox.json" || ! -e "config.json" ]]; then
red "未安装节点,请先安装!"
return 1
fi
hy2_ip=$(get_ip)
if [[ -z "hy2_ip" ]]; then
red "很遗憾,已无可用IP!"
return 1
fi
if ! upInsertFd singbox.json HY2IP "$hy2_ip"; then
red "更新singbox.json配置文件失败!"
return 1
fi

if ! upSingboxFd config.json "inbounds" "tag" "hysteria-in" "listen" "$hy2_ip"; then
red "更新config.json配置文件失败!"
return 1
fi
green "HY2 更换IP成功,当前IP为 $hy2_ip"

echo "正在重启sing-box..."
stopSingBox
startSingBox
fi

}

showMenu(){
art_wrod=$(figlet "serv00-play")
echo "<------------------------------------------------------------------>"
Expand All @@ -2328,7 +2361,7 @@ showMenu(){

options=("安装/更新serv00-play项目" "sun-panel" "webssh" "待开发" "待开发" "设置保活的项目" "配置sing-box" \
"运行sing-box" "停止sing-box" "显示sing-box节点信息" "快照恢复" "系统初始化" "设置中国时区及前置工作" "管理哪吒探针" "卸载探针" "设置彩色开机字样" "显示本机IP" \
"mtproto代理" "alist管理" "端口管理" "域名证书管理" "一键root" "自动检测主机IP状态" "卸载" )
"mtproto代理" "alist管理" "端口管理" "域名证书管理" "一键root" "自动检测主机IP状态" "一键更换hy2的IP" "卸载" )

select opt in "${options[@]}"
do
Expand Down Expand Up @@ -2403,6 +2436,9 @@ showMenu(){
getUnblockIP
;;
24)
changeHy2IP
;;
25)
uninstall
;;
0)
Expand Down
37 changes: 37 additions & 0 deletions utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,40 @@ checkDownload() {
fi
return 0
}

# 对json文件字段进行插入或修改
# usage: upInsertFd jsonfile fieldname value
upInsertFd() {
local jsonfile=$1
local field=$2
local value=$3

jq --arg field "$field" --arg value "$value" '
if has($field) then
.[$field] = $value
else
. + {($field): $value}
end
' "$jsonfile" >tmp.json && mv tmp.json "$jsonfile"

return $?
}

# 针对singbox.json, 对指定字段进行修改
upSingboxFd() {
local jsonfile=$1
local array_name=$2
local selector_key=$3
local selector_value=$4
local field_path=$5
local value=$6

jq --arg selector_key "$selector_key" \
--arg selector_value "$selector_value" \
--arg field_path "$field_path" \
--arg value "$value" "
(.$array_name[] | select(.$selector_key == \$selector_value) | .[\$field_path]) = \$value
" "$jsonfile" >tmp.json && mv tmp.json "$jsonfile"

return $?
}

0 comments on commit e8529f3

Please sign in to comment.