MetaCubeX / mihomo

A simple Python Pydantic model for Honkai: Star Rail parsed data from the Mihomo API.
https://wiki.metacubex.one
MIT License
15.36k stars 2.55k forks source link

TUN 模式下无法代理 ip 请求,所有 ip 请求不经过内核直连 #187

Closed lyaaz closed 1 year ago

lyaaz commented 2 years ago

dns 及 tun 相关配置如下:

sniffer:
  enable: true
  sniffing:
    - tls
    - http
  skip-domain:
    - 'Mijia Cloud'
    - 'dlg.io.mi.com'
    - '+.apple.com'

  port-whitelist:
  - 80
  - 443

dns:
  enable: true
  listen: 127.0.0.1:1053
  ipv6: false
  use-host: true
  default-nameserver:
    - 114.114.114.114
    - 8.8.8.8
  enhanced-mode: fake-ip
  fake-ip-range: 198.18.0.1/16
  nameserver:
    - https://doh.pub/dns-query
    - https://223.5.5.5/dns-query
  fallback:
    - https://1.0.0.1/dns-query
    - https://dns.google/dns-query
    - quic://a.passcloud.xyz:784
  fallback-filter:
    geoip: true
    geoip-code: CN
    geosite:
      - geolocation-!cn
    ipcidr:
      - 240.0.0.0/4
  fake-ip-filter:
    - "*.lan"
    - +.stun.*.*
    - +.stun.*.*.*
    - +.stun.*.*.*.*
    - +.stun.*.*.*.*.*
    - "*.msftncsi.com"
    - "*.msftconnecttest.com"
    - time.windows.com
tun:
  enable: true
  device: meta
  stack: system
  auto-route: true
  auto-detect-interface: true
  dns-hijack:
    - any:53
Adlyq commented 2 years ago

@lyaaz 内网ip还是ipv6?

lyaaz commented 2 years ago

@lyaaz 内网ip还是ipv6?

telegram 无法连接(使用自定义代理正常),以及 https://1.1.1.1 等,都无法在日志中看到任何信息,所以推测是这部分流量没进内核

Skyxim commented 2 years ago

@lyaaz 是否启动成功 tun 网卡, 使用一下命令打印下结果 route print -4

lyaaz commented 2 years ago

@lyaaz 是否启动成功 tun 网卡, 使用一下命令打印下结果 route print -4

tun是启动的,connection 中可以看到,域名请求的都没有问题

===========================================================================
接口列表
 48...........................Meta Tunnel
 47...........................NWPU
  3...58 8a 5a 3f b0 2d ......Realtek PCIe GbE Family Controller
 14...5e ea f4 23 7a d4 ......Qualcomm QCA9377 802.11ac Wireless Adapter
 15...5e ea 1d 23 4c 71 ......Microsoft Wi-Fi Direct Virtual Adapter
 12...6e ea 1d 23 4c 71 ......Microsoft Wi-Fi Direct Virtual Adapter #2
 19...00 50 56 c0 00 01 ......VMware Virtual Ethernet Adapter for VMnet1
 17...00 50 56 c0 00 08 ......VMware Virtual Ethernet Adapter for VMnet8
  1...........................Software Loopback Interface 1
===========================================================================

IPv4 路由表
===========================================================================
活动路由:
网络目标        网络掩码          网关       接口   跃点数
          0.0.0.0          0.0.0.0            在链路上     10.62.103.196     26
          0.0.0.0          0.0.0.0            在链路上        198.18.0.1      0
    10.62.103.196  255.255.255.255            在链路上     10.62.103.196    281
        127.0.0.0        255.0.0.0            在链路上         127.0.0.1   4556
        127.0.0.1  255.255.255.255            在链路上         127.0.0.1   4556
  127.255.255.255  255.255.255.255            在链路上         127.0.0.1   4556
      169.254.0.0      255.255.0.0            在链路上    169.254.61.186   4506
   169.254.61.186  255.255.255.255            在链路上    169.254.61.186   4506
  169.254.255.255  255.255.255.255            在链路上    169.254.61.186   4506
    192.168.182.0    255.255.255.0            在链路上     192.168.182.1   4516
    192.168.182.1  255.255.255.255            在链路上     192.168.182.1   4516
  192.168.182.255  255.255.255.255            在链路上     192.168.182.1   4516
    192.168.213.0    255.255.255.0            在链路上     192.168.213.1   4516
    192.168.213.1  255.255.255.255            在链路上     192.168.213.1   4516
  192.168.213.255  255.255.255.255            在链路上     192.168.213.1   4516
       198.18.0.0      255.255.0.0       198.18.0.2       198.18.0.1      0
       198.18.0.1  255.255.255.255            在链路上        198.18.0.1    256
        224.0.0.0        240.0.0.0            在链路上         127.0.0.1   4556
        224.0.0.0        240.0.0.0            在链路上     192.168.182.1   4516
        224.0.0.0        240.0.0.0            在链路上     192.168.213.1   4516
        224.0.0.0        240.0.0.0            在链路上    169.254.61.186   4506
        224.0.0.0        240.0.0.0            在链路上     10.62.103.196     26
  255.255.255.255  255.255.255.255            在链路上         127.0.0.1   4556
  255.255.255.255  255.255.255.255            在链路上     192.168.182.1   4516
  255.255.255.255  255.255.255.255            在链路上     192.168.213.1   4516
  255.255.255.255  255.255.255.255            在链路上    169.254.61.186   4506
  255.255.255.255  255.255.255.255            在链路上     10.62.103.196    281
