freedom.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package freedom
  2. import (
  3. "io"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dns"
  6. "v2ray.com/core/common/buf"
  7. "v2ray.com/core/common/dice"
  8. "v2ray.com/core/common/errors"
  9. "v2ray.com/core/common/log"
  10. v2net "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/retry"
  12. "v2ray.com/core/common/serial"
  13. "v2ray.com/core/proxy"
  14. "v2ray.com/core/transport/internet"
  15. "v2ray.com/core/transport/internet/tcp"
  16. "v2ray.com/core/transport/ray"
  17. )
  18. type FreedomConnection struct {
  19. domainStrategy Config_DomainStrategy
  20. timeout uint32
  21. dns dns.Server
  22. meta *proxy.OutboundHandlerMeta
  23. }
  24. func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *FreedomConnection {
  25. f := &FreedomConnection{
  26. domainStrategy: config.DomainStrategy,
  27. timeout: config.Timeout,
  28. meta: meta,
  29. }
  30. space.InitializeApplication(func() error {
  31. if config.DomainStrategy == Config_USE_IP {
  32. if !space.HasApp(dns.APP_ID) {
  33. return errors.New("Freedom: DNS server is not found in the space.")
  34. }
  35. f.dns = space.GetApp(dns.APP_ID).(dns.Server)
  36. }
  37. return nil
  38. })
  39. return f
  40. }
  41. // Private: Visible for testing.
  42. func (v *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Destination {
  43. if !destination.Address.Family().IsDomain() {
  44. return destination
  45. }
  46. ips := v.dns.Get(destination.Address.Domain())
  47. if len(ips) == 0 {
  48. log.Info("Freedom: DNS returns nil answer. Keep domain as is.")
  49. return destination
  50. }
  51. ip := ips[dice.Roll(len(ips))]
  52. var newDest v2net.Destination
  53. if destination.Network == v2net.Network_TCP {
  54. newDest = v2net.TCPDestination(v2net.IPAddress(ip), destination.Port)
  55. } else {
  56. newDest = v2net.UDPDestination(v2net.IPAddress(ip), destination.Port)
  57. }
  58. log.Info("Freedom: Changing destination from ", destination, " to ", newDest)
  59. return newDest
  60. }
  61. func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
  62. log.Info("Freedom: Opening connection to ", destination)
  63. defer payload.Release()
  64. defer ray.OutboundInput().Release()
  65. defer ray.OutboundOutput().Close()
  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. input := ray.OutboundInput()
  84. output := ray.OutboundOutput()
  85. if !payload.IsEmpty() {
  86. conn.Write(payload.Bytes())
  87. }
  88. go func() {
  89. v2writer := buf.NewWriter(conn)
  90. defer v2writer.Release()
  91. if err := buf.PipeUntilEOF(input, v2writer); err != nil {
  92. log.Info("Freedom: Failed to transport all TCP request: ", err)
  93. }
  94. if tcpConn, ok := conn.(*tcp.RawConnection); ok {
  95. tcpConn.CloseWrite()
  96. }
  97. }()
  98. var reader io.Reader = conn
  99. timeout := v.timeout
  100. if destination.Network == v2net.Network_UDP {
  101. timeout = 16
  102. }
  103. if timeout > 0 {
  104. reader = v2net.NewTimeOutReader(timeout /* seconds */, conn)
  105. }
  106. v2reader := buf.NewReader(reader)
  107. if err := buf.PipeUntilEOF(v2reader, output); err != nil {
  108. log.Info("Freedom: Failed to transport all TCP response: ", err)
  109. }
  110. v2reader.Release()
  111. ray.OutboundOutput().Close()
  112. }
  113. type FreedomFactory struct{}
  114. func (v *FreedomFactory) StreamCapability() v2net.NetworkList {
  115. return v2net.NetworkList{
  116. Network: []v2net.Network{v2net.Network_RawTCP},
  117. }
  118. }
  119. func (v *FreedomFactory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  120. return NewFreedomConnection(config.(*Config), space, meta), nil
  121. }
  122. func init() {
  123. proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(FreedomFactory))
  124. }