forked from melbahja/goph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoph_test.go
201 lines (167 loc) · 5.78 KB
/
goph_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
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
package goph_test
import (
"fmt"
"log"
"net"
"testing"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
"github.com/tareksalem/goph"
)
var privateBytes = []byte(`
# random generated pk
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEAzjjvQq3c9tl3W8DHUBlY/lMnr+BhYaRuOn1hTuF9wSNyZY0X35xr
j9E1zJ7zLJaON8foXGRCxU1SuKDN6fcK8MJBPwL8M2bPYTpun1zij6nmGTNbqOtxEkqw8U
2A9hZMonlLFvol39X4aNCVj+9tpgrK5fBel476GcSehmckQI0RQLTqopE6KFIIXZbPLaAZ
ycDDowEZeqYBy2p+u7Auy6rxj23fpOvLBQyzm/lo7HezBPfDHyz40Kw6RIaRSVr6kZGhTZ
1roAqSdzwUxVKD1g95jM/RbLnKYppwoiHmJhGn+Ze3p1LhoGx6y5QmvELU+tXHRZU5yWRd
ypos7yKsjX7TsmJCQD5x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AuthTest", gophAuthTest)
t.Run("gophWrongPassTest", gophWrongPassTest)
}
func gophAuthTest(t *testing.T) {
newServer("2020")
_, err := goph.NewConn(&goph.Config{
Addr: "127.0.10.10",
Port: 2020,
User: "melbahja",
Auth: goph.Password("123456"),
Callback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
t.Error(err)
}
}
func gophRunTest(t *testing.T) {
newServer("2021")
client, err := goph.NewConn(&goph.Config{
Addr: "127.0.10.10",
Port: 2021,
User: "melbahja",
Auth: goph.Password("123456"),
Callback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
t.Errorf("connect error: %s", err)
}
_, err = client.Run("ls")
if err != nil {
t.Errorf("run error: %s", err)
}
}
func gophWrongPassTest(t *testing.T) {
newServer("2022")
_, err := goph.NewConn(&goph.Config{
Addr: "127.0.10.10",
Port: 2022,
User: "melbahja",
Auth: goph.Password("12345"),
Callback: ssh.InsecureIgnoreHostKey(),
})
if err == nil {
t.Error("it should return an error")
}
}
func newServer(port string) {
config := &ssh.ServerConfig{
// Remove to disable password auth.
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
// a production setting.
if c.User() == "melbahja" && string(pass) == "123456" {
return nil, nil
}
return nil, fmt.Errorf("password rejected for %q", c.User())
},
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
log.Fatal("Failed to parse private key: ", err)
}
config.AddHostKey(private)
// Once a ServerConfig has been configured, connections can be
// accepted.
listener, err := net.Listen("tcp", "127.0.10.10:"+port)
if err != nil {
log.Fatal("failed to listen for connection: ", err)
}
go func() {
nConn, err := listener.Accept()
if err != nil {
log.Fatal("failed to accept incoming connection: ", err)
}
// Before use, a handshake must be performed on the incoming
// net.Conn.
_, chans, reqs, err := ssh.NewServerConn(nConn, config)
if err != nil {
log.Fatal("failed to handshake: ", err)
}
// The incoming Request channel must be serviced.
go ssh.DiscardRequests(reqs)
// Service the incoming Channel channel.
for newChannel := range chans {
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel, requests, err := newChannel.Accept()
if err != nil {
log.Fatalf("Could not accept channel: %v", err)
}
go func(in <-chan *ssh.Request) {
for req := range in {
switch req.Type {
case "exec":
// just return error 0 without exec.
channel.SendRequest("exit-status", false, []byte{0, 0, 0, 0})
}
req.Reply(req.Type == "exec", nil)
}
}(requests)
term := term.NewTerminal(channel, "> ")
go func() {
defer channel.Close()
for {
line, err := term.ReadLine()
if err != nil {
break
}
fmt.Println(line)
}
}()
}
}()
}