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
|
|
|
"v2ray.com/core/common/errors"
|
|
|
|
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 {
|
|
|
|
DomainStrategy string `json:"domainStrategy"`
|
|
|
|
Timeout uint32 `json:"timeout"`
|
2017-01-26 22:05:24 +00:00
|
|
|
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
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
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 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|