freedom.go 3.6 KB

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