Skip to content

Commit

Permalink
Update stun_bt.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Oniicyan authored Oct 24, 2024
1 parent b43dd27 commit e12f4cb
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions stun_bt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ fi
# 判断脚本运行的环境,选择 DNAT 方式
# 先排除需要 UPnP 的情况
DNAT=0
for LANADDR in $(ip -4 a show dev br-lan | grep inet | awk '{print$2}' | awk -F '/' '{print$1}'); do
for LANADDR in $(ip -4 a | grep inet | awk '{print$2}' | awk -F '/' '{print$1}'); do
[ $DNAT = 1 ] && break
[ "$LANADDR" = $GWLADDR ] && DNAT=1
done
for LANADDR in $(nslookup -type=A $HOSTNAME | grep Address | grep -v :53 | awk '{print$2}'); do
for LANADDR in $(nslookup -type=A $HOSTNAME | grep Address | grep -v -e ':\d' | awk '{print$2}'); do
[ $DNAT = 1 ] && break
[ "$LANADDR" = $GWLADDR ] && DNAT=1
done
Expand All @@ -142,7 +142,7 @@ if [ $DNAT = 0 ]; then
[ -n "$OLDPORT" ] && upnpc -i -d $OLDPORT $L4PROTO
[ -n "$DISPORT" ] && upnpc -i -d $DISPORT
upnpc -i -e "STUN BT $L4PROTO $WANPORT->$LANPORT->$APPPORT" -a $APPADDR $APPPORT $LANPORT $L4PROTO | \
grep $APPADDR | grep $APPPORT | grep $LANPORT | grep -v failed
grep $APPADDR | grep $APPPORT | grep $LANPORT | grep -v failed >/dev/null
[ $? = 0 ] && DNAT=3
fi

Expand All @@ -155,12 +155,12 @@ if [ $DNAT = 0 ]; then
[ -n "$DISPORT" ] && proxychains -f $PROXYCONF upnpc -i -d $DISPORT
proxychains -f $PROXYCONF \
upnpc -i -e "STUN BT $L4PROTO $WANPORT->$LANPORT->$APPPORT" -a $APPADDR $APPPORT $LANPORT $L4PROTO | \
grep $APPADDR | grep $APPPORT | grep $LANPORT | grep -v failed
grep $APPADDR | grep $APPPORT | grep $LANPORT | grep -v failed >/dev/null
[ $? = 0 ] && DNAT=3
fi

# 代理失败,则启用本机 UPnP
[ $DNAT = 0 ] && (upnpc -i -e "STUN BT $L4PROTO $WANPORT->$LANPORT" -a @ $LANPORT $LANPORT $L4PROTO; DNAT=4)
[ $DNAT = 0 ] && (upnpc -i -e "STUN BT $L4PROTO $WANPORT->$LANPORT" -a @ $LANPORT $LANPORT $L4PROTO >/dev/null 2>&1 &; DNAT=4)

# 清理不需要的规则
if [ $DNAT = 3 ]; then
Expand All @@ -181,12 +181,10 @@ SETDNAT() {
if [ "$RELEASE" = "openwrt" ]; then
uci -q delete firewall.stun_foo
if uci show firewall | grep =redirect >/dev/null; then
i=0
for CONFIG in $(uci show firewall | grep =redirect | awk -F = '{print$1}'); do
[ "$(uci -q get $CONFIG.enabled)" = 0 ] && let i++ && break
[ "$(uci -q get $CONFIG.src)" != "wan" ] && let i++
[ "$(uci -q get $CONFIG.src)" = "wan" ] && [ "$(uci -q get $CONFIG.enabled)" != 0 ] && \
RULE=1 && break
done
[ $(uci show firewall | grep =redirect | wc -l) -gt $i ] && RULE=1
fi
if [ "$RULE" != 1 ]; then
uci set firewall.stun_foo=redirect
Expand Down

0 comments on commit e12f4cb

Please sign in to comment.