-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapipe.go
173 lines (144 loc) · 3.75 KB
/
mapipe.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
package mapipe
import (
cxt "context"
"errors"
"fmt"
"io"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
type Opts struct {
Trace *Trace // trace object
MaxBW uint64 // in Bytes/s
Kill <-chan struct{}
}
type ConnErr struct {
Conn manet.Conn
Err error
}
// ListenPipe listens on both multiaddrs, accepts one connection each,
// and pipes them to each other.
func ListenPipe(ctx cxt.Context, l1, l2 ma.Multiaddr, o Opts) error {
list1, err := Listen(l1)
if err != nil {
return err
}
fmt.Fprintln(o.Trace.CW, "listening on", list1.Multiaddr())
list2, err := Listen(l2)
if err != nil {
return err
}
fmt.Fprintln(o.Trace.CW, "listening on", list2.Multiaddr())
conns := make(chan ConnErr, 2)
acceptOneThenClose := func(l manet.Listener) {
c, err := l.Accept()
conns <- ConnErr{c, err}
l.Close()
}
go acceptOneThenClose(list1)
go acceptOneThenClose(list2)
c1 := <-conns
fmt.Fprintln(o.Trace.CW, "accepted", c1.Conn.LocalMultiaddr(), c1.Conn.RemoteMultiaddr())
c2 := <-conns
fmt.Fprintln(o.Trace.CW, "accepted", c2.Conn.LocalMultiaddr(), c2.Conn.RemoteMultiaddr())
defer func() {
if c1.Conn != nil {
c1.Conn.Close()
}
if c2.Conn != nil {
c2.Conn.Close()
}
}()
if c1.Err != nil {
return c1.Err
}
if c2.Err != nil {
return c2.Err
}
return pipeConn(ctx, c1.Conn, c2.Conn, o)
}
// ForwardPipe listens on one multiaddr, accepts one connection,
// dials to the second multiaddr, and pipes them to each other.
func ForwardPipe(ctx cxt.Context, l, d ma.Multiaddr, o Opts) error {
list, err := Listen(l)
if err != nil {
return err
}
fmt.Fprintln(o.Trace.CW, "listening on", list.Multiaddr())
c1, err := list.Accept()
list.Close()
if err != nil {
return err
}
defer c1.Close()
fmt.Fprintln(o.Trace.CW, "accepted", c1.LocalMultiaddr(), c1.RemoteMultiaddr())
fmt.Fprintln(o.Trace.CW, "dialing", d)
c2, err := Dial(d)
if err != nil {
return err
}
defer c2.Close()
fmt.Fprintln(o.Trace.CW, "dialed", c2.LocalMultiaddr(), c2.RemoteMultiaddr())
return pipeConn(ctx, c1, c2, o)
}
// DialPipe dials to both multiaddrs, and pipes them to each other.
func DialPipe(ctx cxt.Context, d1, d2 ma.Multiaddr, o Opts) error {
fmt.Fprintln(o.Trace.CW, "dialing", d1)
fmt.Fprintln(o.Trace.CW, "dialing", d2)
c1, err := Dial(d1)
if err != nil {
return err
}
defer c1.Close()
fmt.Fprintln(o.Trace.CW, "dialed", c1.LocalMultiaddr(), c1.RemoteMultiaddr())
c2, err := Dial(d2)
if err != nil {
return err
}
defer c2.Close()
fmt.Fprintln(o.Trace.CW, "dialed", c2.LocalMultiaddr(), c2.RemoteMultiaddr())
return pipeConn(ctx, c1, c2, o)
}
// ProxyPipe listens on one multiaddr, reads a multiaddr, dials it, pipes them.
func ProxyPipe(ctx cxt.Context, l ma.Multiaddr, o Opts) error {
list, err := Listen(l)
if err != nil {
return err
}
fmt.Fprintln(o.Trace.CW, "listening on", list.Multiaddr())
c1, err := list.Accept()
list.Close()
if err != nil {
return err
}
defer c1.Close()
fmt.Fprintln(o.Trace.CW, "accepted", c1.LocalMultiaddr(), c1.RemoteMultiaddr())
// read until the first newline.
d, err := readMultiaddr(c1)
if err != nil {
return err
}
fmt.Fprintln(o.Trace.CW, "requested proxy to", d)
fmt.Fprintln(o.Trace.CW, "dialing", d)
c2, err := Dial(d)
if err != nil {
return err
}
defer c2.Close()
fmt.Fprintln(o.Trace.CW, "dialed", c2.LocalMultiaddr(), c2.RemoteMultiaddr())
return pipeConn(ctx, c1, c2, o)
}
func readMultiaddr(r io.Reader) (ma.Multiaddr, error) {
buf := make([]byte, 2048)
for i := 0; i < len(buf); i++ {
_, err := r.Read(buf[i : i+1])
if err != nil {
return nil, err
}
if buf[i] == byte('\n') {
// found the newline
return ma.NewMultiaddr(string(buf[0:i]))
}
}
return nil, errors.New("did not find expected multiaddr and newline")
}