Skip to content
sh
#!/bin/bash
# addr=${ipAddr}
# ssh ubuntu bash -x /etc/script/stun_update.sh  Clash_Proxy time time "192.168.5.1" "587" "192.168.5.1:587"
echo "0 $0 1 $1 2 $2 3 $3 4 $4 5 $5 6 $6" >./my.log
ruleName=$1
time=$2
ip=$4
port=$5
addr=$6

function curl_transfer() {
  local method="$1"
  local local_file="$2"
  local relative_path="$3"

  # 预设用户信息
  local user="用户"
  local password="密码"
  local base_url="http://192.168.5.18:5019/dav"

  # 拼接完整的 URL
  local remote_url="$base_url$relative_path"

  case "$method" in
  upload)
    curl -u "$user:$password" -T "$local_file" "$remote_url"
    ;;
  download)
    curl -u "$user:$password" -o "$local_file" "$remote_url" -L
    ;;
  *)
    echo "Invalid method. Please use 'upload' or 'download'."
    exit 1
    ;;
  esac
}
# 示例用法:
# curl_transfer upload myfile.txt /path/to/upload
# curl_transfer download /path/to/download/file.txt downloaded_file

function modify_ini() {
  section="$1"
  key="$2"
  value="$3"
  file="$4"

  sed -i "s/\($section\).*$key *=.*$/\1$key = $value/" "$file"
}

# 发送邮件
function send_email() {
  python3 /etc/script/mail/send_email.py "$1" "$2" "$3" "$4" "$5" "$6" "$7"
}

# 编辑节点信息
function Edit_Proxy_Info() {
  function Edit() {
    proxy_name=$1
    curl_transfer download $proxy_path_local $proxy_path_remote
    sed -i "/$proxy_name/s/port: [0-9]*/port: $port/" $proxy_path_local
    curl_transfer upload $proxy_path_local $proxy_path_remote
  }

  case "$ruleName" in
  Clash_Proxy)
    Edit "Server 2.2"
    ;;
  Clash_Proxy_2)
    Edit "节点名称"
    ;;
  *)
    echo "不是Clash节点,无需修改"
    exit 1
    ;;
  esac

}

base_remote=/%E6%9D%A5%E6%BA%90/%E4%BD%8E%E9%A2%91/DDNS

port_path_local=/tmp/stun/$ruleName.txt
port_path_remote=$base_remote/$ruleName.txt

proxy_path_local=/tmp/proxy.yml
proxy_path_remote=$base_remote/Home.yml

info_path_local=/tmp/stun_info.ini
info_path_remote=$base_remote/info.ini

# sender="qq"
# recipient="qq"
subject="STUN"
body="$ruleName: $port"

mkdir -p /tmp/stun

# 写入端口到每个规则专有的文件
echo $port >$port_path_local
curl_transfer upload $port_path_local $port_path_remote

# 修改 Clash 配置
Edit_Proxy_Info

# curl_transfer download $info_path_local $info_path_remote
# modify_ini "Port" $ruleName ${port} $info_path_local
# curl_transfer upload $info_path_local $info_path_remote.2.ini
# cat $info_path_local

# rm -rf /tmp/stun $info_path_local # 删除垃圾

send_email "$subject" "$body"