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