-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
81 lines (65 loc) · 1.31 KB
/
client.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
package ssh
import (
"errors"
"io"
"sync"
)
type ClientOption struct {
User string
CheckHostKey func([]byte) error
GetPassword func() (string, error)
}
type Client struct {
ClientChannels
*transport
actions chan func()
wait sync.WaitGroup
reading bool
}
func NewClient(conn io.ReadWriteCloser, option *ClientOption) (*Client, error) {
s, err := newTransport(conn)
if err != nil {
return nil, err
}
client := &Client{transport: s}
client.startLoop()
done := make(chan int, 1)
ok := false
client.do(func() {
defer func() {
done<- 0
}()
Log(20, "a")
s.writeKexInit()
b := <-client.packets
k, err := s.parseKexinit(b)
Log(20, "%v", k)
if err != nil {
return
}
err = s.dh(k, option.CheckHostKey)
if err != nil {
return
}
for option.GetPassword != nil {
password, err := option.GetPassword()
if err != nil {
return
}
if client.auth(option.User, password) {
break
}
}
Log(20, "b")
ok = true
})
<-done
if !ok {
return nil, errors.New("failed")
}
return client, nil
}
func (c *Client) Close() {
c.do(nil)
c.wait.Wait()
}