freedom.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package freedom
  2. import (
  3. "io"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dns"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/buf"
  8. "v2ray.com/core/common/dice"
  9. "v2ray.com/core/common/errors"
  10. "v2ray.com/core/common/log"
  11. v2net "v2ray.com/core/common/net"
  12. "v2ray.com/core/common/retry"
  13. "v2ray.com/core/common/serial"
  14. "v2ray.com/core/common/signal"
  15. "v2ray.com/core/proxy"
  16. "v2ray.com/core/transport/internet"
  17. "v2ray.com/core/transport/ray"
  18. )
  19. type Handler struct {
  20. domainStrategy Config_DomainStrategy
  21. timeout uint32
  22. dns dns.Server
  23. meta *proxy.OutboundHandlerMeta
  24. }
  25. func New(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *Handler {
  26. f := &Handler{
  27. domainStrategy: config.DomainStrategy,
  28. timeout: config.Timeout,
  29. meta: meta,
  30. }
  31. space.OnInitialize(func() error {
  32. if config.DomainStrategy == Config_USE_IP {
  33. f.dns = dns.FromSpace(space)
  34. if f.dns == nil {
  35. return errors.New("Freedom: DNS server is not found in the space.")
  36. }
  37. }
  38. return nil
  39. })
  40. return f
  41. }
  42. // Private: Visible for testing.
  43. func (v *Handler) ResolveIP(destination v2net.Destination) v2net.Destination {
  44. if !destination.Address.Family().IsDomain() {
  45. return destination
  46. }
  47. ips := v.dns.Get(destination.Address.Domain())
  48. if len(ips) == 0 {
  49. log.Info("Freedom: DNS returns nil answer. Keep domain as is.")
  50. return destination
  51. }
  52. ip := ips[dice.Roll(len(ips))]
  53. var newDest v2net.Destination
  54. if destination.Network == v2net.Network_TCP {
  55. newDest = v2net.TCPDestination(v2net.IPAddress(ip), destination.Port)
  56. } else {
  57. newDest = v2net.UDPDestination(v2net.IPAddress(ip), destination.Port)
  58. }
  59. log.Info("Freedom: Changing destination from ", destination, " to ", newDest)
  60. return newDest
  61. }
  62. func (v *Handler) Dispatch(destination v2net.Destination, ray ray.OutboundRay) {
  63. log.Info("Freedom: Opening connection to ", destination)
  64. input := ray.OutboundInput()
  65. output := ray.OutboundOutput()
  66. var conn internet.Connection
  67. if v.domainStrategy == Config_USE_IP && destination.Address.Family().IsDomain() {
  68. destination = v.ResolveIP(destination)
  69. }
  70. err := retry.ExponentialBackoff(5, 100).On(func() error {
  71. rawConn, err := internet.Dial(v.meta.Address, destination, v.meta.GetDialerOptions())
  72. if err != nil {
  73. return err
  74. }
  75. conn = rawConn
  76. return nil
  77. })
  78. if err != nil {
  79. log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
  80. return
  81. }
  82. defer conn.Close()
  83. conn.SetReusable(false)
  84. requestDone := signal.ExecuteAsync(func() error {
  85. v2writer := buf.NewWriter(conn)
  86. if err := buf.PipeUntilEOF(input, v2writer); err != nil {
  87. return err
  88. }
  89. return nil
  90. })
  91. var reader io.Reader = conn
  92. timeout := v.timeout
  93. if destination.Network == v2net.Network_UDP {
  94. timeout = 16
  95. }
  96. if timeout > 0 {
  97. reader = v2net.NewTimeOutReader(timeout /* seconds */, conn)
  98. }
  99. responseDone := signal.ExecuteAsync(func() error {
  100. defer output.Close()
  101. v2reader := buf.NewReader(reader)
  102. if err := buf.PipeUntilEOF(v2reader, output); err != nil {
  103. return err
  104. }
  105. return nil
  106. })
  107. if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
  108. log.Info("Freedom: Connection ending with ", err)
  109. input.CloseError()
  110. output.CloseError()
  111. }
  112. }
  113. type Factory struct{}
  114. func (v *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  115. return New(config.(*Config), space, meta), nil
  116. }
  117. func init() {
  118. common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
  119. }