Cancel VPN gateway loops together
This commit is contained in:
@@ -266,15 +266,18 @@ func (g *Gateway) run(ctx context.Context, tun readWriteCloser) error {
|
||||
if closer, ok := g.Transport.(packetTransportCloser); ok {
|
||||
defer closer.Close()
|
||||
}
|
||||
runCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
errCh := make(chan error, 2)
|
||||
go func() { errCh <- g.copyGatewayToClient(ctx, tun) }()
|
||||
go func() { errCh <- g.copyClientToGateway(ctx, tun) }()
|
||||
go func() { errCh <- g.copyGatewayToClient(runCtx, tun) }()
|
||||
go func() { errCh <- g.copyClientToGateway(runCtx, tun) }()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-runCtx.Done():
|
||||
return runCtx.Err()
|
||||
case err := <-errCh:
|
||||
cancel()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user