2019-06-05 15:08:55 +00:00
|
|
|
package streamhandler
|
|
|
|
|
|
|
|
import (
|
2019-06-18 16:47:29 +00:00
|
|
|
"context"
|
2019-06-05 15:08:55 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-06-21 00:05:39 +00:00
|
|
|
"strconv"
|
2019-06-05 15:08:55 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/h2mux"
|
|
|
|
"github.com/cloudflare/cloudflared/tunnelhostnamemapper"
|
2019-06-18 16:47:29 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc"
|
2019-06-05 15:08:55 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2019-06-21 00:05:39 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-06-05 15:08:55 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-06-18 16:47:29 +00:00
|
|
|
"zombiezen.com/go/capnproto2/rpc"
|
2019-06-05 15:08:55 +00:00
|
|
|
)
|
|
|
|
|
2019-06-21 00:05:39 +00:00
|
|
|
const (
|
|
|
|
statusPseudoHeader = ":status"
|
|
|
|
)
|
|
|
|
|
|
|
|
type httpErrorStatus struct {
|
|
|
|
status string
|
|
|
|
text []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
statusBadRequest = newHTTPErrorStatus(http.StatusBadRequest)
|
|
|
|
statusNotFound = newHTTPErrorStatus(http.StatusNotFound)
|
|
|
|
statusBadGateway = newHTTPErrorStatus(http.StatusBadGateway)
|
|
|
|
)
|
|
|
|
|
|
|
|
func newHTTPErrorStatus(status int) *httpErrorStatus {
|
|
|
|
return &httpErrorStatus{
|
|
|
|
status: strconv.Itoa(status),
|
|
|
|
text: []byte(http.StatusText(status)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:08:55 +00:00
|
|
|
// StreamHandler handles new stream opened by the edge. The streams can be used to proxy requests or make RPC.
|
|
|
|
type StreamHandler struct {
|
|
|
|
// newConfigChan is a send-only channel to notify Supervisor of a new ClientConfig
|
|
|
|
newConfigChan chan<- *pogs.ClientConfig
|
|
|
|
// useConfigResultChan is a receive-only channel for Supervisor to communicate the result of applying a new ClientConfig
|
|
|
|
useConfigResultChan <-chan *pogs.UseConfigurationResult
|
|
|
|
// originMapper maps tunnel hostname to origin service
|
|
|
|
tunnelHostnameMapper *tunnelhostnamemapper.TunnelHostnameMapper
|
|
|
|
logger *logrus.Entry
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStreamHandler creates a new StreamHandler
|
|
|
|
func NewStreamHandler(newConfigChan chan<- *pogs.ClientConfig,
|
|
|
|
useConfigResultChan <-chan *pogs.UseConfigurationResult,
|
|
|
|
logger *logrus.Logger,
|
|
|
|
) *StreamHandler {
|
|
|
|
return &StreamHandler{
|
|
|
|
newConfigChan: newConfigChan,
|
|
|
|
useConfigResultChan: useConfigResultChan,
|
|
|
|
tunnelHostnameMapper: tunnelhostnamemapper.NewTunnelHostnameMapper(),
|
|
|
|
logger: logger.WithField("subsystem", "streamHandler"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 16:47:29 +00:00
|
|
|
// UseConfiguration implements ClientService
|
|
|
|
func (s *StreamHandler) UseConfiguration(ctx context.Context, config *pogs.ClientConfig) (*pogs.UseConfigurationResult, error) {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err := fmt.Errorf("Timeout while sending new config to Supervisor")
|
|
|
|
s.logger.Error(err)
|
|
|
|
return nil, err
|
|
|
|
case s.newConfigChan <- config:
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err := fmt.Errorf("Timeout applying new configuration")
|
|
|
|
s.logger.Error(err)
|
|
|
|
return nil, err
|
|
|
|
case result := <-s.useConfigResultChan:
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateConfig replaces current originmapper mapping with mappings from newConfig
|
|
|
|
func (s *StreamHandler) UpdateConfig(newConfig []*pogs.ReverseProxyConfig) (failedConfigs []*pogs.FailedConfig) {
|
|
|
|
// TODO: TUN-1968: Gracefully apply new config
|
|
|
|
s.tunnelHostnameMapper.DeleteAll()
|
|
|
|
for _, tunnelConfig := range newConfig {
|
|
|
|
tunnelHostname := tunnelConfig.TunnelHostname
|
2019-07-31 19:01:23 +00:00
|
|
|
originSerice, err := tunnelConfig.OriginConfigUnmarshaler.OriginConfig.Service()
|
2019-06-18 16:47:29 +00:00
|
|
|
if err != nil {
|
|
|
|
s.logger.WithField("tunnelHostname", tunnelHostname).WithError(err).Error("Invalid origin service config")
|
|
|
|
failedConfigs = append(failedConfigs, &pogs.FailedConfig{
|
|
|
|
Config: tunnelConfig,
|
|
|
|
Reason: tunnelConfig.FailReason(err),
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.tunnelHostnameMapper.Add(tunnelConfig.TunnelHostname, originSerice)
|
|
|
|
s.logger.WithField("tunnelHostname", tunnelHostname).Infof("New origin service config: %v", originSerice.Summary())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:08:55 +00:00
|
|
|
// ServeStream implements MuxedStreamHandler interface
|
|
|
|
func (s *StreamHandler) ServeStream(stream *h2mux.MuxedStream) error {
|
|
|
|
if stream.IsRPCStream() {
|
2019-06-18 16:47:29 +00:00
|
|
|
return s.serveRPC(stream)
|
2019-06-05 15:08:55 +00:00
|
|
|
}
|
2019-06-21 00:05:39 +00:00
|
|
|
if err := s.serveRequest(stream); err != nil {
|
|
|
|
s.logger.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-06-05 15:08:55 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 16:47:29 +00:00
|
|
|
func (s *StreamHandler) serveRPC(stream *h2mux.MuxedStream) error {
|
|
|
|
stream.WriteHeaders([]h2mux.Header{{Name: ":status", Value: "200"}})
|
|
|
|
main := pogs.ClientService_ServerToClient(s)
|
|
|
|
rpcLogger := s.logger.WithField("subsystem", "clientserver-rpc")
|
|
|
|
rpcConn := rpc.NewConn(
|
|
|
|
tunnelrpc.NewTransportLogger(rpcLogger, rpc.StreamTransport(stream)),
|
|
|
|
rpc.MainInterface(main.Client),
|
|
|
|
tunnelrpc.ConnLog(s.logger.WithField("subsystem", "clientserver-rpc-transport")),
|
|
|
|
)
|
|
|
|
return rpcConn.Wait()
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:08:55 +00:00
|
|
|
func (s *StreamHandler) serveRequest(stream *h2mux.MuxedStream) error {
|
|
|
|
tunnelHostname := stream.TunnelHostname()
|
|
|
|
if !tunnelHostname.IsSet() {
|
2019-06-21 00:05:39 +00:00
|
|
|
s.writeErrorStatus(stream, statusBadRequest)
|
|
|
|
return fmt.Errorf("stream doesn't have tunnelHostname")
|
2019-06-05 15:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
originService, ok := s.tunnelHostnameMapper.Get(tunnelHostname)
|
|
|
|
if !ok {
|
2019-06-21 00:05:39 +00:00
|
|
|
s.writeErrorStatus(stream, statusNotFound)
|
|
|
|
return fmt.Errorf("cannot map tunnel hostname %s to origin", tunnelHostname)
|
2019-06-05 15:08:55 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 16:18:59 +00:00
|
|
|
req, err := createRequest(stream, originService.URL())
|
2019-06-05 15:08:55 +00:00
|
|
|
if err != nil {
|
2019-06-21 00:05:39 +00:00
|
|
|
s.writeErrorStatus(stream, statusBadRequest)
|
|
|
|
return errors.Wrap(err, "cannot create request")
|
2019-06-05 15:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger := s.requestLogger(req, tunnelHostname)
|
|
|
|
logger.Debugf("Request Headers %+v", req.Header)
|
|
|
|
|
|
|
|
resp, err := originService.Proxy(stream, req)
|
|
|
|
if err != nil {
|
2019-06-21 00:05:39 +00:00
|
|
|
s.writeErrorStatus(stream, statusBadGateway)
|
|
|
|
return errors.Wrap(err, "cannot proxy request")
|
2019-06-05 15:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.WithField("status", resp.Status).Debugf("Response Headers %+v", resp.Header)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StreamHandler) requestLogger(req *http.Request, tunnelHostname h2mux.TunnelHostname) *logrus.Entry {
|
|
|
|
cfRay := FindCfRayHeader(req)
|
|
|
|
lbProbe := IsLBProbeRequest(req)
|
|
|
|
logger := s.logger.WithField("tunnelHostname", tunnelHostname)
|
|
|
|
if cfRay != "" {
|
|
|
|
logger = logger.WithField("CF-RAY", cfRay)
|
|
|
|
logger.Debugf("%s %s %s", req.Method, req.URL, req.Proto)
|
|
|
|
} else if lbProbe {
|
|
|
|
logger.Debugf("Load Balancer health check %s %s %s", req.Method, req.URL, req.Proto)
|
|
|
|
} else {
|
|
|
|
logger.Warnf("Requests %v does not have CF-RAY header. Please open a support ticket with Cloudflare.", req)
|
|
|
|
}
|
|
|
|
return logger
|
|
|
|
}
|
2019-06-21 00:05:39 +00:00
|
|
|
|
|
|
|
func (s *StreamHandler) writeErrorStatus(stream *h2mux.MuxedStream, status *httpErrorStatus) {
|
|
|
|
stream.WriteHeaders([]h2mux.Header{
|
|
|
|
{
|
|
|
|
Name: statusPseudoHeader,
|
|
|
|
Value: status.status,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
stream.Write(status.text)
|
|
|
|
}
|