taamarin / box_for_magisk

Transparent Proxy for Android(root)
GNU General Public License v3.0
1.31k stars 130 forks source link

[BUG] ipv6 公网前缀改变造成的回环问题 #83

Open flyflas opened 10 months ago

flyflas commented 10 months ago

使用box for magisk。在开启ipv6时,出现回环。

发生回环的日志如下:

 time="2024-01-27T04:08:16.421281998Z" level=info msg="[TCP] 10.8.168.130:45642 --> cdn-mum.alibabachengdun.com:80 match GeoSite(cn) using DIRECT"
time="2024-01-27T04:08:16.787135956Z" level=info msg="[TCP] 10.8.168.130:49332 --> api.kingdata.ksyun.com:80 match GeoSite(cn) using DIRECT"
time="2024-01-27T04:08:16.978507466Z" level=info msg="[UDP] 10.8.168.130:44456(com.taobao.taobao, uid=10289) --> 27.222.18.252:443 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:16.982510695Z" level=info msg="[TCP] 10.8.168.130:38032 --> 59.82.9.145:80 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:17.097762362Z" level=info msg="[UDP] 10.8.168.130:39367(com.taobao.taobao, uid=10289) --> 111.206.225.225:443 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:17.138356164Z" level=info msg="[TCP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40314(com.taobao.taobao, uid=10289) --> [2408:4001:f00::2a2]:443 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:17.153735747Z" level=info msg="[TCP] 10.8.168.130:46286 --> 27.222.18.251:80 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:17.376748456Z" level=info msg="[TCP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40574(com.taobao.taobao, uid=10289) --> [2408:872f:900:9:3::3f7]:443 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:17.392426372Z" level=info msg="[TCP] 10.8.168.130:46070 --> guide-acs.m.taobao.com:443 match GeoSite(cn) using DIRECT"
time="2024-01-27T04:08:17.60409106Z" level=info msg="[TCP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:42246(com.tencent.mm, uid=10264) --> [2408:80f1:21:5001::19]:80 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:17.924612883Z" level=info msg="[TCP] 10.8.168.130:41732 --> cdn.ynuf.aliapp.org:80 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:18.120202101Z" level=info msg="[TCP] 10.8.168.130:40150(com.taobao.taobao, uid=10289) --> 123.125.216.235:80 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:18.332215382Z" level=info msg="[TCP] 10.8.168.130:49498 --> mpush-api.aliyun.com:443 match GeoSite(cn) using DIRECT"
time="2024-01-27T04:08:18.458856893Z" level=info msg="[TCP] 10.8.168.130:37926 --> mum.alibabachengdun.com:443 match GeoSite(cn) using DIRECT"
time="2024-01-27T04:08:19.178144965Z" level=info msg="[TCP] 10.8.168.130:40564(com.taobao.taobao, uid=10289) --> 61.48.83.231:443 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:19.188444288Z" level=info msg="[TCP] 10.8.168.130:43894(com.taobao.taobao, uid=10289) --> 61.48.83.231:80 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:19.485862778Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:42497(com.taobao.taobao, uid=10289) --> [2408:4001:f10::21f]:443 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:19.559882882Z" level=info msg="[UDP] [2408:4001:f10::21f]:443(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:42497 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:19.571622569Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:42007(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:42497 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:19.613739132Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:46502(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:42497 match GeoIP(CN) using DIRECT"
time="2024-01-27T04:08:19.629892257Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:46806(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:42497 match GeoIP(CN) using DIRECT"

在此之后,一直有这样的回环:

time="2024-01-27T04:08:19.629892257Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:46806(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:42497 match GeoIP(CN) using DIRECT"

DNS设置如下

dns:
  enable: true
  prefer-h3: true
  listen: 0.0.0.0:1053
  use-hosts: true
  ipv6: true
  enhanced-mode: fake-ip
  fake-ip-range: 198.18.0.1/16
  fake-ip-filter:
  - '*'
  - +.lan
  - +.local
  default-nameserver:
  - 114.114.114.114
  - 223.5.5.5
  - 127.0.0.1:53
  nameserver:
  - 114.114.114.114
  - 223.5.5.5
  - https://dns.alidns.com/dns-query
  proxy-server-nameserver:
  - https://dns.alidns.com/dns-query
  - https://8.8.8.8/dns-query#🔬 Dns Proxy
  - https://1.1.1.1/dns-query#🔬 Dns Proxy
  fallback:
  - https://8.8.8.8/dns-query#🔬 Dns Proxy
  - https://1.1.1.1/dns-query#🔬 Dns Proxy
  nameserver-policy:
    geosite:private:
    - https://doh.pub/dns-query
    - https://dns.alidns.com/dns-query
    geosite:geolocation-!cn:
    - https://dns.cloudflare.com/dns-query#🔬 Dns Proxy
    - https://dns.google/dns-query#🔬 Dns Proxy

