github.com/igoogolx/clash@v1.19.8/common/net/relay.go (about)

     1  package net
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  	"time"
     7  )
     8  
     9  // Relay copies between left and right bidirectionally.
    10  func Relay(leftConn, rightConn net.Conn) {
    11  	ch := make(chan error)
    12  
    13  	go func() {
    14  		// Wrapping to avoid using *net.TCPConn.(ReadFrom)
    15  		// See also https://github.com/igoogolx/clash/pull/1209
    16  		_, err := io.Copy(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn})
    17  		leftConn.SetReadDeadline(time.Now())
    18  		ch <- err
    19  	}()
    20  
    21  	io.Copy(WriteOnlyWriter{Writer: rightConn}, ReadOnlyReader{Reader: leftConn})
    22  	rightConn.SetReadDeadline(time.Now())
    23  	<-ch
    24  }