Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[shares] notification scripts #12

Open
heiher opened this issue Dec 26, 2022 · 12 comments
Open

[shares] notification scripts #12

heiher opened this issue Dec 26, 2022 · 12 comments
Labels
documentation Improvements or additions to documentation

Comments

@heiher
Copy link
Owner

heiher commented Dec 26, 2022

Welcome to share the notification scripts.
欢迎分享通知脚本。

@heiher heiher added the documentation Improvements or additions to documentation label Dec 26, 2022
@heiher
Copy link
Owner Author

heiher commented Dec 26, 2022

Cloudflare DDNS IP4P(IPv4 and port encoding in AAAA record):

#!/bin/sh

ZONE=''
RECORD=''
EMAIL=''
AUTH=''
DOMAIN=''

IP4P=${3}

while true; do
    curl -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records/${RECORD}" \
        -H "X-Auth-Email: ${EMAIL}" \
        -H "Authorization: Bearer ${AUTH}" \
        -H "Content-Type:application/json" \
        --data "{\"type\":\"AAAA\",\"name\":\"${DOMAIN}\",\"content\":\"${IP4P}\",\"ttl\":60,\"proxied\":false}" > /dev/null 2> /dev/null
    if [ $? -eq 0 ]; then
        break
    fi
done

Applications supports IP4P:

@heiher
Copy link
Owner Author

heiher commented Dec 26, 2022

Natter/NATMap 打洞后自动更新 qBittorrent/Transmission 监听端口和 OpenWrt 防火墙规则并推送到 Telegram:

https://gist.github.com/veltlion/b59d73654f0ae36725f5a571602729cb

据说让BT/PT客户端的侦听端口与打洞后的公网端口保持一致可以有效提高peer间的连通性,从而提升上传速度。
See also: https://www.v2ex.com/t/902093#reply29

@ysc3839
Copy link
Collaborator

ysc3839 commented Dec 27, 2022

ns1.com scripts for update A (IPv4 address) and SRV (port) record. Requires OpenWrt's jshn (/usr/share/libubox/jshn.sh), used to format JSON string.
https://gist.github.com/ysc3839/2c629d69f5fc3541a3f1900a23681f0c

Docs about SRV record:
https://www.cloudflare.com/learning/dns/dns-records/dns-srv-record/
https://en.wikipedia.org/wiki/SRV_record

@heiher heiher pinned this issue Dec 28, 2022
@OpportunityLiu
Copy link

OpportunityLiu commented Jun 27, 2023

据说让BT/PT客户端的侦听端口与打洞后的公网端口保持一致可以有效提高peer间的连通性,从而提升上传速度。
See also: https://www.v2ex.com/t/902093#reply29

image

qBittorrent 的实现应该没啥问题,是不是受 IPV4、V6 监听端口不一致的影响

好像是 tracker 实现的问题,有些 tracker 只能绑定一个端口
arvidn/libtorrent#5746 (comment)

@OpenGG
Copy link

OpenGG commented Sep 8, 2023

dynv6 DDNS IP4P(IPv4 and port encoding in AAAA record):

#!/bin/sh

ZONE=''
TOKEN=''

IP4P=${3}

while true; do
    curl \
      "https://dynv6.com/api/update?hostname=${ZONE}&token=${TOKEN}&ipv6=${IP4P}&ipv6prefix=${IP4P}" \
      > /dev/null 2> /dev/null
    if [ $? -eq 0 ]; then
        break
    fi
done

@Wikeolf
Copy link

Wikeolf commented Oct 2, 2023

Fix someting wrong with my openwrt router from https://github.com/heiher/natmap/wiki/web /usr/bin/wdns

What's more, when you are using HTTP protocol, please create a Configuration Rules (zone-->Rules --> Configuration Rules) to switch SSL option to off for specific subdomain, or turn to use HTTPS

#!/bin/sh

ZONE='ZONE_ID'
RECORD='DNS_RECORD_ID'
RULE='ORIGIN_RULE_ID'
EMAIL='Cloudflare_EMAIL'
AUTH='Global_API_Key' 
#or you can use API Tokens but change "X-Auth-Key: ${AUTH}" to "Authorization: Bearer ${AUTH}"
DOMAIN='DEST_DOMAIN'

ADDR=${1}
PORT=${2}

# DNS
while true; do
    curl -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records/${RECORD}" \
        -H "X-Auth-Email: ${EMAIL}" \
        -H "X-Auth-Key: ${AUTH}" \
        -H "Content-Type:application/json" \
        --data "{\"type\":\"A\",\"name\":\"${DOMAIN}\",\"content\":\"${ADDR}\",\"ttl\":60,\"proxied\":true}" >> /tmp/wdns.log 2>&1
    if [ $? -eq 0 ]; then
        break
    fi
done

# Origin rule
while true; do
    curl -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONE}/rulesets/${RULE}" \
        -H "X-Auth-Email: ${EMAIL}" \
        -H "X-Auth-Key: ${AUTH}" \
        -H "Content-Type:application/json" \
        --data "{\"rules\":[{\"expression\":\"(http.host eq \\\"${DOMAIN}\\\")\",\"description\":\"natmap\",\"action\":\"route\",\"action_parameters\":{\"origin\":{\"port\":${PORT}}}}]}" >> /tmp/wdns.log 2>&1
    if [ $? -eq 0 ]; then
        break
    fi
done

@cwbsw
Copy link

cwbsw commented Apr 26, 2024

自动配置OpenWrt防火墙,将IPv4流量DNAT到内网地址(端口与公网映射端口一致),并且允许同一端口的IPv6流量。

#!/bin/sh

dest_ip="192.168.1.123"
dest_ip6="::1111:2222:3333:4444/::ffff:ffff:ffff:ffff"

. /usr/share/libubox/jshn.sh

json_init
json_add_string name natmap
json_add_object data
json_add_array firewall

for file in /var/run/natmap/*.json; do
	public_port=$(jsonfilter -i $file -e '@.port')
	bind_port=$(jsonfilter -i $file -e '@.inner_port')

	json_add_object
	json_add_string type redirect
	json_add_string target DNAT
	json_add_string proto tcp
	json_add_string src wan
	json_add_string dest lan
	json_add_string src_dport $bind_port
	json_add_string dest_ip $dest_ip
	json_add_string dest_port $public_port
	json_add_boolean reflection false
	json_close_object

	json_add_object
	json_add_string type rule
	json_add_string target ACCEPT
	json_add_string proto tcp
	json_add_string src wan
	json_add_string dest lan
	json_add_string dest_ip $dest_ip6
	json_add_string dest_port $public_port
	json_close_object
done

json_close_array
json_close_object

ubus call service set "$(json_dump)"

#Now fw4 can't reload correctly, so reload-sets first
/sbin/fw4 reload-sets && /sbin/fw4 reload

@ysc3839
Copy link
Collaborator

ysc3839 commented Apr 26, 2024

@cwbsw 看上去使用这种方案可以让防火墙配置跟随 natmap 服务?重启系统是不是可以自动清除?有相关文档吗?

@cwbsw
Copy link

cwbsw commented Apr 27, 2024

@ysc3839 这个不会写入配置文件,停掉natmap,重新加载防火墙就没了。在natmap的启动脚本加上停止时重新加载防火墙的命令可自动化。
https://openwrt.org/docs/guide-developer/ubus/service
https://github.com/openwrt/firewall4/blob/698a53354fd280aae097efe08803c0c9a10c14c2/root/usr/share/ucode/fw4.uc#L573

@ysc3839
Copy link
Collaborator

ysc3839 commented Apr 28, 2024

@cwbsw 去确认了一下代码,使用 service.set 时是会覆盖掉已有的 triggersvalidate,似乎必须先获取所有数据,更新,再设置。
https://github.com/openwrt/procd/blob/946552a7b598a0b88db6101e864679554ec4f221/service/service.c#L123-L141
既然要先获取原有设置,我觉得不如把 firewall 数据放到 instance 下的 data,甚至 natmap 返回的端口信息都可以放在 data 中,这样的话 luci 就可以直接获取到对应数据,不需要额外读取文件。
不过这里的 data 字段设计似乎是在服务启动时设置的,运行时修改可能违反了原有设计用途,有空我先提一个 PR 询问一下吧。
CC @heiher

@ysc3839
Copy link
Collaborator

ysc3839 commented Apr 28, 2024

@dmserver
Copy link

多层路由 UPNP 映射端口, 当运行natmap设备 及上级路由全dhcp随机IP 时 这个很实用。
支持 爱快 京东 磊科 腾达 OpenWrt 路由混插,无论怎么串接都可以。
不支持 h3c 360 等 因为这些路由采用的随机UUID ipc,而upnp还无法跨路由发现,所以不支持。
理论支持 TP-Link ,因为想支持TP-Link,就得提前运行脚本去打开 混插在里面的TP-Link路由的UPNP端口,
否则一旦natmap运行起来,TP-Link将无法进行端口映射 upnp dmz,这属于TP-Link的设计缺陷,测试了手中全部TP-Link都这样
如果natmap 可以增加个先行脚本参数 例如-e 带的脚本 去执行准备任务,那么脚本可以支持TP-Link

#65
下面是脚本

`#!/bin/bash
###################################################################脚本测试模式数据
#捆绑物理网卡
eth="eth0"
#路由层数 有几层路由就填几,当前主机上面只有一台路由就填1,可以使用ssh命令traceroute -n -m 10 223.5.5.5检查经过几个家用路由器(运营商的不算,具体看情况)
wan_upnp=4
#打开外网端口 这个是natmap -b的端口
wan_upnp_port=9090
#打开内网端口 这个是natmap -b的端口
lan_upnp_port=2022
#路由upnp xml 文件名 各家路由可能不一样 尽量搜集就是了
upnp_xml="igd.xml rootDesc.xml UPnP/IGD.xml"
#upnp 备注名
upnp_port_name="STUN-TEXT"
#旁路由跃迁 旁路由跃迁模式(yes/no),开启后,若发现路由网关信息内出现旁路由,将直接由主路映射至下级设备,流量不经转旁路,正常情况不会出现旁路信息
side_route="no"
#测试协议
protocol="tcp"
########################################################################################################下方固定函数及内容
times=$(date +"%F %T")
upnp_text_info=$times" upnp测试"
if ! type upnpc > /dev/null 2>&1; then
echo 'upnpc 未安装,开始安装';
apt-get install -y miniupnpc
else
cd /
fi

if ! type traceroute > /dev/null 2>&1; then
echo 'traceroute 未安装,开始安装';
apt-get install -y traceroute
else
cd /
fi

if ! type fping > /dev/null 2>&1; then
echo 'fping 未安装,开始安装';
apt-get install -y fping
else
cd /
fi

lan_ip=$(ip address show $eth | head -n4 | grep inet | awk '{print$2}' | cut -d/ -f1-1)
break_id=0
for ((;;))
do
if [ -n "$eth" ]; then
wan_gw_info=$(traceroute -n -m $wan_upnp -i $eth 223.5.5.5 | awk '{print$2}' | grep ".")
else
wan_gw_info=$(traceroute -n -m $wan_upnp 223.5.5.5 | awk '{print$2}' | grep ".")
fi
if [ -n "$wan_gw_info" ] || [ $break_id -eq 5 ]; then
break
fi
((break_id++));
done
if [ ! -n "$wan_gw_info" ];then
exit 0
fi
#######################################################################操作函数
get_gw_upnp (){
gw_ip=$1
gx_xml=$2
break_id=1
upnp_xmls=(${gx_xml// / })
for ((upp=1;upp<3;upp++))
do
upnp_port=$(nmap -F -max_rtt_timeout 1 -sT $gw_ip | grep open)
if [ -n "$upnp_port" ] ; then
break
fi
done
upnp_port_info=$(echo $upnp_port | awk '{split($0, a, "open upnp"); print a[1]}' | rev | cut -d' ' -f2 | rev | cut -d/ -f1-1)
if [[ -z $upnp_port_info ]] || [[ "$upnp_port_info" = "p" ]] || [[ "$upnp_port_info" = *"p" ]]; then
upnp_port_info="80 1900 5000"
fi
for upnp_xmlname in ${upnp_xmls[@]}; do
for upnp_porti in ${upnp_port_info[@]}; do
upnpxml_cs="http://"$gw_ip":"$upnp_porti"/"$upnp_xmlname
upnp_xml_info=$(curl -L -k --connect-timeout 1 -m 1 -s "$upnpxml_cs")
if [[ "$upnp_xml_info" = "xml" ]];then
upnpxml_url=$upnpxml_cs
break
fi
done
if [ ! -z $upnpxml_url ];then
echo $upnpxml_url
return 1
fi
done
break_id=1
for ((;;))
do
upnpxml_cs=$(upnpc -i -P | grep "$gw_ip" | grep "desc" | cut -d: -f2-4)
if [ -n "$upnpxml_cs" ]; then
echo $upnpxml_cs
return 1
fi
upnpxml_cs=$(upnpc -P | grep "$gw_ip" | grep "desc" | cut -d: -f2-4)
if [ -n "$upnpxml_cs" ]; then
echo $upnpxml_cs
return 1
fi
if [ $break_id -eq 5 ];then
echo $upnpxml_cs
return 1
fi
((break_id++));
done
}
get_upnp_exip (){
upnpxml=$1
break_id=0
upnp_eth=$2
if [ -n "$upnp_eth" ]; then
upnpeth=" -m "$upnp_eth
else
upnpeth=" "
fi
for ((;;))
do
upnpexip=$(upnpc $upnpeth -u $upnpxml -S | grep ExternalIPAddress | awk '{print $3}')
if [ -n "$upnpexip" ];then
echo $upnpexip
return 1
fi
upnpexip=$(upnpc $upnpeth -i -u $upnpxml -S | grep ExternalIPAddress | awk '{print $3}')
if [ -n "$upnpexip" ];then
echo $upnpexip
return 1
fi
upnpexip=$(upnpc $upnpeth -u $upnpxml -s | grep ExternalIPAddress | awk '{print $3}')
if [ -n "$upnpexip" ];then
echo $upnpexip
return 1
fi
upnpexip=$(upnpc $upnpeth -i -u $upnpxml -s | grep ExternalIPAddress | awk '{print $3}')
if [ -n "$upnpexip" ];then
echo $upnpexip
return 1
fi
if [ $break_id -eq 5 ];then
break
fi
((break_id++));
done
}

get_ip (){
gw_ip=$1
gw_port=$2
protocolas=$3
protocolac=$(echo $protocolas | tr a-z A-Z)
upnp_text_infos=$4
ip_scan=$(echo $gw_ip | cut -d. -f1-3)
ip_a_info=$(fping -a -g -q $ip_scan.0/24)
ip_a_array=(${ip_a_info// / })
for ip_add in ${ip_a_array[@]}; do
if [[ ${protocolac} = "TCP" ]]
then
ip_upnp_port=$(nc -z $ip_add $gw_port -w 2 && echo "open" || echo "close")
ip_upnp_ports_a=$(curl -L -k --connect-timeout 1 -m 2 -s "http://$ip_add:$gw_port")
ip_upnp_ports_b=$(curl -L -k --connect-timeout 1 -m 2 -s "https://$ip_add:$gw_port")
else
ip_upnp_port=$(nc -z $ip_add $gw_port -w 2 -u && echo "open" || echo "close")
fi
if [[ "$ip_upnp_port" = "open" ]] && [[ "$gw_ip" != "$ip_add" ]];then
echo $ip_add
return 1
fi
done
}
upnp_nat_v2 (){
lan_ip_add=$1
route_upnp_xml=$2
lan_ip_port=$3
wan_ip_port=$4
upnp_protocol=$5
upnp_base=$6
upnp_eth=$7
upnp_pa=$upnp_base""$wan_ip_port""$lan_ip_port""$(echo $upnp_protocol | tr a-z A-Z)
upnp_pb=$upnp_base"
"$wan_ip_port""$lan_ip_port""$upnp_protocol
if [ -n "$upnp_eth" ]; then
upnpeth=" -m "$upnp_eth
else
upnpeth=" "
fi
upnp_array=$(upnpc -u $route_upnp_xml $upnpeth -L | grep "$upnp_base" | awk '{print $3""$2}'| sed 's/-.*./ /g' | awk '{print $1""$1""$2}')
for upnp_tp in ${upnp_array[@]}; do
upnpc -u $route_upnp_xml $upnpeth -N $(echo $upnp_tp | sed 's/_/ /g') > /dev/null
done
upnpc -u $route_upnp_xml $upnpeth -N $wan_ip_port $wan_ip_port $upnp_protocol >/dev/null
add_upnp_log=$(upnpc -u $route_upnp_xml $upnpeth -e "$upnp_base" -n $lan_ip_add $lan_ip_port $wan_ip_port $upnp_protocol 0)
add_upnp_log_a=$(echo $add_upnp_log | grep "duration" | grep "InternalIP:Port")
if [ -n "$add_upnp_log_a" ];then
return 1
fi
upnp_arrayp=$(upnpc -u $route_upnp_xml $upnpeth -L | grep "$upnp_base" | awk '{print $4""$3""$2}'| sed "s/->..://g" | sed "s/'//g")
for upnp_tpp in ${upnp_arrayp[@]}; do
if [[ ${upnp_tpp} = ${upnp_pa} ]] || [[ ${upnp_tpp} = ${upnp_pb} ]]
then
return 1
else
cd /
fi
done
return 0
}
upnp_nat_v2i (){
lan_ip_add=$1
route_upnp_xml=$2
lan_ip_port=$3
wan_ip_port=$4
upnp_protocol=$5
upnp_base=$6
upnp_eth=$7
upnp_pa=$upnp_base"
"$wan_ip_port""$lan_ip_port""$(echo $upnp_protocol | tr a-z A-Z)
upnp_pb=$upnp_base""$wan_ip_port""$lan_ip_port""$upnp_protocol
if [ -n "$upnp_eth" ]; then
upnpeth=" -m "$upnp_eth
else
upnpeth=" "
fi
upnp_array=$(upnpc -i -u $route_upnp_xml $upnpeth -L | grep "$upnp_base" | awk '{print $3"
"$2}'| sed 's/-.
./ /g' | awk '{print $1""$1""$2}')
for upnp_tp in ${upnp_array[@]}; do
upnpc -i -u $route_upnp_xml $upnpeth -N $(echo $upnp_tp | sed 's/_/ /g') > /dev/null
done
upnpc -i -u $route_upnp_xml $upnpeth -N $wan_ip_port $wan_ip_port $upnp_protocol >/dev/null
add_upnp_log=$(upnpc -i -u $route_upnp_xml $upnpeth -e "$upnp_base" -n $lan_ip_add $lan_ip_port $wan_ip_port $upnp_protocol 0)
add_upnp_log_a=$(echo $add_upnp_log | grep "duration" | grep "InternalIP:Port")
if [ -n "$add_upnp_log_a" ];then
return 1
fi
upnp_arrayp=$(upnpc -i -u $route_upnp_xml $upnpeth -L | grep "$upnp_base" | awk '{print $4"
"$3""$2}'| sed "s/->.*.://g" | sed "s/'//g")
for upnp_tpp in ${upnp_arrayp[@]}; do
if [[ ${upnp_tpp} = ${upnp_pa} ]] || [[ ${upnp_tpp} = ${upnp_pb} ]]
then
return 1
else
cd /
fi
done
return 0
}
upnp_nat_v1 (){
lan_ip_add=$1
route_upnp_xml=$2
lan_ip_port=$3
wan_ip_port=$4
upnp_protocol=$5
upnp_base=$6
upnp_eth=$7
upnp_pa=$upnp_base""$wan_ip_port""$lan_ip_port""$(echo $upnp_protocol | tr a-z A-Z)
upnp_pb=$upnp_base"
"$wan_ip_port""$lan_ip_port""$upnp_protocol
if [ -n "$upnp_eth" ]; then
upnpeth=" -m "$upnp_eth
else
upnpeth=" "
fi
upnp_array=$(upnpc -u $route_upnp_xml $upnpeth -l | grep "$upnp_base" | awk '{print $3""$2}'| sed 's/-.*.//g')
for upnp_tp in ${upnp_array[@]}; do
upnpc -u $route_upnp_xml $upnpeth -d $(echo $upnp_tp | sed 's/_/ /g') > /dev/null
done
upnpc -u $route_upnp_xml $upnpeth -d $wan_ip_port $upnp_protocol >/dev/null
add_upnp_log=$(upnpc -u $route_upnp_xml $upnpeth -e "$upnp_base" -a $lan_ip_add $lan_ip_port $wan_ip_port $upnp_protocol 0)
add_upnp_log_a=$(echo $add_upnp_log | grep "duration" | grep "InternalIP:Port")
if [ -n "$add_upnp_log_a" ];then
return 1
fi
upnp_arrayp=$(upnpc -u $route_upnp_xml $upnpeth -l | grep "$upnp_base" | awk '{print $4"
"$3""$2}'| sed "s/->.*.://g" | sed "s/'//g")
for upnp_tpp in ${upnp_arrayp[@]}; do
if [[ ${upnp_tpp} = ${upnp_pa} ]] || [[ ${upnp_tpp} = ${upnp_pb} ]]
then
return 1
else
cd /
fi
done
return 0
}
upnp_nat_v1i (){
lan_ip_add=$1
route_upnp_xml=$2
lan_ip_port=$3
wan_ip_port=$4
upnp_protocol=$5
upnp_base=$6
upnp_eth=$7
upnp_pa=$upnp_base""$wan_ip_port""$lan_ip_port""$(echo $upnp_protocol | tr a-z A-Z)
upnp_pb=$upnp_base"
"$wan_ip_port""$lan_ip_port""$upnp_protocol
if [ -n "$upnp_eth" ]; then
upnpeth=" -m "$upnp_eth
else
upnpeth=" "
fi
upnp_array=$(upnpc -i -u $route_upnp_xml $upnpeth -l | grep "$upnp_base" | awk '{print $3""$2}'| sed 's/-.*.//g')
for upnp_tp in ${upnp_array[@]}; do
upnpc -i -u $route_upnp_xml $upnpeth -d $(echo $upnp_tp | sed 's/_/ /g') > /dev/null
done
upnpc -i -u $route_upnp_xml $upnpeth -d $wan_ip_port $upnp_protocol >/dev/null
add_upnp_log=$(upnpc -i -u $route_upnp_xml $upnpeth -e "$upnp_base" -a $lan_ip_add $lan_ip_port $wan_ip_port $upnp_protocol 0)
add_upnp_log_a=$(echo $add_upnp_log | grep "duration" | grep "InternalIP:Port")
if [ -n "$add_upnp_log_a" ];then
return 1
fi
upnp_arrayp=$(upnpc -i -u $route_upnp_xml $upnpeth -l | grep "$upnp_base" | awk '{print $4"
"$3""$2}'| sed "s/->.*.://g" | sed "s/'//g")
for upnp_tpp in ${upnp_arrayp[@]}; do
if [[ ${upnp_tpp} = ${upnp_pa} ]] || [[ ${upnp_tpp} = ${upnp_pb} ]]
then
return 1
else
cd /
fi
done
return 0
}
#######################################################################开始处理数据
fb=$(expr $wan_upnp - 1)
echo
upnp_exip_up=""
upnp_exip_down=""
wan_gw_array=(${wan_gw_info// / })
for ((i=0;i<$wan_upnp;i++))
do
wan_gw_ip=${wan_gw_array[$i]}
gw_upnp_xml=$(get_gw_upnp $wan_gw_ip "$upnp_xml")
echo $(date +"%F %T")"获取网关xml:"$gw_upnp_xml
upnp_exip_down=$(get_upnp_exip "$gw_upnp_xml" $eth)
echo $(date +"%F %T")"获取网关出口IP:"$upnp_exip_down

	if [ ! -n "$gw_upnp_xml" ];then
	    echo "网关"$wan_gw_ip"未找到UPNP服务"
	    exit 0
	fi
    if [[ $i -eq 0 ]]
        then
            #本机IP OR 内网下lan IP
			upnp_exip_up=$lan_ip
        else
			#映射的内网端口
			lan_upnp_port=$wan_upnp_port
			#本机IP OR 内网下lan IP(上一轮的出口IP)
			upnp_exip_up=$upnp_exip_down_d
			if [ ! -n "$upnp_exip_up" ]; then
				#上一轮未获取到出口IP作为LAN IP,将通过服务信息检索出服务IP
				upnp_exip_up=$(get_ip $wan_gw_ip $wan_upnp_port $protocol "$upnp_text_info")
			fi
    fi
	#差值 将本轮出口IP 作为下一网关 内网下lan
	upnp_exip_down_d=$upnp_exip_down
	
	#旁路由判定-基于IP段是否相同的方式判定 因为正常路由wan和lan不能处于相同网段
	gw_wan_domain_a=$(echo ${wan_gw_array[$i]} | cut -d. -f1-3)
	gw_wan_domain_b=$(echo ${wan_gw_array[$i + 1]} | cut -d. -f1-3)
	if [[ "$gw_wan_domain_a" = "$gw_wan_domain_b" ]] && [[ "$side_route" = "yes" ]];then
		echo "疑似遇到旁路由模式,停止本网关映射,进行映射跃迁,不经过旁路映射"
		if [[ $i -eq 0 ]]
			then
				#差值 将本轮 LAN IP 作为下一网关 内网下lan
				upnp_exip_down_d=$lan_ip
			else
				#差值 将本轮 LAN IP 作为下一网关 内网下lan
				upnp_exip_down_d=$upnp_exip_up
		fi
		continue
	fi
	break_id=1
	for ((;;))
		do
			upnp_nat_v2 $upnp_exip_up "$gw_upnp_xml" $lan_upnp_port $wan_upnp_port $protocol "$upnp_port_name" $eth
			if [ $? -eq 1 ];then
				upnp_state="V2映射成功"
				break
			fi
			upnp_nat_v2i $upnp_exip_up "$gw_upnp_xml" $lan_upnp_port $wan_upnp_port $protocol "$upnp_port_name" $eth
			if [ $? -eq 1 ];then
				upnp_state="V2i映射成功"
				break
			fi
			upnp_nat_v1 $upnp_exip_up "$gw_upnp_xml" $lan_upnp_port $wan_upnp_port $protocol "$upnp_port_name" $eth
			if [ $? -eq 1 ];then
				upnp_state="V1映射成功"
				break
			fi
			upnp_nat_v1i $upnp_exip_up "$gw_upnp_xml" $lan_upnp_port $wan_upnp_port $protocol "$upnp_port_name" $eth
			if [ $? -eq 1 ];then
				upnp_state="V1i映射成功"
				break
			fi
			if [ $break_id -eq 1 ];then
				upnp_state="映射失败"
				break
			fi
			((break_id++));
		done
    if [[ "$cspding" != "route" ]]
        then
            echo ""
            echo $upnp_exip_up "$gw_upnp_xml" $lan_upnp_port $wan_upnp_port $protocol "$upnp_port_name" $eth
            echo "路由层 "$i" "$upnp_state"--------"$upnp_exip_up "-->>" $wan_gw_ip "upnpXML" $gw_upnp_xml "ExitIP "$upnp_exip_down
        else
            echo $upnp_exip_up "$gw_upnp_xml" $lan_upnp_port $wan_upnp_port $protocol "$upnp_port_name" $eth
            echo "路由层 "$i" "$upnp_state"--------"$upnp_exip_up "-->>" $wan_gw_ip "upnpXML" $gw_upnp_xml "ExitIP "$upnp_exip_down
    fi
    echo ""
    echo ""
done`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

7 participants