-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
263 lines (219 loc) · 6.12 KB
/
main.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
package main
import (
cxt "context"
"errors"
"flag"
"fmt"
"io"
"os"
"os/signal"
"syscall"
ma "github.com/multiformats/go-multiaddr"
mapipe "github.com/multiformats/ma-pipe"
"github.com/dustin/go-humanize"
)
const VERSION = "1.0.0"
var ErrInvalidBandwidth = errors.New("invalid bandwidth. Must of the form: 10MBps, 1Kbps, 1GB/s, ... ")
const USAGE = `USAGE
ma-pipe <mode> <multiaddrs>...
ma-pipe listen <listen-multiaddr1> <listen-multiaddr2>
ma-pipe dial <dial-multiaddr1> <dial-multiaddr2>
ma-pipe fwd <listen-multiaddr> <dial-multiaddr>
ma-pipe proxy <listen-multiaddr>
OPTIONS
-h, --help display this help message
-v, --version display the version of the program
-t, --trace <dir> save a trace of the connection to <dir>
-e, --tee tee the connection to stdio
--bandwidth <bandwidth> introduce a bandwidth cap (eg 1MB/s)
EXAMPLES
# listen on two multiaddrs, accept 1 conn each, and pipe them
ma-pipe listen /ip4/127.0.0.1/tcp/1234 /ip4/127.0.0.1/tcp/1234
# dial to both multiaddrs, and pipe them
ma-pipe dial /ip4/127.0.0.1/tcp/1234 /ip4/127.0.0.1/tcp/1234
# listen on one multiaddr, accept 1 conn, dial to the other, and pipe them
ma-pipe fwd /ip4/127.0.0.1/tcp/1234 /ip4/127.0.0.1/tcp/1234
# listen on one multiaddr, accept 1 conn.
# read the first line, parse a multiaddr, dial that multiaddr, and pipe them
ma-pipe proxy /ip4/127.0.0.1/tcp/1234
# ma-pipe supports "zero" listen multiaddrs
ma-pipe proxy /ip4/0.0.0.0/tcp/0
# ma-pipe supports the /unix/stdio multiaddr
ma-pipe fwd /unix/stdio /ip4/127.0.0.1/tcp/1234
# ma-pipe supports the --tee option to inspect conn in stdio
ma-pipe --tee fwd /ip4/0.0.0.0/tcp/0 /ip4/127.0.0.1/tcp/1234
# ma-pipe allows throttling connections with a bandwidth max
ma-pipe --bandwidth 1MB/s listen /ip4/127.0.0.1/tcp/1234 /ip4/127.0.0.1/tcp/1234
`
type Opts struct {
Mode string
Trace string
Version bool
Tee bool
BWidthStr string
Bandwidth uint64 // in B/s
Addrs []ma.Multiaddr
}
func parseArgs() (Opts, error) {
// parse options
o := Opts{Mode: "exit"}
flag.BoolVar(&o.Version, "v", false, "")
flag.BoolVar(&o.Version, "version", false, "")
flag.StringVar(&o.Trace, "t", "", "")
flag.StringVar(&o.Trace, "trace", "", "")
flag.BoolVar(&o.Tee, "e", false, "")
flag.BoolVar(&o.Tee, "tee", false, "")
flag.StringVar(&o.BWidthStr, "bandwidth", "", "")
flag.Usage = func() {
fmt.Print(USAGE)
}
flag.Parse()
if o.Version {
fmt.Println("ma-pipe", VERSION)
return o, nil
}
args := flag.Args()
if len(args) < 2 { // <mode> <addrs>+
fmt.Print(USAGE)
return o, errors.New("not enough arguments")
}
// set the mode
o.Mode = args[0]
// parse the multiaddrs
o.Addrs = make([]ma.Multiaddr, len(args)-1)
for i, saddr := range args[1:] {
maddr, err := ma.NewMultiaddr(saddr)
if err != nil {
return o, err
}
o.Addrs[i] = maddr
}
// parse the bandwidth
var err error
o.Bandwidth, err = ParseBandwidth(o.BWidthStr)
if err != nil {
return o, err
}
return o, nil
}
// ParseBandwidth parses a bandwidth string of the form: <size>/s (eg 10MB/s, 1KBps)
func ParseBandwidth(s string) (uint64, error) {
if s == "" {
return 0, nil
}
if len(s) < 4 {
return 0, ErrInvalidBandwidth
}
if s[len(s)-1] != 's' {
return 0, ErrInvalidBandwidth
}
if s[len(s)-2] != '/' && s[len(s)-2] != 'p' {
return 0, ErrInvalidBandwidth
}
return humanize.ParseBytes(s[0 : len(s)-2])
}
func catchSigPipe(ctx cxt.Context, trace *mapipe.Trace) cxt.Context {
ctx2, cancel := cxt.WithCancel(cxt.Background())
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGPIPE)
<-c
fmt.Fprintln(trace.CW, "received SIGPIPE, closing...")
cancel()
}()
return ctx2
}
func runMode(trace *mapipe.Trace, opts Opts) error {
o := mapipe.Opts{Trace: trace, MaxBW: opts.Bandwidth, Kill: nil}
// setup the kill channel for the pipe. This kill channel solves the following problem:
// when SIGPIPE is sent, the output cuts and we do not have a graceful exit. This enables
// nicer printing when using ma-pipe with other programs.
ctx := catchSigPipe(cxt.Background(), trace)
switch opts.Mode {
case "listen":
if len(opts.Addrs) != 2 {
return errors.New("listen mode takes exactly 2 multiaddrs")
}
return mapipe.ListenPipe(ctx, opts.Addrs[0], opts.Addrs[1], o)
case "dial":
if len(opts.Addrs) != 2 {
return errors.New("dial mode takes exactly 2 multiaddrs")
}
return mapipe.DialPipe(ctx, opts.Addrs[0], opts.Addrs[1], o)
case "fwd":
if len(opts.Addrs) != 2 {
return errors.New("fwd mode takes exactly 2 multiaddrs")
}
return mapipe.ForwardPipe(ctx, opts.Addrs[0], opts.Addrs[1], o)
case "proxy":
if len(opts.Addrs) != 1 {
return errors.New("proxy mode takes exactly 1 multiaddr")
}
return mapipe.ProxyPipe(ctx, opts.Addrs[0], o)
}
return fmt.Errorf("invalid mode %s", opts.Mode)
}
func run() error {
opts, err := parseArgs()
if err != nil {
return nil
}
switch opts.Mode {
case "listen", "dial", "fwd", "proxy":
trace := &mapipe.Trace{
CW: os.Stderr,
AW: io.Discard,
BW: io.Discard,
}
if opts.Tee {
trace.CW = NewPrefixWriter(os.Stderr, "# ")
trace.AW = NewPrefixWriter(os.Stdout, "> ")
trace.BW = NewPrefixWriter(os.Stdout, "< ")
}
if opts.Trace != "" {
err := mapipe.OpenTraceFiles(trace, opts.Trace)
if err != nil {
return err
}
}
err := runMode(trace, opts)
if err != nil {
fmt.Fprintf(trace.CW, "error: %s\n", err)
}
return err
case "exit":
return nil
default:
fmt.Print(USAGE)
return fmt.Errorf("invalid mode %s", opts.Mode)
}
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}
type PrefixWriter struct {
W io.Writer
Prefix []byte
}
func NewPrefixWriter(W io.Writer, pre string) *PrefixWriter {
return &PrefixWriter{W, []byte(pre)}
}
func (pw *PrefixWriter) Write(buf []byte) (int, error) {
buf = append(pw.Prefix, buf...)
n, err := pw.W.Write(buf)
// have to remove the length
n = n - len(pw.Prefix)
if n < 0 {
n = 0
}
return n, err
}
func (pw *PrefixWriter) Close() error {
if c, ok := pw.W.(io.Closer); ok {
return c.Close()
}
return nil
}