This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
211 lines (190 loc) · 4.17 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
204
205
206
207
208
209
210
211
package tunnel
import (
"io"
"sync"
"github.com/crypto-zero/go-micro/v2/logger"
)
type tunListener struct {
// address of the listener
channel string
// token is the tunnel token
token string
// the accept channel
accept chan *session
// the tunnel closed channel
tunClosed chan bool
// the listener session
session *session
// del func to kill listener
delFunc func()
sync.RWMutex
// the channel to close
closed chan bool
}
func (t *tunListener) process() {
// our connection map for session
conns := make(map[string]*session)
defer func() {
// close the sessions
for id, conn := range conns {
conn.Close()
delete(conns, id)
}
// unassign
conns = nil
}()
for {
select {
case <-t.closed:
return
case <-t.tunClosed:
t.Close()
return
// receive a new message
case m := <-t.session.recv:
var sessionId string
var linkId string
switch t.session.mode {
case Multicast:
sessionId = "multicast"
linkId = "multicast"
case Broadcast:
sessionId = "broadcast"
linkId = "broadcast"
default:
sessionId = m.session
linkId = m.link
}
// get a session
sess, ok := conns[sessionId]
if logger.V(logger.TraceLevel, log) {
log.Tracef("Tunnel listener received channel %s session %s type %s exists: %t", m.channel, m.session, m.typ, ok)
}
if !ok {
// we only process open and session types
switch m.typ {
case "open", "session":
default:
continue
}
// create a new session session
sess = &session{
// the session key
key: []byte(t.token + m.channel + sessionId),
// the id of the remote side
tunnel: m.tunnel,
// the channel
channel: m.channel,
// the session id
session: sessionId,
// tunnel token
token: t.token,
// is loopback conn
loopback: m.loopback,
// the link the message was received on
link: linkId,
// set the connection mode
mode: t.session.mode,
// close chan
closed: make(chan bool),
// recv called by the acceptor
recv: make(chan *message, 128),
// use the internal send buffer
send: t.session.send,
// error channel
errChan: make(chan error, 1),
// set the read timeout
readTimeout: t.session.readTimeout,
}
// save the session
conns[sessionId] = sess
select {
case <-t.closed:
return
// send to accept chan
case t.accept <- sess:
}
}
// an existing session was found
switch m.typ {
case "close":
// don't close multicast sessions
if sess.mode > Unicast {
continue
}
// received a close message
select {
// check if the session is closed
case <-sess.closed:
// no op
delete(conns, sessionId)
default:
// only close if unicast session
// close and delete session
close(sess.closed)
delete(conns, sessionId)
}
// continue
continue
case "session":
// operate on this
default:
// non operational type
continue
}
// send this to the accept chan
select {
case <-sess.closed:
delete(conns, sessionId)
case sess.recv <- m:
if logger.V(logger.TraceLevel, log) {
log.Tracef("Tunnel listener sent to recv chan channel %s session %s type %s", m.channel, sessionId, m.typ)
}
}
}
}
}
func (t *tunListener) Channel() string {
return t.channel
}
// Close closes tunnel listener
func (t *tunListener) Close() error {
t.Lock()
defer t.Unlock()
select {
case <-t.closed:
return nil
default:
// close and delete
t.delFunc()
t.session.Close()
close(t.closed)
}
return nil
}
// Everytime accept is called we essentially block till we get a new connection
func (t *tunListener) Accept() (Session, error) {
select {
// if the session is closed return
case <-t.closed:
return nil, io.EOF
case <-t.tunClosed:
// close the listener when the tunnel closes
return nil, io.EOF
// wait for a new connection
case c, ok := <-t.accept:
// check if the accept chan is closed
if !ok {
return nil, io.EOF
}
// return without accept
if c.mode != Unicast {
return c, nil
}
// send back the accept
if err := c.Accept(); err != nil {
return nil, err
}
return c, nil
}
}