mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-11-02 21:04:01 +08:00
Feature: support mark on FreeBSD & OpenBSD
This commit is contained in:
33
component/dialer/sockopt_freebsd.go
Normal file
33
component/dialer/sockopt_freebsd.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"net"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func setSocketOptions(network, address string, c syscall.RawConn, opts *Options) (err error) {
|
||||
if opts == nil || !isTCPSocket(network) && !isUDPSocket(network) {
|
||||
return
|
||||
}
|
||||
|
||||
var innerErr error
|
||||
err = c.Control(func(fd uintptr) {
|
||||
host, _, _ := net.SplitHostPort(address)
|
||||
if ip := net.ParseIP(host); ip != nil && !ip.IsGlobalUnicast() {
|
||||
return
|
||||
}
|
||||
|
||||
if opts.RoutingMark != 0 {
|
||||
if innerErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_USER_COOKIE, opts.RoutingMark); innerErr != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if innerErr != nil {
|
||||
err = innerErr
|
||||
}
|
||||
return
|
||||
}
|
||||
33
component/dialer/sockopt_openbsd.go
Normal file
33
component/dialer/sockopt_openbsd.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"net"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func setSocketOptions(network, address string, c syscall.RawConn, opts *Options) (err error) {
|
||||
if opts == nil || !isTCPSocket(network) && !isUDPSocket(network) {
|
||||
return
|
||||
}
|
||||
|
||||
var innerErr error
|
||||
err = c.Control(func(fd uintptr) {
|
||||
host, _, _ := net.SplitHostPort(address)
|
||||
if ip := net.ParseIP(host); ip != nil && !ip.IsGlobalUnicast() {
|
||||
return
|
||||
}
|
||||
|
||||
if opts.RoutingMark != 0 {
|
||||
if innerErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RTABLE, opts.RoutingMark); innerErr != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if innerErr != nil {
|
||||
err = innerErr
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build !linux && !darwin && !windows
|
||||
//go:build !unix && !windows
|
||||
|
||||
package dialer
|
||||
|
||||
|
||||
Reference in New Issue
Block a user