-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
211 lines (177 loc) · 4.42 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"bufio"
"bytes"
"encoding/gob"
"errors"
"fmt"
"io"
"net"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/dustin/go-humanize"
"github.com/tardisx/netgiv/secure"
)
type Client struct {
address string
port int
list bool
send bool
receiveNum int
authToken string
}
func (c *Client) Connect() error {
address := fmt.Sprintf("%s:%d", c.address, c.port)
d := net.Dialer{Timeout: 5 * time.Second}
conn, err := d.Dial("tcp", address)
if err != nil {
return fmt.Errorf("problem connecting to server, is it running?: %v", err)
}
defer conn.Close()
log.Debugf("established connection on %s", address)
tcpConn, ok := conn.(*net.TCPConn)
if !ok {
log.Fatal("could not assert")
}
sharedKey := secure.Handshake(tcpConn)
secureConnection := secure.SecureConnection{Conn: conn, SharedKey: sharedKey, Buffer: &bytes.Buffer{}}
enc := gob.NewEncoder(&secureConnection)
dec := gob.NewDecoder(&secureConnection)
if c.list {
log.Debugf("requesting file list")
err := c.connectToServer(secure.OperationTypeList, enc, dec)
if err != nil {
return fmt.Errorf("could not connect and auth: %v", err)
}
// now we expect to get stuff back until we don't
numFiles := 0
for {
listPacket := secure.PacketListData{}
err := dec.Decode(&listPacket)
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
fmt.Printf("%d: %s (%s) - %s\n", listPacket.Id, listPacket.Kind, humanize.Bytes(uint64(listPacket.FileSize)), listPacket.Timestamp)
numFiles++
}
fmt.Printf("total: %d files\n", numFiles)
conn.Close()
log.Debugf("done listing")
} else if c.receiveNum >= 0 {
log.Debugf("receiving file %d", c.receiveNum)
err := c.connectToServer(secure.OperationTypeReceive, enc, dec)
if err != nil {
return fmt.Errorf("could not connect and auth: %v", err)
}
req := secure.PacketReceiveDataStartRequest{
Id: uint32(c.receiveNum),
}
err = enc.Encode(req)
if err != nil {
panic(err)
}
// expect a response telling us if we can go ahead
res := secure.PacketReceiveDataStartResponse{}
err = dec.Decode(&res)
if err != nil {
panic(err)
}
if res.Status == secure.ReceiveDataStartResponseOK {
for {
res := secure.PacketReceiveDataNext{}
err = dec.Decode(&res)
if err != nil {
panic(err)
}
os.Stdout.Write(res.Data[:res.Size])
if res.Last {
break
}
}
log.Debugf("finished")
} else if res.Status == secure.ReceiveDataStartResponseNotFound {
log.Error("ngf not found")
} else {
panic("unknown status")
}
conn.Close()
} else if c.send {
// send mode
err := c.connectToServer(secure.OperationTypeSend, enc, dec)
if err != nil {
return fmt.Errorf("could not connect and auth: %v", err)
}
data := secure.PacketSendDataStart{
Filename: "",
TotalSize: 0,
}
err = enc.Encode(data)
if err != nil {
panic(err)
}
nBytes, nChunks := int64(0), int64(0)
reader := bufio.NewReader(os.Stdin)
buf := make([]byte, 0, 1024)
for {
n, err := reader.Read(buf[:cap(buf)])
buf = buf[:n]
if n == 0 {
if err == nil {
continue
}
if err == io.EOF {
break
}
log.Fatal(err)
}
nChunks++
nBytes += int64(len(buf))
send := secure.PacketSendDataNext{
Size: 5000,
Data: buf,
}
err = enc.Encode(send)
// time.Sleep(time.Second)
if err != nil {
log.Fatal(err)
}
}
log.Debugf("Sent %s in %d chunks", humanize.Bytes(uint64(nBytes)), nChunks)
conn.Close()
} else {
panic("no client mode set")
}
return nil
}
func (c *Client) connectToServer(op secure.OperationTypeEnum, enc *gob.Encoder, dec *gob.Decoder) error {
// list mode
startPacket := secure.PacketStartRequest{
OperationType: op,
ClientName: "",
ProtocolVersion: ProtocolVersion,
AuthToken: c.authToken,
}
err := enc.Encode(startPacket)
if err != nil {
return fmt.Errorf("could not send start packet: %v", err)
}
// check the response is ok
response := secure.PacketStartResponse{}
err = dec.Decode(&response)
if err != nil {
return fmt.Errorf("could not receive start packet response: %v", err)
}
if response.Response == secure.PacketStartResponseEnumWrongProtocol {
log.Print("wrong protocol version")
return errors.New("protocol version mismatch")
}
if response.Response == secure.PacketStartResponseEnumBadAuthToken {
log.Print("bad authtoken")
return errors.New("bad authtoken")
}
return nil
}