mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-04 12:27:37 +03:00
Add control conn func
This commit is contained in:
parent
9719bdcd4a
commit
fb82be7f3f
8 changed files with 18 additions and 10 deletions
|
@ -44,7 +44,7 @@ func BindToInterfaceIndexFunc(interfaceIndexFunc func() int) Func {
|
||||||
}
|
}
|
||||||
|
|
||||||
func bindToInterface(conn syscall.RawConn, network string, index int) error {
|
func bindToInterface(conn syscall.RawConn, network string, index int) error {
|
||||||
return Control(conn, func(fd uintptr) error {
|
return Raw(conn, func(fd uintptr) error {
|
||||||
switch network {
|
switch network {
|
||||||
case "tcp6", "udp6":
|
case "tcp6", "udp6":
|
||||||
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, index)
|
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, index)
|
||||||
|
|
|
@ -10,7 +10,7 @@ func NewBindManager() BindManager {
|
||||||
|
|
||||||
func BindToInterface(manager BindManager, interfaceName string) Func {
|
func BindToInterface(manager BindManager, interfaceName string) Func {
|
||||||
return func(network, address string, conn syscall.RawConn) error {
|
return func(network, address string, conn syscall.RawConn) error {
|
||||||
return Control(conn, func(fd uintptr) error {
|
return Raw(conn, func(fd uintptr) error {
|
||||||
return syscall.BindToDevice(int(fd), interfaceName)
|
return syscall.BindToDevice(int(fd), interfaceName)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ func BindToInterfaceFunc(manager BindManager, interfaceNameFunc func() string) F
|
||||||
if interfaceName == "" {
|
if interfaceName == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return Control(conn, func(fd uintptr) error {
|
return Raw(conn, func(fd uintptr) error {
|
||||||
return syscall.BindToDevice(int(fd), interfaceName)
|
return syscall.BindToDevice(int(fd), interfaceName)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ func bindInterfaceIndex(network string, address string, conn syscall.RawConn, in
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Control(conn, func(fd uintptr) error {
|
return Raw(conn, func(fd uintptr) error {
|
||||||
handle := syscall.Handle(fd)
|
handle := syscall.Handle(fd)
|
||||||
// handle ip empty, e.g. net.Listen("udp", ":0")
|
// handle ip empty, e.g. net.Listen("udp", ":0")
|
||||||
if ipStr == "" {
|
if ipStr == "" {
|
||||||
|
|
|
@ -22,9 +22,17 @@ func Append(oldFunc Func, newFunc Func) Func {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Control(conn syscall.RawConn, block func(fd uintptr) error) error {
|
func Conn(conn syscall.Conn, block func(fd uintptr) error) error {
|
||||||
|
rawConn, err := conn.SyscallConn()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return Raw(rawConn, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Raw(rawConn syscall.RawConn, block func(fd uintptr) error) error {
|
||||||
var innerErr error
|
var innerErr error
|
||||||
err := conn.Control(func(fd uintptr) {
|
err := rawConn.Control(func(fd uintptr) {
|
||||||
innerErr = block(fd)
|
innerErr = block(fd)
|
||||||
})
|
})
|
||||||
return E.Errors(innerErr, err)
|
return E.Errors(innerErr, err)
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
func RoutingMark(mark int) Func {
|
func RoutingMark(mark int) Func {
|
||||||
return func(network, address string, conn syscall.RawConn) error {
|
return func(network, address string, conn syscall.RawConn) error {
|
||||||
return Control(conn, func(fd uintptr) error {
|
return Raw(conn, func(fd uintptr) error {
|
||||||
return syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, mark)
|
return syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, mark)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ func sendAncillaryFileDescriptors(protectPath string, fileDescriptors []int) err
|
||||||
|
|
||||||
func ProtectPath(protectPath string) Func {
|
func ProtectPath(protectPath string) Func {
|
||||||
return func(network, address string, conn syscall.RawConn) error {
|
return func(network, address string, conn syscall.RawConn) error {
|
||||||
return Control(conn, func(fd uintptr) error {
|
return Raw(conn, func(fd uintptr) error {
|
||||||
return sendAncillaryFileDescriptors(protectPath, []int{int(fd)})
|
return sendAncillaryFileDescriptors(protectPath, []int{int(fd)})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
|
|
||||||
func ReuseAddr() Func {
|
func ReuseAddr() Func {
|
||||||
return func(network, address string, conn syscall.RawConn) error {
|
return func(network, address string, conn syscall.RawConn) error {
|
||||||
return Control(conn, func(fd uintptr) error {
|
return Raw(conn, func(fd uintptr) error {
|
||||||
return E.Errors(
|
return E.Errors(
|
||||||
unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1),
|
unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1),
|
||||||
unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1),
|
unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1),
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
func ReuseAddr() Func {
|
func ReuseAddr() Func {
|
||||||
return func(network, address string, conn syscall.RawConn) error {
|
return func(network, address string, conn syscall.RawConn) error {
|
||||||
return Control(conn, func(fd uintptr) error {
|
return Raw(conn, func(fd uintptr) error {
|
||||||
return syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
|
return syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue