mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 03:32:33 +08:00
Fix: set SO_REUSEADDR for UDP listeners on linux (#630)
This commit is contained in:
parent
98db1988c6
commit
af00355b28
19
common/sockopt/reuseaddr_linux.go
Normal file
19
common/sockopt/reuseaddr_linux.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
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
|
||||
}
|
11
common/sockopt/reuseaddr_other.go
Normal file
11
common/sockopt/reuseaddr_other.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
// +build !linux
|
||||
|
||||
package sockopt
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
func UDPReuseaddr(c *net.UDPConn) (err error) {
|
||||
return
|
||||
}
|
|
@ -3,6 +3,8 @@ package dns
|
|||
import (
|
||||
"net"
|
||||
|
||||
"github.com/Dreamacro/clash/common/sockopt"
|
||||
|
||||
D "github.com/miekg/dns"
|
||||
)
|
||||
|
||||
|
@ -58,6 +60,11 @@ func ReCreateServer(addr string, resolver *Resolver) error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = sockopt.UDPReuseaddr(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
address = addr
|
||||
handler := newHandler(resolver)
|
||||
server = &Server{handler: handler}
|
||||
|
|
|
@ -31,7 +31,11 @@ func setsockopt(c *net.UDPConn, addr string) error {
|
|||
}
|
||||
|
||||
rc.Control(func(fd uintptr) {
|
||||
err = syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1)
|
||||
err = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
|
||||
|
||||
if err == nil {
|
||||
err = syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1)
|
||||
}
|
||||
if err == nil && isIPv6 {
|
||||
err = syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, IPV6_TRANSPARENT, 1)
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
adapters "github.com/Dreamacro/clash/adapters/inbound"
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
"github.com/Dreamacro/clash/common/sockopt"
|
||||
"github.com/Dreamacro/clash/component/socks5"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/tunnel"
|
||||
|
@ -22,6 +23,11 @@ func NewSocksUDPProxy(addr string) (*SockUDPListener, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
err = sockopt.UDPReuseaddr(l.(*net.UDPConn))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sl := &SockUDPListener{l, addr, false}
|
||||
go func() {
|
||||
for {
|
||||
|
|
Loading…
Reference in New Issue
Block a user