github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/net.go (about)

     1  //go:build !freebsd
     2  // +build !freebsd
     3  
     4  package utils
     5  
     6  import (
     7  	"context"
     8  	"github.com/iwind/TeaGo/logs"
     9  	"net"
    10  	"syscall"
    11  )
    12  
    13  // ListenReuseAddr 监听可重用的端口
    14  func ListenReuseAddr(network string, addr string) (net.Listener, error) {
    15  	config := &net.ListenConfig{
    16  		Control: func(network, address string, c syscall.RawConn) error {
    17  			return c.Control(func(fd uintptr) {
    18  				err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, SO_REUSEPORT, 1)
    19  				if err != nil {
    20  					logs.Println("[LISTEN]" + err.Error())
    21  				}
    22  			})
    23  		},
    24  		KeepAlive: 0,
    25  	}
    26  	return config.Listen(context.Background(), network, addr)
    27  }