1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 09:17:55 -04:00
v2fly/tools/conf/freedom.go

51 lines
1.3 KiB
Go
Raw Normal View History

2016-10-17 08:35:13 -04:00
package conf
import (
"net"
2016-10-17 08:35:13 -04:00
"strings"
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
}
if len(v.Redirect) > 0 {
host, portStr, err := net.SplitHostPort(v.Redirect)
if err != nil {
2017-04-08 19:43:25 -04:00
return nil, newError("Config: Invalid redirect address: ", v.Redirect, ": ", err).Base(err)
}
port, err := v2net.PortFromString(portStr)
if err != nil {
2017-04-08 19:43:25 -04:00
return nil, newError("Config: Invalid redirect port: ", v.Redirect, ": ", err).Base(err)
}
2017-01-26 17:09:46 -05:00
if len(host) == 0 {
host = "127.0.0.1"
}
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
}