2020-02-09 02:07:07 +00:00
|
|
|
package h2mux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2020-04-09 20:59:15 +00:00
|
|
|
"encoding/json"
|
2020-02-09 02:07:07 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-03-23 15:22:58 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-02-09 02:07:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Header struct {
|
|
|
|
Name, Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
var headerEncoding = base64.RawStdEncoding
|
|
|
|
|
2020-02-21 02:51:46 +00:00
|
|
|
const (
|
|
|
|
RequestUserHeadersField = "cf-cloudflared-request-headers"
|
|
|
|
ResponseUserHeadersField = "cf-cloudflared-response-headers"
|
2020-04-09 20:59:15 +00:00
|
|
|
|
|
|
|
ResponseMetaHeaderField = "cf-cloudflared-response-meta"
|
|
|
|
ResponseSourceCloudflared = "cloudflared"
|
|
|
|
ResponseSourceOrigin = "origin"
|
2020-05-04 20:15:17 +00:00
|
|
|
|
|
|
|
CFAccessTokenHeader = "cf-access-token"
|
|
|
|
CFJumpDestinationHeader = "CF-Access-Jump-Destination"
|
|
|
|
CFAccessClientIDHeader = "CF-Access-Client-Id"
|
|
|
|
CFAccessClientSecretHeader = "CF-Access-Client-Secret"
|
2020-02-21 02:51:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// H2RequestHeadersToH1Request converts the HTTP/2 headers coming from origintunneld
|
|
|
|
// to an HTTP/1 Request object destined for the local origin web service.
|
|
|
|
// This operation includes conversion of the pseudo-headers into their closest
|
2020-02-09 02:07:07 +00:00
|
|
|
// HTTP/1 equivalents. See https://tools.ietf.org/html/rfc7540#section-8.1.2.3
|
2020-02-21 02:51:46 +00:00
|
|
|
func H2RequestHeadersToH1Request(h2 []Header, h1 *http.Request) error {
|
|
|
|
for _, header := range h2 {
|
2020-07-25 04:23:00 +00:00
|
|
|
name := strings.ToLower(header.Name)
|
|
|
|
if !IsControlHeader(name) {
|
2020-03-30 21:51:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-07-25 04:23:00 +00:00
|
|
|
switch name {
|
2020-02-21 02:51:46 +00:00
|
|
|
case ":method":
|
|
|
|
h1.Method = header.Value
|
|
|
|
case ":scheme":
|
|
|
|
// noop - use the preexisting scheme from h1.URL
|
|
|
|
case ":authority":
|
|
|
|
// Otherwise the host header will be based on the origin URL
|
|
|
|
h1.Host = header.Value
|
|
|
|
case ":path":
|
|
|
|
// We don't want to be an "opinionated" proxy, so ideally we would use :path as-is.
|
|
|
|
// However, this HTTP/1 Request object belongs to the Go standard library,
|
|
|
|
// whose URL package makes some opinionated decisions about the encoding of
|
|
|
|
// URL characters: see the docs of https://godoc.org/net/url#URL,
|
|
|
|
// in particular the EscapedPath method https://godoc.org/net/url#URL.EscapedPath,
|
|
|
|
// which is always used when computing url.URL.String(), whether we'd like it or not.
|
|
|
|
//
|
|
|
|
// Well, not *always*. We could circumvent this by using url.URL.Opaque. But
|
|
|
|
// that would present unusual difficulties when using an HTTP proxy: url.URL.Opaque
|
|
|
|
// is treated differently when HTTP_PROXY is set!
|
|
|
|
// See https://github.com/golang/go/issues/5684#issuecomment-66080888
|
|
|
|
//
|
|
|
|
// This means we are subject to the behavior of net/url's function `shouldEscape`
|
|
|
|
// (as invoked with mode=encodePath): https://github.com/golang/go/blob/go1.12.7/src/net/url/url.go#L101
|
|
|
|
|
|
|
|
if header.Value == "*" {
|
|
|
|
h1.URL.Path = "*"
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Due to the behavior of validation.ValidateUrl, h1.URL may
|
|
|
|
// already have a partial value, with or without a trailing slash.
|
|
|
|
base := h1.URL.String()
|
|
|
|
base = strings.TrimRight(base, "/")
|
|
|
|
// But we know :path begins with '/', because we handled '*' above - see RFC7540
|
|
|
|
requestURL, err := url.Parse(base + header.Value)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, fmt.Sprintf("invalid path '%v'", header.Value))
|
|
|
|
}
|
|
|
|
h1.URL = requestURL
|
|
|
|
case "content-length":
|
|
|
|
contentLength, err := strconv.ParseInt(header.Value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unparseable content length")
|
|
|
|
}
|
|
|
|
h1.ContentLength = contentLength
|
2020-03-30 21:51:48 +00:00
|
|
|
case RequestUserHeadersField:
|
|
|
|
// Do not forward the serialized headers to the origin -- deserialize them, and ditch the serialized version
|
|
|
|
// Find and parse user headers serialized into a single one
|
|
|
|
userHeaders, err := ParseUserHeaders(RequestUserHeadersField, h2)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Unable to parse user headers")
|
|
|
|
}
|
|
|
|
for _, userHeader := range userHeaders {
|
|
|
|
h1.Header.Add(http.CanonicalHeaderKey(userHeader.Name), userHeader.Value)
|
|
|
|
}
|
2020-02-21 02:51:46 +00:00
|
|
|
default:
|
2020-03-30 21:51:48 +00:00
|
|
|
// All other control headers shall just be proxied transparently
|
|
|
|
h1.Header.Add(http.CanonicalHeaderKey(header.Name), header.Value)
|
2020-02-21 02:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseUserHeaders(headerNameToParseFrom string, headers []Header) ([]Header, error) {
|
|
|
|
for _, header := range headers {
|
|
|
|
if header.Name == headerNameToParseFrom {
|
|
|
|
return DeserializeHeaders(header.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("%v header not found", RequestUserHeadersField)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsControlHeader(headerName string) bool {
|
2020-03-19 01:39:59 +00:00
|
|
|
return headerName == "content-length" ||
|
|
|
|
headerName == "connection" || headerName == "upgrade" || // Websocket headers
|
2020-02-21 02:51:46 +00:00
|
|
|
strings.HasPrefix(headerName, ":") ||
|
|
|
|
strings.HasPrefix(headerName, "cf-")
|
|
|
|
}
|
|
|
|
|
2020-07-25 04:23:00 +00:00
|
|
|
// isWebsocketClientHeader returns true if the header name is required by the client to upgrade properly
|
2020-09-21 08:45:51 +00:00
|
|
|
func IsWebsocketClientHeader(headerName string) bool {
|
2020-03-23 15:22:58 +00:00
|
|
|
return headerName == "sec-websocket-accept" ||
|
|
|
|
headerName == "connection" ||
|
|
|
|
headerName == "upgrade"
|
|
|
|
}
|
|
|
|
|
2020-02-21 02:51:46 +00:00
|
|
|
func H1ResponseToH2ResponseHeaders(h1 *http.Response) (h2 []Header) {
|
|
|
|
h2 = []Header{
|
|
|
|
{Name: ":status", Value: strconv.Itoa(h1.StatusCode)},
|
|
|
|
}
|
2020-07-25 04:23:00 +00:00
|
|
|
userHeaders := make(http.Header, len(h1.Header))
|
2020-02-21 02:51:46 +00:00
|
|
|
for header, values := range h1.Header {
|
2020-07-25 04:23:00 +00:00
|
|
|
h2name := strings.ToLower(header)
|
|
|
|
if h2name == "content-length" {
|
|
|
|
// This header has meaning in HTTP/2 and will be used by the edge,
|
|
|
|
// so it should be sent as an HTTP/2 response header.
|
|
|
|
|
|
|
|
// Since these are http2 headers, they're required to be lowercase
|
|
|
|
h2 = append(h2, Header{Name: "content-length", Value: values[0]})
|
2020-09-21 08:45:51 +00:00
|
|
|
} else if !IsControlHeader(h2name) || IsWebsocketClientHeader(h2name) {
|
2020-07-25 04:23:00 +00:00
|
|
|
// User headers, on the other hand, must all be serialized so that
|
|
|
|
// HTTP/2 header validation won't be applied to HTTP/1 header values
|
|
|
|
userHeaders[header] = values
|
2020-02-21 02:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform user header serialization and set them in the single header
|
2020-07-25 04:23:00 +00:00
|
|
|
h2 = append(h2, Header{ResponseUserHeadersField, SerializeHeaders(userHeaders)})
|
2020-02-21 02:51:46 +00:00
|
|
|
|
|
|
|
return h2
|
|
|
|
}
|
|
|
|
|
2020-02-09 02:07:07 +00:00
|
|
|
// Serialize HTTP1.x headers by base64-encoding each header name and value,
|
|
|
|
// and then joining them in the format of [key:value;]
|
2020-02-21 02:51:46 +00:00
|
|
|
func SerializeHeaders(h1Headers http.Header) string {
|
2020-07-25 04:23:00 +00:00
|
|
|
// compute size of the fully serialized value and largest temp buffer we will need
|
|
|
|
serializedLen := 0
|
|
|
|
maxTempLen := 0
|
2020-02-21 02:51:46 +00:00
|
|
|
for headerName, headerValues := range h1Headers {
|
2020-02-09 02:07:07 +00:00
|
|
|
for _, headerValue := range headerValues {
|
2020-07-25 04:23:00 +00:00
|
|
|
nameLen := headerEncoding.EncodedLen(len(headerName))
|
|
|
|
valueLen := headerEncoding.EncodedLen(len(headerValue))
|
|
|
|
const delims = 2
|
|
|
|
serializedLen += delims + nameLen + valueLen
|
|
|
|
if nameLen > maxTempLen {
|
|
|
|
maxTempLen = nameLen
|
|
|
|
}
|
|
|
|
if valueLen > maxTempLen {
|
|
|
|
maxTempLen = valueLen
|
|
|
|
}
|
2020-02-09 02:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-25 04:23:00 +00:00
|
|
|
var buf strings.Builder
|
|
|
|
buf.Grow(serializedLen)
|
|
|
|
|
|
|
|
temp := make([]byte, maxTempLen)
|
|
|
|
writeB64 := func(s string) {
|
|
|
|
n := headerEncoding.EncodedLen(len(s))
|
|
|
|
if n > len(temp) {
|
|
|
|
temp = make([]byte, n)
|
|
|
|
}
|
|
|
|
headerEncoding.Encode(temp[:n], []byte(s))
|
|
|
|
buf.Write(temp[:n])
|
|
|
|
}
|
2020-02-09 02:07:07 +00:00
|
|
|
|
2020-07-25 04:23:00 +00:00
|
|
|
for headerName, headerValues := range h1Headers {
|
|
|
|
for _, headerValue := range headerValues {
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
buf.WriteByte(';')
|
|
|
|
}
|
|
|
|
writeB64(headerName)
|
|
|
|
buf.WriteByte(':')
|
|
|
|
writeB64(headerValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
2020-02-09 02:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize headers serialized by `SerializeHeader`
|
2020-02-21 02:51:46 +00:00
|
|
|
func DeserializeHeaders(serializedHeaders string) ([]Header, error) {
|
2020-02-09 02:07:07 +00:00
|
|
|
const unableToDeserializeErr = "Unable to deserialize headers"
|
|
|
|
|
2020-02-21 02:51:46 +00:00
|
|
|
var deserialized []Header
|
|
|
|
for _, serializedPair := range strings.Split(serializedHeaders, ";") {
|
2020-02-09 02:07:07 +00:00
|
|
|
if len(serializedPair) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-21 02:51:46 +00:00
|
|
|
serializedHeaderParts := strings.Split(serializedPair, ":")
|
2020-02-09 02:07:07 +00:00
|
|
|
if len(serializedHeaderParts) != 2 {
|
|
|
|
return nil, errors.New(unableToDeserializeErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedName := serializedHeaderParts[0]
|
|
|
|
serializedValue := serializedHeaderParts[1]
|
|
|
|
deserializedName := make([]byte, headerEncoding.DecodedLen(len(serializedName)))
|
|
|
|
deserializedValue := make([]byte, headerEncoding.DecodedLen(len(serializedValue)))
|
|
|
|
|
2020-02-21 02:51:46 +00:00
|
|
|
if _, err := headerEncoding.Decode(deserializedName, []byte(serializedName)); err != nil {
|
2020-02-09 02:07:07 +00:00
|
|
|
return nil, errors.Wrap(err, unableToDeserializeErr)
|
|
|
|
}
|
2020-02-21 02:51:46 +00:00
|
|
|
if _, err := headerEncoding.Decode(deserializedValue, []byte(serializedValue)); err != nil {
|
2020-02-09 02:07:07 +00:00
|
|
|
return nil, errors.Wrap(err, unableToDeserializeErr)
|
|
|
|
}
|
|
|
|
|
2020-02-21 02:51:46 +00:00
|
|
|
deserialized = append(deserialized, Header{
|
|
|
|
Name: string(deserializedName),
|
|
|
|
Value: string(deserializedValue),
|
|
|
|
})
|
2020-02-09 02:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return deserialized, nil
|
|
|
|
}
|
2020-02-21 02:51:46 +00:00
|
|
|
|
2020-04-10 23:59:34 +00:00
|
|
|
type ResponseMetaHeader struct {
|
2020-04-09 20:59:15 +00:00
|
|
|
Source string `json:"src"`
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:26:09 +00:00
|
|
|
func CreateResponseMetaHeader(headerName, source string) Header {
|
2020-04-10 23:59:34 +00:00
|
|
|
jsonResponseMetaHeader, err := json.Marshal(ResponseMetaHeader{Source: source})
|
2020-04-09 20:59:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Header{
|
2020-04-10 19:26:09 +00:00
|
|
|
Name: headerName,
|
2020-04-09 20:59:15 +00:00
|
|
|
Value: string(jsonResponseMetaHeader),
|
|
|
|
}
|
|
|
|
}
|