-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucat.go
192 lines (160 loc) · 3.81 KB
/
ucat.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
// package ucat provides an implementation of netcat using the go utp package.
// It is meant to exercise the utp implementation.
// Usage:
// ucat [<local address>] <remote address>
// ucat -l <local address>
//
// Address format is: [host]:port
//
// Note that uTP's congestion control gives priority to tcp flows (web traffic),
// so you could use this ucat tool to transfer massive files without hogging
// all the bandwidth.
package main
import (
"flag"
"fmt"
"io"
"net"
"os"
"os/signal"
"syscall"
utp "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/h2so5/utp"
)
var verbose = false
// Usage prints out the usage of this module.
// Assumes flags use go stdlib flag pacakage.
var Usage = func() {
text := `ucat - uTP netcat in Go
Usage:
listen: %s [<local address>] <remote address>
dial: %s -l <local address>
Address format is Go's: [host]:port
`
fmt.Fprintf(os.Stderr, text, os.Args[0], os.Args[0])
flag.PrintDefaults()
}
type args struct {
listen bool
verbose bool
localAddr string
remoteAddr string
}
func parseArgs() args {
var a args
// setup + parse flags
flag.BoolVar(&a.listen, "listen", false, "listen for connections")
flag.BoolVar(&a.listen, "l", false, "listen for connections (short)")
flag.BoolVar(&a.verbose, "v", false, "verbose debugging")
flag.Usage = Usage
flag.Parse()
osArgs := flag.Args()
if len(osArgs) < 1 {
exit("")
}
if a.listen {
a.localAddr = osArgs[0]
} else {
if len(osArgs) > 1 {
a.localAddr = osArgs[0]
a.remoteAddr = osArgs[1]
} else {
a.remoteAddr = osArgs[0]
}
}
return a
}
func main() {
args := parseArgs()
verbose = args.verbose
var err error
if args.listen {
err = Listen(args.localAddr)
} else {
err = Dial(args.localAddr, args.remoteAddr)
}
if err != nil {
exit("%s", err)
}
}
func exit(format string, vals ...interface{}) {
if format != "" {
fmt.Fprintf(os.Stderr, "ucat error: "+format+"\n", vals...)
}
Usage()
os.Exit(1)
}
func log(format string, vals ...interface{}) {
if verbose {
fmt.Fprintf(os.Stderr, "ucat log: "+format+"\n", vals...)
}
}
// Listen listens and accepts one incoming uTP connection on a given port,
// and pipes all incoming data to os.Stdout.
func Listen(localAddr string) error {
laddr, err := utp.ResolveAddr("utp", localAddr)
if err != nil {
return fmt.Errorf("failed to resolve address %s", localAddr)
}
l, err := utp.Listen("utp", laddr)
if err != nil {
return err
}
log("listening at %s", l.Addr())
c, err := l.Accept()
if err != nil {
return err
}
log("accepted connection from %s", c.RemoteAddr())
// should be able to close listener here, but utp.Listener.Close
// closes all open connections.
defer l.Close()
netcat(c)
return c.Close()
}
// Dial connects to a remote address and pipes all os.Stdin to the remote end.
// If localAddr is set, uses it to Dial from.
func Dial(localAddr, remoteAddr string) error {
var laddr net.Addr
var err error
if localAddr != "" {
laddr, err = utp.ResolveAddr("utp", localAddr)
if err != nil {
return fmt.Errorf("failed to resolve address %s", localAddr)
}
}
if laddr != nil {
log("dialing %s from %s", remoteAddr, laddr)
} else {
log("dialing %s", remoteAddr)
}
d := utp.Dialer{LocalAddr: laddr}
c, err := d.Dial("utp", remoteAddr)
if err != nil {
return err
}
log("connected to %s", c.RemoteAddr())
netcat(c)
return c.Close()
}
func netcat(c net.Conn) {
log("piping stdio to connection")
done := make(chan struct{})
go func() {
n, _ := io.Copy(c, os.Stdin)
log("sent %d bytes", n)
done <- struct{}{}
}()
go func() {
n, _ := io.Copy(os.Stdout, c)
log("received %d bytes", n)
done <- struct{}{}
}()
// wait until we exit.
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT,
syscall.SIGTERM, syscall.SIGQUIT)
select {
case <-done:
case <-sigc:
}
}