-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
188 lines (159 loc) · 3.72 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
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
package bash
import (
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"math/rand"
"net"
"net/netip"
"strings"
"sync"
"time"
"github.com/lainio/err2"
"github.com/lainio/err2/try"
)
type Client struct {
ID string
Endpoint string
Conn net.Conn
Timeout time.Duration
nextStreamID uint32
streamHooks map[uint32]*StreamHook
locker *sync.RWMutex
}
var _ Session = (*Client)(nil)
func NewClient(conn net.Conn) *Client {
ap := netip.MustParseAddrPort(conn.LocalAddr().String())
return &Client{
Endpoint: fmt.Sprintf("%s/%d", ap.Addr(), ap.Port()),
Conn: conn,
Timeout: 2 * time.Second,
streamHooks: make(map[uint32]*StreamHook),
locker: &sync.RWMutex{},
}
}
func (c *Client) Close() (err error) {
return c.Conn.Close()
}
func (c *Client) Start(cmd string) (stream io.ReadWriteCloser, err error) {
var hdr Header
id := c.genID()
hdr.encode(id, 0)
hook := &StreamHook{
Header: hdr,
StreamCh: make(chan io.ReadWriteCloser),
}
func() {
c.locker.Lock()
defer c.locker.Unlock()
c.streamHooks[id] = hook
}()
time.AfterFunc(c.Timeout, func() {
c.locker.Lock()
defer c.locker.Unlock()
if !hook.handled {
close(hook.StreamCh)
}
c.streamHooks[id] = nil
})
c.exec(hdr, cmd)
stream, ok := <-hook.StreamCh
if !ok {
return nil, fmt.Errorf("exec cmd failed. cmd: %s. %w", cmd, ErrStreamOpenTimeout)
}
return stream, nil
}
var (
ErrStreamOpenTimeout = fmt.Errorf("open stream timeout")
)
type StreamHook struct {
Header
StreamCh chan io.ReadWriteCloser
handled bool
}
func (c *Client) genID() uint32 {
c.locker.RLock()
defer c.locker.RUnlock()
for {
id := c.nextStreamID
c.nextStreamID++
if stream, ok := c.streamHooks[id]; !ok || stream == nil {
return id
}
}
}
func (c *Client) Run(cmd string) (result []byte, err error) {
defer err2.Handle(&err)
stream := try.To1(c.Start(cmd))
defer stream.Close()
result = try.To1(io.ReadAll(stream))
return
}
func (c *Client) exec(hdr Header, cmd string) {
uniqueID := c.ID + hdr.String()
f := strings.Join([]string{
fmt.Sprintf("1>/dev/tcp/%s", c.Endpoint),
fmt.Sprintf("0>&1"),
fmt.Sprintf("4> >(echo %s) 4>&1", uniqueID),
}, " ")
cmd = fmt.Sprintf(" %s 1> >(0>&1 %s) &", f, cmd)
fmt.Fprintln(c.Conn, cmd)
}
type Header [12]byte
const headerSize = int64(cap(Header{}) * 2)
func (h *Header) String() string {
return hex.EncodeToString(h[:])
}
func (h *Header) Version() uint8 {
return h[0]
}
func (h *Header) ID() uint32 {
return binary.BigEndian.Uint32(h[4:8])
}
func (h *Header) MagicCode() uint32 {
return binary.BigEndian.Uint32(h[8:12])
}
func (h *Header) encode(id uint32, code uint32) {
if code == 0 {
code = rand.Uint32()
}
binary.BigEndian.PutUint32(h[4:8], id)
binary.BigEndian.PutUint32(h[8:12], code)
}
func (c *Client) HandleConn(stream io.ReadWriteCloser) (err error) {
defer err2.Handle(&err)
var hdr Header
r := hex.NewDecoder(io.LimitReader(stream, headerSize))
try.To1(io.ReadFull(r, hdr[:]))
var sep [1]byte
try.To1(io.ReadFull(stream, sep[:]))
if v := hdr.Version(); v != 0 {
return fmt.Errorf("expect header version: 0, but got %d", v)
}
hook := try.To1(c.getHook(hdr))
hook.StreamCh <- stream
return
}
func (c *Client) getHook(h Header) (stream *StreamHook, err error) {
c.locker.RLock()
defer c.locker.RUnlock()
id, code := h.ID(), h.MagicCode()
hook, ok := c.streamHooks[id]
if !ok {
return nil, ErrStreamIDWrong
}
if hook == nil {
return nil, ErrStreamIDExpired
}
if hook.MagicCode() != code {
return nil, ErrStreamIDReused
}
hook.handled = true
return hook, nil
}
var (
ErrStreamIDWrong = fmt.Errorf("wrong stream id")
ErrStreamIDExpired = fmt.Errorf("expired stream id")
ErrStreamIDReused = fmt.Errorf("stream id is be reused. %w", ErrStreamIDExpired)
)