Skip to content

Commit 9c298e4

Browse files
ipostelniknmldiegues
authored andcommitted
TUN-3855: Add ability to override target of 'access ssh' command to a different host for testing
1 parent 8b79439 commit 9c298e4

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

carrier/carrier.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package carrier
55

66
import (
7+
"crypto/tls"
78
"io"
89
"net"
910
"net/http"
@@ -20,8 +21,10 @@ import (
2021
const LogFieldOriginURL = "originURL"
2122

2223
type StartOptions struct {
23-
OriginURL string
24-
Headers http.Header
24+
OriginURL string
25+
Headers http.Header
26+
Host string
27+
TLSClientConfig *tls.Config
2528
}
2629

2730
// Connection wraps up all the needed functions to forward over the tunnel

carrier/websocket.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,17 @@ func createWebsocketStream(options *StartOptions, log *zerolog.Logger) (*cfwebso
8282
return nil, err
8383
}
8484
req.Header = options.Headers
85+
if options.Host != "" {
86+
req.Host = options.Host
87+
}
8588

8689
dump, err := httputil.DumpRequest(req, false)
8790
log.Debug().Msgf("Websocket request: %s", string(dump))
8891

89-
wsConn, resp, err := cfwebsocket.ClientConnect(req, nil)
92+
dialer := &websocket.Dialer{
93+
TLSClientConfig: options.TLSClientConfig,
94+
}
95+
wsConn, resp, err := cfwebsocket.ClientConnect(req, dialer)
9096
defer closeRespBody(resp)
9197

9298
if err != nil && IsAccessResponse(resp) {

cmd/cloudflared/access/carrier.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package access
22

33
import (
4+
"crypto/tls"
5+
"fmt"
46
"net/http"
57
"strings"
68

@@ -84,6 +86,26 @@ func ssh(c *cli.Context) error {
8486
options := &carrier.StartOptions{
8587
OriginURL: originURL,
8688
Headers: headers,
89+
Host: hostname,
90+
}
91+
92+
if connectTo := c.String(sshConnectTo); connectTo != "" {
93+
parts := strings.Split(connectTo, ":")
94+
switch len(parts) {
95+
case 1:
96+
options.OriginURL = fmt.Sprintf("https://%s", parts[0])
97+
case 2:
98+
options.OriginURL = fmt.Sprintf("https://%s:%s", parts[0], parts[1])
99+
case 3:
100+
options.OriginURL = fmt.Sprintf("https://%s:%s", parts[2], parts[1])
101+
options.TLSClientConfig = &tls.Config{
102+
InsecureSkipVerify: true,
103+
ServerName: parts[0],
104+
}
105+
log.Warn().Msgf("Using insecure SSL connection because SNI overridden to %s", parts[0])
106+
default:
107+
return fmt.Errorf("invalid connection override: %s", connectTo)
108+
}
87109
}
88110

89111
// we could add a cmd line variable for this bool if we want the SOCK5 server to be on the client side

cmd/cloudflared/access/cmd.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
sshTokenIDFlag = "service-token-id"
3434
sshTokenSecretFlag = "service-token-secret"
3535
sshGenCertFlag = "short-lived-cert"
36+
sshConnectTo = "connect-to"
3637
sshConfigTemplate = `
3738
Add to your {{.Home}}/.ssh/config:
3839
@@ -54,7 +55,7 @@ Host cfpipe-{{.Hostname}}
5455
const sentryDSN = "https://[email protected]/189878"
5556

5657
var (
57-
shutdownC chan struct{}
58+
shutdownC chan struct{}
5859
)
5960

6061
// Init will initialize and store vars from the main program
@@ -164,6 +165,11 @@ func Commands() []*cli.Command {
164165
Aliases: []string{"loglevel"}, //added to match the tunnel side
165166
Usage: "Application logging level {fatal, error, info, debug}. ",
166167
},
168+
&cli.StringFlag{
169+
Name: sshConnectTo,
170+
Hidden: true,
171+
Usage: "Connect to alternate location for testing, value is host, host:port, or sni:port:host",
172+
},
167173
},
168174
},
169175
{

0 commit comments

Comments
 (0)