chore: Adopt sing-tun's update
This commit is contained in:
@ -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
|
||||
}
|
||||
|
11
listener/sing_tun/tun_name_darwin.go
Normal file
11
listener/sing_tun/tun_name_darwin.go
Normal 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 */
|
||||
)
|
||||
}
|
25
listener/sing_tun/tun_name_linux.go
Normal file
25
listener/sing_tun/tun_name_linux.go
Normal 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
|
||||
}
|
9
listener/sing_tun/tun_name_other.go
Normal file
9
listener/sing_tun/tun_name_other.go
Normal file
@ -0,0 +1,9 @@
|
||||
//go:build !(darwin || linux)
|
||||
|
||||
package sing_tun
|
||||
|
||||
import "os"
|
||||
|
||||
func getTunnelName(fd int32) (string, error) {
|
||||
return "", os.ErrInvalid
|
||||
}
|
Reference in New Issue
Block a user