freedom.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package freedom
  2. import (
  3. "net"
  4. "sync"
  5. "github.com/v2ray/v2ray-core"
  6. "github.com/v2ray/v2ray-core/common/log"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. )
  9. type FreedomConnection struct {
  10. packet v2net.Packet
  11. }
  12. func NewFreedomConnection(firstPacket v2net.Packet) *FreedomConnection {
  13. return &FreedomConnection{
  14. packet: firstPacket,
  15. }
  16. }
  17. func (vconn *FreedomConnection) Start(ray core.OutboundRay) error {
  18. conn, err := net.Dial(vconn.packet.Destination().Network(), vconn.packet.Destination().Address().String())
  19. log.Info("Freedom: Opening connection to %s", vconn.packet.Destination().String())
  20. if err != nil {
  21. if ray != nil {
  22. close(ray.OutboundOutput())
  23. }
  24. return log.Error("Freedom: Failed to open connection: %s : %v", vconn.packet.Destination().String(), err)
  25. }
  26. input := ray.OutboundInput()
  27. output := ray.OutboundOutput()
  28. var readMutex, writeMutex sync.Mutex
  29. readMutex.Lock()
  30. writeMutex.Lock()
  31. if chunk := vconn.packet.Chunk(); chunk != nil {
  32. conn.Write(chunk)
  33. }
  34. if !vconn.packet.MoreChunks() {
  35. writeMutex.Unlock()
  36. } else {
  37. go dumpInput(conn, input, &writeMutex)
  38. }
  39. go dumpOutput(conn, output, &readMutex, vconn.packet.Destination().IsUDP())
  40. go func() {
  41. writeMutex.Lock()
  42. if tcpConn, ok := conn.(*net.TCPConn); ok {
  43. tcpConn.CloseWrite()
  44. }
  45. readMutex.Lock()
  46. conn.Close()
  47. }()
  48. return nil
  49. }
  50. func dumpInput(conn net.Conn, input <-chan []byte, finish *sync.Mutex) {
  51. v2net.ChanToWriter(conn, input)
  52. finish.Unlock()
  53. }
  54. func dumpOutput(conn net.Conn, output chan<- []byte, finish *sync.Mutex, udp bool) {
  55. defer finish.Unlock()
  56. defer close(output)
  57. response, err := v2net.ReadFrom(conn)
  58. log.Info("Freedom receives %d bytes from %s", len(response), conn.RemoteAddr().String())
  59. if len(response) > 0 {
  60. output <- response
  61. }
  62. if err != nil {
  63. return
  64. }
  65. if udp {
  66. return
  67. }
  68. v2net.ReaderToChan(output, conn)
  69. }