freedom.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 = v.Timeout
  24. if len(v.Redirect) > 0 {
  25. host, portStr, err := net.SplitHostPort(v.Redirect)
  26. if err != nil {
  27. return nil, errors.Base(err).Message("Config: Invalid redirect address: ", v.Redirect, ": ", err)
  28. }
  29. port, err := v2net.PortFromString(portStr)
  30. if err != nil {
  31. return nil, errors.Base(err).Message("Config: Invalid redirect port: ", v.Redirect, ": ", err)
  32. }
  33. config.DestinationOverride = &freedom.DestinationOverride{
  34. Server: &protocol.ServerEndpoint{
  35. Address: v2net.NewIPOrDomain(v2net.ParseAddress(host)),
  36. Port: uint32(port),
  37. },
  38. }
  39. }
  40. return serial.ToTypedMessage(config), nil
  41. }