freedom.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package freedom
  2. import (
  3. "net"
  4. "sync"
  5. "github.com/v2ray/v2ray-core/app"
  6. v2io "github.com/v2ray/v2ray-core/common/io"
  7. "github.com/v2ray/v2ray-core/common/log"
  8. v2net "github.com/v2ray/v2ray-core/common/net"
  9. "github.com/v2ray/v2ray-core/common/retry"
  10. "github.com/v2ray/v2ray-core/transport/dialer"
  11. "github.com/v2ray/v2ray-core/transport/ray"
  12. )
  13. type FreedomConnection struct {
  14. space app.Space
  15. }
  16. func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
  17. log.Info("Freedom: Opening connection to ", firstPacket.Destination())
  18. var conn net.Conn
  19. err := retry.Timed(5, 100).On(func() error {
  20. rawConn, err := dialer.Dial(firstPacket.Destination())
  21. if err != nil {
  22. return err
  23. }
  24. conn = rawConn
  25. return nil
  26. })
  27. if err != nil {
  28. close(ray.OutboundOutput())
  29. log.Error("Freedom: Failed to open connection to ", firstPacket.Destination(), ": ", err)
  30. return err
  31. }
  32. defer conn.Close()
  33. input := ray.OutboundInput()
  34. output := ray.OutboundOutput()
  35. var readMutex, writeMutex sync.Mutex
  36. readMutex.Lock()
  37. writeMutex.Lock()
  38. if chunk := firstPacket.Chunk(); chunk != nil {
  39. conn.Write(chunk.Value)
  40. chunk.Release()
  41. }
  42. if !firstPacket.MoreChunks() {
  43. writeMutex.Unlock()
  44. } else {
  45. go func() {
  46. v2io.ChanToWriter(conn, input)
  47. writeMutex.Unlock()
  48. }()
  49. }
  50. go func() {
  51. defer readMutex.Unlock()
  52. defer close(output)
  53. response, err := v2io.ReadFrom(conn, nil)
  54. log.Info("Freedom receives ", response.Len(), " bytes from ", conn.RemoteAddr())
  55. if response.Len() > 0 {
  56. output <- response
  57. } else {
  58. response.Release()
  59. }
  60. if err != nil {
  61. return
  62. }
  63. if firstPacket.Destination().IsUDP() {
  64. return
  65. }
  66. v2io.RawReaderToChan(output, conn)
  67. }()
  68. if this.space.HasDnsCache() {
  69. if firstPacket.Destination().Address().IsDomain() {
  70. domain := firstPacket.Destination().Address().Domain()
  71. addr := conn.RemoteAddr()
  72. switch typedAddr := addr.(type) {
  73. case *net.TCPAddr:
  74. this.space.DnsCache().Add(domain, typedAddr.IP)
  75. case *net.UDPAddr:
  76. this.space.DnsCache().Add(domain, typedAddr.IP)
  77. }
  78. }
  79. }
  80. writeMutex.Lock()
  81. if tcpConn, ok := conn.(*net.TCPConn); ok {
  82. tcpConn.CloseWrite()
  83. }
  84. readMutex.Lock()
  85. return nil
  86. }