github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/infra/conf/freedom.go (about)

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