-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.go
More file actions
224 lines (190 loc) · 5.62 KB
/
Copy pathtransfer.go
File metadata and controls
224 lines (190 loc) · 5.62 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package teamspeak
import (
"context"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/honeybbq/teamspeak-go/commands"
)
var (
errFileTransferFailed = errors.New("file transfer failed")
errUnexpectedRespType = errors.New("unexpected response type")
errFileTransferTimedOut = errors.New("timeout waiting for file transfer notification")
)
// fileTransferTracker correlates clientftfid with notifystart* responses.
type fileTransferTracker struct {
pending map[uint16]chan any
mu sync.Mutex
nextID uint16
}
func newFileTransferTracker() *fileTransferTracker {
return &fileTransferTracker{
pending: make(map[uint16]chan any),
}
}
func (t *fileTransferTracker) register() (uint16, <-chan any) {
t.mu.Lock()
t.nextID++
if t.nextID == 0 {
t.nextID++
}
cftid := t.nextID
ch := make(chan any, 1)
t.pending[cftid] = ch
t.mu.Unlock()
return cftid, ch
}
func (t *fileTransferTracker) unregister(cftid uint16) {
t.mu.Lock()
delete(t.pending, cftid)
t.mu.Unlock()
}
func (t *fileTransferTracker) notify(cftid uint16, v any) {
t.mu.Lock()
if ch, ok := t.pending[cftid]; ok {
ch <- v
}
t.mu.Unlock()
}
func (t *fileTransferTracker) reset() {
t.mu.Lock()
t.pending = make(map[uint16]chan any)
t.mu.Unlock()
}
// FileTransferInitUpload sends ftinitupload to the server and waits for the
// notifystartupload response containing the TCP port and transfer key.
func (c *Client) FileTransferInitUpload(
channelID uint64, path string, password string, size uint64, overwrite bool,
) (*FileUploadInfo, error) {
cftid, ch := c.ftTrack.register()
defer c.ftTrack.unregister(cftid)
targetPath := path
if !strings.HasPrefix(targetPath, "/") {
targetPath = "/" + targetPath
}
overwriteVal := "0"
if overwrite {
overwriteVal = "1"
}
cmd := commands.BuildCommand("ftinitupload", map[string]string{
"cid": strconv.FormatUint(channelID, 10),
"name": targetPath,
"cpw": password,
"size": strconv.FormatUint(size, 10),
"clientftfid": strconv.Itoa(int(cftid)),
"overwrite": overwriteVal,
"resume": "0",
})
err := c.ExecCommand(cmd, 10*time.Second)
if err != nil {
return nil, err
}
select {
case res := <-ch:
switch v := res.(type) {
case FileUploadInfo:
return &v, nil
case FileTransferStatusInfo:
return nil, fmt.Errorf("%w: %s (status=%d)", errFileTransferFailed, v.Message, v.Status)
default:
return nil, fmt.Errorf("%w: %T", errUnexpectedRespType, v)
}
case <-time.After(10 * time.Second):
return nil, errFileTransferTimedOut
}
}
// FileTransferInitDownload sends ftinitdownload to the server and waits for the
// notifystartdownload response containing the TCP port and transfer key.
func (c *Client) FileTransferInitDownload(channelID uint64, path string, password string) (*FileDownloadInfo, error) {
cftid, ch := c.ftTrack.register()
defer c.ftTrack.unregister(cftid)
targetPath := path
if !strings.HasPrefix(targetPath, "/") {
targetPath = "/" + targetPath
}
cmd := commands.BuildCommand("ftinitdownload", map[string]string{
"cid": strconv.FormatUint(channelID, 10),
"name": targetPath,
"cpw": password,
"clientftfid": strconv.Itoa(int(cftid)),
"seekpos": "0",
})
err := c.ExecCommand(cmd, 10*time.Second)
if err != nil {
return nil, err
}
select {
case res := <-ch:
switch v := res.(type) {
case FileDownloadInfo:
return &v, nil
case FileTransferStatusInfo:
return nil, fmt.Errorf("%w: %s (status=%d)", errFileTransferFailed, v.Message, v.Status)
default:
return nil, fmt.Errorf("%w: %T", errUnexpectedRespType, v)
}
case <-time.After(10 * time.Second):
return nil, errFileTransferTimedOut
}
}
// FileTransferDeleteFile sends ftdeletefile to delete files on the server.
func (c *Client) FileTransferDeleteFile(channelID uint64, paths []string) error {
if len(paths) == 0 {
return nil
}
pathStr := strings.Join(paths, "|")
cmd := commands.BuildCommand("ftdeletefile", map[string]string{
"cid": strconv.FormatUint(channelID, 10),
"cpw": "",
"name": pathStr,
})
return c.ExecCommand(cmd, 10*time.Second)
}
// DialFileTransfer opens TCP to the TeamSpeak file-transfer port
// and performs the ftkey handshake. The caller is responsible for closing the
// returned connection.
func DialFileTransfer(host string, port uint16, key string) (net.Conn, error) {
addr := net.JoinHostPort(host, strconv.Itoa(int(port)))
dialer := &net.Dialer{Timeout: 10 * time.Second}
conn, err := dialer.DialContext(context.Background(), "tcp", addr)
if err != nil {
return nil, fmt.Errorf("failed to connect to file transfer server %s: %w", addr, err)
}
_, err = conn.Write([]byte(key))
if err != nil {
_ = conn.Close()
return nil, fmt.Errorf("failed to send transfer key: %w", err)
}
return conn, nil
}
// UploadFileData transfers data to the server using credentials from FileTransferInitUpload.
func UploadFileData(host string, info *FileUploadInfo, data io.Reader) error {
conn, err := DialFileTransfer(host, info.Port, info.FileTransferKey)
if err != nil {
return err
}
defer func() { _ = conn.Close() }()
_, err = io.Copy(conn, data)
if err != nil {
return fmt.Errorf("failed to upload file data: %w", err)
}
return nil
}
// DownloadFileData receives data from the server using credentials from FileTransferInitDownload.
func DownloadFileData(host string, info *FileDownloadInfo, dest io.Writer) error {
conn, err := DialFileTransfer(host, info.Port, info.FileTransferKey)
if err != nil {
return err
}
defer func() { _ = conn.Close() }()
_, err = io.Copy(dest, conn)
if err != nil {
return fmt.Errorf("failed to download file data: %w", err)
}
return nil
}