mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-12-24 13:27:56 +08:00
上个commit又忘添加文件了,这回确认补充上文件了
This commit is contained in:
39
netLayer/sockopt_darwin.go
Normal file
39
netLayer/sockopt_darwin.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
44
netLayer/sockopt_windows.go
Normal file
44
netLayer/sockopt_windows.go
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user