freedom.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package conf
  2. import (
  3. "net"
  4. "strings"
  5. "v2ray.com/core/common/errors"
  6. v2net "v2ray.com/core/common/net"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/common/serial"
  9. "v2ray.com/core/proxy/freedom"
  10. )
  11. type FreedomConfig struct {
  12. DomainStrategy string `json:"domainStrategy"`
  13. Timeout *uint32 `json:"timeout"`
  14. Redirect string `json:"redirect"`
  15. }
  16. func (v *FreedomConfig) Build() (*serial.TypedMessage, error) {
  17. config := new(freedom.Config)
  18. config.DomainStrategy = freedom.Config_AS_IS
  19. domainStrategy := strings.ToLower(v.DomainStrategy)
  20. if domainStrategy == "useip" || domainStrategy == "use_ip" {
  21. config.DomainStrategy = freedom.Config_USE_IP
  22. }
  23. config.Timeout = 600
  24. if v.Timeout != nil {
  25. config.Timeout = *v.Timeout
  26. }
  27. if len(v.Redirect) > 0 {
  28. host, portStr, err := net.SplitHostPort(v.Redirect)
  29. if err != nil {
  30. return nil, errors.Base(err).Message("Config: Invalid redirect address: ", v.Redirect, ": ", err)
  31. }
  32. port, err := v2net.PortFromString(portStr)
  33. if err != nil {
  34. return nil, errors.Base(err).Message("Config: Invalid redirect port: ", v.Redirect, ": ", err)
  35. }
  36. if len(host) == 0 {
  37. host = "127.0.0.1"
  38. }
  39. config.DestinationOverride = &freedom.DestinationOverride{
  40. Server: &protocol.ServerEndpoint{
  41. Address: v2net.NewIPOrDomain(v2net.ParseAddress(host)),
  42. Port: uint32(port),
  43. },
  44. }
  45. }
  46. return serial.ToTypedMessage(config), nil
  47. }