2021-09-04 12:19:18 -04:00
|
|
|
package v5cfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-28 06:34:19 -04:00
|
|
|
|
2021-09-04 12:19:18 -04:00
|
|
|
"github.com/golang/protobuf/proto"
|
2021-10-28 06:34:19 -04:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common/serial"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet"
|
2021-09-04 12:19:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s StreamConfig) BuildV5(ctx context.Context) (proto.Message, error) {
|
|
|
|
config := &internet.StreamConfig{}
|
|
|
|
|
|
|
|
if s.Transport == "" {
|
|
|
|
s.Transport = "tcp"
|
|
|
|
}
|
|
|
|
if s.Security == "" {
|
|
|
|
s.Security = "none"
|
|
|
|
}
|
|
|
|
|
2021-09-05 05:02:05 -04:00
|
|
|
if s.TransportSettings == nil {
|
|
|
|
s.TransportSettings = []byte("{}")
|
|
|
|
}
|
2021-11-27 04:16:41 -05:00
|
|
|
transportConfigPack, err := loadHeterogeneousConfigFromRawJSON("transport", s.Transport, s.TransportSettings)
|
2021-09-04 12:19:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("unable to load transport config").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.ProtocolName = s.Transport
|
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
|
|
|
ProtocolName: s.Transport,
|
|
|
|
Settings: serial.ToTypedMessage(transportConfigPack),
|
|
|
|
})
|
|
|
|
|
2021-09-05 06:09:20 -04:00
|
|
|
if s.Security != "none" {
|
2021-09-05 06:08:29 -04:00
|
|
|
if s.SecuritySettings == nil {
|
|
|
|
s.SecuritySettings = []byte("{}")
|
|
|
|
}
|
2021-11-27 04:16:41 -05:00
|
|
|
securityConfigPack, err := loadHeterogeneousConfigFromRawJSON("security", s.Security, s.SecuritySettings)
|
2021-09-05 06:08:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("unable to load security config").Base(err)
|
|
|
|
}
|
2021-09-05 11:29:45 -04:00
|
|
|
securityConfigPackTypedMessage := serial.ToTypedMessage(securityConfigPack)
|
|
|
|
config.SecurityType = serial.V2Type(securityConfigPackTypedMessage)
|
|
|
|
config.SecuritySettings = append(config.SecuritySettings, securityConfigPackTypedMessage)
|
2021-09-05 05:02:05 -04:00
|
|
|
}
|
2021-09-04 12:19:18 -04:00
|
|
|
|
|
|
|
config.SocketSettings, err = s.SocketSettings.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("unable to build socket config").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|