mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-02-26 16:57:08 +00:00
chore: listening tcp together for dns server (#1792)
This commit is contained in:
30
common/sockopt/reuse_common.go
Normal file
30
common/sockopt/reuse_common.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package sockopt
|
||||
|
||||
import (
|
||||
"net"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func RawConnReuseaddr(rc syscall.RawConn) (err error) {
|
||||
var innerErr error
|
||||
err = rc.Control(func(fd uintptr) {
|
||||
innerErr = reuseControl(fd)
|
||||
})
|
||||
|
||||
if innerErr != nil {
|
||||
err = innerErr
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func UDPReuseaddr(c net.PacketConn) error {
|
||||
if c, ok := c.(syscall.Conn); ok {
|
||||
rc, err := c.SyscallConn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return RawConnReuseaddr(rc)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
5
common/sockopt/reuse_other.go
Normal file
5
common/sockopt/reuse_other.go
Normal file
@@ -0,0 +1,5 @@
|
||||
//go:build !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris && !windows
|
||||
|
||||
package sockopt
|
||||
|
||||
func reuseControl(fd uintptr) error { return nil }
|
||||
22
common/sockopt/reuse_unix.go
Normal file
22
common/sockopt/reuse_unix.go
Normal file
@@ -0,0 +1,22 @@
|
||||
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
||||
|
||||
package sockopt
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func reuseControl(fd uintptr) error {
|
||||
e1 := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
|
||||
e2 := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
|
||||
|
||||
if e1 != nil {
|
||||
return e1
|
||||
}
|
||||
|
||||
if e2 != nil {
|
||||
return e2
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
9
common/sockopt/reuse_windows.go
Normal file
9
common/sockopt/reuse_windows.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package sockopt
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
func reuseControl(fd uintptr) error {
|
||||
return windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_REUSEADDR, 1)
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package sockopt
|
||||
|
||||
import (
|
||||
"net"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func UDPReuseaddr(c *net.UDPConn) (err error) {
|
||||
rc, err := c.SyscallConn()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
rc.Control(func(fd uintptr) {
|
||||
err = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
//go:build !linux
|
||||
|
||||
package sockopt
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
func UDPReuseaddr(c *net.UDPConn) (err error) {
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user