github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/tun/gvisor/open.go (about)

     1  package tun
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Asutorufa/yuhaiin/pkg/net/netlink"
     7  	"github.com/Asutorufa/yuhaiin/pkg/protos/config/listener"
     8  	"gvisor.dev/gvisor/pkg/tcpip/stack"
     9  )
    10  
    11  var openFD func(sc netlink.TunScheme, mtu int) (stack.LinkEndpoint, error)
    12  
    13  func Open(sc netlink.TunScheme, driver listener.TunEndpointDriver, mtu int) (_ stack.LinkEndpoint, err error) {
    14  	if driver == listener.Tun_fdbased && openFD != nil {
    15  		return openFD(sc, mtu)
    16  	}
    17  
    18  	w, err := OpenWriter(sc, mtu)
    19  	if err != nil {
    20  		return nil, fmt.Errorf("open tun failed: %w", err)
    21  	}
    22  	return NewEndpoint(w, uint32(mtu)), nil
    23  }