github.com/xxf098/lite-proxy@v0.15.1-0.20230422081941-12c69f323218/transport/dialer/system_dialer.go (about)

     1  package dialer
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  	"syscall"
     7  	"time"
     8  
     9  	"github.com/xxf098/lite-proxy/log"
    10  )
    11  
    12  type controller func(network, address string, fd uintptr) error
    13  
    14  var controllers []controller
    15  
    16  func Dialer() (*net.Dialer, error) {
    17  	dialer := &net.Dialer{
    18  		Timeout: 5 * time.Second,
    19  	}
    20  	if len(controllers) > 0 {
    21  		dialer.Control = func(network, address string, c syscall.RawConn) error {
    22  			return c.Control(func(fd uintptr) {
    23  
    24  				for _, ctl := range controllers {
    25  					if err := ctl(network, address, fd); err != nil {
    26  						// errors.New("failed to apply external controller").Base(err).WriteToLog(session.ExportIDToError(ctx))
    27  						log.E("failed to apply external controller")
    28  						continue
    29  					}
    30  				}
    31  			})
    32  		}
    33  	}
    34  	return dialer, nil
    35  }
    36  
    37  func RegisterDialerController(ctl func(network, address string, fd uintptr) error) error {
    38  	if ctl == nil {
    39  		return errors.New("nil listener controller")
    40  	}
    41  
    42  	controllers = append(controllers, ctl)
    43  	return nil
    44  }