You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Xray-core/infra/conf/transport.go

131 lines
3.9 KiB

4 years ago
package conf
import (
"github.com/xtls/xray-core/common/errors"
4 years ago
"github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/transport/global"
4 years ago
"github.com/xtls/xray-core/transport/internet"
4 years ago
)
type TransportConfig struct {
TCPConfig *TCPConfig `json:"tcpSettings"`
KCPConfig *KCPConfig `json:"kcpSettings"`
WSConfig *WebSocketConfig `json:"wsSettings"`
HTTPConfig *HTTPConfig `json:"httpSettings"`
DSConfig *DomainSocketConfig `json:"dsSettings"`
QUICConfig *QUICConfig `json:"quicSettings"`
GRPCConfig *GRPCConfig `json:"grpcSettings"`
GUNConfig *GRPCConfig `json:"gunSettings"`
HTTPUPGRADEConfig *HttpUpgradeConfig `json:"httpupgradeSettings"`
SplitHTTPConfig *SplitHTTPConfig `json:"splithttpSettings"`
4 years ago
}
// Build implements Buildable.
func (c *TransportConfig) Build() (*global.Config, error) {
config := new(global.Config)
4 years ago
if c.TCPConfig != nil {
ts, err := c.TCPConfig.Build()
if err != nil {
return nil, errors.New("failed to build TCP config").Base(err).AtError()
4 years ago
}
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()
4 years ago
}
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)
4 years ago
}
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)
4 years ago
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "http",
Settings: serial.ToTypedMessage(ts),
})
}
if c.DSConfig != nil {
ds, err := c.DSConfig.Build()
if err != nil {
return nil, errors.New("Failed to build DomainSocket config.").Base(err)
4 years ago
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "domainsocket",
Settings: serial.ToTypedMessage(ds),
})
}
if c.QUICConfig != nil {
qs, err := c.QUICConfig.Build()
if err != nil {
return nil, errors.New("Failed to build QUIC config.").Base(err)
4 years ago
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "quic",
Settings: serial.ToTypedMessage(qs),
})
}
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),
})
}
if c.HTTPUPGRADEConfig != nil {
hs, err := c.HTTPUPGRADEConfig.Build()
if err != nil {
return nil, errors.New("failed to build HttpUpgrade config").Base(err)
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "httpupgrade",
Settings: serial.ToTypedMessage(hs),
})
}
if c.SplitHTTPConfig != nil {
shs, err := c.SplitHTTPConfig.Build()
if err != nil {
return nil, errors.New("failed to build SplitHTTP config").Base(err)
}
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
ProtocolName: "splithttp",
Settings: serial.ToTypedMessage(shs),
})
}
4 years ago
return config, nil
}