freedom.go 3.7 KB

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