-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseproxy.go
228 lines (196 loc) · 6.73 KB
/
reverseproxy.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package sshproxy
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"golang.org/x/crypto/ssh"
)
// ReverseProxy is an SSH Handler that takes an incoming request and sends it
// to another server, proxying the response back to the client.
type ReverseProxy struct {
TargetAddress string
TargetClientConfig *ssh.ClientConfig
// ErrorLog specifies an optional logger for errors
// that occur when attempting to proxy.
// If nil, logging is done via the log package's standard logger.
ErrorLog *log.Logger
}
// New constructs a new *ReverseProxy instance.
func New(targetAddr string, clientConfig *ssh.ClientConfig) *ReverseProxy {
return &ReverseProxy{
TargetAddress: targetAddr,
TargetClientConfig: clientConfig,
}
}
// Serve executes the reverse proxy between the specified target client and the server connection.
func (r *ReverseProxy) Serve(ctx context.Context, serverConn *ssh.ServerConn, serverChans <-chan ssh.NewChannel, serverReqs <-chan *ssh.Request) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var logger logger = defaultLogger{}
if r.ErrorLog != nil {
logger = r.ErrorLog
}
// TODO: do we need to make "network" an argument?
targetConn, err := net.DialTimeout("tcp", r.TargetAddress, r.TargetClientConfig.Timeout)
if err != nil {
return fmt.Errorf("dial reverse proxy target: %w", err)
}
defer targetConn.Close()
destConn, destChans, destReqs, err := ssh.NewClientConn(targetConn, r.TargetAddress, r.TargetClientConfig)
if err != nil {
return fmt.Errorf("new ssh client conn: %w", err)
}
shutdownErr := make(chan error, 1)
go func() {
shutdownErr <- serverConn.Conn.Wait()
}()
go processChannels(ctx, destConn, serverChans, logger)
go processChannels(ctx, serverConn.Conn, destChans, logger)
go processRequests(ctx, destConn, serverReqs, logger)
go processRequests(ctx, serverConn.Conn, destReqs, logger)
select {
case <-ctx.Done():
return ctx.Err()
case err := <-shutdownErr:
return err
}
}
type defaultLogger struct{}
// wrap the default logger
func (defaultLogger) Printf(format string, v ...any) { log.Printf(format, v...) }
type logger interface {
Printf(format string, v ...any)
}
// processChannels handles each ssh.NewChannel concurrently.
func processChannels(ctx context.Context, destConn ssh.Conn, chans <-chan ssh.NewChannel, logger logger) {
defer destConn.Close()
for newCh := range chans {
// reset the var scope for each goroutine
newCh := newCh
go func() {
err := handleChannel(ctx, destConn, newCh, logger)
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, context.Canceled) {
logger.Printf("sshproxy: ReverseProxy handle channel error: %v", err)
}
}()
}
}
// processRequests handles each *ssh.Request in series.
func processRequests(ctx context.Context, dest requestDest, requests <-chan *ssh.Request, logger logger) {
for req := range requests {
err := handleRequest(ctx, dest, req)
if err != nil && !errors.Is(err, io.EOF) {
logger.Printf("sshproxy: ReverseProxy handle request error: %v", err)
}
}
}
// handleChannel performs the bicopy between the destination SSH connection and a
// new incoming channel.
func handleChannel(ctx context.Context, destConn ssh.Conn, newChannel ssh.NewChannel, logger logger) error {
destCh, destReqs, err := destConn.OpenChannel(newChannel.ChannelType(), newChannel.ExtraData())
if err != nil {
if openChanErr, ok := err.(*ssh.OpenChannelError); ok {
_ = newChannel.Reject(openChanErr.Reason, openChanErr.Message)
} else {
_ = newChannel.Reject(ssh.ConnectionFailed, err.Error())
}
return fmt.Errorf("open channel: %w", err)
}
defer destCh.Close()
originCh, originRequests, err := newChannel.Accept()
if err != nil {
return fmt.Errorf("accept new channel: %w", err)
}
defer originCh.Close()
destRequestsDone := make(chan struct{})
go func() {
defer close(destRequestsDone)
processRequests(ctx, channelRequestDest{originCh}, destReqs, logger)
}()
// This request channel does not get closed
// by the client causing this function to hang if we wait on it.
go processRequests(ctx, channelRequestDest{destCh}, originRequests, logger)
if err := bicopy(ctx, originCh, destCh, logger); err != nil {
return fmt.Errorf("channel bidirectional copy: %w", err)
}
select {
case <-destRequestsDone:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
// bicopy copies data between the two channels,
// but does not perform complete closure.
// It will block until the context is cancelled or the `alpha` channel
// has completed writing its data. Writes from the `beta` channel are not
// waited on.
func bicopy(ctx context.Context, alpha, beta ssh.Channel, logger logger) error {
alphaWriteDone := make(chan struct{})
go func() {
defer close(alphaWriteDone)
copyChannels(alpha, beta, logger)
}()
go copyChannels(beta, alpha, logger)
select {
case <-alphaWriteDone:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
// copyChannels pipes data from the writer to the reader channel, calling
// w.CloseWrite when writes have completed. This operation blocks until
// both the stderr and primary copy streams exit. Non EOF errors are logged
// to the given logger.
func copyChannels(w, r ssh.Channel, logger logger) {
defer func() { _ = w.CloseWrite() }()
copyDone := make(chan struct{})
go func() {
defer close(copyDone)
_, err := io.Copy(w, r)
if err != nil && !errors.Is(err, io.EOF) {
logger.Printf("sshproxy: bicopy channel: %v", err)
}
}()
_, err := io.Copy(w.Stderr(), r.Stderr())
if err != nil && !errors.Is(err, io.EOF) {
logger.Printf("sshproxy: bicopy channel: %v", err)
}
<-copyDone
}
// channelRequestDest wraps the ssh.Channel type to conform with the standard
// SendRequest function signiture. This allows for convenient code re-use in
// piping channel-level requests as well as global, connection-level
// requests.
type channelRequestDest struct {
ssh.Channel
}
func (c channelRequestDest) SendRequest(name string, wantReply bool, payload []byte) (bool, []byte, error) {
ok, err := c.Channel.SendRequest(name, wantReply, payload)
return ok, nil, err
}
// requestDest defines a resource capable of receiving requests, (global or channel).
type requestDest interface {
SendRequest(name string, wantReply bool, payload []byte) (bool, []byte, error)
}
func handleRequest(ctx context.Context, dest requestDest, request *ssh.Request) error {
ok, payload, err := dest.SendRequest(request.Type, request.WantReply, request.Payload)
if err != nil {
if request.WantReply {
if err := request.Reply(ok, payload); err != nil {
return fmt.Errorf("reply after send failure: %w", err)
}
}
return fmt.Errorf("send request: %w", err)
}
if request.WantReply {
if err := request.Reply(ok, payload); err != nil {
return fmt.Errorf("reply: %w", err)
}
}
return nil
}