github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/v5cfg/outbound.go (about)

     1  package v5cfg
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/golang/protobuf/proto"
     7  
     8  	core "github.com/v2fly/v2ray-core/v5"
     9  	"github.com/v2fly/v2ray-core/v5/app/proxyman"
    10  	"github.com/v2fly/v2ray-core/v5/common/serial"
    11  	"github.com/v2fly/v2ray-core/v5/transport/internet"
    12  )
    13  
    14  func (c OutboundConfig) BuildV5(ctx context.Context) (proto.Message, error) {
    15  	senderSettings := &proxyman.SenderConfig{}
    16  
    17  	if c.SendThrough != nil {
    18  		address := c.SendThrough
    19  		if address.Family().IsDomain() {
    20  			return nil, newError("unable to send through: " + address.String())
    21  		}
    22  		senderSettings.Via = address.Build()
    23  	}
    24  
    25  	if c.StreamSetting != nil {
    26  		ss, err := c.StreamSetting.BuildV5(ctx)
    27  		if err != nil {
    28  			return nil, err
    29  		}
    30  		senderSettings.StreamSettings = ss.(*internet.StreamConfig)
    31  	}
    32  
    33  	if c.ProxySettings != nil {
    34  		ps, err := c.ProxySettings.Build()
    35  		if err != nil {
    36  			return nil, newError("invalid outbound detour proxy settings.").Base(err)
    37  		}
    38  		senderSettings.ProxySettings = ps
    39  	}
    40  
    41  	if c.MuxSettings != nil {
    42  		senderSettings.MultiplexSettings = c.MuxSettings.Build()
    43  	}
    44  
    45  	senderSettings.DomainStrategy = proxyman.SenderConfig_AS_IS
    46  	switch c.DomainStrategy {
    47  	case "UseIP":
    48  		senderSettings.DomainStrategy = proxyman.SenderConfig_USE_IP
    49  	case "UseIP4":
    50  		senderSettings.DomainStrategy = proxyman.SenderConfig_USE_IP4
    51  	case "UseIP6":
    52  		senderSettings.DomainStrategy = proxyman.SenderConfig_USE_IP6
    53  	case "AsIs", "":
    54  	default:
    55  		return nil, newError("unknown domain strategy: ", c.DomainStrategy)
    56  	}
    57  
    58  	if c.Settings == nil {
    59  		c.Settings = []byte("{}")
    60  	}
    61  
    62  	outboundConfigPack, err := loadHeterogeneousConfigFromRawJSON("outbound", c.Protocol, c.Settings)
    63  	if err != nil {
    64  		return nil, newError("unable to load outbound protocol config").Base(err)
    65  	}
    66  
    67  	return &core.OutboundHandlerConfig{
    68  		SenderSettings: serial.ToTypedMessage(senderSettings),
    69  		Tag:            c.Tag,
    70  		ProxySettings:  serial.ToTypedMessage(outboundConfigPack),
    71  	}, nil
    72  }