2019-06-05 15:08:55 +00:00
|
|
|
package streamhandler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/h2mux"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
lbProbeUserAgentPrefix = "Mozilla/5.0 (compatible; Cloudflare-Traffic-Manager/1.0; +https://www.cloudflare.com/traffic-manager/;"
|
|
|
|
)
|
|
|
|
|
|
|
|
func FindCfRayHeader(h1 *http.Request) string {
|
|
|
|
return h1.Header.Get("Cf-Ray")
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsLBProbeRequest(req *http.Request) bool {
|
|
|
|
return strings.HasPrefix(req.UserAgent(), lbProbeUserAgentPrefix)
|
|
|
|
}
|
|
|
|
|
2019-06-20 16:18:59 +00:00
|
|
|
func createRequest(stream *h2mux.MuxedStream, url *url.URL) (*http.Request, error) {
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url.String(), h2mux.MuxedStreamReader{MuxedStream: stream})
|
2019-06-05 15:08:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "unexpected error from http.NewRequest")
|
|
|
|
}
|
2020-03-06 13:49:09 +00:00
|
|
|
err = h2mux.H2RequestHeadersToH1Request(stream.Headers, req)
|
2019-06-05 15:08:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "invalid request received")
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|