github.com/imannamdari/v2ray-core/v5@v5.0.5/infra/conf/v5cfg/inbound.go (about)

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