===========================================================================
永久路由:
  无
lyaaz commented 2 years ago

看到一个类似 issue https://github.com/Dreamacro/clash/issues/2302 ,但奇怪的是我用原版 clash 测试是正常的

lyaaz commented 2 years ago

补充信息: system: win10 19044.1889 clash.meta: v1.13.1 网络为 PPPOE 拨号连接

tun 模式 system/gvisor 结果相同。

刚发现 tun 模式下,wsl1 中的进程也都无法被代理(未测试 wsl2),包括 curl, wget, pacman 等,同样在日志和 connection 中看不到任何信息。

Skyxim commented 2 years ago

PPPOE 尝试将网卡名字改成英文

zjy7723 commented 2 years ago

用TUN 模式的人很多,telegram等ip直连不经过内核的问题应该是个例。感觉问题出在用户的windows路由表上, 重置路由表试试。如果重装系统不麻烦的话,可以重装系统试试。能找出问题对开发者及类似问题的用户很有帮助不是。

lyaaz commented 2 years ago

PPPOE 尝试将网卡名字改成英文

名字一直都是纯英文无空格

用TUN 模式的人很多,telegram等ip直连不经过内核的问题应该是个例。感觉问题出在用户的windows路由表上, 重置路由表试试。如果重装系统不麻烦的话,可以重装系统试试。能找出问题对开发者及类似问题的用户很有帮助不是。

路由表也重置过了,还是一样,重装系统就太麻烦了,暂不考虑

412999826 commented 1 year ago

看到一个类似 issue Dreamacro#2302 ,但奇怪的是我用原版 clash 测试是正常的

你说的原版正常是啥意思?意思是使用premium版本正常么?

lyaaz commented 1 year ago

看到一个类似 issue Dreamacro#2302 ,但奇怪的是我用原版 clash 测试是正常的

你说的原版正常是啥意思?意思是使用premium版本正常么?

是的

Lemmingh commented 1 year ago

或许是动态生成的约束导致了跳 route?

常见情况:

不妨以 管理员 权限执行:

Test-NetConnection -InformationLevel Detailed -Verbose -DiagnoseRouting -ComputerName '1.1.1.1'

关注 SelectedNetRouteRouteSelectionEvents


在我的机器上,Clash Premium 2022.08.26 注册的 route 为:

ifIndex DestinationPrefix  NextHop    RouteMetric ifMetric PolicyStore
------- -----------------  -------    ----------- -------- -----------
2       255.255.255.255/32 0.0.0.0            256          ActiveStore
2       224.0.0.0/4        0.0.0.0            256          ActiveStore
2       198.18.255.255/32  0.0.0.0            256          ActiveStore
2       198.18.0.1/32      0.0.0.0            256          ActiveStore
2       198.18.0.0/16      0.0.0.0            256          ActiveStore
2       128.0.0.0/1        198.18.0.2           0          ActiveStore
2       0.0.0.0/1          198.18.0.2           0          ActiveStore
2       ff00::/8           ::                 256 5        ActiveStore

值得关注的就 3 条:

ifIndex DestinationPrefix  NextHop    RouteMetric ifMetric PolicyStore
------- -----------------  -------    ----------- -------- -----------
2       198.18.0.0/16      0.0.0.0            256          ActiveStore
2       128.0.0.0/1        198.18.0.2           0          ActiveStore
2       0.0.0.0/1          198.18.0.2           0          ActiveStore

其中,128.0.0.0/10.0.0.0/1 的并集相当于 0.0.0.0/0,但优先级更高。

