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

     1  package v4
     2  
     3  import (
     4  	"net"
     5  	"strings"
     6  
     7  	"github.com/golang/protobuf/proto"
     8  
     9  	v2net "github.com/imannamdari/v2ray-core/v5/common/net"
    10  	"github.com/imannamdari/v2ray-core/v5/common/protocol"
    11  	"github.com/imannamdari/v2ray-core/v5/proxy/freedom"
    12  )
    13  
    14  type FreedomConfig struct {
    15  	DomainStrategy string  `json:"domainStrategy"`
    16  	Timeout        *uint32 `json:"timeout"`
    17  	Redirect       string  `json:"redirect"`
    18  	UserLevel      uint32  `json:"userLevel"`
    19  }
    20  
    21  // Build implements Buildable
    22  func (c *FreedomConfig) Build() (proto.Message, error) {
    23  	config := new(freedom.Config)
    24  	config.DomainStrategy = freedom.Config_AS_IS
    25  	switch strings.ToLower(c.DomainStrategy) {
    26  	case "useip", "use_ip", "use-ip":
    27  		config.DomainStrategy = freedom.Config_USE_IP
    28  	case "useip4", "useipv4", "use_ip4", "use_ipv4", "use_ip_v4", "use-ip4", "use-ipv4", "use-ip-v4":
    29  		config.DomainStrategy = freedom.Config_USE_IP4
    30  	case "useip6", "useipv6", "use_ip6", "use_ipv6", "use_ip_v6", "use-ip6", "use-ipv6", "use-ip-v6":
    31  		config.DomainStrategy = freedom.Config_USE_IP6
    32  	}
    33  
    34  	if c.Timeout != nil {
    35  		config.Timeout = *c.Timeout
    36  	}
    37  	config.UserLevel = c.UserLevel
    38  	if len(c.Redirect) > 0 {
    39  		host, portStr, err := net.SplitHostPort(c.Redirect)
    40  		if err != nil {
    41  			return nil, newError("invalid redirect address: ", c.Redirect, ": ", err).Base(err)
    42  		}
    43  		port, err := v2net.PortFromString(portStr)
    44  		if err != nil {
    45  			return nil, newError("invalid redirect port: ", c.Redirect, ": ", err).Base(err)
    46  		}
    47  		config.DestinationOverride = &freedom.DestinationOverride{
    48  			Server: &protocol.ServerEndpoint{
    49  				Port: uint32(port),
    50  			},
    51  		}
    52  
    53  		if len(host) > 0 {
    54  			config.DestinationOverride.Server.Address = v2net.NewIPOrDomain(v2net.ParseAddress(host))
    55  		}
    56  	}
    57  	return config, nil
    58  }