2020-10-15 21:41:03 +00:00
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
2020-10-30 21:37:40 +00:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2022-03-14 17:51:10 +00:00
|
|
|
"encoding/json"
|
2020-10-15 21:41:03 +00:00
|
|
|
"fmt"
|
2020-11-05 13:37:30 +00:00
|
|
|
"io"
|
2020-10-15 21:41:03 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-10-28 14:48:10 +00:00
|
|
|
"strconv"
|
2020-10-30 21:37:40 +00:00
|
|
|
"time"
|
2020-10-15 21:41:03 +00:00
|
|
|
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2020-10-15 21:41:03 +00:00
|
|
|
"github.com/cloudflare/cloudflared/hello"
|
2021-03-01 22:26:37 +00:00
|
|
|
"github.com/cloudflare/cloudflared/ipaccess"
|
2020-10-15 21:41:03 +00:00
|
|
|
"github.com/cloudflare/cloudflared/socks"
|
2020-10-30 21:37:40 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tlsconfig"
|
2020-10-15 21:41:03 +00:00
|
|
|
)
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
const (
|
2022-03-14 17:51:10 +00:00
|
|
|
HelloWorldService = "hello_world"
|
2023-03-06 23:19:10 +00:00
|
|
|
HelloWorldFlag = "hello-world"
|
2022-03-14 17:51:10 +00:00
|
|
|
HttpStatusService = "http_status"
|
2022-02-11 10:49:06 +00:00
|
|
|
)
|
|
|
|
|
2021-07-01 18:30:26 +00:00
|
|
|
// OriginService is something a tunnel can proxy traffic to.
|
|
|
|
type OriginService interface {
|
2020-10-30 21:37:40 +00:00
|
|
|
String() string
|
2020-10-15 21:41:03 +00:00
|
|
|
// Start the origin service if it's managed by cloudflared, e.g. proxy servers or Hello World.
|
|
|
|
// If it's not managed by cloudflared, this is a no-op because the user is responsible for
|
|
|
|
// starting the origin service.
|
2022-02-11 10:49:06 +00:00
|
|
|
// Implementor of services managed by cloudflared should terminate the service if shutdownC is closed
|
|
|
|
start(log *zerolog.Logger, shutdownC <-chan struct{}, cfg OriginRequestConfig) error
|
2022-03-14 17:51:10 +00:00
|
|
|
MarshalJSON() ([]byte, error)
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 20:07:47 +00:00
|
|
|
// unixSocketPath is an OriginService representing a unix socket (which accepts HTTP or HTTPS)
|
2020-10-30 21:37:40 +00:00
|
|
|
type unixSocketPath struct {
|
|
|
|
path string
|
2022-02-28 20:07:47 +00:00
|
|
|
scheme string
|
2020-10-30 21:37:40 +00:00
|
|
|
transport *http.Transport
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:37:40 +00:00
|
|
|
func (o *unixSocketPath) String() string {
|
2022-03-14 17:51:10 +00:00
|
|
|
scheme := ""
|
|
|
|
if o.scheme == "https" {
|
|
|
|
scheme = "+tls"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("unix%s:%s", scheme, o.path)
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
func (o *unixSocketPath) start(log *zerolog.Logger, _ <-chan struct{}, cfg OriginRequestConfig) error {
|
2020-11-15 02:03:29 +00:00
|
|
|
transport, err := newHTTPTransport(o, cfg, log)
|
2020-10-30 21:37:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.transport = transport
|
2020-10-15 21:41:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
func (o unixSocketPath) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(o.String())
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
type httpService struct {
|
2021-02-08 19:25:08 +00:00
|
|
|
url *url.URL
|
|
|
|
hostHeader string
|
|
|
|
transport *http.Transport
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
func (o *httpService) start(log *zerolog.Logger, _ <-chan struct{}, cfg OriginRequestConfig) error {
|
2020-11-15 02:03:29 +00:00
|
|
|
transport, err := newHTTPTransport(o, cfg, log)
|
2020-10-30 21:37:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-08 19:25:08 +00:00
|
|
|
o.hostHeader = cfg.HTTPHostHeader
|
2020-10-30 21:37:40 +00:00
|
|
|
o.transport = transport
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func (o *httpService) String() string {
|
|
|
|
return o.url.String()
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
func (o httpService) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(o.String())
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
// rawTCPService dials TCP to the destination specified by the client
|
|
|
|
// It's used by warp routing
|
|
|
|
type rawTCPService struct {
|
2022-06-13 16:44:27 +00:00
|
|
|
name string
|
|
|
|
dialer net.Dialer
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
func (o *rawTCPService) String() string {
|
|
|
|
return o.name
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
func (o *rawTCPService) start(log *zerolog.Logger, _ <-chan struct{}, cfg OriginRequestConfig) error {
|
2020-12-09 21:46:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-20 17:09:02 +00:00
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
func (o rawTCPService) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(o.String())
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
// tcpOverWSService models TCP origins serving eyeballs connecting over websocket, such as
|
|
|
|
// cloudflared access commands.
|
|
|
|
type tcpOverWSService struct {
|
2022-05-06 17:47:03 +00:00
|
|
|
scheme string
|
2021-02-05 13:01:53 +00:00
|
|
|
dest string
|
|
|
|
isBastion bool
|
|
|
|
streamHandler streamHandlerFunc
|
2022-06-13 16:44:27 +00:00
|
|
|
dialer net.Dialer
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
2020-10-15 21:41:03 +00:00
|
|
|
|
2021-03-01 22:26:37 +00:00
|
|
|
type socksProxyOverWSService struct {
|
|
|
|
conn *socksProxyOverWSConnection
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
func newTCPOverWSService(url *url.URL) *tcpOverWSService {
|
2020-12-09 21:46:53 +00:00
|
|
|
switch url.Scheme {
|
2020-10-15 21:41:03 +00:00
|
|
|
case "ssh":
|
2020-12-09 21:46:53 +00:00
|
|
|
addPortIfMissing(url, 22)
|
2020-10-15 21:41:03 +00:00
|
|
|
case "rdp":
|
2020-12-09 21:46:53 +00:00
|
|
|
addPortIfMissing(url, 3389)
|
2020-10-15 21:41:03 +00:00
|
|
|
case "smb":
|
2020-12-09 21:46:53 +00:00
|
|
|
addPortIfMissing(url, 445)
|
2020-10-15 21:41:03 +00:00
|
|
|
case "tcp":
|
2020-12-09 21:46:53 +00:00
|
|
|
addPortIfMissing(url, 7864) // just a random port since there isn't a default in this case
|
|
|
|
}
|
2021-02-05 13:01:53 +00:00
|
|
|
return &tcpOverWSService{
|
2022-05-06 17:47:03 +00:00
|
|
|
scheme: url.Scheme,
|
|
|
|
dest: url.Host,
|
2021-02-05 13:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBastionService() *tcpOverWSService {
|
|
|
|
return &tcpOverWSService{
|
|
|
|
isBastion: true,
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
2020-10-15 21:41:03 +00:00
|
|
|
|
2021-03-01 22:26:37 +00:00
|
|
|
func newSocksProxyOverWSService(accessPolicy *ipaccess.Policy) *socksProxyOverWSService {
|
|
|
|
proxy := socksProxyOverWSService{
|
|
|
|
conn: &socksProxyOverWSConnection{
|
|
|
|
accessPolicy: accessPolicy,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proxy
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func addPortIfMissing(uri *url.URL, port int) {
|
2022-10-28 09:33:23 +00:00
|
|
|
hostname := uri.Hostname()
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
if uri.Port() == "" {
|
2022-10-28 14:48:10 +00:00
|
|
|
uri.Host = net.JoinHostPort(hostname, strconv.FormatInt(int64(port), 10))
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
func (o *tcpOverWSService) String() string {
|
|
|
|
if o.isBastion {
|
|
|
|
return ServiceBastion
|
|
|
|
}
|
2022-05-06 17:47:03 +00:00
|
|
|
|
|
|
|
if o.scheme != "" {
|
|
|
|
return fmt.Sprintf("%s://%s", o.scheme, o.dest)
|
|
|
|
} else {
|
|
|
|
return o.dest
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
func (o *tcpOverWSService) start(log *zerolog.Logger, _ <-chan struct{}, cfg OriginRequestConfig) error {
|
2020-12-09 21:46:53 +00:00
|
|
|
if cfg.ProxyType == socksProxy {
|
2021-02-05 13:01:53 +00:00
|
|
|
o.streamHandler = socks.StreamHandler
|
2020-12-09 21:46:53 +00:00
|
|
|
} else {
|
2021-02-05 13:01:53 +00:00
|
|
|
o.streamHandler = DefaultStreamHandler
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
2022-06-13 16:44:27 +00:00
|
|
|
o.dialer.Timeout = cfg.ConnectTimeout.Duration
|
|
|
|
o.dialer.KeepAlive = cfg.TCPKeepAlive.Duration
|
2020-12-09 21:46:53 +00:00
|
|
|
return nil
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
func (o tcpOverWSService) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(o.String())
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
func (o *socksProxyOverWSService) start(log *zerolog.Logger, _ <-chan struct{}, cfg OriginRequestConfig) error {
|
2021-03-01 22:26:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *socksProxyOverWSService) String() string {
|
|
|
|
return ServiceSocksProxy
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
func (o socksProxyOverWSService) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(o.String())
|
|
|
|
}
|
|
|
|
|
2020-10-30 21:37:40 +00:00
|
|
|
// HelloWorld is an OriginService for the built-in Hello World server.
|
|
|
|
// Users only use this for testing and experimenting with cloudflared.
|
|
|
|
type helloWorld struct {
|
2021-04-02 06:10:43 +00:00
|
|
|
httpService
|
|
|
|
server net.Listener
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:37:40 +00:00
|
|
|
func (o *helloWorld) String() string {
|
2022-02-11 10:49:06 +00:00
|
|
|
return HelloWorldService
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts a HelloWorld server and stores its address in the Service receiver.
|
2020-11-25 06:55:13 +00:00
|
|
|
func (o *helloWorld) start(
|
|
|
|
log *zerolog.Logger,
|
|
|
|
shutdownC <-chan struct{},
|
|
|
|
cfg OriginRequestConfig,
|
|
|
|
) error {
|
2022-02-11 10:49:06 +00:00
|
|
|
if err := o.httpService.start(log, shutdownC, cfg); err != nil {
|
2020-10-30 21:37:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-02 06:10:43 +00:00
|
|
|
|
2020-10-15 21:41:03 +00:00
|
|
|
helloListener, err := hello.CreateTLSListener("127.0.0.1:")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Cannot start Hello World Server")
|
|
|
|
}
|
2022-02-11 10:49:06 +00:00
|
|
|
go hello.StartHelloWorldServer(log, helloListener, shutdownC)
|
2020-10-15 21:41:03 +00:00
|
|
|
o.server = helloListener
|
2021-04-02 06:10:43 +00:00
|
|
|
|
|
|
|
o.httpService.url = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: o.server.Addr().String(),
|
|
|
|
}
|
|
|
|
|
2020-10-15 21:41:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
func (o helloWorld) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(o.String())
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:22:21 +00:00
|
|
|
// statusCode is an OriginService that just responds with a given HTTP status.
|
|
|
|
// Typical use-case is "user wants the catch-all rule to just respond 404".
|
|
|
|
type statusCode struct {
|
2022-05-18 11:11:38 +00:00
|
|
|
code int
|
2023-03-09 23:23:11 +00:00
|
|
|
|
|
|
|
// Set only when the user has not defined any ingress rules
|
|
|
|
defaultResp bool
|
|
|
|
log *zerolog.Logger
|
2020-11-04 18:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newStatusCode(status int) statusCode {
|
2022-05-18 11:11:38 +00:00
|
|
|
return statusCode{code: status}
|
2020-11-04 18:22:21 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 23:23:11 +00:00
|
|
|
// default status code (502) that is returned for requests to cloudflared that don't have any ingress rules setup
|
|
|
|
func newDefaultStatusCode(log *zerolog.Logger) statusCode {
|
|
|
|
return statusCode{code: 502, defaultResp: true, log: log}
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:22:21 +00:00
|
|
|
func (o *statusCode) String() string {
|
2022-05-18 11:11:38 +00:00
|
|
|
return fmt.Sprintf("http_status:%d", o.code)
|
2020-11-04 18:22:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
func (o *statusCode) start(
|
|
|
|
log *zerolog.Logger,
|
2022-02-11 10:49:06 +00:00
|
|
|
_ <-chan struct{},
|
2020-11-25 06:55:13 +00:00
|
|
|
cfg OriginRequestConfig,
|
|
|
|
) error {
|
2020-11-04 18:22:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
func (o statusCode) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(o.String())
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:22:21 +00:00
|
|
|
type NopReadCloser struct{}
|
|
|
|
|
2020-11-05 13:37:30 +00:00
|
|
|
// Read always returns EOF to signal end of input
|
2020-11-04 18:22:21 +00:00
|
|
|
func (nrc *NopReadCloser) Read(buf []byte) (int, error) {
|
2020-11-05 13:37:30 +00:00
|
|
|
return 0, io.EOF
|
2020-11-04 18:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (nrc *NopReadCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-01 18:30:26 +00:00
|
|
|
func newHTTPTransport(service OriginService, cfg OriginRequestConfig, log *zerolog.Logger) (*http.Transport, error) {
|
2020-11-15 02:03:29 +00:00
|
|
|
originCertPool, err := tlsconfig.LoadOriginCA(cfg.CAPool, log)
|
2020-10-30 21:37:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error loading cert pool")
|
|
|
|
}
|
|
|
|
|
|
|
|
httpTransport := http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
MaxIdleConns: cfg.KeepAliveConnections,
|
|
|
|
MaxIdleConnsPerHost: cfg.KeepAliveConnections,
|
2022-03-14 17:51:10 +00:00
|
|
|
IdleConnTimeout: cfg.KeepAliveTimeout.Duration,
|
|
|
|
TLSHandshakeTimeout: cfg.TLSTimeout.Duration,
|
2020-10-30 21:37:40 +00:00
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
TLSClientConfig: &tls.Config{RootCAs: originCertPool, InsecureSkipVerify: cfg.NoTLSVerify},
|
2022-06-01 00:51:59 +00:00
|
|
|
ForceAttemptHTTP2: cfg.Http2Origin,
|
2020-10-30 21:37:40 +00:00
|
|
|
}
|
|
|
|
if _, isHelloWorld := service.(*helloWorld); !isHelloWorld && cfg.OriginServerName != "" {
|
|
|
|
httpTransport.TLSClientConfig.ServerName = cfg.OriginServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
dialer := &net.Dialer{
|
2022-03-14 17:51:10 +00:00
|
|
|
Timeout: cfg.ConnectTimeout.Duration,
|
|
|
|
KeepAlive: cfg.TCPKeepAlive.Duration,
|
2020-10-30 21:37:40 +00:00
|
|
|
}
|
|
|
|
if cfg.NoHappyEyeballs {
|
|
|
|
dialer.FallbackDelay = -1 // As of Golang 1.12, a negative delay disables "happy eyeballs"
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext depends on which kind of origin is being used.
|
|
|
|
dialContext := dialer.DialContext
|
|
|
|
switch service := service.(type) {
|
|
|
|
|
|
|
|
// If this origin is a unix socket, enforce network type "unix".
|
|
|
|
case *unixSocketPath:
|
|
|
|
httpTransport.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
|
|
return dialContext(ctx, "unix", service.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, use the regular network config.
|
|
|
|
default:
|
|
|
|
httpTransport.DialContext = dialContext
|
|
|
|
}
|
|
|
|
|
|
|
|
return &httpTransport, nil
|
|
|
|
}
|
2020-10-30 11:41:14 +00:00
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
// MockOriginHTTPService should only be used by other packages to mock OriginService. Set Transport to configure desired RoundTripper behavior.
|
|
|
|
type MockOriginHTTPService struct {
|
2020-10-30 11:41:14 +00:00
|
|
|
Transport http.RoundTripper
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func (mos MockOriginHTTPService) RoundTrip(req *http.Request) (*http.Response, error) {
|
2020-10-30 11:41:14 +00:00
|
|
|
return mos.Transport.RoundTrip(req)
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func (mos MockOriginHTTPService) String() string {
|
2020-10-30 11:41:14 +00:00
|
|
|
return "MockOriginService"
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:49:06 +00:00
|
|
|
func (mos MockOriginHTTPService) start(log *zerolog.Logger, _ <-chan struct{}, cfg OriginRequestConfig) error {
|
2020-10-30 11:41:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-03-14 17:51:10 +00:00
|
|
|
|
|
|
|
func (mos MockOriginHTTPService) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(mos.String())
|
|
|
|
}
|