Last active 1 year ago

根据给定 IPv4 和端口生成 IP4P 格式并更新 Cloudflare AAAA 记录(用于 Lucky STUN 内网穿透脚本),关于 IP4P 格式详见:[NATMap - FAQ -域名访问是如何实现的?](https://github.com/heiher/natmap/wiki/faq#%E5%9F%9F%E5%90%8D%E8%AE%BF%E9%97%AE%E6%98%AF%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E7%9A%84)

GabrielxD's Avatar GabrielxD revised this gist 1 year ago. Go to revision

1 file changed, 33 insertions, 33 deletions

ip4p_cloudflare.sh

@@ -34,42 +34,42 @@ echo "Generated IP4P Address: $ip4p"
34 34 # 初始化重试计数器
35 35 retry_count=0
36 36
37 - # 重试循环
38 - while true; do
39 - # 根据 USE_PROXY 和 PROXY_TYPE 变量决定使用的代理选项
40 - if [ "$USE_PROXY" = true ]; then
41 - auth=""
42 - if [ -n "$PROXY_USER" ]; then
43 - auth="$PROXY_USER"
44 - fi
45 - if [ -n "$PROXY_PASS" ]; then
46 - if [ -n "$auth" ]; then
47 - auth="$auth:$PROXY_PASS"
48 - else
49 - auth="$PROXY_PASS"
50 - fi
37 + # 根据 USE_PROXY 和 PROXY_TYPE 变量决定使用的代理选项
38 + if [ "$USE_PROXY" = true ]; then
39 + auth=""
40 + if [ -n "$PROXY_USER" ]; then
41 + auth="$PROXY_USER"
42 + fi
43 + if [ -n "$PROXY_PASS" ]; then
44 + if [ -n "$auth" ]; then
45 + auth="$auth:$PROXY_PASS"
46 + else
47 + auth="$PROXY_PASS"
51 48 fi
52 -
53 - case "$PROXY_TYPE" in
54 - http)
55 - proxy_option="--proxy http://$auth@$PROXY_URL"
56 - ;;
57 - socks5)
58 - if [ -n "$auth" ]; then
59 - proxy_option="--socks5 $PROXY_URL --proxy-user $auth"
60 - else
61 - proxy_option="--socks5 $PROXY_URL"
62 - fi
63 - ;;
64 - *)
65 - echo "Unsupported proxy type: $PROXY_TYPE"
66 - exit 1
67 - ;;
68 - esac
69 - else
70 - proxy_option=""
71 49 fi
72 50
51 + case "$PROXY_TYPE" in
52 + http)
53 + proxy_option="--proxy http://$auth@$PROXY_URL"
54 + ;;
55 + socks5)
56 + if [ -n "$auth" ]; then
57 + proxy_option="--socks5 $PROXY_URL --proxy-user $auth"
58 + else
59 + proxy_option="--socks5 $PROXY_URL"
60 + fi
61 + ;;
62 + *)
63 + echo "Unsupported proxy type: $PROXY_TYPE"
64 + exit 1
65 + ;;
66 + esac
67 + else
68 + proxy_option=""
69 + fi
70 +
71 + # 重试循环
72 + while true; do
73 73 # 发送请求并获取响应
74 74 response=$(curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records/${RECORD}" \
75 75 -H "X-Auth-Email: ${EMAIL}" \

GabrielxD's Avatar GabrielxD revised this gist 1 year ago. Go to revision

No changes

GabrielxD's Avatar GabrielxD revised this gist 1 year ago. Go to revision

1 file changed, 1 insertion, 1 deletion

ip4p_cloudflare.sh

@@ -75,7 +75,7 @@ while true; do
75 75 -H "X-Auth-Email: ${EMAIL}" \
76 76 -H "Authorization: Bearer ${TOKEN}" \
77 77 -H "Content-Type:application/json" \
78 - --data "{\"data\":{\"content\":\"${ip4p}\"}}" $proxy_option)
78 + --data "{\"content\":\"${ip4p}\"}" $proxy_option)
79 79
80 80 # 检查响应中是否包含成功字符串
81 81 if echo "$response" | grep -q "$SUCCESS_STRING"; then

GabrielxD's Avatar GabrielxD revised this gist 1 year ago. Go to revision

No changes

GabrielxD's Avatar GabrielxD revised this gist 1 year ago. Go to revision

No changes

GabrielxD's Avatar GabrielxD revised this gist 1 year ago. Go to revision

No changes

GabrielxD's Avatar GabrielxD revised this gist 1 year ago. Go to revision

