1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-16 04:35:24 +00:00
v2fly/tools/conf/freedom.go

51 lines
1.3 KiB
Go
Raw Normal View History

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