func Proxy()

in pkg/api/proxy/proxy.go [19:47]


func Proxy(source io.Reader, sink io.Writer, conn *net.UnixConn) error {
	eg := errgroup.Group{}

	// pipe source to the connection
	eg.Go(func() (err error) {
		defer func() {
			err = errors.Join(err, conn.CloseRead()) // ensure the writing loop exits too...
		}()
		_, err = io.Copy(conn, source)
		if err != nil {
			return fmt.Errorf("proxying stdin to %q: %w", conn.RemoteAddr().String(), err)
		}
		return nil
	})

	// pipe the connection to sink
	eg.Go(func() (err error) {
		defer func() {
			err = errors.Join(err, conn.CloseWrite()) // ensure the reading loop exists too...
		}()
		_, err = io.Copy(sink, conn)
		if err != nil {
			return fmt.Errorf("proxying %q to stdout: %w", conn.RemoteAddr().String(), err)
		}
		return nil
	})

	return eg.Wait()
}