-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetdrop.go
323 lines (286 loc) · 7.95 KB
/
netdrop.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package main
import (
"context"
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"time"
petname "github.com/dustinkirkland/golang-petname"
"github.com/grandcat/zeroconf"
"github.com/secure-io/sio-go"
"github.com/urfave/cli/v2"
)
var (
// Version is the applications build version.
Version = "unset"
)
func main() {
app := &cli.App{
Name: "netdrop",
Usage: "share data encrypted between peers in a network",
Description: `With netdrop you can send files or stream data from one peer to another inside a local network. Data is encrypted on transport and there is zero configuration necessary, just share the password with the receiver.`,
Version: Version,
Commands: []*cli.Command{
{
Name: "send",
Aliases: []string{"s"},
Action: sendAction,
ArgsUsage: "/path/to/file",
Description: `send a file or a stream of data
EXAMPLES:
netdrop send my-picture.png
tar -cz /some/directory | netdrop send`,
},
{
Name: "receive",
Aliases: []string{"r", "recv"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Usage: "/path/to/output/file",
},
},
Action: receiveAction,
ArgsUsage: "password",
Description: `receive a file or folder
EXAMPLES:
netdrop receive --output my-picture.png
netdrop send | tar -xf-`,
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func receiveAction(c *cli.Context) error {
password := c.Args().First()
filepath := c.String("output")
if filepath == "" {
return receive(c.Context, password, os.Stdout, os.Stderr)
}
f, err := os.OpenFile(filepath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
if err != nil {
return fmt.Errorf("failed to open %q: %w", filepath, err)
}
defer f.Close()
return receive(c.Context, password, f, os.Stderr)
}
func resolveServices(ctx context.Context) ([]*zeroconf.ServiceEntry, error) {
resolver, err := zeroconf.NewResolver()
if err != nil {
return nil, err
}
serviceCh := make(chan *zeroconf.ServiceEntry)
err = resolver.Lookup(ctx, zeroconfInstance, zeroconfService, zeroconfDomain, serviceCh)
if err != nil {
return nil, err
}
var entries []*zeroconf.ServiceEntry
select {
case entry := <-serviceCh:
entries = append(entries, entry)
case <-time.After(500 * time.Millisecond):
break
}
return entries, nil
}
func receive(ctx context.Context, password string, output, stderr io.Writer) error {
entries, err := resolveServices(ctx)
if err != nil {
return fmt.Errorf("failed to resolve netdrop services: %w", err)
}
for _, entry := range entries {
err = receiveFrom(ctx, fmt.Sprintf("%s:%d", entry.AddrIPv4[0].String(), entry.Port), password, output, stderr)
if err != nil {
if errors.Is(err, ErrWrongPassword) {
continue
}
return err
}
return nil
}
return fmt.Errorf("nothing to receive")
}
func receiveFrom(ctx context.Context, addr, password string, output, stderr io.Writer) error {
conn, err := net.Dial("tcp", addr)
if err != nil {
return fmt.Errorf("dial %q failed: %w", addr, err)
}
defer conn.Close()
hash, err := hashPassword(password)
if err != nil {
return fmt.Errorf("password hash failed: %w", err)
}
n, err := conn.Write(hash)
if err != nil {
return fmt.Errorf("sending password hash failed: %w", err)
}
if n != 32 {
return fmt.Errorf("expected 32 bytes to be send but was %d", n)
}
stream, err := newStreamCipher(password)
if err != nil {
return fmt.Errorf("stream cipher failed: %w", err)
}
zeroNonce := make([]byte, stream.NonceSize())
decR := stream.DecryptReader(conn, zeroNonce, nil)
n64, err := io.Copy(output, decR)
if err != nil {
if n64 == 0 {
// EOF
// sio intentionally hides the error cause to prevent side-channel attacks: https://github.com/secure-io/sio-go/pull/58
return ErrWrongPassword
}
return fmt.Errorf("copy failed after %d bytes: %w", n64, err)
}
return nil
}
const (
zeroconfService = "_netdrop._tcp"
zeroconfDomain = "local."
zeroconfInstance = "netdrop"
)
func announce(port int) (shutdown func(), err error) {
server, err := zeroconf.Register("netdrop", zeroconfService, zeroconfDomain, port, []string{"github.com/klingtnet/netdrop server"}, nil)
if err != nil {
return nil, fmt.Errorf("failed to register zerconf service")
}
return server.Shutdown, nil
}
const listenAddr = ":0"
func sendAction(c *cli.Context) error {
filepath := c.Args().First()
if filepath == "" {
inf, err := os.Stdin.Stat()
if err != nil {
return fmt.Errorf("could not open stdin: %w", err)
}
if inf.Mode()&os.ModeNamedPipe == 0 {
return fmt.Errorf("input is not a pipe")
}
return send(c.Context, os.Stdin, os.Stderr)
}
var f *os.File
f, err := os.Open(filepath)
if err != nil {
return fmt.Errorf("failed to open %q for reading: %w", filepath, err)
}
defer f.Close()
return send(c.Context, f, os.Stderr)
}
func hashPassword(password string) ([]byte, error) {
hasher := sha256.New()
_, err := hasher.Write([]byte(password))
if err != nil {
return nil, err
}
return hasher.Sum(nil), nil
}
func newStreamCipher(password string) (*sio.Stream, error) {
key := make([]byte, 32)
copy(key, []byte(password))
block, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("failed to create encryption cipher: %w", err)
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, fmt.Errorf("failed to create 'authenticated encryption with associated data': %w", err)
}
return sio.NewStream(gcm, sio.BufSize), nil
}
type Error string
func (e Error) Error() string { return string(e) }
const ErrWrongPassword = Error("client sent wrong password")
func handleSend(conn *net.TCPConn, in io.Reader, password string) error {
actualHash := make([]byte, 32)
nR, err := conn.Read(actualHash)
if err != nil {
return fmt.Errorf("failed to read password hash from client connection: %w", err)
}
if nR != 32 {
return fmt.Errorf("expected 32 bytes but read %d", nR)
}
expectedHash, err := hashPassword(password)
if err != nil {
return fmt.Errorf("failed to hash password: %w", err)
}
if hex.EncodeToString(expectedHash) != hex.EncodeToString(actualHash) {
return ErrWrongPassword
}
stream, err := newStreamCipher(password)
if err != nil {
return fmt.Errorf("failed to create stream cipher: %w", err)
}
zeroNonce := make([]byte, stream.NonceSize())
encW := stream.EncryptWriter(conn, zeroNonce, nil)
defer encW.Close()
n, err := io.Copy(encW, in)
if err != nil {
return fmt.Errorf("send failed: %w", err)
}
if n == 0 {
return fmt.Errorf("send nothing")
}
return nil
}
func send(ctx context.Context, in io.Reader, stderr io.Writer) error {
listener, err := net.Listen("tcp", listenAddr)
if err != nil {
return fmt.Errorf("failed to listen on %q: %w", listenAddr, err)
}
tcpListener, ok := listener.(*net.TCPListener)
if !ok {
return fmt.Errorf("not a TCP listener: %#v", listener.Addr())
}
defer tcpListener.Close()
addr, ok := listener.Addr().(*net.TCPAddr)
if !ok {
return fmt.Errorf("not a TCP addr: %w", err)
}
shutdownFn, err := announce(addr.Port)
if err != nil {
return fmt.Errorf("zeroconf announcement failed: %w", err)
}
// TODO: catch SIGINT and clean exit
defer shutdownFn()
petname.NonDeterministicMode()
password := petname.Generate(2, "-")
fmt.Fprintln(stderr, "password:", password)
for {
fmt.Fprintln(stderr, "waiting for connection...")
conn, err := tcpListener.AcceptTCP()
if err != nil {
return fmt.Errorf("accept failed: %w", err)
}
err = handleSend(conn, in, password)
if err != nil {
conn.Close()
if errors.Is(err, ErrWrongPassword) {
fmt.Fprintf(stderr, "%q: %s\n", conn.RemoteAddr(), err.Error())
continue
}
return err
}
// shutdown server
err = conn.Close()
if err != nil {
// ErrNetClosing is not exposed: https://github.com/golang/go/issues/4373
if strings.HasSuffix(err.Error(), "use of closed network connection") {
return nil
}
return fmt.Errorf("close failed: %w", err)
}
return nil
}
}