freedom.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.InitializeApplication(func() error {
  32. if config.DomainStrategy == Config_USE_IP {
  33. if !space.HasApp(dns.APP_ID) {
  34. return errors.New("Freedom: DNS server is not found in the space.")
  35. }
  36. f.dns = space.GetApp(dns.APP_ID).(dns.Server)
  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, payload *buf.Buffer, ray ray.OutboundRay) {
  63. log.Info("Freedom: Opening connection to ", destination)
  64. defer payload.Release()
  65. input := ray.OutboundInput()
  66. output := ray.OutboundOutput()
  67. defer input.ForceClose()
  68. defer output.Close()
  69. var conn internet.Connection
  70. if v.domainStrategy == Config_USE_IP && destination.Address.Family().IsDomain() {
  71. destination = v.ResolveIP(destination)
  72. }
  73. err := retry.ExponentialBackoff(5, 100).On(func() error {
  74. rawConn, err := internet.Dial(v.meta.Address, destination, v.meta.GetDialerOptions())
  75. if err != nil {
  76. return err
  77. }
  78. conn = rawConn
  79. return nil
  80. })
  81. if err != nil {
  82. log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
  83. return
  84. }
  85. defer conn.Close()
  86. conn.SetReusable(false)
  87. if !payload.IsEmpty() {
  88. if _, err := conn.Write(payload.Bytes()); err != nil {
  89. log.Warning("Freedom: Failed to write to destination: ", destination, ": ", err)
  90. return
  91. }
  92. }
  93. requestDone := signal.ExecuteAsync(func() error {
  94. defer input.ForceClose()
  95. v2writer := buf.NewWriter(conn)
  96. defer v2writer.Release()
  97. if err := buf.PipeUntilEOF(input, v2writer); err != nil {
  98. return err
  99. }
  100. return nil
  101. })
  102. var reader io.Reader = conn
  103. timeout := v.timeout
  104. if destination.Network == v2net.Network_UDP {
  105. timeout = 16
  106. }
  107. if timeout > 0 {
  108. reader = v2net.NewTimeOutReader(timeout /* seconds */, conn)
  109. }
  110. responseDone := signal.ExecuteAsync(func() error {
  111. defer output.Close()
  112. v2reader := buf.NewReader(reader)
  113. defer v2reader.Release()
  114. if err := buf.PipeUntilEOF(v2reader, output); err != nil {
  115. return err
  116. }
  117. return nil
  118. })
  119. if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
  120. log.Info("Freedom: Connection ending with ", err)
  121. }
  122. }
  123. type Factory struct{}
  124. func (v *Factory) StreamCapability() v2net.NetworkList {
  125. return v2net.NetworkList{
  126. Network: []v2net.Network{v2net.Network_TCP},
  127. }
  128. }
  129. func (v *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  130. return New(config.(*Config), space, meta), nil
  131. }
  132. func init() {
  133. common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
  134. }