2020-10-15 21:41:03 +00:00
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
2020-10-30 21:37:40 +00:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
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"
|
|
|
|
"sync"
|
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
|
|
|
gws "github.com/gorilla/websocket"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2021-03-26 09:45:26 +00:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
|
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
|
|
|
"github.com/cloudflare/cloudflared/websocket"
|
|
|
|
)
|
|
|
|
|
2020-12-09 21:46:53 +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.
|
2020-11-25 06:55:13 +00:00
|
|
|
start(wg *sync.WaitGroup, log *zerolog.Logger, shutdownC <-chan struct{}, errC chan error, cfg OriginRequestConfig) error
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:37:40 +00:00
|
|
|
// unixSocketPath is an OriginService representing a unix socket (which accepts HTTP)
|
|
|
|
type unixSocketPath struct {
|
|
|
|
path string
|
|
|
|
transport *http.Transport
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:37:40 +00:00
|
|
|
func (o *unixSocketPath) String() string {
|
|
|
|
return "unix socket: " + o.path
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
func (o *unixSocketPath) start(wg *sync.WaitGroup, log *zerolog.Logger, shutdownC <-chan struct{}, errC chan error, 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
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:52:46 +00:00
|
|
|
func (o *unixSocketPath) Dial(reqURL *url.URL, headers http.Header) (*gws.Conn, *http.Response, error) {
|
|
|
|
d := &gws.Dialer{
|
|
|
|
NetDial: o.transport.Dial,
|
|
|
|
NetDialContext: o.transport.DialContext,
|
|
|
|
TLSClientConfig: o.transport.TLSClientConfig,
|
|
|
|
}
|
|
|
|
reqURL.Scheme = websocket.ChangeRequestScheme(reqURL)
|
|
|
|
return d.Dial(reqURL.String(), headers)
|
2020-10-30 21:37:40 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func (o *httpService) start(wg *sync.WaitGroup, log *zerolog.Logger, shutdownC <-chan struct{}, errC chan error, 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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
name string
|
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
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
func (o *rawTCPService) start(wg *sync.WaitGroup, log *zerolog.Logger, shutdownC <-chan struct{}, errC chan error, cfg OriginRequestConfig) error {
|
2020-12-09 21:46:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-20 17:09:02 +00:00
|
|
|
|
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 {
|
|
|
|
dest string
|
|
|
|
isBastion bool
|
|
|
|
streamHandler streamHandlerFunc
|
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{
|
|
|
|
dest: url.Host,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if uri.Port() == "" {
|
|
|
|
uri.Host = fmt.Sprintf("%s:%d", uri.Hostname(), port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
func (o *tcpOverWSService) String() string {
|
|
|
|
if o.isBastion {
|
|
|
|
return ServiceBastion
|
|
|
|
}
|
2020-12-09 21:46:53 +00:00
|
|
|
return o.dest
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
func (o *tcpOverWSService) start(wg *sync.WaitGroup, log *zerolog.Logger, shutdownC <-chan struct{}, errC chan error, 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
|
|
|
}
|
|
|
|
return nil
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 22:26:37 +00:00
|
|
|
func (o *socksProxyOverWSService) start(wg *sync.WaitGroup, log *zerolog.Logger, shutdownC <-chan struct{}, errC chan error, cfg OriginRequestConfig) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *socksProxyOverWSService) String() string {
|
|
|
|
return ServiceSocksProxy
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
server net.Listener
|
|
|
|
transport *http.Transport
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:37:40 +00:00
|
|
|
func (o *helloWorld) String() string {
|
|
|
|
return "Hello World test origin"
|
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(
|
|
|
|
wg *sync.WaitGroup,
|
|
|
|
log *zerolog.Logger,
|
|
|
|
shutdownC <-chan struct{},
|
|
|
|
errC chan error,
|
|
|
|
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
|
|
|
helloListener, err := hello.CreateTLSListener("127.0.0.1:")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Cannot start Hello World Server")
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
_ = hello.StartHelloWorldServer(log, helloListener, shutdownC)
|
|
|
|
}()
|
|
|
|
o.server = helloListener
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
resp *http.Response
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStatusCode(status int) statusCode {
|
|
|
|
resp := &http.Response{
|
|
|
|
StatusCode: status,
|
2020-11-02 11:21:34 +00:00
|
|
|
Status: fmt.Sprintf("%d %s", status, http.StatusText(status)),
|
2020-11-04 18:22:21 +00:00
|
|
|
Body: new(NopReadCloser),
|
|
|
|
}
|
|
|
|
return statusCode{resp: resp}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *statusCode) String() string {
|
|
|
|
return fmt.Sprintf("HTTP %d", o.resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
func (o *statusCode) start(
|
|
|
|
wg *sync.WaitGroup,
|
|
|
|
log *zerolog.Logger,
|
|
|
|
shutdownC <-chan struct{},
|
|
|
|
errC chan error,
|
|
|
|
cfg OriginRequestConfig,
|
|
|
|
) error {
|
2020-11-04 18:22:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +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,
|
|
|
|
IdleConnTimeout: cfg.KeepAliveTimeout,
|
|
|
|
TLSHandshakeTimeout: cfg.TLSTimeout,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
TLSClientConfig: &tls.Config{RootCAs: originCertPool, InsecureSkipVerify: cfg.NoTLSVerify},
|
|
|
|
}
|
|
|
|
if _, isHelloWorld := service.(*helloWorld); !isHelloWorld && cfg.OriginServerName != "" {
|
|
|
|
httpTransport.TLSClientConfig.ServerName = cfg.OriginServerName
|
|
|
|
}
|
|
|
|
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
Timeout: cfg.ConnectTimeout,
|
|
|
|
KeepAlive: cfg.TCPKeepAlive,
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:45:26 +00:00
|
|
|
http2.ConfigureTransport(&httpTransport)
|
2020-10-30 21:37:40 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
func (mos MockOriginHTTPService) start(wg *sync.WaitGroup, log *zerolog.Logger, shutdownC <-chan struct{}, errC chan error, cfg OriginRequestConfig) error {
|
2020-10-30 11:41:14 +00:00
|
|
|
return nil
|
|
|
|
}
|