2020-10-08 10:12:26 +00:00
|
|
|
package origin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-10-20 15:26:55 +00:00
|
|
|
"context"
|
2020-11-02 11:21:34 +00:00
|
|
|
"fmt"
|
2020-10-08 10:12:26 +00:00
|
|
|
"io"
|
2020-10-30 11:41:14 +00:00
|
|
|
"net"
|
2020-10-08 10:12:26 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/buffer"
|
|
|
|
"github.com/cloudflare/cloudflared/connection"
|
2020-11-02 11:21:34 +00:00
|
|
|
"github.com/cloudflare/cloudflared/ingress"
|
2020-10-08 10:12:26 +00:00
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
|
|
|
"github.com/cloudflare/cloudflared/websocket"
|
2020-11-25 06:55:13 +00:00
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2020-10-08 10:12:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TagHeaderNamePrefix = "Cf-Warp-Tag-"
|
|
|
|
)
|
|
|
|
|
|
|
|
type client struct {
|
2020-11-02 11:21:34 +00:00
|
|
|
ingressRules ingress.Ingress
|
|
|
|
tags []tunnelpogs.Tag
|
2020-11-25 06:55:13 +00:00
|
|
|
log *zerolog.Logger
|
2020-11-02 11:21:34 +00:00
|
|
|
bufferPool *buffer.Pool
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
func NewClient(ingressRules ingress.Ingress, tags []tunnelpogs.Tag, log *zerolog.Logger) connection.OriginClient {
|
2020-10-08 10:12:26 +00:00
|
|
|
return &client{
|
2020-11-02 11:21:34 +00:00
|
|
|
ingressRules: ingressRules,
|
|
|
|
tags: tags,
|
2020-11-25 06:55:13 +00:00
|
|
|
log: log,
|
2020-11-02 11:21:34 +00:00
|
|
|
bufferPool: buffer.NewPool(512 * 1024),
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) Proxy(w connection.ResponseWriter, req *http.Request, isWebsocket bool) error {
|
|
|
|
incrementRequests()
|
|
|
|
defer decrementConcurrentRequests()
|
|
|
|
|
|
|
|
cfRay := findCfRayHeader(req)
|
|
|
|
lbProbe := isLBProbeRequest(req)
|
|
|
|
|
|
|
|
c.appendTagHeaders(req)
|
2020-11-02 11:21:34 +00:00
|
|
|
rule, ruleNum := c.ingressRules.FindMatchingRule(req.Host, req.URL.Path)
|
|
|
|
c.logRequest(req, cfRay, lbProbe, ruleNum)
|
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
var (
|
|
|
|
resp *http.Response
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if isWebsocket {
|
2020-11-02 11:21:34 +00:00
|
|
|
resp, err = c.proxyWebsocket(w, req, rule)
|
2020-10-08 10:12:26 +00:00
|
|
|
} else {
|
2020-11-02 11:21:34 +00:00
|
|
|
resp, err = c.proxyHTTP(w, req, rule)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2020-11-02 11:21:34 +00:00
|
|
|
c.logRequestError(err, cfRay, ruleNum)
|
2020-10-27 22:27:15 +00:00
|
|
|
w.WriteErrorResponse()
|
2020-10-08 10:12:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-02 11:21:34 +00:00
|
|
|
c.logOriginResponse(resp, cfRay, lbProbe, ruleNum)
|
2020-10-08 10:12:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
func (c *client) proxyHTTP(w connection.ResponseWriter, req *http.Request, rule *ingress.Rule) (*http.Response, error) {
|
2020-10-08 10:12:26 +00:00
|
|
|
// Support for WSGI Servers by switching transfer encoding from chunked to gzip/deflate
|
2020-11-02 11:21:34 +00:00
|
|
|
if rule.Config.DisableChunkedEncoding {
|
2020-10-08 10:12:26 +00:00
|
|
|
req.TransferEncoding = []string{"gzip", "deflate"}
|
|
|
|
cLength, err := strconv.Atoi(req.Header.Get("Content-Length"))
|
|
|
|
if err == nil {
|
|
|
|
req.ContentLength = int64(cLength)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request origin to keep connection alive to improve performance
|
|
|
|
req.Header.Set("Connection", "keep-alive")
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
if hostHeader := rule.Config.HTTPHostHeader; hostHeader != "" {
|
|
|
|
req.Header.Set("Host", hostHeader)
|
|
|
|
req.Host = hostHeader
|
|
|
|
}
|
2020-10-08 10:12:26 +00:00
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
resp, err := rule.Service.RoundTrip(req)
|
2020-10-08 10:12:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error proxying request to origin")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
err = w.WriteRespHeaders(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error writing response header")
|
|
|
|
}
|
2020-11-18 11:53:59 +00:00
|
|
|
if connection.IsServerSentEvent(resp.Header) {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msg("Detected Server-Side Events from Origin")
|
2020-10-08 10:12:26 +00:00
|
|
|
c.writeEventStream(w, resp.Body)
|
|
|
|
} else {
|
|
|
|
// Use CopyBuffer, because Copy only allocates a 32KiB buffer, and cross-stream
|
|
|
|
// compression generates dictionary on first write
|
|
|
|
buf := c.bufferPool.Get()
|
|
|
|
defer c.bufferPool.Put(buf)
|
2020-11-25 06:55:13 +00:00
|
|
|
_, _ = io.CopyBuffer(w, resp.Body, buf)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
func (c *client) proxyWebsocket(w connection.ResponseWriter, req *http.Request, rule *ingress.Rule) (*http.Response, error) {
|
|
|
|
if hostHeader := rule.Config.HTTPHostHeader; hostHeader != "" {
|
|
|
|
req.Header.Set("Host", hostHeader)
|
|
|
|
req.Host = hostHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
dialler, ok := rule.Service.(websocket.Dialler)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Websockets aren't supported by the origin service '%s'", rule.Service)
|
|
|
|
}
|
|
|
|
conn, resp, err := websocket.ClientConnect(req, dialler)
|
2020-10-08 10:12:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-20 15:26:55 +00:00
|
|
|
|
|
|
|
serveCtx, cancel := context.WithCancel(req.Context())
|
2020-10-30 11:41:14 +00:00
|
|
|
connClosedChan := make(chan struct{})
|
2020-10-20 15:26:55 +00:00
|
|
|
go func() {
|
2020-10-30 11:41:14 +00:00
|
|
|
// serveCtx is done if req is cancelled, or streamWebsocket returns
|
2020-10-20 15:26:55 +00:00
|
|
|
<-serveCtx.Done()
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = conn.Close()
|
2020-10-30 11:41:14 +00:00
|
|
|
close(connClosedChan)
|
2020-10-20 15:26:55 +00:00
|
|
|
}()
|
2020-10-30 11:41:14 +00:00
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
// Copy to/from stream to the undelying connection. Use the underlying
|
|
|
|
// connection because cloudflared doesn't operate on the message themselves
|
2020-10-30 11:41:14 +00:00
|
|
|
err = c.streamWebsocket(w, conn.UnderlyingConn(), resp)
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
// We need to make sure conn is closed before returning, otherwise we might write to conn after Proxy returns
|
|
|
|
<-connClosedChan
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) streamWebsocket(w connection.ResponseWriter, conn net.Conn, resp *http.Response) error {
|
|
|
|
err := w.WriteRespHeaders(resp)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Error writing websocket response header")
|
|
|
|
}
|
|
|
|
websocket.Stream(conn, w)
|
|
|
|
return nil
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) writeEventStream(w connection.ResponseWriter, respBody io.ReadCloser) {
|
|
|
|
reader := bufio.NewReader(respBody)
|
|
|
|
for {
|
|
|
|
line, err := reader.ReadBytes('\n')
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
_, _ = w.Write(line)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) appendTagHeaders(r *http.Request) {
|
2020-11-02 11:21:34 +00:00
|
|
|
for _, tag := range c.tags {
|
2020-10-08 10:12:26 +00:00
|
|
|
r.Header.Add(TagHeaderNamePrefix+tag.Name, tag.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
func (c *client) logRequest(r *http.Request, cfRay string, lbProbe bool, ruleNum int) {
|
2020-10-08 10:12:26 +00:00
|
|
|
if cfRay != "" {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("CF-RAY: %s %s %s %s", cfRay, r.Method, r.URL, r.Proto)
|
2020-10-08 10:12:26 +00:00
|
|
|
} else if lbProbe {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("CF-RAY: %s Load Balancer health check %s %s %s", cfRay, r.Method, r.URL, r.Proto)
|
2020-10-08 10:12:26 +00:00
|
|
|
} else {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("All requests should have a CF-RAY header. Please open a support ticket with Cloudflare. %s %s %s ", r.Method, r.URL, r.Proto)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("CF-RAY: %s Request Headers %+v", cfRay, r.Header)
|
|
|
|
c.log.Debug().Msgf("CF-RAY: %s Serving with ingress rule %d", cfRay, ruleNum)
|
2020-10-08 10:12:26 +00:00
|
|
|
|
|
|
|
if contentLen := r.ContentLength; contentLen == -1 {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("CF-RAY: %s Request Content length unknown", cfRay)
|
2020-10-08 10:12:26 +00:00
|
|
|
} else {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("CF-RAY: %s Request content length %d", cfRay, contentLen)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
func (c *client) logOriginResponse(r *http.Response, cfRay string, lbProbe bool, ruleNum int) {
|
|
|
|
responseByCode.WithLabelValues(strconv.Itoa(r.StatusCode)).Inc()
|
2020-10-08 10:12:26 +00:00
|
|
|
if cfRay != "" {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Info().Msgf("CF-RAY: %s Status: %s served by ingress %d", cfRay, r.Status, ruleNum)
|
2020-10-08 10:12:26 +00:00
|
|
|
} else if lbProbe {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("Response to Load Balancer health check %s", r.Status)
|
2020-10-08 10:12:26 +00:00
|
|
|
} else {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("Status: %s served by ingress %d", r.Status, ruleNum)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("CF-RAY: %s Response Headers %+v", cfRay, r.Header)
|
2020-10-08 10:12:26 +00:00
|
|
|
|
|
|
|
if contentLen := r.ContentLength; contentLen == -1 {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("CF-RAY: %s Response content length unknown", cfRay)
|
2020-10-08 10:12:26 +00:00
|
|
|
} else {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Debug().Msgf("CF-RAY: %s Response content length %d", cfRay, contentLen)
|
2020-10-08 10:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:21:34 +00:00
|
|
|
func (c *client) logRequestError(err error, cfRay string, ruleNum int) {
|
|
|
|
requestErrors.Inc()
|
|
|
|
if cfRay != "" {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Error().Msgf("CF-RAY: %s Proxying to ingress %d error: %v", cfRay, ruleNum, err)
|
2020-11-02 11:21:34 +00:00
|
|
|
} else {
|
2020-11-25 06:55:13 +00:00
|
|
|
c.log.Error().Msgf("Proxying to ingress %d error: %v", ruleNum, err)
|
2020-11-02 11:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-08 10:12:26 +00:00
|
|
|
func findCfRayHeader(req *http.Request) string {
|
|
|
|
return req.Header.Get("Cf-Ray")
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLBProbeRequest(req *http.Request) bool {
|
|
|
|
return strings.HasPrefix(req.UserAgent(), lbProbeUserAgentPrefix)
|
|
|
|
}
|