Chore: use gateway address of fake ip pool as the TUN device address

This commit is contained in:
gVisor bot 2022-03-17 07:41:18 +08:00
parent 6a735b10d7
commit 9ef90782ce
7 changed files with 22 additions and 11 deletions

View file

@ -34,8 +34,12 @@ func ShouldFindProcess(metadata *C.Metadata) bool {
return false return false
} }
func AppendLocalIPs(ip ...net.IP) {
localIPs = append(ip, localIPs...)
}
func getLocalIPs() []net.IP { func getLocalIPs() []net.IP {
ips := []net.IP{net.IPv4(198, 18, 0, 1), net.IPv4zero, net.IPv6zero} ips := []net.IP{net.IPv4zero, net.IPv6zero}
netInterfaces, err := net.Interfaces() netInterfaces, err := net.Interfaces()
if err != nil { if err != nil {

View file

@ -81,7 +81,7 @@ func ApplyConfig(cfg *config.Config, force bool) {
updateDNS(cfg.DNS, cfg.Tun) updateDNS(cfg.DNS, cfg.Tun)
updateGeneral(cfg.General, force) updateGeneral(cfg.General, force)
updateIPTables(cfg.DNS, cfg.General.TProxyPort, cfg.General.Interface, cfg.Tun.Enable) updateIPTables(cfg.DNS, cfg.General.TProxyPort, cfg.General.Interface, cfg.Tun.Enable)
updateTun(cfg.Tun) updateTun(cfg.Tun, cfg.DNS.FakeIPRange.IPNet().String())
updateExperimental(cfg) updateExperimental(cfg)
log.SetLevel(cfg.General.LogLevel) log.SetLevel(cfg.General.LogLevel)
@ -176,8 +176,8 @@ func updateRules(rules []C.Rule) {
tunnel.UpdateRules(rules) tunnel.UpdateRules(rules)
} }
func updateTun(tun *config.Tun) { func updateTun(tun *config.Tun, tunAddressPrefix string) {
P.ReCreateTun(tun, tunnel.TCPIn(), tunnel.UDPIn()) P.ReCreateTun(tun, tunAddressPrefix, tunnel.TCPIn(), tunnel.UDPIn())
} }
func updateGeneral(general *config.General, force bool) { func updateGeneral(general *config.General, force bool) {

View file

@ -307,7 +307,7 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address()) log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address())
} }
func ReCreateTun(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) { func ReCreateTun(tunConf *config.Tun, tunAddressPrefix string, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
tunMux.Lock() tunMux.Lock()
defer tunMux.Unlock() defer tunMux.Unlock()
@ -328,7 +328,7 @@ func ReCreateTun(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *
return return
} }
tunStackListener, err = tun.New(tunConf, tcpIn, udpIn) tunStackListener, err = tun.New(tunConf, tunAddressPrefix, tcpIn, udpIn)
} }
// GetPorts return the ports of proxy servers // GetPorts return the ports of proxy servers

View file

@ -20,7 +20,7 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int,
var ( var (
interfaceName = dev.Name() interfaceName = dev.Name()
ip = addr.Masked().Addr().Next() ip = addr.Masked().Addr().Next()
gw = addr.Addr() gw = ip
netmask = IPv4MaskString(addr.Bits()) netmask = IPv4MaskString(addr.Bits())
) )

View file

@ -121,7 +121,7 @@ startOver:
// add gateway // add gateway
deduplicatedRoutes = append(deduplicatedRoutes, &winipcfg.RouteData{ deduplicatedRoutes = append(deduplicatedRoutes, &winipcfg.RouteData{
Destination: addr.Masked(), Destination: addr.Masked(),
NextHop: addr.Addr(), NextHop: addr.Masked().Addr().Next().Next(),
Metric: 0, Metric: 0,
}) })

View file

@ -40,8 +40,8 @@ var ipv4LoopBack = netip.MustParsePrefix("127.0.0.0/8")
func New(device device.Device, dnsHijack []netip.AddrPort, tunAddress netip.Prefix, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (ipstack.Stack, error) { func New(device device.Device, dnsHijack []netip.AddrPort, tunAddress netip.Prefix, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (ipstack.Stack, error) {
var ( var (
portal = tunAddress.Addr()
gateway = tunAddress.Masked().Addr().Next() gateway = tunAddress.Masked().Addr().Next()
portal = gateway.Next()
) )
stack, err := mars.StartListener(device, gateway, portal) stack, err := mars.StartListener(device, gateway, portal)

View file

@ -9,6 +9,7 @@ import (
"github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/common/cmd" "github.com/Dreamacro/clash/common/cmd"
"github.com/Dreamacro/clash/component/process"
"github.com/Dreamacro/clash/config" "github.com/Dreamacro/clash/config"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/tun/device" "github.com/Dreamacro/clash/listener/tun/device"
@ -22,9 +23,9 @@ import (
) )
// New TunAdapter // New TunAdapter
func New(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (ipstack.Stack, error) { func New(tunConf *config.Tun, tunAddressPrefix string, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (ipstack.Stack, error) {
var ( var (
tunAddress = netip.MustParsePrefix("198.18.255.254/16") tunAddress = netip.MustParsePrefix(tunAddressPrefix)
devName = tunConf.Device devName = tunConf.Device
stackType = tunConf.Stack stackType = tunConf.Stack
autoRoute = tunConf.AutoRoute autoRoute = tunConf.AutoRoute
@ -40,6 +41,12 @@ func New(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.
devName = generateDeviceName() devName = generateDeviceName()
} }
if !tunAddress.IsValid() || !tunAddress.Addr().Is4() {
tunAddress = netip.MustParsePrefix("198.18.0.1/16")
}
process.AppendLocalIPs(tunAddress.Masked().Addr().Next().AsSlice())
// open tun device // open tun device
tunDevice, err = parseDevice(devName, uint32(mtu)) tunDevice, err = parseDevice(devName, uint32(mtu))
if err != nil { if err != nil {