refactor: 添加no_gvisor 编译tag, 剔除gvisor stack支持, 方便在arm设备上debug

This commit is contained in:
gVisor bot 2022-06-03 20:07:30 +08:00
parent 9ccc89ad95
commit 739502ae30
26 changed files with 197 additions and 57 deletions

View file

@ -1,5 +1,3 @@
//go:build !no_doq
package dns
import (

View file

@ -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
}

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package device
import (

View 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
}

View file

@ -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 {

View 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
}

View file

@ -0,0 +1,14 @@
//go:build no_gvisor
package fdbased
import (
"os"
)
type FD struct {
fd int
mtu uint32
file *os.File
}

View 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 {

View 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
}

View 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")
}

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
// Package iobased provides the implementation of io.ReadWriter
// based data-link layer endpoints.
package iobased

View file

@ -0,0 +1 @@
package iobased

View file

@ -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 {

View 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
}

View 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
}

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package adapter
import (

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package adapter
// Handler is a TCP/UDP connection handler that implements

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package gvisor
import (

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package gvisor
import (

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package option
import (

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package gvisor
import (

View file

@ -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),

View 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")
}

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package gvisor
import (

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package gvisor
import (

View file

@ -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()