-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistener.go
203 lines (167 loc) · 3.52 KB
/
listener.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
package authConn
import (
"context"
"crypto/tls"
"io"
"log"
"net"
"sync"
"time"
"github.com/eleztian/authConn/auth"
)
const (
defaultTimeout = 5 * time.Second
defaultConnBufSize = 5
)
type ConnWithCredential struct {
auth.Credential
net.Conn
}
// Auth if auth pass return 0 and Credential.
type Auth func(authPacket *AuthPacket) (rc byte, credential auth.Credential)
type Listener struct {
ctx context.Context
cancel func()
wg *sync.WaitGroup
err error
ln net.Listener
authFunc Auth
ch chan net.Conn
}
func Listen(network, address string, authFunc Auth) (net.Listener, error) {
ln, err := net.Listen(network, address)
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
res := &Listener{
ln: ln,
ctx: ctx,
cancel: cancel,
wg: &sync.WaitGroup{},
authFunc: authFunc,
ch: make(chan net.Conn, defaultConnBufSize),
}
go res.start()
return res, nil
}
func ListenTls(network, address string, tlsConfig *tls.Config, authFunc Auth) (net.Listener, error) {
ln, err := tls.Listen(network, address, tlsConfig)
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
res := &Listener{
ln: ln,
ctx: ctx,
cancel: cancel,
wg: &sync.WaitGroup{},
authFunc: authFunc,
ch: make(chan net.Conn, defaultConnBufSize),
}
go res.start()
return res, nil
}
func ListenWithAuth(ln net.Listener, authFunc Auth) net.Listener {
ctx, cancel := context.WithCancel(context.Background())
res := &Listener{
ln: ln,
ctx: ctx,
cancel: cancel,
wg: &sync.WaitGroup{},
authFunc: authFunc,
ch: make(chan net.Conn, defaultConnBufSize),
}
go res.start()
return res
}
func (l *Listener) start() {
defer l.cancel()
defer close(l.ch)
for {
select {
case <-l.ctx.Done():
return
default:
}
conn, err := l.ln.Accept()
if err != nil {
l.err = err
return
}
l.wg.Add(1)
go func() {
defer l.wg.Done()
authPacket := &AuthPacket{data: map[string]string{}}
_ = conn.SetReadDeadline(time.Now().Add(defaultTimeout))
err = authPacket.Reset(conn)
_ = conn.SetReadDeadline(time.Time{})
if err != nil {
_ = conn.Close()
return
}
select {
case <-l.ctx.Done():
_ = conn.Close()
return
default:
authRsp := AuthRspPacket{ReturnCode: 0}
var cre auth.Credential
authRsp.ReturnCode, cre = l.authFunc(authPacket)
_ = conn.SetWriteDeadline(time.Now().Add(defaultTimeout))
_, err = conn.Write(authRsp.Packet())
_ = conn.SetWriteDeadline(time.Time{})
if err != nil {
_ = conn.Close()
if err != io.EOF {
log.Printf("Conn: write auth response %v", err)
}
return
}
if authRsp.ReturnCode != 0 {
_ = conn.Close()
log.Printf("Conn: auth failed %d", authRsp.ReturnCode)
return
}
conn = &ConnWithCredential{
Credential: cre,
Conn: conn,
}
select {
case l.ch <- conn:
case <-l.ctx.Done():
_ = conn.Close()
}
}
}()
}
}
func (l *Listener) Accept() (net.Conn, error) {
select {
case <-l.ctx.Done():
if l.err != nil {
return nil, l.err
}
return nil, io.EOF
case conn := <-l.ch:
return conn, nil
}
}
func (l *Listener) Close() error {
l.cancel()
err := l.ln.Close()
if err != nil {
return err
}
l.wg.Wait()
for conn := range l.ch {
_ = conn.Close()
}
return nil
}
func (l *Listener) Addr() net.Addr {
return l.ln.Addr()
}
func (l *Listener) Error() error {
return l.err
}