freedom.go 2.1 KB

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