freedom.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. space.InitializeApplication(func() error {
  30. if config.DomainStrategy == DomainStrategyUseIP {
  31. if !space.HasApp(dns.APP_ID) {
  32. log.Error("Freedom: DNS server is not found in the space.")
  33. return app.ErrorMissingApplication
  34. }
  35. f.dns = space.GetApp(dns.APP_ID).(dns.Server)
  36. }
  37. return nil
  38. })
  39. return f
  40. }
  41. // @Private
  42. func (this *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Destination {
  43. if !destination.Address().IsDomain() {
  44. return destination
  45. }
  46. ips := this.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.IsTCP() {
  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 (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
  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 net.Conn
  67. if this.domainStrategy == DomainStrategyUseIP && destination.Address().IsDomain() {
  68. destination = this.ResolveIP(destination)
  69. }
  70. err := retry.Timed(5, 100).On(func() error {
  71. rawConn, err := hub.DialWithoutCache(destination)
  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 err
  81. }
  82. defer conn.Close()
  83. input := ray.OutboundInput()
  84. output := ray.OutboundOutput()
  85. var readMutex, writeMutex sync.Mutex
  86. readMutex.Lock()
  87. writeMutex.Lock()
  88. conn.Write(payload.Value)
  89. go func() {
  90. v2writer := v2io.NewAdaptiveWriter(conn)
  91. defer v2writer.Release()
  92. v2io.Pipe(input, v2writer)
  93. writeMutex.Unlock()
  94. }()
  95. go func() {
  96. defer readMutex.Unlock()
  97. var reader io.Reader = conn
  98. timeout := this.timeout
  99. if destination.IsUDP() {
  100. timeout = 16
  101. }
  102. if timeout > 0 {
  103. reader = v2net.NewTimeOutReader(int(timeout) /* seconds */, conn)
  104. }
  105. v2reader := v2io.NewAdaptiveReader(reader)
  106. defer v2reader.Release()
  107. v2io.Pipe(v2reader, output)
  108. ray.OutboundOutput().Close()
  109. }()
  110. writeMutex.Lock()
  111. if tcpConn, ok := conn.(*net.TCPConn); ok {
  112. tcpConn.CloseWrite()
  113. }
  114. readMutex.Lock()
  115. return nil
  116. }
  117. func init() {
  118. internal.MustRegisterOutboundHandlerCreator("freedom",
  119. func(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
  120. return NewFreedomConnection(config.(*Config), space), nil
  121. })
  122. }