-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdestinations.go
More file actions
187 lines (166 loc) · 5.32 KB
/
destinations.go
File metadata and controls
187 lines (166 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package connet
import (
"context"
"crypto/tls"
"crypto/x509"
"log/slog"
"net"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/connet-dev/connet/pkg/netc"
"github.com/connet-dev/connet/pkg/slogc"
)
// DestinationTCP creates a new destination which connects to a downstream TCP server
func (c *Client) DestinationTCP(ctx context.Context, cfg DestinationConfig, addr string) error {
dst, err := c.Destination(ctx, cfg)
if err != nil {
return err
}
go func() {
dstSrv := NewTCPDestination(dst, addr, cfg.DialTimeout, c.logger)
if err := dstSrv.Run(ctx); err != nil {
c.logger.Info("shutting down destination tcp", "err", err)
}
}()
return nil
}
// DestinationTLS creates a new destination which connects to a downstream TLS server
func (c *Client) DestinationTLS(ctx context.Context, cfg DestinationConfig, addr string, cas *x509.CertPool) error {
dst, err := c.Destination(ctx, cfg)
if err != nil {
return err
}
go func() {
dstSrv := NewTLSDestination(dst, addr, &tls.Config{RootCAs: cas}, cfg.DialTimeout, c.logger)
if err := dstSrv.Run(ctx); err != nil {
c.logger.Info("shutting down destination tls", "err", err)
}
}()
return nil
}
// DestinationHTTP creates a new destination which exposes an HTTP server for a given [http.Handler]
func (c *Client) DestinationHTTP(ctx context.Context, cfg DestinationConfig, handler http.Handler) error {
dst, err := c.Destination(ctx, cfg)
if err != nil {
return err
}
go func() {
dstSrv := NewHTTPDestination(dst, handler)
if err := dstSrv.Run(ctx); err != nil {
c.logger.Info("shutting down destination http", "err", err)
}
}()
return nil
}
// DestinationHTTPProxy creates a new destination which exposes an HTTP proxy server to another HTTP server
func (c *Client) DestinationHTTPProxy(ctx context.Context, cfg DestinationConfig, dstUrl *url.URL) error {
dst, err := c.Destination(ctx, cfg)
if err != nil {
return err
}
go func() {
dstSrv := NewHTTPProxyDestination(dst, dstUrl, nil, cfg.DialTimeout)
if err := dstSrv.Run(ctx); err != nil {
c.logger.Info("shutting down destination http", "err", err)
}
}()
return nil
}
// DestinationHTTPSProxy creates a new destination which exposes an HTTP proxy server to another HTTPS server
func (c *Client) DestinationHTTPSProxy(ctx context.Context, cfg DestinationConfig, dstUrl *url.URL, cas *x509.CertPool) error {
dst, err := c.Destination(ctx, cfg)
if err != nil {
return err
}
go func() {
dstSrv := NewHTTPProxyDestination(dst, dstUrl, &tls.Config{RootCAs: cas}, cfg.DialTimeout)
if err := dstSrv.Run(ctx); err != nil {
c.logger.Info("shutting down destination http", "err", err)
}
}()
return nil
}
type dialer interface {
DialContext(ctx context.Context, network, address string) (net.Conn, error)
}
type TCPDestination struct {
dst *Destination
dialer dialer
addr string
logger *slog.Logger
}
func newTCPDestination(dst *Destination, d dialer, addr string, logger *slog.Logger) *TCPDestination {
return &TCPDestination{
dst: dst,
addr: addr,
dialer: d,
logger: logger.With("destination", dst.Config().Endpoint, "addr", addr),
}
}
func NewTCPDestination(dst *Destination, addr string, timeout time.Duration, logger *slog.Logger) *TCPDestination {
return newTCPDestination(dst, &net.Dialer{Timeout: timeout}, addr, logger)
}
func NewTLSDestination(dst *Destination, addr string, cfg *tls.Config, timeout time.Duration, logger *slog.Logger) *TCPDestination {
return newTCPDestination(dst, &tls.Dialer{NetDialer: &net.Dialer{Timeout: timeout}, Config: cfg}, addr, logger)
}
func (d *TCPDestination) Run(ctx context.Context) error {
return (&netc.Joiner{
Accept: func(ctx context.Context) (net.Conn, error) {
conn, err := d.dst.AcceptContext(ctx)
d.logger.Debug("destination accept", "err", err)
return conn, err
},
Dial: func(ctx context.Context) (net.Conn, error) {
conn, err := d.dialer.DialContext(ctx, "tcp", d.addr)
d.logger.Debug("destination dial", "err", err)
return conn, err
},
Join: func(ctx context.Context, acceptConn, dialConn net.Conn) {
err := netc.Join(acceptConn, dialConn)
d.logger.Debug("destination disconnected", "err", err)
},
}).Run(ctx)
}
type HTTPDestination struct {
dst *Destination
handler http.Handler
}
func NewHTTPDestination(dst *Destination, handler http.Handler) *HTTPDestination {
return &HTTPDestination{dst, handler}
}
func NewHTTPFileDestination(dst *Destination, root string) *HTTPDestination {
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir(root)))
return NewHTTPDestination(dst, mux)
}
func NewHTTPProxyDestination(dst *Destination, dstURL *url.URL, cfg *tls.Config, timeout time.Duration) *HTTPDestination {
return NewHTTPDestination(dst, &httputil.ReverseProxy{
Rewrite: func(pr *httputil.ProxyRequest) {
pr.SetURL(dstURL)
pr.SetXForwarded()
},
Transport: &http.Transport{
TLSClientConfig: cfg,
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: 30 * time.Second,
}).DialContext,
},
})
}
func (d *HTTPDestination) Run(ctx context.Context) error {
srv := &http.Server{
Handler: d.handler,
ReadHeaderTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
}
go func() {
<-ctx.Done()
if err := srv.Close(); err != nil {
slogc.FineDefault("error closing destination http server", "err", err)
}
}()
return srv.Serve(d.dst)
}