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