2016-10-17 12:35:13 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
2017-01-26 22:05:24 +00:00
|
|
|
"net"
|
2016-10-17 12:35:13 +00:00
|
|
|
"strings"
|
|
|
|
|
2017-01-26 22:05:24 +00:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/protocol"
|
2016-12-15 10:51:09 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-10-17 12:35:13 +00:00
|
|
|
"v2ray.com/core/proxy/freedom"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FreedomConfig struct {
|
2017-01-27 08:01:39 +00:00
|
|
|
DomainStrategy string `json:"domainStrategy"`
|
|
|
|
Timeout *uint32 `json:"timeout"`
|
|
|
|
Redirect string `json:"redirect"`
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
func (v *FreedomConfig) Build() (*serial.TypedMessage, error) {
|
2016-10-17 12:35:13 +00:00
|
|
|
config := new(freedom.Config)
|
|
|
|
config.DomainStrategy = freedom.Config_AS_IS
|
2016-11-27 20:39:09 +00:00
|
|
|
domainStrategy := strings.ToLower(v.DomainStrategy)
|
2016-10-17 12:35:13 +00:00
|
|
|
if domainStrategy == "useip" || domainStrategy == "use_ip" {
|
|
|
|
config.DomainStrategy = freedom.Config_USE_IP
|
|
|
|
}
|
2017-01-27 08:01:39 +00:00
|
|
|
config.Timeout = 600
|
|
|
|
if v.Timeout != nil {
|
|
|
|
config.Timeout = *v.Timeout
|
|
|
|
}
|
2017-01-26 22:05:24 +00:00
|
|
|
if len(v.Redirect) > 0 {
|
|
|
|
host, portStr, err := net.SplitHostPort(v.Redirect)
|
|
|
|
if err != nil {
|
2017-04-09 13:04:04 +00:00
|
|
|
return nil, newError("invalid redirect address: ", v.Redirect, ": ", err).Base(err)
|
2017-01-26 22:05:24 +00:00
|
|
|
}
|
|
|
|
port, err := v2net.PortFromString(portStr)
|
|
|
|
if err != nil {
|
2017-04-09 13:04:04 +00:00
|
|
|
return nil, newError("invalid redirect port: ", v.Redirect, ": ", err).Base(err)
|
2017-01-26 22:05:24 +00:00
|
|
|
}
|
2017-01-26 22:09:46 +00:00
|
|
|
if len(host) == 0 {
|
|
|
|
host = "127.0.0.1"
|
|
|
|
}
|
2017-01-26 22:05:24 +00:00
|
|
|
config.DestinationOverride = &freedom.DestinationOverride{
|
|
|
|
Server: &protocol.ServerEndpoint{
|
|
|
|
Address: v2net.NewIPOrDomain(v2net.ParseAddress(host)),
|
|
|
|
Port: uint32(port),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2016-12-15 10:51:09 +00:00
|
|
|
return serial.ToTypedMessage(config), nil
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|