refactor: 添加no_gvisor 编译tag, 剔除gvisor stack支持, 方便在arm设备上debug
This commit is contained in:
parent
1298d2f8b6
commit
ed9b9ce3c5
26 changed files with 197 additions and 57 deletions
|
@ -1,5 +1,3 @@
|
|||
//go:build !no_doq
|
||||
|
||||
package dns
|
||||
|
||||
import (
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
//go:build no_doq
|
||||
|
||||
package dns
|
||||
|
||||
import "github.com/Dreamacro/clash/log"
|
||||
|
||||
func newDOQ(r *Resolver, addr, proxyAdapter string) dnsClient {
|
||||
log.Fatalln("unsupported feature on the build")
|
||||
return nil
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
package device
|
||||
|
||||
import (
|
||||
|
|
29
listener/tun/device/device_no_gvisor.go
Normal file
29
listener/tun/device/device_no_gvisor.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
//go:build no_gvisor
|
||||
|
||||
package device
|
||||
|
||||
// Device is the interface that implemented by network layer devices (e.g. tun),
|
||||
// and easy to use as stack.LinkEndpoint.
|
||||
type Device interface {
|
||||
|
||||
// Name returns the current name of the device.
|
||||
Name() string
|
||||
|
||||
// Type returns the driver type of the device.
|
||||
Type() string
|
||||
|
||||
// Read packets from tun device
|
||||
Read(packet []byte) (int, error)
|
||||
|
||||
// Write packets to tun device
|
||||
Write(packet []byte) (int, error)
|
||||
|
||||
// Close stops and closes the device.
|
||||
Close() error
|
||||
|
||||
// UseEndpoint work for gVisor stack
|
||||
UseEndpoint() error
|
||||
|
||||
// UseIOBased work for other ip stack
|
||||
UseIOBased() error
|
||||
}
|
|
@ -4,24 +4,13 @@ package fdbased
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/Dreamacro/clash/listener/tun/device"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
||||
)
|
||||
|
||||
type FD struct {
|
||||
stack.LinkEndpoint
|
||||
|
||||
fd int
|
||||
mtu uint32
|
||||
|
||||
file *os.File
|
||||
}
|
||||
|
||||
func Open(name string, mtu uint32) (device.Device, error) {
|
||||
fd, err := strconv.Atoi(name)
|
||||
if err != nil {
|
||||
|
|
17
listener/tun/device/fdbased/fd_unix_gvisor.go
Normal file
17
listener/tun/device/fdbased/fd_unix_gvisor.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
package fdbased
|
||||
|
||||
import (
|
||||
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
||||
"os"
|
||||
)
|
||||
|
||||
type FD struct {
|
||||
stack.LinkEndpoint
|
||||
|
||||
fd int
|
||||
mtu uint32
|
||||
|
||||
file *os.File
|
||||
}
|
14
listener/tun/device/fdbased/fd_unix_no_gvisor.go
Normal file
14
listener/tun/device/fdbased/fd_unix_no_gvisor.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
//go:build no_gvisor
|
||||
|
||||
package fdbased
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
type FD struct {
|
||||
fd int
|
||||
mtu uint32
|
||||
|
||||
file *os.File
|
||||
}
|
|
@ -7,7 +7,6 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/Dreamacro/clash/listener/tun/device"
|
||||
"github.com/Dreamacro/clash/listener/tun/device/iobased"
|
||||
)
|
||||
|
||||
func open(fd int, mtu uint32) (device.Device, error) {
|
||||
|
@ -17,12 +16,7 @@ func open(fd int, mtu uint32) (device.Device, error) {
|
|||
}
|
||||
|
||||
func (f *FD) useEndpoint() error {
|
||||
ep, err := iobased.New(os.NewFile(uintptr(f.fd), f.Name()), f.mtu, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create endpoint: %w", err)
|
||||
}
|
||||
f.LinkEndpoint = ep
|
||||
return nil
|
||||
return newEp(f)
|
||||
}
|
||||
|
||||
func (f *FD) useIOBased() error {
|
||||
|
|
19
listener/tun/device/fdbased/open_others_gvisor.go
Normal file
19
listener/tun/device/fdbased/open_others_gvisor.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
//go:build !no_gvisor && !linux && !windows
|
||||
|
||||
package fdbased
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/Dreamacro/clash/listener/tun/device/iobased"
|
||||
)
|
||||
|
||||
func newEp(f *FD) error {
|
||||
ep, err := iobased.New(os.NewFile(uintptr(f.fd), f.Name()), f.mtu, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create endpoint: %w", err)
|
||||
}
|
||||
f.LinkEndpoint = ep
|
||||
return nil
|
||||
}
|
11
listener/tun/device/fdbased/open_others_no_gvisor.go
Normal file
11
listener/tun/device/fdbased/open_others_no_gvisor.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
//go:build no_gvisor && !linux && !windows
|
||||
|
||||
package fdbased
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func newEp(f *FD) error {
|
||||
return fmt.Errorf("unsupported gvisor on the build")
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
// Package iobased provides the implementation of io.ReadWriter
|
||||
// based data-link layer endpoints.
|
||||
package iobased
|
||||
|
|
1
listener/tun/device/iobased/iobased.go
Normal file
1
listener/tun/device/iobased/iobased.go
Normal file
|
@ -0,0 +1 @@
|
|||
package iobased
|
|
@ -8,22 +8,10 @@ import (
|
|||
"runtime"
|
||||
|
||||
"github.com/Dreamacro/clash/listener/tun/device"
|
||||
"github.com/Dreamacro/clash/listener/tun/device/iobased"
|
||||
|
||||
"golang.zx2c4.com/wireguard/tun"
|
||||
)
|
||||
|
||||
type TUN struct {
|
||||
*iobased.Endpoint
|
||||
|
||||
nt *tun.NativeTun
|
||||
mtu uint32
|
||||
name string
|
||||
offset int
|
||||
|
||||
cache []byte
|
||||
}
|
||||
|
||||
func Open(name string, mtu uint32) (_ device.Device, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
|
@ -91,11 +79,7 @@ func (t *TUN) Write(packet []byte) (int, error) {
|
|||
}
|
||||
|
||||
func (t *TUN) Close() error {
|
||||
defer func(ep *iobased.Endpoint) {
|
||||
if ep != nil {
|
||||
ep.Close()
|
||||
}
|
||||
}(t.Endpoint)
|
||||
defer closeIO(t)
|
||||
return t.nt.Close()
|
||||
}
|
||||
|
||||
|
@ -105,12 +89,7 @@ func (t *TUN) Name() string {
|
|||
}
|
||||
|
||||
func (t *TUN) UseEndpoint() error {
|
||||
ep, err := iobased.New(t, t.mtu, t.offset)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create endpoint: %w", err)
|
||||
}
|
||||
t.Endpoint = ep
|
||||
return nil
|
||||
return newEq(t)
|
||||
}
|
||||
|
||||
func (t *TUN) UseIOBased() error {
|
||||
|
|
34
listener/tun/device/tun/tun_wireguard_gvisor.go
Normal file
34
listener/tun/device/tun/tun_wireguard_gvisor.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
//go:build !linux && !no_gvisor
|
||||
|
||||
package tun
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Dreamacro/clash/listener/tun/device/iobased"
|
||||
"golang.zx2c4.com/wireguard/tun"
|
||||
)
|
||||
|
||||
type TUN struct {
|
||||
*iobased.Endpoint
|
||||
nt *tun.NativeTun
|
||||
mtu uint32
|
||||
name string
|
||||
offset int
|
||||
|
||||
cache []byte
|
||||
}
|
||||
|
||||
func closeIO(t *TUN) {
|
||||
if t.Endpoint != nil {
|
||||
t.Endpoint.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func newEq(t *TUN) error {
|
||||
ep, err := iobased.New(t, t.mtu, t.offset)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create endpoint: %w", err)
|
||||
}
|
||||
t.Endpoint = ep
|
||||
return nil
|
||||
}
|
24
listener/tun/device/tun/tun_wireguard_no_gvisor.go
Normal file
24
listener/tun/device/tun/tun_wireguard_no_gvisor.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
//go:build !linux && no_gvisor
|
||||
|
||||
package tun
|
||||
|
||||
import (
|
||||
"golang.zx2c4.com/wireguard/tun"
|
||||
)
|
||||
|
||||
type TUN struct {
|
||||
nt *tun.NativeTun
|
||||
mtu uint32
|
||||
name string
|
||||
offset int
|
||||
|
||||
cache []byte
|
||||
}
|
||||
|
||||
func closeIO(t *TUN) {
|
||||
|
||||
}
|
||||
|
||||
func newEq(t *TUN) error {
|
||||
return nil
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
package adapter
|
||||
|
||||
import (
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
package adapter
|
||||
|
||||
// Handler is a TCP/UDP connection handler that implements
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
package gvisor
|
||||
|
||||
import (
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
package gvisor
|
||||
|
||||
import (
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
package option
|
||||
|
||||
import (
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
package gvisor
|
||||
|
||||
import (
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
// Package gvisor provides a thin wrapper around a gVisor's stack.
|
||||
package gvisor
|
||||
|
||||
|
@ -64,8 +66,9 @@ func New(device device.Device, dnsHijack []netip.AddrPort, tunAddress netip.Pref
|
|||
|
||||
// Generate unique NIC id.
|
||||
nicID := tcpip.NICID(s.Stack.UniqueID())
|
||||
|
||||
opts = append(opts,
|
||||
defaultOpts := []option.Option{option.WithDefault()}
|
||||
defaultOpts = append(defaultOpts, opts...)
|
||||
opts = append(defaultOpts,
|
||||
// Create stack NIC and then bind link endpoint to it.
|
||||
withCreatingNIC(nicID, device),
|
||||
|
||||
|
|
19
listener/tun/ipstack/gvisor/stack_no_gvisor.go
Normal file
19
listener/tun/ipstack/gvisor/stack_no_gvisor.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
//go:build no_gvisor
|
||||
|
||||
package gvisor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Dreamacro/clash/adapter/inbound"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/listener/tun/device"
|
||||
"github.com/Dreamacro/clash/listener/tun/ipstack"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
// New allocates a new *gvStack with given options.
|
||||
func New(device device.Device, dnsHijack []netip.AddrPort, tunAddress netip.Prefix, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (ipstack.Stack, error) {
|
||||
log.Fatalln("unsupported gvisor stack on the build")
|
||||
return nil, fmt.Errorf("unsupported gvisor stack on the build")
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
package gvisor
|
||||
|
||||
import (
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
//go:build !no_gvisor
|
||||
|
||||
package gvisor
|
||||
|
||||
import (
|
||||
|
|
|
@ -13,7 +13,6 @@ import (
|
|||
"github.com/Dreamacro/clash/listener/tun/ipstack"
|
||||
"github.com/Dreamacro/clash/listener/tun/ipstack/commons"
|
||||
"github.com/Dreamacro/clash/listener/tun/ipstack/gvisor"
|
||||
"github.com/Dreamacro/clash/listener/tun/ipstack/gvisor/option"
|
||||
"github.com/Dreamacro/clash/listener/tun/ipstack/system"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"net/netip"
|
||||
|
@ -63,7 +62,7 @@ func New(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.
|
|||
return nil, fmt.Errorf("can't attach endpoint to tun: %w", err)
|
||||
}
|
||||
|
||||
tunStack, err = gvisor.New(tunDevice, tunConf.DNSHijack, tunAddress, tcpIn, udpIn, option.WithDefault())
|
||||
tunStack, err = gvisor.New(tunDevice, tunConf.DNSHijack, tunAddress, tcpIn, udpIn)
|
||||
|
||||
if err != nil {
|
||||
_ = tunDevice.Close()
|
||||
|
|
Loading…
Reference in a new issue