From 4cee70d16477dcd9d8b096d9e4e8ffcf90c00f06 Mon Sep 17 00:00:00 2001 From: e1732a364fed <75717694+e1732a364fed@users.noreply.github.com> Date: Sat, 1 Jan 2000 00:00:00 +0000 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=B8=AAcommit=E5=8F=88=E5=BF=98?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6=E4=BA=86=EF=BC=8C=E8=BF=99?= =?UTF-8?q?=E5=9B=9E=E7=A1=AE=E8=AE=A4=E8=A1=A5=E5=85=85=E4=B8=8A=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- netLayer/sockopt_darwin.go | 39 ++++++++++++++++++++++++++++++++ netLayer/sockopt_windows.go | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 netLayer/sockopt_darwin.go create mode 100644 netLayer/sockopt_windows.go diff --git a/netLayer/sockopt_darwin.go b/netLayer/sockopt_darwin.go new file mode 100644 index 0000000..8dcf4a9 --- /dev/null +++ b/netLayer/sockopt_darwin.go @@ -0,0 +1,39 @@ +package netLayer + +import ( + "net" + + "github.com/e1732a364fed/v2ray_simple/utils" + "go.uber.org/zap" + "golang.org/x/sys/unix" +) + +func SetSockOpt(fd int, sockopt *Sockopt, isudp bool, isipv6 bool) { + if sockopt.Device != "" { + bindToDevice(fd, sockopt.Device) + } +} + +func bindToDevice(fd int, device string) { + iface, err := net.InterfaceByName(device) + + if err != nil { + if ce := utils.CanLogErr("BindToDevice failed, seems name wrong."); ce != nil { + ce.Write(zap.Error(err)) + } + return + } + + if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index); err != nil { + if ce := utils.CanLogErr("BindToDevice failed"); ce != nil { + ce.Write(zap.Error(err)) + } + return + } + if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, iface.Index); err != nil { + if ce := utils.CanLogErr("BindToDevice failed, ipv6"); ce != nil { + ce.Write(zap.Error(err)) + } + return + } +} diff --git a/netLayer/sockopt_windows.go b/netLayer/sockopt_windows.go new file mode 100644 index 0000000..7e5f1bc --- /dev/null +++ b/netLayer/sockopt_windows.go @@ -0,0 +1,44 @@ +package netLayer + +import ( + "net" + + "github.com/e1732a364fed/v2ray_simple/utils" + "go.uber.org/zap" + "golang.org/x/sys/windows" +) + +// SetSockOpt 是平台相关的. +func SetSockOpt(fd int, sockopt *Sockopt, isudp bool, isipv6 bool) { + if sockopt.Device != "" { + bindToDevice(fd, sockopt.Device) + } +} + +func bindToDevice(fd int, device string) { + iface, err := net.InterfaceByName(device) + if err != nil { + if ce := utils.CanLogErr("BindToDevice failed, seems name wrong."); ce != nil { + ce.Write(zap.Error(err)) + } + return + } + + const ( + IP_UNICAST_IF = 31 + IPV6_UNICAST_IF = 31 + ) + + if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, IP_UNICAST_IF, iface.Index); err != nil { + if ce := utils.CanLogErr("BindToDevice failed"); ce != nil { + ce.Write(zap.Error(err)) + } + return + } + if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IPV6, IPV6_UNICAST_IF, iface.Index); err != nil { + if ce := utils.CanLogErr("BindToDevice failed, ipv6"); ce != nil { + ce.Write(zap.Error(err)) + } + return + } +}