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"
|
2018-09-21 15:18:23 +00:00
|
|
|
"github.com/cloudflare/cloudflared/validation"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
cli "gopkg.in/urfave/cli.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
hostname, err := validation.ValidateHostname(c.String("hostname"))
|
2018-10-19 20:44:35 +00:00
|
|
|
if err != nil || c.String("hostname") == "" {
|
|
|
|
return cli.ShowCommandHelp(c, "ssh")
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
2019-02-07 16:56:33 +00:00
|
|
|
headers := buildRequestHeaders(c.StringSlice("header"))
|
2018-09-21 15:18:23 +00:00
|
|
|
|
|
|
|
if c.NArg() > 0 || c.IsSet("url") {
|
2018-10-19 20:44:35 +00:00
|
|
|
localForwarder, err := config.ValidateUrl(c)
|
2018-09-21 15:18:23 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("Error validating origin URL")
|
|
|
|
return errors.Wrap(err, "error validating origin URL")
|
|
|
|
}
|
|
|
|
forwarder, err := url.Parse(localForwarder)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("Error validating origin URL")
|
|
|
|
return errors.Wrap(err, "error validating origin URL")
|
|
|
|
}
|
2019-02-07 16:56:33 +00:00
|
|
|
return carrier.StartServer(logger, forwarder.Host, "https://"+hostname, shutdownC, headers)
|
2018-09-21 15:18:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 16:56:33 +00:00
|
|
|
return carrier.StartClient(logger, "https://"+hostname, &carrier.StdinoutStream{}, headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|