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

     1  package v5cfg
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  
     7  	"github.com/golang/protobuf/proto"
     8  
     9  	core "github.com/v2fly/v2ray-core/v5"
    10  	"github.com/v2fly/v2ray-core/v5/app/proxyman"
    11  	"github.com/v2fly/v2ray-core/v5/common/serial"
    12  	"github.com/v2fly/v2ray-core/v5/proxy/dokodemo"
    13  	"github.com/v2fly/v2ray-core/v5/transport/internet"
    14  )
    15  
    16  func (c InboundConfig) BuildV5(ctx context.Context) (proto.Message, error) {
    17  	receiverSettings := &proxyman.ReceiverConfig{}
    18  
    19  	if c.ListenOn == nil {
    20  		// Listen on anyip, must set PortRange
    21  		if c.PortRange == nil {
    22  			return nil, newError("Listen on AnyIP but no Port(s) set in InboundDetour.")
    23  		}
    24  		receiverSettings.PortRange = c.PortRange.Build()
    25  	} else {
    26  		// Listen on specific IP or Unix Domain Socket
    27  		receiverSettings.Listen = c.ListenOn.Build()
    28  		listenDS := c.ListenOn.Family().IsDomain() && (filepath.IsAbs(c.ListenOn.Domain()) || c.ListenOn.Domain()[0] == '@')
    29  		listenIP := c.ListenOn.Family().IsIP() || (c.ListenOn.Family().IsDomain() && c.ListenOn.Domain() == "localhost")
    30  		switch {
    31  		case listenIP:
    32  			// Listen on specific IP, must set PortRange
    33  			if c.PortRange == nil {
    34  				return nil, newError("Listen on specific ip without port in InboundDetour.")
    35  			}
    36  			// Listen on IP:Port
    37  			receiverSettings.PortRange = c.PortRange.Build()
    38  		case listenDS:
    39  			if c.PortRange != nil {
    40  				// Listen on Unix Domain Socket, PortRange should be nil
    41  				receiverSettings.PortRange = nil
    42  			}
    43  		default:
    44  			return nil, newError("unable to listen on domain address: ", c.ListenOn.Domain())
    45  		}
    46  	}
    47  
    48  	if c.StreamSetting != nil {
    49  		ss, err := c.StreamSetting.BuildV5(ctx)
    50  		if err != nil {
    51  			return nil, err
    52  		}
    53  		receiverSettings.StreamSettings = ss.(*internet.StreamConfig)
    54  	}
    55  
    56  	if c.SniffingConfig != nil {
    57  		s, err := c.SniffingConfig.Build()
    58  		if err != nil {
    59  			return nil, newError("failed to build sniffing config").Base(err)
    60  		}
    61  		receiverSettings.SniffingSettings = s
    62  	}
    63  
    64  	if c.Settings == nil {
    65  		c.Settings = []byte("{}")
    66  	}
    67  
    68  	inboundConfigPack, err := loadHeterogeneousConfigFromRawJSON("inbound", c.Protocol, c.Settings)
    69  	if err != nil {
    70  		return nil, newError("unable to load inbound protocol config").Base(err)
    71  	}
    72  
    73  	if content, ok := inboundConfigPack.(*dokodemo.SimplifiedConfig); ok {
    74  		receiverSettings.ReceiveOriginalDestination = content.FollowRedirect
    75  	}
    76  	if content, ok := inboundConfigPack.(*dokodemo.Config); ok {
    77  		receiverSettings.ReceiveOriginalDestination = content.FollowRedirect
    78  	}
    79  
    80  	return &core.InboundHandlerConfig{
    81  		Tag:              c.Tag,
    82  		ReceiverSettings: serial.ToTypedMessage(receiverSettings),
    83  		ProxySettings:    serial.ToTypedMessage(inboundConfigPack),
    84  	}, nil
    85  }