chore: Adopt sing-tun's update

This commit is contained in:
wwqgtxx
2023-04-17 20:38:37 +08:00
parent cae9cf44cf
commit f100ce6a04
6 changed files with 60 additions and 6 deletions

View File

@ -229,8 +229,8 @@ func New(options LC.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapte
err = E.Cause(err, "configure tun interface")
return
}
l.tunIf = tunIf
l.tunStack, err = tun.NewStack(strings.ToLower(options.Stack.String()), tun.StackOptions{
stackOptions := tun.StackOptions{
Context: context.TODO(),
Tun: tunIf,
MTU: tunOptions.MTU,
@ -241,7 +241,16 @@ func New(options LC.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapte
UDPTimeout: udpTimeout,
Handler: handler,
Logger: log.SingLogger,
})
}
if options.FileDescriptor > 0 {
if tunName, err := getTunnelName(int32(options.FileDescriptor)); err != nil {
stackOptions.Name = tunName
stackOptions.UnderPlatform = true
}
}
l.tunIf = tunIf
l.tunStack, err = tun.NewStack(strings.ToLower(options.Stack.String()), stackOptions)
if err != nil {
return
}

View File

@ -0,0 +1,11 @@
package sing_tun
import "golang.org/x/sys/unix"
func getTunnelName(fd int32) (string, error) {
return unix.GetsockoptString(
int(fd),
2, /* #define SYSPROTO_CONTROL 2 */
2, /* #define UTUN_OPT_IFNAME 2 */
)
}

View File

@ -0,0 +1,25 @@
package sing_tun
import (
"fmt"
"golang.org/x/sys/unix"
"syscall"
"unsafe"
)
const ifReqSize = unix.IFNAMSIZ + 64
func getTunnelName(fd int32) (string, error) {
var ifr [ifReqSize]byte
var errno syscall.Errno
_, _, errno = unix.Syscall(
unix.SYS_IOCTL,
uintptr(fd),
uintptr(unix.TUNGETIFF),
uintptr(unsafe.Pointer(&ifr[0])),
)
if errno != 0 {
return "", fmt.Errorf("failed to get name of TUN device: %w", errno)
}
return unix.ByteSliceToString(ifr[:]), nil
}

View File

@ -0,0 +1,9 @@
//go:build !(darwin || linux)
package sing_tun
import "os"
func getTunnelName(fd int32) (string, error) {
return "", os.ErrInvalid
}