2020-12-09 21:46:53 +00:00
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
2021-02-05 13:01:53 +00:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2020-12-09 21:46:53 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2021-03-23 14:30:43 +00:00
|
|
|
gws "github.com/gorilla/websocket"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2021-03-01 22:26:37 +00:00
|
|
|
"github.com/cloudflare/cloudflared/ipaccess"
|
|
|
|
"github.com/cloudflare/cloudflared/socks"
|
2020-12-09 21:46:53 +00:00
|
|
|
"github.com/cloudflare/cloudflared/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OriginConnection is a way to stream to a service running on the user's origin.
|
|
|
|
// Different concrete implementations will stream different protocols as long as they are io.ReadWriters.
|
|
|
|
type OriginConnection interface {
|
|
|
|
// Stream should generally be implemented as a bidirectional io.Copy.
|
2021-02-05 13:01:53 +00:00
|
|
|
Stream(ctx context.Context, tunnelConn io.ReadWriter, log *zerolog.Logger)
|
2020-12-09 21:46:53 +00:00
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:07:18 +00:00
|
|
|
type streamHandlerFunc func(originConn io.ReadWriter, remoteConn net.Conn, log *zerolog.Logger)
|
2021-01-11 19:59:45 +00:00
|
|
|
|
|
|
|
// Stream copies copy data to & from provided io.ReadWriters.
|
2021-02-04 15:07:18 +00:00
|
|
|
func Stream(conn, backendConn io.ReadWriter, log *zerolog.Logger) {
|
2021-01-11 19:59:45 +00:00
|
|
|
proxyDone := make(chan struct{}, 2)
|
|
|
|
|
|
|
|
go func() {
|
2021-02-04 15:07:18 +00:00
|
|
|
_, err := io.Copy(conn, backendConn)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Msgf("conn to backendConn copy: %v", err)
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
proxyDone <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2021-02-04 15:07:18 +00:00
|
|
|
_, err := io.Copy(backendConn, conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Msgf("backendConn to conn copy: %v", err)
|
|
|
|
}
|
2021-01-11 19:59:45 +00:00
|
|
|
proxyDone <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// If one side is done, we are done.
|
|
|
|
<-proxyDone
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultStreamHandler is an implementation of streamHandlerFunc that
|
|
|
|
// performs a two way io.Copy between originConn and remoteConn.
|
2021-02-04 15:07:18 +00:00
|
|
|
func DefaultStreamHandler(originConn io.ReadWriter, remoteConn net.Conn, log *zerolog.Logger) {
|
|
|
|
Stream(originConn, remoteConn, log)
|
2021-01-11 19:59:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 21:46:53 +00:00
|
|
|
// tcpConnection is an OriginConnection that directly streams to raw TCP.
|
|
|
|
type tcpConnection struct {
|
2021-02-05 13:01:53 +00:00
|
|
|
conn net.Conn
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
func (tc *tcpConnection) Stream(ctx context.Context, tunnelConn io.ReadWriter, log *zerolog.Logger) {
|
|
|
|
Stream(tunnelConn, tc.conn, log)
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *tcpConnection) Close() {
|
|
|
|
tc.conn.Close()
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
// tcpOverWSConnection is an OriginConnection that streams to TCP over WS.
|
|
|
|
type tcpOverWSConnection struct {
|
|
|
|
conn net.Conn
|
|
|
|
streamHandler streamHandlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *tcpOverWSConnection) Stream(ctx context.Context, tunnelConn io.ReadWriter, log *zerolog.Logger) {
|
|
|
|
wc.streamHandler(websocket.NewConn(ctx, tunnelConn, log), wc.conn, log)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *tcpOverWSConnection) Close() {
|
|
|
|
wc.conn.Close()
|
2021-02-02 18:27:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
// wsConnection is an OriginConnection that streams WS between eyeball and origin.
|
2020-12-09 21:46:53 +00:00
|
|
|
type wsConnection struct {
|
|
|
|
wsConn *gws.Conn
|
|
|
|
resp *http.Response
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
func (wsc *wsConnection) Stream(ctx context.Context, tunnelConn io.ReadWriter, log *zerolog.Logger) {
|
2021-02-04 15:07:18 +00:00
|
|
|
Stream(tunnelConn, wsc.wsConn.UnderlyingConn(), log)
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wsc *wsConnection) Close() {
|
|
|
|
wsc.resp.Body.Close()
|
|
|
|
wsc.wsConn.Close()
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:01:53 +00:00
|
|
|
func newWSConnection(clientTLSConfig *tls.Config, r *http.Request) (OriginConnection, *http.Response, error) {
|
2020-12-09 21:46:53 +00:00
|
|
|
d := &gws.Dialer{
|
2021-02-05 13:01:53 +00:00
|
|
|
TLSClientConfig: clientTLSConfig,
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
wsConn, resp, err := websocket.ClientConnect(r, d)
|
|
|
|
if err != nil {
|
2021-02-04 18:03:34 +00:00
|
|
|
return nil, nil, err
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
|
|
|
return &wsConnection{
|
|
|
|
wsConn,
|
|
|
|
resp,
|
2021-02-04 18:03:34 +00:00
|
|
|
}, resp, nil
|
2020-12-09 21:46:53 +00:00
|
|
|
}
|
2021-03-01 22:26:37 +00:00
|
|
|
|
|
|
|
// socksProxyOverWSConnection is an OriginConnection that streams SOCKS connections over WS.
|
|
|
|
// The connection to the origin happens inside the SOCKS code as the client specifies the origin
|
|
|
|
// details in the packet.
|
|
|
|
type socksProxyOverWSConnection struct {
|
|
|
|
accessPolicy *ipaccess.Policy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *socksProxyOverWSConnection) Stream(ctx context.Context, tunnelConn io.ReadWriter, log *zerolog.Logger) {
|
|
|
|
socks.StreamNetHandler(websocket.NewConn(ctx, tunnelConn, log), sp.accessPolicy, log)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *socksProxyOverWSConnection) Close() {
|
|
|
|
}
|