github.com/igoogolx/clash@v1.19.8/component/dialer/reuse_unix.go (about)

     1  //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     2  
     3  package dialer
     4  
     5  import (
     6  	"net"
     7  	"syscall"
     8  
     9  	"golang.org/x/sys/unix"
    10  )
    11  
    12  func addrReuseToListenConfig(lc *net.ListenConfig) {
    13  	chain := lc.Control
    14  
    15  	lc.Control = func(network, address string, c syscall.RawConn) (err error) {
    16  		defer func() {
    17  			if err == nil && chain != nil {
    18  				err = chain(network, address, c)
    19  			}
    20  		}()
    21  
    22  		return c.Control(func(fd uintptr) {
    23  			unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
    24  			unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
    25  		})
    26  	}
    27  }