-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
181 lines (163 loc) · 4.31 KB
/
mux.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
package controlmaster
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
const (
mux_MSG_HELLO = 0x00000001
mux_C_PROXY = 0x1000000f
mux_S_PROXY = 0x8000000f
mux_S_FAILURE = 0x80000003
)
// handshakeControlProxy attempts to establish a transport connection with an
// ssh ControlMaster socket in proxy mode. For details see:
// https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.mux
func handshakeControlProxy(rw io.ReadWriteCloser) (*transport, error) {
b := &controlBuffer{}
if err := b.WriteUint32(mux_MSG_HELLO); err != nil {
return nil, err
}
// Protocol Version
if err := b.WriteUint32(4); err != nil {
return nil, err
}
tmp, err := b.LengthPrefixedBytes()
if err != nil {
return nil, err
}
if _, err := rw.Write(tmp); err != nil {
return nil, fmt.Errorf("mux hello write failed: %v", err)
}
b.Reset()
if err := b.WriteUint32(mux_C_PROXY); err != nil {
return nil, err
}
// Request ID
if err := b.WriteUint32(0); err != nil {
return nil, err
}
tmp, err = b.LengthPrefixedBytes()
if err != nil {
return nil, err
}
if _, err := rw.Write(tmp); err != nil {
return nil, fmt.Errorf("mux client proxy write failed: %v", err)
}
r := controlReader{rw}
m, err := r.Next()
if err != nil {
return nil, fmt.Errorf("mux hello read failed: %v", err)
}
if m.messageType != mux_MSG_HELLO {
return nil, fmt.Errorf("mux reply not hello")
}
if v, err := m.ReadUint32(); err != nil || v != 4 {
return nil, fmt.Errorf("mux reply hello has bad protocol version")
}
m, err = r.Next()
if err != nil {
return nil, fmt.Errorf("error reading mux server proxy: %v", err)
}
if m.messageType != mux_S_PROXY {
return nil, fmt.Errorf("expected server proxy response got %d", m.messageType)
}
return &transport{rw}, nil
}
// transport implements the ssh.Transport interface for
// ControlMaster connections. Each controlMessage has zero length padding and
// no MAC.
type transport struct {
rw io.ReadWriteCloser
}
func (p *transport) Close() error {
return p.rw.Close()
}
func (p *transport) getSessionID() []byte {
return nil
}
func (p *transport) ReadPacket() ([]byte, error) {
var l uint32
err := binary.Read(p.rw, binary.BigEndian, &l)
if err == nil {
buf := &bytes.Buffer{}
_, err = io.CopyN(buf, p.rw, int64(l))
if err == nil {
// Discard the padding byte.
buf.ReadByte()
return buf.Bytes(), nil
}
}
return nil, err
}
func (p *transport) WritePacket(controlMessage []byte) error {
l := uint32(len(controlMessage)) + 1
b := &bytes.Buffer{}
binary.Write(b, binary.BigEndian, &l) // controlMessage Length.
b.WriteByte(0) // Padding Length.
b.Write(controlMessage)
_, err := p.rw.Write(b.Bytes())
return err
}
type controlBuffer struct {
bytes.Buffer
}
func (b *controlBuffer) WriteUint32(i uint32) error {
return binary.Write(b, binary.BigEndian, i)
}
func (b *controlBuffer) LengthPrefixedBytes() ([]byte, error) {
b2 := &bytes.Buffer{}
if err := binary.Write(b2, binary.BigEndian, uint32(b.Len())); err != nil {
return nil, err
}
if _, err := b2.Write(b.Bytes()); err != nil {
return nil, err
}
return b2.Bytes(), nil
}
type controlMessage struct {
body bytes.Buffer
messageType uint32
}
func (p controlMessage) ReadUint32() (uint32, error) {
var u uint32
err := binary.Read(&p.body, binary.BigEndian, &u)
return u, err
}
func (p controlMessage) ReadString() (string, error) {
var l uint32
err := binary.Read(&p.body, binary.BigEndian, &l)
if err != nil {
return "", fmt.Errorf("error reading string length: %v", err)
}
b := p.body.Next(int(l))
if len(b) != int(l) {
return string(b), fmt.Errorf("EOF on string read")
}
return string(b), nil
}
type controlReader struct {
r io.Reader
}
func (r controlReader) Next() (*controlMessage, error) {
p := &controlMessage{}
var len uint32
err := binary.Read(r.r, binary.BigEndian, &len)
if err != nil {
return nil, fmt.Errorf("error reading message length: %v", err)
}
_, err = io.CopyN(&p.body, r.r, int64(len))
if err != nil {
return nil, fmt.Errorf("error reading message payload: %v", err)
}
err = binary.Read(&p.body, binary.BigEndian, &p.messageType)
if err != nil {
return nil, fmt.Errorf("error reading message type: %v", err)
}
if p.messageType == mux_S_FAILURE {
reason, _ := p.ReadString()
return nil, fmt.Errorf("server failure: '%s'", reason)
}
return p, nil
}