fix: no_gvisor compile failed for target linux

This commit is contained in:
Skyxim 2022-07-16 19:35:52 +08:00
parent 850c52d07c
commit 9a035d3c51
6 changed files with 70 additions and 35 deletions

View File

@ -1,12 +1,7 @@
package fdbased
import (
"fmt"
"github.com/Dreamacro/clash/listener/tun/device"
"gvisor.dev/gvisor/pkg/tcpip/link/fdbased"
"gvisor.dev/gvisor/pkg/tcpip/link/rawfile"
)
func open(fd int, mtu uint32) (device.Device, error) {
@ -16,17 +11,7 @@ func open(fd int, mtu uint32) (device.Device, error) {
}
func (f *FD) useEndpoint() error {
ep, err := fdbased.New(&fdbased.Options{
FDs: []int{f.fd},
MTU: f.mtu,
// TUN only, ignore ethernet header.
EthernetHeader: false,
})
if err != nil {
return fmt.Errorf("create endpoint: %w", err)
}
f.LinkEndpoint = ep
return nil
return f.newLinuxEp()
}
func (f *FD) useIOBased() error {
@ -34,23 +19,9 @@ func (f *FD) useIOBased() error {
}
func (f *FD) Read(packet []byte) (int, error) {
n, gvErr := rawfile.BlockingRead(f.fd, packet)
if gvErr != nil {
return 0, fmt.Errorf("read error: %s", gvErr.String())
}
return n, nil
return f.read(packet)
}
func (f *FD) Write(packet []byte) (int, error) {
n := len(packet)
if n == 0 {
return 0, nil
}
gvErr := rawfile.NonBlockingWrite(f.fd, packet)
if gvErr != nil {
return 0, fmt.Errorf("write error: %s", gvErr.String())
}
return n, nil
return f.write(packet)
}

View File

@ -0,0 +1,45 @@
//go:build !no_gvisor && (linux || android)
package fdbased
import (
"fmt"
"gvisor.dev/gvisor/pkg/tcpip/link/fdbased"
"gvisor.dev/gvisor/pkg/tcpip/link/rawfile"
)
func (f *FD) newLinuxEp() error {
ep, err := fdbased.New(&fdbased.Options{
FDs: []int{f.fd},
MTU: f.mtu,
// TUN only, ignore ethernet header.
EthernetHeader: false,
})
if err != nil {
return fmt.Errorf("create endpoint: %w", err)
}
f.LinkEndpoint = ep
return nil
}
func (f *FD) read(packet []byte) (int, error) {
n, gvErr := rawfile.BlockingRead(f.fd, packet)
if gvErr != nil {
return 0, fmt.Errorf("read error: %s", gvErr.String())
}
return n, nil
}
func (f *FD) write(packet []byte) (int, error) {
n := len(packet)
if n == 0 {
return 0, nil
}
gvErr := rawfile.NonBlockingWrite(f.fd, packet)
if gvErr != nil {
return 0, fmt.Errorf("write error: %s", gvErr.String())
}
return n, nil
}

View File

@ -0,0 +1,19 @@
//go:build no_gvisor
package fdbased
import (
"fmt"
)
func (f *FD) newLinuxEp() error {
return fmt.Errorf("unsupported gvisor on the build")
}
func (f *FD) read(packet []byte) (int, error) {
return 0, fmt.Errorf("unsupported gvisor on the build")
}
func (f *FD) write(packet []byte) (int, error) {
return 0, fmt.Errorf("unsupported gvisor on the build")
}

View File

@ -16,7 +16,7 @@ func open(fd int, mtu uint32) (device.Device, error) {
}
func (f *FD) useEndpoint() error {
return newEp(f)
return f.newEpOther()
}
func (f *FD) useIOBased() error {

View File

@ -9,7 +9,7 @@ import (
"github.com/Dreamacro/clash/listener/tun/device/iobased"
)
func newEp(f *FD) error {
func (f *FD) newEpOther() 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)

View File

@ -6,6 +6,6 @@ import (
"fmt"
)
func newEp(f *FD) error {
func (f *FD) newEpOther() error {
return fmt.Errorf("unsupported gvisor on the build")
}