Skip to content

Commit 492706b

Browse files
ndeloofthaJeztah
authored andcommitted
Re-apply "connhelper: add ssh multiplexing"
This reverts commit 2a08462. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 278ac25 commit 492706b

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

cli/connhelper/connhelper.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import (
99
"fmt"
1010
"net"
1111
"net/url"
12+
"os"
1213
"slices"
14+
"strconv"
1315
"strings"
1416

17+
"github.com/docker/cli/cli/config"
1518
"github.com/docker/cli/cli/connhelper/commandconn"
1619
"github.com/docker/cli/cli/connhelper/ssh"
1720
)
@@ -50,6 +53,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*ConnectionHelper
5053
return nil, fmt.Errorf("ssh host connection is not valid: %w", err)
5154
}
5255
sshFlags = addSSHTimeout(sshFlags)
56+
sshFlags = addMultiplexingArgs(sshFlags)
5357
sshFlags = disablePseudoTerminalAllocation(sshFlags)
5458

5559
remoteCommand := []string{"docker", "system", "dial-stdio"}
@@ -83,6 +87,22 @@ func GetCommandConnectionHelper(cmd string, flags ...string) (*ConnectionHelper,
8387
}, nil
8488
}
8589

90+
func addMultiplexingArgs(sshFlags []string) []string {
91+
if v := os.Getenv("DOCKER_SSH_NO_MUX"); v != "" {
92+
if b, err := strconv.ParseBool(v); err == nil && b {
93+
return sshFlags
94+
}
95+
}
96+
if err := os.MkdirAll(config.Dir(), 0o700); err != nil {
97+
return sshFlags
98+
}
99+
sshFlags = append(sshFlags, "-o", "ControlMaster=auto", "-o", "ControlPath="+config.Dir()+"/%r@%h:%p")
100+
if v := os.Getenv("DOCKER_SSH_MUX_PERSIST"); v != "" {
101+
sshFlags = append(sshFlags, "-o", "ControlPersist="+v)
102+
}
103+
return sshFlags
104+
}
105+
86106
func addSSHTimeout(sshFlags []string) []string {
87107
if !strings.Contains(strings.Join(sshFlags, ""), "ConnectTimeout") {
88108
sshFlags = append(sshFlags, "-o ConnectTimeout=30")

0 commit comments

Comments
 (0)