2021-06-19 08:45:43 -04:00
|
|
|
package v4
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
|
|
|
v2net "github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/protocol"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/proxy/freedom"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type FreedomConfig struct {
|
|
|
|
DomainStrategy string `json:"domainStrategy"`
|
|
|
|
Timeout *uint32 `json:"timeout"`
|
|
|
|
Redirect string `json:"redirect"`
|
|
|
|
UserLevel uint32 `json:"userLevel"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements Buildable
|
|
|
|
func (c *FreedomConfig) Build() (proto.Message, error) {
|
|
|
|
config := new(freedom.Config)
|
|
|
|
config.DomainStrategy = freedom.Config_AS_IS
|
|
|
|
switch strings.ToLower(c.DomainStrategy) {
|
2021-03-19 03:55:18 -04:00
|
|
|
case "useip", "use_ip", "use-ip":
|
2019-02-10 13:04:11 -05:00
|
|
|
config.DomainStrategy = freedom.Config_USE_IP
|
2021-03-19 03:55:18 -04:00
|
|
|
case "useip4", "useipv4", "use_ip4", "use_ipv4", "use_ip_v4", "use-ip4", "use-ipv4", "use-ip-v4":
|
2019-02-10 13:04:11 -05:00
|
|
|
config.DomainStrategy = freedom.Config_USE_IP4
|
2021-03-19 03:55:18 -04:00
|
|
|
case "useip6", "useipv6", "use_ip6", "use_ipv6", "use_ip_v6", "use-ip6", "use-ipv6", "use-ip-v6":
|
2019-02-10 13:04:11 -05:00
|
|
|
config.DomainStrategy = freedom.Config_USE_IP6
|
|
|
|
}
|
2019-02-16 15:25:19 -05:00
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
if c.Timeout != nil {
|
|
|
|
config.Timeout = *c.Timeout
|
|
|
|
}
|
|
|
|
config.UserLevel = c.UserLevel
|
|
|
|
if len(c.Redirect) > 0 {
|
|
|
|
host, portStr, err := net.SplitHostPort(c.Redirect)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("invalid redirect address: ", c.Redirect, ": ", err).Base(err)
|
|
|
|
}
|
|
|
|
port, err := v2net.PortFromString(portStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("invalid redirect port: ", c.Redirect, ": ", err).Base(err)
|
|
|
|
}
|
|
|
|
config.DestinationOverride = &freedom.DestinationOverride{
|
|
|
|
Server: &protocol.ServerEndpoint{
|
|
|
|
Port: uint32(port),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(host) > 0 {
|
|
|
|
config.DestinationOverride.Server.Address = v2net.NewIPOrDomain(v2net.ParseAddress(host))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|