-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
nc.go
62 lines (58 loc) · 1.34 KB
/
nc.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
package inet256cmd
import (
"bufio"
"github.com/brendoncarroll/stdctx/logctx"
"github.com/inet256/inet256/pkg/inet256"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
func NewNetCatCmd(newNode NodeFactory) *cobra.Command {
return &cobra.Command{
Use: "nc <target_addr>",
Short: "nc is like netcat",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
remote, err := parseAddr(args[0])
if err != nil {
return err
}
in := cmd.InOrStdin()
out := cmd.OutOrStdout()
pk := generateKey()
node, err := newNode(ctx, pk)
if err != nil {
return err
}
defer node.Close()
logctx.Infoln(ctx, node.LocalAddr())
eg := errgroup.Group{}
eg.Go(func() error {
var msg inet256.Message
for {
if err := inet256.Receive(ctx, node, &msg); err != nil {
return err
}
if msg.Src != remote {
logctx.Warnf(ctx, "discarding message from %v", msg.Src)
continue
}
out.Write(msg.Payload)
out.Write([]byte("\n"))
}
})
eg.Go(func() error {
scn := bufio.NewScanner(in)
for scn.Scan() {
if err := node.Send(ctx, remote, scn.Bytes()); err != nil {
return err
}
}
return scn.Err()
})
return eg.Wait()
},
}
}
func parseAddr(x string) (inet256.Addr, error) {
return inet256.ParseAddrBase64([]byte(x))
}