-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseproxy_test.go
160 lines (140 loc) · 3.91 KB
/
reverseproxy_test.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
package sshproxy
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"log"
"net"
"sync"
"testing"
"time"
"golang.org/x/crypto/ssh"
)
var (
addr = flag.String("ssh-addr", "", "specify a target address to dial SSH")
user = flag.String("ssh-user", "", "specify the SSH user with which to dial")
password = flag.String("ssh-passwd", "", "specify the password with which to dial")
)
func Test_reverseProxy(t *testing.T) {
t.Parallel()
if *addr == "" || *user == "" || *password == "" {
t.Fatalf("-ssh-addr, -ssh-user, and -ssh-passwd are all required flags")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
left, right, err := tcpPipeWithDialer(net.Dial, net.Listen)
if err != nil {
t.Fatalf("new net pipe: %v", err)
}
clientConfig := &ssh.ClientConfig{
User: *user,
Auth: []ssh.AuthMethod{ssh.Password(*password)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 3 * time.Second,
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
defer left.Close()
clientSSHConn, clientChans, clientReqs, err := ssh.NewClientConn(left, "localhost", clientConfig)
if err != nil {
t.Errorf("new client conn: %v", err)
}
client := ssh.NewClient(clientSSHConn, clientChans, clientReqs)
testSSHClient(t, client)
}()
serverConfig := &ssh.ServerConfig{
NoClientAuth: true,
}
signer, err := generateSigner()
if err != nil {
t.Fatalf("generate signer: %v", err)
}
serverConfig.AddHostKey(signer)
serverConn, serverChans, serverReqs, err := ssh.NewServerConn(right, serverConfig)
if err != nil {
t.Fatalf("accept server conn: %v", err)
}
proxy := New(*addr, clientConfig)
proxy.ErrorLog = log.Default()
err = proxy.Serve(ctx, serverConn, serverChans, serverReqs)
if err == nil {
t.Fatalf("expected error from reverse proxy, got: %v", err)
}
wg.Wait()
}
func Test_dialFailure(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
clientConfig := &ssh.ClientConfig{
User: *user,
Auth: []ssh.AuthMethod{ssh.Password(*password)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 3 * time.Second,
}
proxy := New("/tmp/sshproxy-null.sock", clientConfig)
err := proxy.Serve(ctx, nil, nil, nil)
if err == nil {
t.Fatalf("expected error from reverse proxy, got: %v", err)
}
if err.Error() != "dial reverse proxy target: dial tcp: address /tmp/sshproxy-null.sock: missing port in address" {
t.Fatalf("unexpected error, got: %v", err)
}
}
func Test_serverConnFailure(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
clientConfig := &ssh.ClientConfig{
User: *user,
Auth: []ssh.AuthMethod{ssh.Password(*password)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 3 * time.Second,
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}
go func() {
conn, err := listener.Accept()
if err != nil {
t.Errorf("unexpected error, got: %v", err)
return
}
err = conn.Close()
if err != nil {
t.Errorf("unexpected error, got: %v", err)
}
}()
proxy := New(listener.Addr().String(), clientConfig)
err = proxy.Serve(ctx, nil, nil, nil)
if err == nil {
t.Fatalf("expected error from reverse proxy, got: %v", err)
}
}
func generateSigner() (ssh.Signer, error) {
const blockType = "EC PRIVATE KEY"
pkey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, fmt.Errorf("generate rsa private key: %w", err)
}
byt, err := x509.MarshalECPrivateKey(pkey)
if err != nil {
return nil, fmt.Errorf("marshal private key: %w", err)
}
pb := pem.Block{
Type: blockType,
Headers: nil,
Bytes: byt,
}
p, err := ssh.ParsePrivateKey(pem.EncodeToMemory(&pb))
if err != nil {
return nil, err
}
return p, nil
}