Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package v5cfg
 
import (
    "context"
 
    "github.com/golang/protobuf/proto"
 
    "github.com/v2fly/v2ray-core/v5/common/serial"
    "github.com/v2fly/v2ray-core/v5/transport/internet"
)
 
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"
    }
 
    if s.TransportSettings == nil {
        s.TransportSettings = []byte("{}")
    }
    transportConfigPack, err := loadHeterogeneousConfigFromRawJSON("transport", s.Transport, s.TransportSettings)
    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),
    })
 
    if s.Security != "none" {
        if s.SecuritySettings == nil {
            s.SecuritySettings = []byte("{}")
        }
        securityConfigPack, err := loadHeterogeneousConfigFromRawJSON("security", s.Security, s.SecuritySettings)
        if err != nil {
            return nil, newError("unable to load security config").Base(err)
        }
        securityConfigPackTypedMessage := serial.ToTypedMessage(securityConfigPack)
        config.SecurityType = serial.V2Type(securityConfigPackTypedMessage)
        config.SecuritySettings = append(config.SecuritySettings, securityConfigPackTypedMessage)
    }
 
    config.SocketSettings, err = s.SocketSettings.Build()
    if err != nil {
        return nil, newError("unable to build socket config").Base(err)
    }
 
    return config, nil
}