mihomo issue: #926

不知道是不是防火墙规则的问题。还请各位大佬指教。

flyflas commented 9 months ago

问题定位

问题应该出现在这里: 177 378

box for magisk 在177行的位置,获得本机的ipv6地址,并将其添加到intranet6这个数组中。

intranet6+=($(ip -6 address | busybox awk '/inet6/ && !/::1/ && !/fe80/ {print $2}'))

在378行,将intranet6数组中的ip段放行

# Add rules for intranet6 subnets
    for subnet6 in "${intranet6[@]}"; do
      if [ "${bin_name}" = "clash" ]; then
        ${iptables} -t mangle -A BOX_EXTERNAL -d "${subnet6}" -j RETURN
      else
        ${iptables} -t mangle -A BOX_EXTERNAL -d "${subnet6}" -p udp ! --dport 53 -j RETURN
        ${iptables} -t mangle -A BOX_EXTERNAL -d "${subnet6}" ! -p udp -j RETURN
      fi
    done

当我们的ipv6地址前缀发生变化的时候(比如出去吃饭,移动基站变化),这会造成ip6tables规则中,没有直接放行本机的ipv6公网地址段,就会造成回环。

因为大家使用的ipv4地址大多是私有地址,所以即使ipv4地址变化,依旧是私有的ipv4地址。intranet数组默认包含了所有的私有地址,因此不会存在回环问题。

intranet=(
  0.0.0.0/8
  10.0.0.0/8
  100.64.0.0/10
  127.0.0.0/8
  169.254.0.0/16
  172.16.0.0/12
  192.0.0.0/24
  192.0.2.0/24
  192.88.99.0/24
  192.168.0.0/16
  198.51.100.0/24
  203.0.113.0/24
  224.0.0.0/4
  240.0.0.0/4
  255.0.0.0/4
  255.255.255.0/24
  # 255.255.255.255/32
)

问题复现

在关闭网络的情况下打开box for magisk。再打开WIFI获取ipv6地址(一定是WIFI,数据网络默认会保存上一次的ip地址),使用ipv6代理,3~5分钟即可出现回环。

出现回环的日志如下

time="2024-02-11T10:44:01.356641283Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:43666(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40847 match GeoIP(CN) using DIRECT"
time="2024-02-11T10:44:01.383465762Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:38557(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40847 match GeoIP(CN) using DIRECT"
time="2024-02-11T10:44:01.42293722Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:49612(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40847 match GeoIP(CN) using DIRECT"
time="2024-02-11T10:44:01.457308574Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:46316(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40847 match GeoIP(CN) using DIRECT"
time="2024-02-11T10:44:01.490456179Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:42886(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40847 match GeoIP(CN) using DIRECT"
time="2024-02-11T10:44:01.536314668Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:43773(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40847 match GeoIP(CN) using DIRECT"
time="2024-02-11T10:44:01.563910241Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:47476(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40847 match GeoIP(CN) using DIRECT"
time="2024-02-11T10:44:01.581299668Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:39568(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40847 match GeoIP(CN) using DIRECT"
time="2024-02-11T10:44:01.599732168Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:46655(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40847 match GeoIP(CN) using DIRECT"
time="2024-02-11T10:44:01.631097376Z" level=info msg="[UDP] [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:38314(clash) --> [2408:8406:1981:2bf1:2017:68c9:9146:2d02]:40847 match GeoIP(CN) using DIRECT"

adb shell中,执行以下代码,回环停止。

ip6tables -t mangle -I BOX_EXTERNAL -d 2408:8406:1981:2bf1:2017:68c9:9146:2d02/64 -j RETURN

建议

似乎通过ip a命令获取本机ipv6,并且在iptables放行的做法并不可行。一旦本机的公网ipv6发生变化,就有出现回环的危险。

Xray 透明代理的教程中提供了其他两种规避方法(mark 和 GID),不知道通过GID的方式是否可行。

透明代理教程 透明代理通过 gid 规避 Xray 流量

flyflas commented 9 months ago

添加了一个定时检测本机公网ipv6的脚本以后,问题已经基本解决。经过几天的测试,并未出现回环问题。 如果需要,我可以提PR。

不知道还有没有其他更加优雅的解决方法。

kxzxxx commented 4 months ago

intranet6=( # CU-CN 2408:8000::/20 # CT 240e::/18 # CM 2409:8000::/20 )

把三大运营商的公网前缀添加到intranet6即可

MeepoMaster commented 3 months ago

intranet6=( # CU-CN 2408:8000::/20 # CT 240e::/18 # CM 2409:8000::/20 )

把三大运营商的公网前缀添加到intranet6即可

我也遇到了,用了这个。好方法,再也不会看到右上角几百M/s地跑流量了。作者要不要考虑把这个加到默认里。