-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.go
182 lines (144 loc) · 2.95 KB
/
tcp.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
package main
import (
"fmt"
"io"
"net"
"os"
"strconv"
"syscall"
"time"
"github.com/codemodus/freenet/coms"
)
const (
sockOptOrigDst = 80
)
func listenTCP(cs *coms.Coms, port int, secure bool) {
a := &net.TCPAddr{
IP: net.ParseIP("0.0.0.0"),
Port: port,
}
l, err := net.ListenTCP("tcp", a)
if err != nil {
cs.Errorf("cannot setup listener: %s", err)
return
}
defer qtClose(l)
cs.Infof("listener opened (:%d)", port)
go func() {
<-cs.Done()
qtClose(l)
}()
spawn(cs, l)
cs.Infof("listener closed (:%d)", port)
}
func spawn(cs *coms.Coms, l *net.TCPListener) {
for {
c, err := acceptTCP(l)
if err != nil {
select {
case <-cs.Done():
return
default:
cs.Errorf("cannot accept connection: %s", err)
}
}
cs.Infof("accepted connection on %s", c.RemoteAddr().String())
done := make(chan struct{})
go func() {
select {
case <-cs.Done():
case <-done:
}
qtClose(c)
}()
cs.Conc(func() {
if err := bond(cs, done, c); err != nil {
cs.Errorf("bond broken: %s", err)
}
select {
case <-done:
default:
close(done)
}
})
}
}
func bond(cs *coms.Coms, done chan struct{}, conn *net.TCPConn) error {
f, err := conn.File()
if err != nil {
return fmt.Errorf("cannot get conn file: %s", err)
}
defer qtClose(f)
a, err := address(f)
if err != nil {
return fmt.Errorf("cannot determine source address: %s", err)
}
cl, err := dial(a)
if err != nil {
return fmt.Errorf("cannot dial destination (%s): %s", a, err)
}
defer qtClose(cl)
cs.Infof("connected to remote at %s", a)
ec := make(chan error)
defer close(ec)
go func() {
_, _ = io.Copy(cl, conn)
ec <- nil
}()
_, err = intercept(conn, cl, isSecure(a))
if err != nil {
<-ec
return err
}
return <-ec
}
func acceptTCP(l *net.TCPListener) (*net.TCPConn, error) {
c, err := l.AcceptTCP()
if err != nil {
return nil, err
}
if err := c.SetLinger(10); err != nil {
return nil, err
}
return c, nil
}
func dial(address string) (net.Conn, error) {
cl, err := net.DialTimeout("tcp", address, time.Second*16)
if err != nil {
return nil, err
}
if err = cl.SetReadDeadline(time.Now().Add(time.Second * 16)); err != nil {
return nil, err
}
return cl, nil
}
func qtClose(c io.Closer) {
_ = c.Close()
}
func address(f *os.File) (string, error) {
fd := int(f.Fd())
a, err := syscall.GetsockoptIPv6Mreq(fd, syscall.SOL_IP, sockOptOrigDst)
if err != nil {
return "", err
}
return multiaddrToString(a.Multiaddr), nil
}
func multiaddrToString(multiaddr [16]byte) string {
ip := multiaddr[4:8]
ipStr := net.IPv4(ip[0], ip[1], ip[2], ip[3]).String()
port := multiaddr[2:4]
portUint := int64((uint32(port[0]) << 8) + uint32(port[1]))
portStr := strconv.FormatInt(portUint, 10)
return (ipStr + ":" + portStr)
}
func isSecure(s string) bool {
return splitPort(s) == "443"
}
func splitPort(s string) string {
for i := len(s) - 1; i > 0; i-- {
if s[i] == ':' {
return s[i+1:]
}
}
return "0"
}