看上去,跟 Clash.Meta (https://github.com/MetaCubeX/Clash.Meta/issues/187#issuecomment-1248882135) 的基本一致。


https://github.com/Dreamacro/clash/issues/2302

我感觉关联不大。

Linux 采用 Weak ES Model,send 总是查询整张 routing table。

BSD 系的系统(包括 macOS 和 Windows)采用 Strong ES Model,如果 socket 已经 bound,则 source address 决定 outgoing interface 进而约束 route lookup。

Windows 也允许切换 model,不过,我好像没见过谁改。

lyaaz commented 1 year ago

不妨以 管理员 权限执行:

Test-NetConnection -InformationLevel Detailed -Verbose -DiagnoseRouting -ComputerName '1.1.1.1'

关注 SelectedNetRouteRouteSelectionEvents

感谢回复,我电脑上执行结果如下:

ComputerName                      : 1.1.1.1
RemoteAddress                     : 1.1.1.1
SelectedSourceAddress             : 10.62.103.196
OutgoingInterfaceIndex            : 47
SelectedNetRoute                  : DestinationPrefix: 0.0.0.0/0
                                    NextHop: 0.0.0.0
DestinationAddressSelectionEvents :
RouteSelectionEvents              : IP: Route [DestinationPrefix: 0.0.0.0/0 NextHop: 0.0.0.0 InterfaceIndex: 48 RouteMe
                                    tric: 0] is blocked for Destination: 1.1.1.1 ConstrainInterfaceIndex: 0 ConstrainSc
                                    opeZone: 1 in Compartment: 1, Reason: Default Routes Disabled On Interface.
SourceAddressSelectionEvents      : IP: Source address 10.62.103.196 is preferred over 198.18.0.1 for Destination 1.1.1
                                    .1 in Compartment 1, Reason: Prefer Outgoing Interface (Rule 5.0).
RouteDiagnosticsSucceeded         : True

我对路由相关知识不太了解,请问这种情况应该如何修复?

Lemmingh commented 1 year ago

[^ras]: 在 Windows RAS 中,拨号和 VPN 使用基本一致的逻辑,所以,对 PPPoE 和 VPN 的讨论可以互换。

Default Routes Disabled On Interface

RAS [^ras] 禁用了其他所有 default route (0.0.0.0/0)。于是,直接流向 PPPoE (NWPU)。

解决方案:增加 route

https://github.com/MetaCubeX/Clash.Meta/blob/5987f8e3b5d489c577328c4cbc147432cb45a498/listener/tun/ipstack/commons/router_windows.go#L91

增加 128.0.0.0/10.0.0.0/1,从而回避涉及 default route 的问题。

目测,Clash Premium 的更新日志

2021.09.07

fix auto-detect-interface auto-route in PPPoE networks on Windows

就是这个事情。

临时设置

#Requires -Version 5.1
#Requires -Modules NetTCPIP
#Requires -RunAsAdministrator

Set-StrictMode -Version Latest
$ConfirmPreference = 'None'
$ErrorActionPreference = 'SilentlyContinue'

# 根据实际情况填写。
$InterfaceIndex = (Get-NetIPConfiguration -InterfaceAlias 'Meta').InterfaceIndex

$RouteBase = @{
    InterfaceIndex = $InterfaceIndex
    NextHop        = '0.0.0.0' # 假装 on-link。
    PolicyStore    = 'ActiveStore' # 重启系统后消除。
    RouteMetric    = 0
}

$Destinations = [string[]] @(
    '128.0.0.0/1'
    '0.0.0.0/1'
    '0.0.0.0/0'
)

# 静默执行足矣。
Remove-NetRoute -InterfaceIndex $InterfaceIndex -DestinationPrefix $Destinations -PolicyStore ActiveStore -Confirm:$false

foreach ($Item in $Destinations)
{
    New-NetRoute @RouteBase -DestinationPrefix $Item
}

解决方案:Split tunneling

作为临时处理,也可以试试 split tunneling

Split tunneling 的定义

https://docs.microsoft.com/en-us/windows/security/identity-protection/vpn/vpn-routing

In a split tunnel configuration, routes can be specified to go over VPN and all other traffic will go over the physical interface.

https://docs.microsoft.com/en-us/powershell/module/vpnclient/set-vpnconnection

When you enable split tunneling, traffic to destinations outside the intranet does not flow through the VPN tunnel.

存档

为便于日后参考,可以请你打一份详尽的 routing table 吗?

#Requires -Version 5.1
#Requires -Modules NetTCPIP

$Interfaces = Get-NetIPInterface -ConnectionState Connected
$Query = @{
    AddressFamily  = 'IPv4'
    InterfaceIndex = $Interfaces | Select-Object -ExpandProperty InterfaceIndex -Unique
}
$RoutingTable = Get-NetRoute @Query
$DefaultRoutes = Get-NetRoute @Query -DestinationPrefix '0.0.0.0/0'

$Interfaces | Format-Table -AutoSize
$RoutingTable | Format-Table -AutoSize
$DefaultRoutes | Format-Table -AutoSize ifIndex, NextHop, State, Protocol, Publish, PreferredLifetime, ValidLifetime
lyaaz commented 1 year ago

临时设置

非常感谢,使用脚本后网络恢复正常

routing table

ClashMeta 启动后(脚本运行之前):


ifIndex InterfaceAlias                AddressFamily NlMtu(Bytes) InterfaceMetric Dhcp     ConnectionState PolicyStore
------- --------------                ------------- ------------ --------------- ----     --------------- -----------
17      VMware Network Adapter VMnet8 IPv6                  1500            4260 Enabled  Connected       ActiveStore
19      VMware Network Adapter VMnet1 IPv6                  1500            4260 Enabled  Connected       ActiveStore
3       以太网                        IPv6                  1500            4250 Enabled  Connected       ActiveStore
53      meta                          IPv6                  9000            4230 Disabled Connected       ActiveStore
1       Loopback Pseudo-Interface 1   IPv6            4294967295            4300 Disabled Connected       ActiveStore
47      NWPU                          IPv6                  1480              25 Enabled  Connected       ActiveStore
17      VMware Network Adapter VMnet8 IPv4                  1500            4260 Enabled  Connected       ActiveStore
19      VMware Network Adapter VMnet1 IPv4                  1500            4260 Enabled  Connected       ActiveStore
3       以太网                        IPv4                  1500            4250 Enabled  Connected       ActiveStore
53      meta                          IPv4                  9000                 Disabled Connected       ActiveStore
1       Loopback Pseudo-Interface 1   IPv4            4294967295            4300 Disabled Connected       ActiveStore
47      NWPU                          IPv4                  1480              25 Disabled Connected       ActiveStore

ifIndex DestinationPrefix  NextHop    RouteMetric ifMetric PolicyStore
------- -----------------  -------    ----------- -------- -----------
47      255.255.255.255/32 0.0.0.0            256 25       ActiveStore
3       255.255.255.255/32 0.0.0.0            256 4250     ActiveStore
17      255.255.255.255/32 0.0.0.0            256 4260     ActiveStore
19      255.255.255.255/32 0.0.0.0            256 4260     ActiveStore
1       255.255.255.255/32 0.0.0.0            256 4300     ActiveStore
47      224.0.0.0/4        0.0.0.0              1 25       ActiveStore
3       224.0.0.0/4        0.0.0.0            256 4250     ActiveStore
17      224.0.0.0/4        0.0.0.0            256 4260     ActiveStore
19      224.0.0.0/4        0.0.0.0            256 4260     ActiveStore
1       224.0.0.0/4        0.0.0.0            256 4300     ActiveStore
53      198.18.0.1/32      0.0.0.0            256          ActiveStore
53      198.18.0.0/16      198.18.0.2           0          ActiveStore
17      192.168.213.255/32 0.0.0.0            256 4260     ActiveStore
17      192.168.213.1/32   0.0.0.0            256 4260     ActiveStore
17      192.168.213.0/24   0.0.0.0            256 4260     ActiveStore
19      192.168.182.255/32 0.0.0.0            256 4260     ActiveStore
19      192.168.182.1/32   0.0.0.0            256 4260     ActiveStore
19      192.168.182.0/24   0.0.0.0            256 4260     ActiveStore
3       169.254.255.255/32 0.0.0.0            256 4250     ActiveStore
3       169.254.61.186/32  0.0.0.0            256 4250     ActiveStore
3       169.254.0.0/16     0.0.0.0            256 4250     ActiveStore
1       127.255.255.255/32 0.0.0.0            256 4300     ActiveStore
1       127.0.0.1/32       0.0.0.0            256 4300     ActiveStore
1       127.0.0.0/8        0.0.0.0            256 4300     ActiveStore
47      10.62.103.196/32   0.0.0.0            256 25       ActiveStore
53      0.0.0.0/0          0.0.0.0              0          ActiveStore
47      0.0.0.0/0          0.0.0.0              1 25       ActiveStore

ifIndex NextHop State Protocol Publish PreferredLifetime         ValidLifetime
------- ------- ----- -------- ------- -----------------         -------------
     53 0.0.0.0 Alive  NetMgmt      No 10675199.02:48:05.4775807 10675199.02:48:05.4775807
     47 0.0.0.0 Alive  NetMgmt      No 10675199.02:48:05.4775807 10675199.02:48:05.4775807
MetaCubeX commented 1 year ago

https://github.com/MetaCubeX/Clash.Meta/blob/Alpha/listener/tun/ipstack/commons/router.go#L11

lyaaz commented 1 year ago

手动指定路由地址后正常

  inet4_route_address:
    - 0.0.0.0/1
    - 128.0.0.0/1