freedom.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package freedom
  2. import (
  3. "io"
  4. "net"
  5. "sync"
  6. "github.com/v2ray/v2ray-core/app"
  7. "github.com/v2ray/v2ray-core/app/dns"
  8. "github.com/v2ray/v2ray-core/common/alloc"
  9. "github.com/v2ray/v2ray-core/common/dice"
  10. v2io "github.com/v2ray/v2ray-core/common/io"
  11. "github.com/v2ray/v2ray-core/common/log"
  12. v2net "github.com/v2ray/v2ray-core/common/net"
  13. "github.com/v2ray/v2ray-core/common/retry"
  14. "github.com/v2ray/v2ray-core/proxy"
  15. "github.com/v2ray/v2ray-core/proxy/internal"
  16. "github.com/v2ray/v2ray-core/transport/hub"
  17. "github.com/v2ray/v2ray-core/transport/ray"
  18. )
  19. type FreedomConnection struct {
  20. domainStrategy DomainStrategy
  21. timeout uint32
  22. dns dns.Server
  23. }
  24. func NewFreedomConnection(config *Config, space app.Space) *FreedomConnection {
  25. f := &FreedomConnection{
  26. domainStrategy: config.DomainStrategy,
  27. timeout: config.Timeout,
  28. }
  29. log.Info("Freedom: Domain strategy: ", f.domainStrategy)
  30. space.InitializeApplication(func() error {
  31. if config.DomainStrategy == DomainStrategyUseIP {
  32. if !space.HasApp(dns.APP_ID) {
  33. log.Error("Freedom: DNS server is not found in the space.")
  34. return app.ErrorMissingApplication
  35. }
  36. f.dns = space.GetApp(dns.APP_ID).(dns.Server)
  37. }
  38. return nil
  39. })
  40. return f
  41. }
  42. // @Private
  43. func (this *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Destination {
  44. if !destination.Address().IsDomain() {
  45. return destination
  46. }
  47. ips := this.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.IsTCP() {
  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 (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
  63. log.Info("Freedom: Opening connection to ", destination)
  64. defer payload.Release()
  65. defer ray.OutboundInput().Release()
  66. defer ray.OutboundOutput().Close()
  67. var conn net.Conn
  68. if this.domainStrategy == DomainStrategyUseIP && destination.Address().IsDomain() {
  69. destination = this.ResolveIP(destination)
  70. }
  71. err := retry.Timed(5, 100).On(func() error {
  72. rawConn, err := hub.DialWithoutCache(destination)
  73. if err != nil {
  74. return err
  75. }
  76. conn = rawConn
  77. return nil
  78. })
  79. if err != nil {
  80. log.Error("Freedom: Failed to open connection to ", destination, ": ", err)
  81. return err
  82. }
  83. defer conn.Close()
  84. input := ray.OutboundInput()
  85. output := ray.OutboundOutput()
  86. var readMutex, writeMutex sync.Mutex
  87. readMutex.Lock()
  88. writeMutex.Lock()
  89. conn.Write(payload.Value)
  90. go func() {
  91. v2writer := v2io.NewAdaptiveWriter(conn)
  92. defer v2writer.Release()
  93. v2io.Pipe(input, v2writer)
  94. writeMutex.Unlock()
  95. }()
  96. go func() {
  97. defer readMutex.Unlock()
  98. var reader io.Reader = conn
  99. timeout := this.timeout
  100. if destination.IsUDP() {
  101. timeout = 16
  102. }
  103. if timeout > 0 {
  104. reader = v2net.NewTimeOutReader(int(timeout) /* seconds */, conn)
  105. }
  106. v2reader := v2io.NewAdaptiveReader(reader)
  107. defer v2reader.Release()
  108. v2io.Pipe(v2reader, output)
  109. ray.OutboundOutput().Close()
  110. }()
  111. writeMutex.Lock()
  112. if tcpConn, ok := conn.(*net.TCPConn); ok {
  113. tcpConn.CloseWrite()
  114. }
  115. readMutex.Lock()
  116. return nil
  117. }
  118. func init() {
  119. internal.MustRegisterOutboundHandlerCreator("freedom",
  120. func(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
  121. return NewFreedomConnection(config.(*Config), space), nil
  122. })
  123. }