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
|
package socket
import (
"errors"
"fmt"
"log"
"net"
"strings"
"sync"
"tunnel/pkg/server/env"
"tunnel/pkg/server/opts"
"tunnel/pkg/server/queue"
)
var errAlreadyClosed = errors.New("already closed")
type exportChannel struct {
info string
Channel
}
type Channel interface {
Send(wq queue.Q) error
Recv(rq queue.Q) error
Close() error
}
type S interface {
Open(env env.Env) (Channel, error)
Close()
}
type listenSocket struct {
proto, addr string
listen net.Listener
}
type dialSocket struct {
proto, addr string
}
type connChannel struct {
conn net.Conn
once sync.Once
}
type loopSocket struct{}
type loopChannel struct {
q queue.Q
}
func (c exportChannel) String() string {
return c.info
}
func newConnChannel(conn net.Conn) Channel {
return &connChannel{conn: conn}
}
func (c *connChannel) final(f func() error, err error) error {
if e := f(); e != nil {
if e == errAlreadyClosed {
return nil
} else {
return e
}
}
return err
}
func (c *connChannel) Send(wq queue.Q) error {
err := queue.IoCopy(c.conn, wq.Writer())
return c.final(c.Close, err)
}
func (c *connChannel) Recv(rq queue.Q) error {
err := queue.IoCopy(rq.Reader(), c.conn)
return c.final(c.Close, err)
}
func (c *connChannel) String() string {
local, remote := c.conn.LocalAddr(), c.conn.RemoteAddr()
return fmt.Sprintf("%s/%s->%s", local.Network(), local, remote)
}
func (c *connChannel) Close() error {
err := errAlreadyClosed
c.once.Do(func() {
log.Println("close", c)
err = c.conn.Close()
})
return err
}
func newListenSocket(proto, addr string) (S, error) {
if proto == "tcp" {
if !strings.Contains(addr, ":") {
addr = ":" + addr
}
}
listen, err := net.Listen(proto, addr)
if err != nil {
return nil, err
}
s := &listenSocket{
proto: proto,
addr: addr,
listen: listen,
}
return s, nil
}
func (s *listenSocket) Open(env env.Env) (Channel, error) {
conn, err := s.listen.Accept()
if err != nil {
return nil, err
}
addr := conn.RemoteAddr()
info := fmt.Sprintf("%s/%s", addr.Network(), addr)
return exportChannel{info, newConnChannel(conn)}, nil
}
func (s *listenSocket) String() string {
return fmt.Sprintf("%s/%s,listen", s.proto, s.addr)
}
func (s *listenSocket) Close() {
s.listen.Close()
}
func newDialSocket(proto, addr string) (S, error) {
switch proto {
case "tcp", "udp":
if !strings.Contains(addr, ":") {
addr = "localhost:" + addr
}
}
return &dialSocket{proto: proto, addr: addr}, nil
}
func (s *dialSocket) String() string {
return fmt.Sprintf("%s/%s", s.proto, s.addr)
}
func (s *dialSocket) Open(env env.Env) (Channel, error) {
conn, err := net.Dial(s.proto, s.addr)
if err != nil {
return nil, err
}
return exportChannel{"-", newConnChannel(conn)}, nil
}
func (s *dialSocket) Close() {
}
func (c *loopChannel) Send(wq queue.Q) error {
return queue.Copy(c.q, wq)
}
func (c *loopChannel) Recv(rq queue.Q) error {
defer close(c.q)
return queue.Copy(rq, c.q)
}
func (c *loopChannel) Close() error {
return nil
}
func (c *loopChannel) String() string {
return "loop"
}
func (s *loopSocket) Open(env.Env) (Channel, error) {
return &loopChannel{queue.New()}, nil
}
func (s *loopSocket) String() string {
return "loop"
}
func (s *loopSocket) Close() {
}
func newLoopSocket() (S, error) {
return &loopSocket{}, nil
}
func New(desc string, env env.Env) (S, error) {
base, opts := opts.Parse(desc)
args := strings.SplitN(base, "/", 2)
var proto string
var addr string
if len(args) < 2 {
addr = args[0]
} else {
proto, addr = args[0], args[1]
}
if addr == "loop" {
return newLoopSocket()
}
if proto == "" {
proto = "tcp"
}
if addr == "" {
return nil, fmt.Errorf("bad socket '%s'", desc)
}
if _, ok := opts["listen"]; ok {
return newListenSocket(proto, addr)
}
return newDialSocket(proto, addr)
}
|