Files
orchard/internal/proxy/proxy.go
T
Nikolay Edigaryev 10f56bb5e3 Introduce "orchard ssh" and "orchard vnc" commands (#36)
* proxy.Connections(): handle "use of closed network connection" error

* Controller: less strict timeouts that work nicely for WebSockets

* Worker: only attempt connect to the gRPC once our UID is known

* Introduce "orchard ssh" and "orchard vnc" commands

* Worker: prevent context leak by moving logic into a separate function

* Fix linter errors

* Port forwarding integration test

* Check for "uname -mo" output
2023-03-21 14:58:24 -04:00

48 lines
857 B
Go

package proxy
import (
"io"
"net"
"strings"
)
func Connections(left net.Conn, right net.Conn) (finalErr error) {
leftErrCh := make(chan error, 1)
rightErrCh := make(chan error, 1)
recordErr := func(newErr error) {
if newErr != nil && finalErr == nil {
finalErr = newErr
}
}
go func() {
_, err := io.Copy(left, right)
rightErrCh <- err
}()
go func() {
_, err := io.Copy(right, left)
leftErrCh <- err
}()
// Wait for some goroutine and then unlock the other goroutine
// by closing its source io.Reader
select {
case err := <-rightErrCh:
recordErr(err)
recordErr(left.Close())
recordErr(<-leftErrCh)
case err := <-leftErrCh:
recordErr(err)
recordErr(right.Close())
recordErr(<-rightErrCh)
}
if strings.Contains(finalErr.Error(), "use of closed network connection") {
finalErr = nil
}
return finalErr
}