github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/cfgcommon/socketcfg/socket.go (about)

     1  package socketcfg
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/v2fly/v2ray-core/v5/transport/internet"
     7  )
     8  
     9  type SocketConfig struct {
    10  	Mark                 uint32 `json:"mark"`
    11  	TFO                  *bool  `json:"tcpFastOpen"`
    12  	TProxy               string `json:"tproxy"`
    13  	AcceptProxyProtocol  bool   `json:"acceptProxyProtocol"`
    14  	TCPKeepAliveInterval int32  `json:"tcpKeepAliveInterval"`
    15  	TCPKeepAliveIdle     int32  `json:"tcpKeepAliveIdle"`
    16  	TFOQueueLength       uint32 `json:"tcpFastOpenQueueLength"`
    17  	BindToDevice         string `json:"bindToDevice"`
    18  	RxBufSize            uint64 `json:"rxBufSize"`
    19  	TxBufSize            uint64 `json:"txBufSize"`
    20  	ForceBufSize         bool   `json:"forceBufSize"`
    21  }
    22  
    23  // Build implements Buildable.
    24  func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
    25  	var tfoSettings internet.SocketConfig_TCPFastOpenState
    26  	if c.TFO != nil {
    27  		if *c.TFO {
    28  			tfoSettings = internet.SocketConfig_Enable
    29  		} else {
    30  			tfoSettings = internet.SocketConfig_Disable
    31  		}
    32  	}
    33  
    34  	tfoQueueLength := c.TFOQueueLength
    35  	if tfoQueueLength == 0 {
    36  		tfoQueueLength = 4096
    37  	}
    38  
    39  	var tproxy internet.SocketConfig_TProxyMode
    40  	switch strings.ToLower(c.TProxy) {
    41  	case "tproxy":
    42  		tproxy = internet.SocketConfig_TProxy
    43  	case "redirect":
    44  		tproxy = internet.SocketConfig_Redirect
    45  	default:
    46  		tproxy = internet.SocketConfig_Off
    47  	}
    48  
    49  	return &internet.SocketConfig{
    50  		Mark:                 c.Mark,
    51  		Tfo:                  tfoSettings,
    52  		TfoQueueLength:       tfoQueueLength,
    53  		Tproxy:               tproxy,
    54  		AcceptProxyProtocol:  c.AcceptProxyProtocol,
    55  		TcpKeepAliveInterval: c.TCPKeepAliveInterval,
    56  		TcpKeepAliveIdle:     c.TCPKeepAliveIdle,
    57  		RxBufSize:            int64(c.RxBufSize),
    58  		TxBufSize:            int64(c.TxBufSize),
    59  		ForceBufSize:         c.ForceBufSize,
    60  		BindToDevice:         c.BindToDevice,
    61  	}, nil
    62  }