1 file changed, 2 insertions

ip4p_cloudflare.sh

@@ -1,3 +1,5 @@
1 + #!/bin/sh
2 +
1 3 # 配置变量
2 4 ZONE=''
3 5 RECORD=''

GabrielxD's Avatar GabrielxD revised this gist 1 year ago. Go to revision

1 file changed, 92 insertions

ip4p_cloudflare.sh(file created)

@@ -0,0 +1,92 @@
1 + # 配置变量
2 + ZONE=''
3 + RECORD=''
4 + EMAIL=''
5 + TOKEN=''
6 + MAX_RETRIES=5 # 最大重试次数
7 + RETRY_INTERVAL_MS=1500 # 重试间隔时间(毫秒)
8 + SUCCESS_STRING='"success":true' # 接口调用成功时响应应包含的字符串
9 +
10 + # 代理配置
11 + USE_PROXY=false # 是否使用代理
12 + PROXY_TYPE='' # 代理类型 ('http' 或 'socks5')
13 + PROXY_URL='' # 代理 URL
14 + PROXY_USER='' # 代理用户名(如果不需要认证则留空)
15 + PROXY_PASS='' # 代理密码(如果不需要认证则留空)
16 +
17 + # 提取 IPv4 地址的各个八位段
18 + ip_oct1=$(echo "${ip}" | cut -d'.' -f1)
19 + ip_oct2=$(echo "${ip}" | cut -d'.' -f2)
20 + ip_oct3=$(echo "${ip}" | cut -d'.' -f3)
21 + ip_oct4=$(echo "${ip}" | cut -d'.' -f4)
22 +
23 + # 计算 IP4P 地址的各个部分
24 + ip_hex_hi=$(printf '%02x%02x' "$ip_oct1" "$ip_oct2")
25 + ip_hex_lo=$(printf '%02x%02x' "$ip_oct3" "$ip_oct4")
26 + port_hex=$(printf '%04x' "${port}")
27 +
28 + # 构造 IP4P 地址
29 + ip4p="2001::${port_hex}:${ip_hex_hi}:${ip_hex_lo}"
30 + echo "Generated IP4P Address: $ip4p"
31 +
32 + # 初始化重试计数器
33 + retry_count=0
34 +
35 + # 重试循环
36 + while true; do
37 + # 根据 USE_PROXY 和 PROXY_TYPE 变量决定使用的代理选项
38 + if [ "$USE_PROXY" = true ]; then
39 + auth=""
40 + if [ -n "$PROXY_USER" ]; then
41 + auth="$PROXY_USER"
42 + fi
43 + if [ -n "$PROXY_PASS" ]; then
44 + if [ -n "$auth" ]; then
45 + auth="$auth:$PROXY_PASS"
46 + else
47 + auth="$PROXY_PASS"
48 + fi
49 + fi
50 +
51 + case "$PROXY_TYPE" in
52 + http)
53 + proxy_option="--proxy http://$auth@$PROXY_URL"
54 + ;;
55 + socks5)
56 + if [ -n "$auth" ]; then
57 + proxy_option="--socks5 $PROXY_URL --proxy-user $auth"
58 + else
59 + proxy_option="--socks5 $PROXY_URL"
60 + fi
61 + ;;
62 + *)
63 + echo "Unsupported proxy type: $PROXY_TYPE"
64 + exit 1
65 + ;;
66 + esac
67 + else
68 + proxy_option=""
69 + fi
70 +
71 + # 发送请求并获取响应
72 + response=$(curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records/${RECORD}" \
73 + -H "X-Auth-Email: ${EMAIL}" \
74 + -H "Authorization: Bearer ${TOKEN}" \
75 + -H "Content-Type:application/json" \
76 + --data "{\"data\":{\"content\":\"${ip4p}\"}}" $proxy_option)
77 +
78 + # 检查响应中是否包含成功字符串
79 + if echo "$response" | grep -q "$SUCCESS_STRING"; then
80 + echo "Successfully updated DNS record."
81 + break
82 + else
83 + echo "Failed to update DNS record. Response: $response"
84 + retry_count=$((retry_count + 1))
85 + if [ "$retry_count" -ge "$MAX_RETRIES" ]; then
86 + echo "Reached maximum retry limit. Exiting."
87 + break
88 + fi
89 + echo "Retrying... ($retry_count/$MAX_RETRIES)"
90 + sleep "$(echo "scale=3; $RETRY_INTERVAL_MS / 1000" | bc)"
91 + fi
92 + done
Newer Older