...
1
2
3
4
5 package ssh
6
7 import (
8 "fmt"
9 "net"
10 )
11
12
13
14 type OpenChannelError struct {
15 Reason RejectionReason
16 Message string
17 }
18
19 func (e *OpenChannelError) Error() string {
20 return fmt.Sprintf("ssh: rejected: %s (%s)", e.Reason, e.Message)
21 }
22
23
24 type ConnMetadata interface {
25
26 User() string
27
28
29 SessionID() []byte
30
31
32
33 ClientVersion() []byte
34
35
36
37 ServerVersion() []byte
38
39
40 RemoteAddr() net.Addr
41
42
43 LocalAddr() net.Addr
44 }
45
46
47
48
49
50 type Conn interface {
51 ConnMetadata
52
53
54
55
56 SendRequest(name string, wantReply bool, payload []byte) (bool, []byte, error)
57
58
59
60
61
62
63 OpenChannel(name string, data []byte) (Channel, <-chan *Request, error)
64
65
66 Close() error
67
68
69
70 Wait() error
71
72
73
74
75 }
76
77
78
79 func DiscardRequests(in <-chan *Request) {
80 for req := range in {
81 if req.WantReply {
82 req.Reply(false, nil)
83 }
84 }
85 }
86
87
88 type connection struct {
89 transport *handshakeTransport
90 sshConn
91
92
93 *mux
94 }
95
96 func (c *connection) Close() error {
97 return c.sshConn.conn.Close()
98 }
99
100
101
102 type sshConn struct {
103 conn net.Conn
104
105 user string
106 sessionID []byte
107 clientVersion []byte
108 serverVersion []byte
109 }
110
111 func dup(src []byte) []byte {
112 dst := make([]byte, len(src))
113 copy(dst, src)
114 return dst
115 }
116
117 func (c *sshConn) User() string {
118 return c.user
119 }
120
121 func (c *sshConn) RemoteAddr() net.Addr {
122 return c.conn.RemoteAddr()
123 }
124
125 func (c *sshConn) Close() error {
126 return c.conn.Close()
127 }
128
129 func (c *sshConn) LocalAddr() net.Addr {
130 return c.conn.LocalAddr()
131 }
132
133 func (c *sshConn) SessionID() []byte {
134 return dup(c.sessionID)
135 }
136
137 func (c *sshConn) ClientVersion() []byte {
138 return dup(c.clientVersion)
139 }
140
141 func (c *sshConn) ServerVersion() []byte {
142 return dup(c.serverVersion)
143 }
144
View as plain text