Skip to content

Commit

Permalink
Update stun_bt_lucky.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Oniicyan authored Oct 24, 2024
1 parent e12f4cb commit dff2604
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions stun_bt_lucky.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ OWNADDR=

OWNNAME=$(echo stun_bt_$APPADDR:$APPPORT$([ -n "$IFNAME" ] && echo @$IFNAME) | sed 's/[[:punct:]]/_/g')
STUNIFO=/tmp/$OWNNAME.info
OLDPORT=$LANPORT # Lucky 使用固定本地端口
OLDPORT= # Lucky 使用固定本地端口,无需清理 UPnP 规则
RELEASE=$(grep ^ID= /etc/os-release | awk -F '=' '{print$2}' | tr -d \")

# 判断 TCP 或 UDP 的穿透是否启用
Expand Down Expand Up @@ -105,11 +105,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 @@ -119,7 +119,7 @@ done
if [ $DNAT = 0 ]; then
[ -n "$OLDPORT" ] && upnpc -i -d $OLDPORT $L4PROTO
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 @@ -131,12 +131,12 @@ if [ $DNAT = 0 ]; then
[ -n "$OLDPORT" ] && proxychains -f $PROXYCONF upnpc -i -d $OLDPORT $L4PROTO
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 @@ -157,12 +157,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 dff2604

Please sign in to comment.