github.com/xxf098/lite-proxy@v0.15.1-0.20230422081941-12c69f323218/utils/util_linux.go (about)

     1  // +build linux
     2  
     3  package utils
     4  
     5  import (
     6  	"context"
     7  	"net"
     8  	"syscall"
     9  
    10  	"golang.org/x/sys/unix"
    11  )
    12  
    13  func Listen(ctx context.Context, network, address string) (net.Listener, error) {
    14  	lc := &net.ListenConfig{
    15  		Control: func(network, address string, conn syscall.RawConn) error {
    16  			return conn.Control(func(fd uintptr) {
    17  				_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEADDR|unix.SO_REUSEPORT, 1)
    18  			})
    19  		},
    20  	}
    21  
    22  	conn, err := lc.Listen(ctx, network, address)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	return conn, nil
    28  }