2020-12-09 21:46:53 +00:00
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
2022-05-18 11:11:38 +00:00
|
|
|
"fmt"
|
2020-12-09 21:46:53 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-12-09 21:46:53 +00:00
|
|
|
)
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
var (
|
2021-04-02 06:10:43 +00:00
|
|
|
errUnsupportedConnectionType = errors.New("internal error: unsupported connection type")
|
2021-02-05 13:01:53 +00:00
|
|
|
)
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
// HTTPOriginProxy can be implemented by origin services that want to proxy http requests.
|
|
|
|
type HTTPOriginProxy interface {
|
|
|
|
// RoundTrip is how cloudflared proxies eyeball requests to the actual origin services
|
|
|
|
http.RoundTripper
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
// StreamBasedOriginProxy can be implemented by origin services that want to proxy ws/TCP.
|
2020-12-09 21:46:53 +00:00
|
|
|
type StreamBasedOriginProxy interface {
|
2021-07-01 18:30:26 +00:00
|
|
|
EstablishConnection(dest string) (OriginConnection, error)
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *unixSocketPath) RoundTrip(req *http.Request) (*http.Response, error) {
|
2022-02-28 20:07:47 +00:00
|
|
|
req.URL.Scheme = o.scheme
|
2020-12-09 21:46:53 +00:00
|
|
|
return o.transport.RoundTrip(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *httpService) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
// Rewrite the request URL so that it goes to the origin service.
|
|
|
|
req.URL.Host = o.url.Host
|
2021-07-01 09:29:53 +00:00
|
|
|
switch o.url.Scheme {
|
2021-04-02 06:10:43 +00:00
|
|
|
case "ws":
|
|
|
|
req.URL.Scheme = "http"
|
|
|
|
case "wss":
|
|
|
|
req.URL.Scheme = "https"
|
2021-07-01 09:29:53 +00:00
|
|
|
default:
|
|
|
|
req.URL.Scheme = o.url.Scheme
|
2021-04-02 06:10:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 19:25:08 +00:00
|
|
|
if o.hostHeader != "" {
|
|
|
|
// For incoming requests, the Host header is promoted to the Request.Host field and removed from the Header map.
|
2021-10-22 08:27:29 +00:00
|
|
|
// Pass the original Host header as X-Forwarded-Host.
|
|
|
|
req.Header.Set("X-Forwarded-Host", req.Host)
|
2021-02-08 19:25:08 +00:00
|
|
|
req.Host = o.hostHeader
|
|
|
|
}
|
2021-07-01 09:29:53 +00:00
|
|
|
return o.transport.RoundTrip(req)
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *statusCode) RoundTrip(_ *http.Request) (*http.Response, error) {
|
2022-05-18 11:11:38 +00:00
|
|
|
resp := &http.Response{
|
|
|
|
StatusCode: o.code,
|
|
|
|
Status: fmt.Sprintf("%d %s", o.code, http.StatusText(o.code)),
|
|
|
|
Body: new(NopReadCloser),
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 18:30:26 +00:00
|
|
|
func (o *rawTCPService) EstablishConnection(dest string) (OriginConnection, error) {
|
2021-02-05 13:01:53 +00:00
|
|
|
conn, err := net.Dial("tcp", dest)
|
|
|
|
if err != nil {
|
2021-07-01 18:30:26 +00:00
|
|
|
return nil, err
|
2021-02-05 13:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
originConn := &tcpConnection{
|
|
|
|
conn: conn,
|
|
|
|
}
|
2021-07-01 18:30:26 +00:00
|
|
|
return originConn, nil
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 18:30:26 +00:00
|
|
|
func (o *tcpOverWSService) EstablishConnection(dest string) (OriginConnection, error) {
|
2021-02-05 13:01:53 +00:00
|
|
|
var err error
|
2021-07-01 18:30:26 +00:00
|
|
|
if !o.isBastion {
|
|
|
|
dest = o.dest
|
2021-02-05 13:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", dest)
|
|
|
|
if err != nil {
|
2021-07-01 18:30:26 +00:00
|
|
|
return nil, err
|
2021-02-05 13:01:53 +00:00
|
|
|
}
|
|
|
|
originConn := &tcpOverWSConnection{
|
|
|
|
conn: conn,
|
|
|
|
streamHandler: o.streamHandler,
|
|
|
|
}
|
2021-07-01 18:30:26 +00:00
|
|
|
return originConn, nil
|
2021-02-05 13:01:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-01 18:30:26 +00:00
|
|
|
func (o *socksProxyOverWSService) EstablishConnection(dest string) (OriginConnection, error) {
|
|
|
|
return o.conn, nil
|
2021-03-01 22:26:37 +00:00
|
|
|
}
|