-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
packetconn.go
163 lines (144 loc) · 3.22 KB
/
packetconn.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
// Copyright (c) 2016-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package brook
import (
"errors"
"net"
"sync"
"time"
"github.com/txthinking/socks5"
)
type PacketConnFactory struct {
Conns map[string]*PacketConn
Lock *sync.Mutex
}
func NewPacketConnFactory() *PacketConnFactory {
return &PacketConnFactory{
Conns: make(map[string]*PacketConn),
Lock: &sync.Mutex{},
}
}
func (f *PacketConnFactory) Handle(addr *net.UDPAddr, dstb, data []byte, w func([]byte) (int, error), timeout int) (net.Conn, error) {
dst := socks5.ToAddress(dstb[0], dstb[1:len(dstb)-2], dstb[len(dstb)-2:])
f.Lock.Lock()
c, ok := f.Conns[addr.String()+dst]
f.Lock.Unlock()
if ok {
_ = c.In(data)
return nil, nil
}
f.Lock.Lock()
c = NewPacketConn(data, w, timeout, func() {
f.Lock.Lock()
delete(f.Conns, addr.String()+dst)
f.Lock.Unlock()
})
f.Conns[addr.String()+dst] = c
f.Lock.Unlock()
return c, nil
}
type PacketConn struct {
First []byte
InCh chan []byte
Done chan byte
W func([]byte) (int, error)
Clean func()
Timeout int
}
func NewPacketConn(fb []byte, w func([]byte) (int, error), timeout int, clean func()) *PacketConn {
c := &PacketConn{
InCh: make(chan []byte),
Done: make(chan byte),
W: w,
First: fb,
Clean: clean,
Timeout: timeout,
}
return c
}
func (c *PacketConn) In(b []byte) error {
select {
case c.InCh <- b:
return nil
case <-c.Done:
return errors.New("closed")
}
return nil
}
func (c *PacketConn) Read(b []byte) (int, error) {
if c.First != nil {
i := copy(b, c.First)
c.First = nil
return i, nil
}
tm := time.NewTimer(time.Duration(c.Timeout) * time.Second)
defer tm.Stop()
select {
case <-tm.C:
return 0, errors.New("timeout")
case bb := <-c.InCh:
i := copy(b, bb)
return i, nil
case <-c.Done:
return 0, errors.New("closed")
}
return 0, nil
}
func (c *PacketConn) Write(b []byte) (int, error) {
select {
case <-c.Done:
return 0, errors.New("closed")
default:
return c.W(b)
}
return 0, nil
}
func (c *PacketConn) Close() error {
select {
case <-c.Done:
default:
c.Clean()
close(c.Done)
}
return nil
}
func (c *PacketConn) LocalAddr() net.Addr {
panic("no")
return nil
}
func (c *PacketConn) RemoteAddr() net.Addr {
panic("no")
return nil
}
func (c *PacketConn) SetDeadline(t time.Time) error {
return nil
}
func (c *PacketConn) SetReadDeadline(t time.Time) error {
return nil
}
func (c *PacketConn) SetWriteDeadline(t time.Time) error {
return nil
}
type ConnFirst struct {
*net.UDPConn
First []byte
}
func (c *ConnFirst) Read(b []byte) (int, error) {
if c.First != nil {
i := copy(b, c.First)
c.First = nil
return i, nil
}
return c.UDPConn.Read(b)
}