2021-06-19 08:45:43 -04:00
|
|
|
package v4
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
import (
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/serial"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
2021-03-11 03:46:17 -05:00
|
|
|
GunConfig *GunConfig `json:"gunSettings"`
|
2021-03-11 11:01:57 -05:00
|
|
|
GRPCConfig *GunConfig `json:"grpcSettings"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements Buildable.
|
|
|
|
func (c *TransportConfig) Build() (*transport.Config, error) {
|
|
|
|
config := new(transport.Config)
|
|
|
|
|
|
|
|
if c.TCPConfig != nil {
|
|
|
|
ts, err := c.TCPConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to build TCP config").Base(err).AtError()
|
|
|
|
}
|
|
|
|
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, newError("failed to build mKCP config").Base(err).AtError()
|
|
|
|
}
|
|
|
|
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, newError("failed to build WebSocket config").Base(err)
|
|
|
|
}
|
|
|
|
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, newError("Failed to build HTTP config.").Base(err)
|
|
|
|
}
|
|
|
|
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, newError("Failed to build DomainSocket config.").Base(err)
|
|
|
|
}
|
|
|
|
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, newError("Failed to build QUIC config.").Base(err)
|
|
|
|
}
|
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
|
|
|
ProtocolName: "quic",
|
|
|
|
Settings: serial.ToTypedMessage(qs),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-11 11:01:57 -05:00
|
|
|
if c.GunConfig == nil {
|
|
|
|
c.GunConfig = c.GRPCConfig
|
|
|
|
}
|
2021-03-11 03:46:17 -05:00
|
|
|
if c.GunConfig != nil {
|
|
|
|
gs, err := c.GunConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Failed to build Gun config.").Base(err)
|
|
|
|
}
|
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
|
|
|
ProtocolName: "gun",
|
|
|
|
Settings: serial.ToTypedMessage(gs),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
return config, nil
|
|
|
|
}
|