Xray-core/infra/conf/transport.go

107 lines
3.1 KiB
Go
Raw Normal View History

2020-11-25 11:01:53 +00:00
package conf
import (
"github.com/xtls/xray-core/common/errors"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common/serial"
2021-03-06 16:29:17 +00:00
"github.com/xtls/xray-core/transport/global"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/transport/internet"
2020-11-25 11:01:53 +00:00
)
type TransportConfig struct {
2024-03-03 05:12:38 +00:00
TCPConfig *TCPConfig `json:"tcpSettings"`
KCPConfig *KCPConfig `json:"kcpSettings"`
WSConfig *WebSocketConfig `json:"wsSettings"`
HTTPConfig *HTTPConfig `json:"httpSettings"`
GRPCConfig *GRPCConfig `json:"grpcSettings"`
GUNConfig *GRPCConfig `json:"gunSettings"`
HTTPUPGRADEConfig *HttpUpgradeConfig `json:"httpupgradeSettings"`
2024-06-18 05:36:36 +00:00
SplitHTTPConfig *SplitHTTPConfig `json:"splithttpSettings"`
2020-11-25 11:01:53 +00:00
}
// Build implements Buildable.
2021-03-06 16:29:17 +00:00
func (c *TransportConfig) Build() (*global.Config, error) {
config := new(global.Config)
2020-11-25 11:01:53 +00:00
if c.TCPConfig != nil {
ts, err := c.TCPConfig.Build()
if err != nil {
return nil, errors.New("failed to build TCP config").Base(err).AtError()
2020-11-25 11:01:53 +00:00
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "tcp",
Settings: serial.ToTypedMessage(ts),
})
}
if c.KCPConfig != nil {
ts, err := c.KCPConfig.Build()
if err != nil {
return nil, errors.New("failed to build mKCP config").Base(err).AtError()
2020-11-25 11:01:53 +00:00
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "mkcp",
Settings: serial.ToTypedMessage(ts),
})
}
if c.WSConfig != nil {
ts, err := c.WSConfig.Build()
if err != nil {
return nil, errors.New("failed to build WebSocket config").Base(err)
2020-11-25 11:01:53 +00:00
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "websocket",
Settings: serial.ToTypedMessage(ts),
})
}
if c.HTTPConfig != nil {
ts, err := c.HTTPConfig.Build()
if err != nil {
return nil, errors.New("Failed to build HTTP config.").Base(err)
2020-11-25 11:01:53 +00:00
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "http",
Settings: serial.ToTypedMessage(ts),
})
}
if c.GRPCConfig == nil {
c.GRPCConfig = c.GUNConfig
}
if c.GRPCConfig != nil {
gs, err := c.GRPCConfig.Build()
if err != nil {
return nil, errors.New("Failed to build gRPC config.").Base(err)
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "grpc",
Settings: serial.ToTypedMessage(gs),
})
}
2024-03-03 05:12:38 +00:00
if c.HTTPUPGRADEConfig != nil {
hs, err := c.HTTPUPGRADEConfig.Build()
if err != nil {
return nil, errors.New("failed to build HttpUpgrade config").Base(err)
2024-03-03 05:12:38 +00:00
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "httpupgrade",
Settings: serial.ToTypedMessage(hs),
})
}
2024-06-18 05:36:36 +00:00
if c.SplitHTTPConfig != nil {
shs, err := c.SplitHTTPConfig.Build()
if err != nil {
return nil, errors.New("failed to build SplitHTTP config").Base(err)
2024-06-18 05:36:36 +00:00
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "splithttp",
Settings: serial.ToTypedMessage(shs),
})
}
2020-11-25 11:01:53 +00:00
return config, nil
}