handleConnection copies data between the local connection and the stream to the remote server.
(conn net.Conn, port ForwardedPort)
| 322 | // handleConnection copies data between the local connection and the stream to |
| 323 | // the remote server. |
| 324 | func (pf *PortForwarder) handleConnection(conn net.Conn, port ForwardedPort) { |
| 325 | defer conn.Close() |
| 326 | |
| 327 | if pf.out != nil { |
| 328 | fmt.Fprintf(pf.out, "Handling connection for %d\n", port.Local) |
| 329 | } |
| 330 | |
| 331 | requestID := pf.nextRequestID() |
| 332 | |
| 333 | // create error stream |
| 334 | headers := http.Header{} |
| 335 | headers.Set(v1.StreamType, v1.StreamTypeError) |
| 336 | headers.Set(v1.PortHeader, fmt.Sprintf("%d", port.Remote)) |
| 337 | headers.Set(v1.PortForwardRequestIDHeader, strconv.Itoa(requestID)) |
| 338 | errorStream, err := pf.streamConn.CreateStream(headers) |
| 339 | if err != nil { |
| 340 | runtime.HandleError(fmt.Errorf("error creating error stream for port %d -> %d: %v", port.Local, port.Remote, err)) |
| 341 | return |
| 342 | } |
| 343 | // we're not writing to this stream |
| 344 | errorStream.Close() |
| 345 | |
| 346 | errorChan := make(chan error) |
| 347 | go func() { |
| 348 | message, err := ioutil.ReadAll(errorStream) |
| 349 | switch { |
| 350 | case err != nil: |
| 351 | errorChan <- fmt.Errorf("error reading from error stream for port %d -> %d: %v", port.Local, port.Remote, err) |
| 352 | case len(message) > 0: |
| 353 | errorChan <- fmt.Errorf("an error occurred forwarding %d -> %d: %v", port.Local, port.Remote, string(message)) |
| 354 | } |
| 355 | close(errorChan) |
| 356 | }() |
| 357 | |
| 358 | // create data stream |
| 359 | headers.Set(v1.StreamType, v1.StreamTypeData) |
| 360 | dataStream, err := pf.streamConn.CreateStream(headers) |
| 361 | if err != nil { |
| 362 | runtime.HandleError(fmt.Errorf("error creating forwarding stream for port %d -> %d: %v", port.Local, port.Remote, err)) |
| 363 | return |
| 364 | } |
| 365 | |
| 366 | localError := make(chan struct{}) |
| 367 | remoteDone := make(chan struct{}) |
| 368 | |
| 369 | go func() { |
| 370 | // Copy from the remote side to the local port. |
| 371 | if _, err := io.Copy(conn, dataStream); err != nil && !strings.Contains(err.Error(), "use of closed network connection") { |
| 372 | runtime.HandleError(fmt.Errorf("error copying from remote stream to local connection: %v", err)) |
| 373 | } |
| 374 | |
| 375 | // inform the select below that the remote copy is done |
| 376 | close(remoteDone) |
| 377 | }() |
| 378 | |
| 379 | go func() { |
| 380 | // inform server we're not sending any more data after copy unblocks |
| 381 | defer dataStream.Close() |
no test coverage detected