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

     1  package v4
     2  
     3  import (
     4  	"github.com/v2fly/v2ray-core/v5/common/serial"
     5  	"github.com/v2fly/v2ray-core/v5/transport"
     6  	"github.com/v2fly/v2ray-core/v5/transport/internet"
     7  )
     8  
     9  type TransportConfig struct {
    10  	TCPConfig  *TCPConfig          `json:"tcpSettings"`
    11  	KCPConfig  *KCPConfig          `json:"kcpSettings"`
    12  	WSConfig   *WebSocketConfig    `json:"wsSettings"`
    13  	HTTPConfig *HTTPConfig         `json:"httpSettings"`
    14  	DSConfig   *DomainSocketConfig `json:"dsSettings"`
    15  	QUICConfig *QUICConfig         `json:"quicSettings"`
    16  	GunConfig  *GunConfig          `json:"gunSettings"`
    17  	GRPCConfig *GunConfig          `json:"grpcSettings"`
    18  }
    19  
    20  // Build implements Buildable.
    21  func (c *TransportConfig) Build() (*transport.Config, error) {
    22  	config := new(transport.Config)
    23  
    24  	if c.TCPConfig != nil {
    25  		ts, err := c.TCPConfig.Build()
    26  		if err != nil {
    27  			return nil, newError("failed to build TCP config").Base(err).AtError()
    28  		}
    29  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    30  			ProtocolName: "tcp",
    31  			Settings:     serial.ToTypedMessage(ts),
    32  		})
    33  	}
    34  
    35  	if c.KCPConfig != nil {
    36  		ts, err := c.KCPConfig.Build()
    37  		if err != nil {
    38  			return nil, newError("failed to build mKCP config").Base(err).AtError()
    39  		}
    40  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    41  			ProtocolName: "mkcp",
    42  			Settings:     serial.ToTypedMessage(ts),
    43  		})
    44  	}
    45  
    46  	if c.WSConfig != nil {
    47  		ts, err := c.WSConfig.Build()
    48  		if err != nil {
    49  			return nil, newError("failed to build WebSocket config").Base(err)
    50  		}
    51  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    52  			ProtocolName: "websocket",
    53  			Settings:     serial.ToTypedMessage(ts),
    54  		})
    55  	}
    56  
    57  	if c.HTTPConfig != nil {
    58  		ts, err := c.HTTPConfig.Build()
    59  		if err != nil {
    60  			return nil, newError("Failed to build HTTP config.").Base(err)
    61  		}
    62  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    63  			ProtocolName: "http",
    64  			Settings:     serial.ToTypedMessage(ts),
    65  		})
    66  	}
    67  
    68  	if c.DSConfig != nil {
    69  		ds, err := c.DSConfig.Build()
    70  		if err != nil {
    71  			return nil, newError("Failed to build DomainSocket config.").Base(err)
    72  		}
    73  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    74  			ProtocolName: "domainsocket",
    75  			Settings:     serial.ToTypedMessage(ds),
    76  		})
    77  	}
    78  
    79  	if c.QUICConfig != nil {
    80  		qs, err := c.QUICConfig.Build()
    81  		if err != nil {
    82  			return nil, newError("Failed to build QUIC config.").Base(err)
    83  		}
    84  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    85  			ProtocolName: "quic",
    86  			Settings:     serial.ToTypedMessage(qs),
    87  		})
    88  	}
    89  
    90  	if c.GunConfig == nil {
    91  		c.GunConfig = c.GRPCConfig
    92  	}
    93  	if c.GunConfig != nil {
    94  		gs, err := c.GunConfig.Build()
    95  		if err != nil {
    96  			return nil, newError("Failed to build Gun config.").Base(err)
    97  		}
    98  		config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
    99  			ProtocolName: "gun",
   100  			Settings:     serial.ToTypedMessage(gs),
   101  		})
   102  	}
   103  
   104  	return config, nil
   105  }