2018-10-19 20:44:35 +00:00
|
|
|
package access
|
2018-09-21 15:18:23 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-07 16:56:33 +00:00
|
|
|
"net/http"
|
2018-09-21 15:18:23 +00:00
|
|
|
"net/url"
|
2019-02-07 16:56:33 +00:00
|
|
|
"strings"
|
2018-09-21 15:18:23 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/carrier"
|
2018-10-19 20:44:35 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/config"
|
2020-05-04 20:15:17 +00:00
|
|
|
"github.com/cloudflare/cloudflared/h2mux"
|
2020-04-29 20:51:32 +00:00
|
|
|
"github.com/cloudflare/cloudflared/logger"
|
2018-09-21 15:18:23 +00:00
|
|
|
"github.com/cloudflare/cloudflared/validation"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
cli "gopkg.in/urfave/cli.v2"
|
|
|
|
)
|
|
|
|
|
2020-04-13 17:22:00 +00:00
|
|
|
// StartForwarder starts a client side websocket forward
|
2020-04-29 20:51:32 +00:00
|
|
|
func StartForwarder(forwarder config.Forwarder, shutdown <-chan struct{}, logger logger.Service) error {
|
2020-04-13 17:22:00 +00:00
|
|
|
validURLString, err := validation.ValidateUrl(forwarder.Listener)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error validating origin URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
validURL, err := url.Parse(validURLString)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error parsing origin URL")
|
|
|
|
}
|
|
|
|
|
2020-06-08 22:01:48 +00:00
|
|
|
// get the headers from the config file and add to the request
|
|
|
|
headers := make(http.Header)
|
|
|
|
if forwarder.TokenClientID != "" {
|
|
|
|
headers.Set(h2mux.CFAccessClientIDHeader, forwarder.TokenClientID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if forwarder.TokenSecret != "" {
|
|
|
|
headers.Set(h2mux.CFAccessClientSecretHeader, forwarder.TokenSecret)
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:22:00 +00:00
|
|
|
options := &carrier.StartOptions{
|
|
|
|
OriginURL: forwarder.URL,
|
2020-06-08 22:01:48 +00:00
|
|
|
Headers: headers, //TODO: TUN-2688 support custom headers from config file
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// we could add a cmd line variable for this bool if we want the SOCK5 server to be on the client side
|
|
|
|
wsConn := carrier.NewWSConnection(logger, false)
|
|
|
|
|
|
|
|
logger.Infof("Start Websocket listener on: %s", validURL.Host)
|
|
|
|
return carrier.StartForwarder(wsConn, validURL.Host, shutdown, options)
|
|
|
|
}
|
|
|
|
|
2018-09-21 15:18:23 +00:00
|
|
|
// ssh will start a WS proxy server for server mode
|
|
|
|
// or copy from stdin/stdout for client mode
|
|
|
|
// useful for proxying other protocols (like ssh) over websockets
|
|
|
|
// (which you can put Access in front of)
|
|
|
|
func ssh(c *cli.Context) error {
|
2020-04-29 20:51:32 +00:00
|
|
|
logDirectory, logLevel := config.FindLogSettings()
|
2020-06-05 15:18:40 +00:00
|
|
|
|
|
|
|
flagLogDirectory := c.String(sshLogDirectoryFlag)
|
|
|
|
if flagLogDirectory != "" {
|
|
|
|
logDirectory = flagLogDirectory
|
|
|
|
}
|
|
|
|
|
|
|
|
flagLogLevel := c.String(sshLogLevelFlag)
|
|
|
|
if flagLogLevel != "" {
|
|
|
|
logLevel = flagLogLevel
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:51:32 +00:00
|
|
|
logger, err := logger.New(logger.DefaultFile(logDirectory), logger.LogLevelString(logLevel))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error setting up logger")
|
|
|
|
}
|
|
|
|
|
2019-01-23 21:42:10 +00:00
|
|
|
// get the hostname from the cmdline and error out if its not provided
|
|
|
|
rawHostName := c.String(sshHostnameFlag)
|
|
|
|
hostname, err := validation.ValidateHostname(rawHostName)
|
|
|
|
if err != nil || rawHostName == "" {
|
2018-10-19 20:44:35 +00:00
|
|
|
return cli.ShowCommandHelp(c, "ssh")
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
2019-10-22 15:41:44 +00:00
|
|
|
originURL := ensureURLScheme(hostname)
|
2019-01-23 21:42:10 +00:00
|
|
|
|
|
|
|
// get the headers from the cmdline and add them
|
|
|
|
headers := buildRequestHeaders(c.StringSlice(sshHeaderFlag))
|
|
|
|
if c.IsSet(sshTokenIDFlag) {
|
2020-06-08 22:01:48 +00:00
|
|
|
headers.Set(h2mux.CFAccessClientIDHeader, c.String(sshTokenIDFlag))
|
2019-01-23 21:42:10 +00:00
|
|
|
}
|
|
|
|
if c.IsSet(sshTokenSecretFlag) {
|
2020-06-08 22:01:48 +00:00
|
|
|
headers.Set(h2mux.CFAccessClientSecretHeader, c.String(sshTokenSecretFlag))
|
2019-03-06 19:09:13 +00:00
|
|
|
}
|
2019-01-23 21:42:10 +00:00
|
|
|
|
2019-10-02 20:56:28 +00:00
|
|
|
destination := c.String(sshDestinationFlag)
|
2019-10-11 17:26:23 +00:00
|
|
|
if destination != "" {
|
2020-05-04 20:15:17 +00:00
|
|
|
headers.Add(h2mux.CFJumpDestinationHeader, destination)
|
2019-10-02 20:56:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 21:42:10 +00:00
|
|
|
options := &carrier.StartOptions{
|
2019-05-22 20:41:21 +00:00
|
|
|
OriginURL: originURL,
|
|
|
|
Headers: headers,
|
2019-03-06 19:09:13 +00:00
|
|
|
}
|
2018-09-21 15:18:23 +00:00
|
|
|
|
2020-03-31 14:56:22 +00:00
|
|
|
// we could add a cmd line variable for this bool if we want the SOCK5 server to be on the client side
|
|
|
|
wsConn := carrier.NewWSConnection(logger, false)
|
|
|
|
|
2019-01-23 21:42:10 +00:00
|
|
|
if c.NArg() > 0 || c.IsSet(sshURLFlag) {
|
2018-10-19 20:44:35 +00:00
|
|
|
localForwarder, err := config.ValidateUrl(c)
|
2018-09-21 15:18:23 +00:00
|
|
|
if err != nil {
|
2020-04-29 20:51:32 +00:00
|
|
|
logger.Errorf("Error validating origin URL: %s", err)
|
2018-09-21 15:18:23 +00:00
|
|
|
return errors.Wrap(err, "error validating origin URL")
|
|
|
|
}
|
|
|
|
forwarder, err := url.Parse(localForwarder)
|
|
|
|
if err != nil {
|
2020-04-29 20:51:32 +00:00
|
|
|
logger.Errorf("Error validating origin URL: %s", err)
|
2018-09-21 15:18:23 +00:00
|
|
|
return errors.Wrap(err, "error validating origin URL")
|
|
|
|
}
|
2020-03-31 14:56:22 +00:00
|
|
|
|
|
|
|
logger.Infof("Start Websocket listener on: %s", forwarder.Host)
|
2020-04-29 20:51:32 +00:00
|
|
|
err = carrier.StartForwarder(wsConn, forwarder.Host, shutdownC, options)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Error on Websocket listener: %s", err)
|
|
|
|
}
|
|
|
|
return err
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 14:56:22 +00:00
|
|
|
return carrier.StartClient(wsConn, &carrier.StdinoutStream{}, options)
|
2019-02-07 16:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildRequestHeaders(values []string) http.Header {
|
|
|
|
headers := make(http.Header)
|
|
|
|
for _, valuePair := range values {
|
|
|
|
split := strings.Split(valuePair, ":")
|
|
|
|
if len(split) > 1 {
|
|
|
|
headers.Add(strings.TrimSpace(split[0]), strings.TrimSpace(split[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return headers
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|