freedom.go 1.3 KB

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