forked from getlantern/go-udtwrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathudtcat.go
199 lines (164 loc) · 3.77 KB
/
udtcat.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
// package udtcat provides an implementation of netcat using the go-udtwrapper package.
// It is meant to exercise the udt implementation.
// Usage:
// ucat [<local address>] <remote address>
// ucat -l <local address>
//
// Address format is: [host]:port
//
// Note that UDT'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"
udt "github.com/fffw/go-udtwrapper"
)
var verbose = false
// Usage prints out the usage of this module.
// Assumes flags use go stdlib flag pacakage.
var Usage = func() {
text := `udtcat - UDT 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
go func() {
// wait until we exit.
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGABRT)
<-sigc
panic("ABORT! ABORT! ABORT!")
}()
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 UDT connection on a given port,
// and pipes all incoming data to os.Stdout.
func Listen(localAddr string) error {
l, err := udt.Listen("udt", localAddr)
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())
// done with listener
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 = udt.ResolveUDTAddr("udt", 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 := udt.Dialer{LocalAddr: laddr}
c, err := d.Dial("udt", 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{}, 2)
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:
return
}
c.Close()
}