mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-12-24 13:27:56 +08:00
试图修正bindToDevice在windows上的问题
运行一次,发现遇到错误 The requested address is not valid in its context. 参考下面代码试一下 https://github.com/xjasonlyu/tun2socks/pull/192/files
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package netLayer
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"unsafe"
|
||||
|
||||
"github.com/e1732a364fed/v2ray_simple/utils"
|
||||
"go.uber.org/zap"
|
||||
@@ -39,7 +41,17 @@ func bindToDevice(fd int, device string, is6 bool) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, IP_UNICAST_IF, iface.Index); err != nil {
|
||||
|
||||
//https://github.com/xjasonlyu/tun2socks/pull/192/files
|
||||
|
||||
// For IPv4, this parameter must be an interface index in network byte order.
|
||||
// Ref: https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options
|
||||
|
||||
var bytes [4]byte
|
||||
binary.BigEndian.PutUint32(bytes[:], uint32(iface.Index))
|
||||
index := *(*uint32)(unsafe.Pointer(&bytes[0]))
|
||||
|
||||
if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, IP_UNICAST_IF, int(index)); err != nil {
|
||||
if ce := utils.CanLogErr("BindToDevice failed"); ce != nil {
|
||||
ce.Write(zap.Error(err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user