2019-08-07 19:44:33 +02:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2019-12-12 19:10:32 +02:00
|
|
|
"sync"
|
2019-08-11 19:11:33 +02:00
|
|
|
|
2020-02-23 15:45:20 +02:00
|
|
|
log "github.com/micro/go-micro/v2/logger"
|
2019-08-07 19:44:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type tunListener struct {
|
|
|
|
// address of the listener
|
2019-08-30 21:05:00 +02:00
|
|
|
channel string
|
2019-11-25 20:56:00 +02:00
|
|
|
// token is the tunnel token
|
|
|
|
token string
|
2019-08-07 19:44:33 +02:00
|
|
|
// the accept channel
|
2019-08-30 21:05:00 +02:00
|
|
|
accept chan *session
|
2019-08-08 16:20:53 +02:00
|
|
|
// the tunnel closed channel
|
|
|
|
tunClosed chan bool
|
2019-08-30 21:05:00 +02:00
|
|
|
// the listener session
|
|
|
|
session *session
|
2019-09-04 10:48:05 +02:00
|
|
|
// del func to kill listener
|
|
|
|
delFunc func()
|
2019-12-12 19:10:32 +02:00
|
|
|
|
|
|
|
sync.RWMutex
|
|
|
|
// the channel to close
|
|
|
|
closed chan bool
|
2019-09-04 10:48:05 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
func (t *tunListener) process() {
|
|
|
|
// our connection map for session
|
2019-08-30 21:05:00 +02:00
|
|
|
conns := make(map[string]*session)
|
2019-08-07 19:44:33 +02:00
|
|
|
|
2019-09-04 10:48:05 +02:00
|
|
|
defer func() {
|
|
|
|
// close the sessions
|
2019-12-06 01:11:42 +02:00
|
|
|
for id, conn := range conns {
|
2019-09-04 10:48:05 +02:00
|
|
|
conn.Close()
|
2019-12-06 01:11:42 +02:00
|
|
|
delete(conns, id)
|
2019-09-04 10:48:05 +02:00
|
|
|
}
|
2019-12-06 01:11:42 +02:00
|
|
|
// unassign
|
|
|
|
conns = nil
|
2019-09-04 10:48:05 +02:00
|
|
|
}()
|
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.closed:
|
|
|
|
return
|
2019-09-04 10:48:05 +02:00
|
|
|
case <-t.tunClosed:
|
|
|
|
t.Close()
|
|
|
|
return
|
2019-08-07 19:44:33 +02:00
|
|
|
// receive a new message
|
2019-08-30 21:05:00 +02:00
|
|
|
case m := <-t.session.recv:
|
2019-12-08 02:53:55 +02:00
|
|
|
var sessionId string
|
|
|
|
var linkId string
|
|
|
|
|
2019-12-12 19:10:32 +02:00
|
|
|
switch t.session.mode {
|
2019-12-08 02:53:55 +02:00
|
|
|
case Multicast:
|
|
|
|
sessionId = "multicast"
|
|
|
|
linkId = "multicast"
|
|
|
|
case Broadcast:
|
|
|
|
sessionId = "broadcast"
|
|
|
|
linkId = "broadcast"
|
|
|
|
default:
|
|
|
|
sessionId = m.session
|
|
|
|
linkId = m.link
|
|
|
|
}
|
2019-12-06 01:11:42 +02:00
|
|
|
|
2019-08-30 21:05:00 +02:00
|
|
|
// get a session
|
2019-12-06 01:11:42 +02:00
|
|
|
sess, ok := conns[sessionId]
|
2019-12-08 02:53:55 +02:00
|
|
|
log.Tracef("Tunnel listener received channel %s session %s type %s exists: %t", m.channel, m.session, m.typ, ok)
|
2019-08-07 19:44:33 +02:00
|
|
|
if !ok {
|
2019-11-30 03:14:40 +02:00
|
|
|
// we only process open and session types
|
2019-09-04 16:55:37 +02:00
|
|
|
switch m.typ {
|
|
|
|
case "open", "session":
|
|
|
|
default:
|
2019-09-04 10:48:05 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-30 21:05:00 +02:00
|
|
|
// create a new session session
|
|
|
|
sess = &session{
|
2020-02-24 16:15:20 +02:00
|
|
|
// the session key
|
|
|
|
key: t.token + m.channel + sessionId,
|
2019-08-30 21:05:00 +02:00
|
|
|
// the id of the remote side
|
2019-09-03 16:56:37 +02:00
|
|
|
tunnel: m.tunnel,
|
2019-08-30 21:05:00 +02:00
|
|
|
// the channel
|
|
|
|
channel: m.channel,
|
2019-08-07 19:44:33 +02:00
|
|
|
// the session id
|
2019-12-06 01:11:42 +02:00
|
|
|
session: sessionId,
|
2019-11-25 20:56:00 +02:00
|
|
|
// tunnel token
|
|
|
|
token: t.token,
|
2019-08-29 00:12:22 +02:00
|
|
|
// is loopback conn
|
|
|
|
loopback: m.loopback,
|
2019-08-29 13:42:27 +02:00
|
|
|
// the link the message was received on
|
2019-12-08 02:53:55 +02:00
|
|
|
link: linkId,
|
2019-10-15 16:40:04 +02:00
|
|
|
// set the connection mode
|
2019-12-12 19:10:32 +02:00
|
|
|
mode: t.session.mode,
|
2019-08-07 19:44:33 +02:00
|
|
|
// close chan
|
|
|
|
closed: make(chan bool),
|
|
|
|
// recv called by the acceptor
|
|
|
|
recv: make(chan *message, 128),
|
|
|
|
// use the internal send buffer
|
2019-08-30 21:05:00 +02:00
|
|
|
send: t.session.send,
|
|
|
|
// error channel
|
|
|
|
errChan: make(chan error, 1),
|
2019-12-08 01:28:39 +02:00
|
|
|
// set the read timeout
|
|
|
|
readTimeout: t.session.readTimeout,
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 21:05:00 +02:00
|
|
|
// save the session
|
2019-12-06 01:11:42 +02:00
|
|
|
conns[sessionId] = sess
|
2019-08-07 19:44:33 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-t.closed:
|
|
|
|
return
|
2019-09-04 10:48:05 +02:00
|
|
|
// send to accept chan
|
2019-08-30 21:05:00 +02:00
|
|
|
case t.accept <- sess:
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
2019-09-04 10:48:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// an existing session was found
|
|
|
|
|
|
|
|
switch m.typ {
|
|
|
|
case "close":
|
2019-12-12 19:10:32 +02:00
|
|
|
// don't close multicast sessions
|
|
|
|
if sess.mode > Unicast {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-06 01:11:42 +02:00
|
|
|
// received a close message
|
2019-09-04 10:48:05 +02:00
|
|
|
select {
|
2019-12-06 01:11:42 +02:00
|
|
|
// check if the session is closed
|
2019-09-04 10:48:05 +02:00
|
|
|
case <-sess.closed:
|
|
|
|
// no op
|
2019-12-06 01:11:42 +02:00
|
|
|
delete(conns, sessionId)
|
2019-09-04 10:48:05 +02:00
|
|
|
default:
|
2019-12-08 01:28:39 +02:00
|
|
|
// only close if unicast session
|
|
|
|
// close and delete session
|
|
|
|
close(sess.closed)
|
|
|
|
delete(conns, sessionId)
|
2019-09-04 10:48:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// continue
|
|
|
|
continue
|
|
|
|
case "session":
|
|
|
|
// operate on this
|
|
|
|
default:
|
|
|
|
// non operational type
|
|
|
|
continue
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// send this to the accept chan
|
|
|
|
select {
|
2019-08-30 21:05:00 +02:00
|
|
|
case <-sess.closed:
|
2019-12-06 01:11:42 +02:00
|
|
|
delete(conns, sessionId)
|
2019-08-30 21:05:00 +02:00
|
|
|
case sess.recv <- m:
|
2019-12-07 21:54:29 +02:00
|
|
|
log.Tracef("Tunnel listener sent to recv chan channel %s session %s type %s", m.channel, sessionId, m.typ)
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 21:05:00 +02:00
|
|
|
func (t *tunListener) Channel() string {
|
|
|
|
return t.channel
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
|
|
|
|
2019-08-08 16:20:53 +02:00
|
|
|
// Close closes tunnel listener
|
2019-08-07 19:44:33 +02:00
|
|
|
func (t *tunListener) Close() error {
|
2019-12-12 19:10:32 +02:00
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
select {
|
|
|
|
case <-t.closed:
|
|
|
|
return nil
|
|
|
|
default:
|
2019-09-04 10:48:05 +02:00
|
|
|
// close and delete
|
|
|
|
t.delFunc()
|
|
|
|
t.session.Close()
|
2019-08-07 19:44:33 +02:00
|
|
|
close(t.closed)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everytime accept is called we essentially block till we get a new connection
|
2019-08-30 21:05:00 +02:00
|
|
|
func (t *tunListener) Accept() (Session, error) {
|
2019-08-07 19:44:33 +02:00
|
|
|
select {
|
2019-08-30 21:05:00 +02:00
|
|
|
// if the session is closed return
|
2019-08-07 19:44:33 +02:00
|
|
|
case <-t.closed:
|
|
|
|
return nil, io.EOF
|
2019-08-08 16:20:53 +02:00
|
|
|
case <-t.tunClosed:
|
|
|
|
// close the listener when the tunnel closes
|
|
|
|
return nil, io.EOF
|
2019-08-07 19:44:33 +02:00
|
|
|
// wait for a new connection
|
|
|
|
case c, ok := <-t.accept:
|
2019-09-04 10:48:05 +02:00
|
|
|
// check if the accept chan is closed
|
2019-08-07 19:44:33 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
2019-12-06 02:18:40 +02:00
|
|
|
// return without accept
|
|
|
|
if c.mode != Unicast {
|
|
|
|
return c, nil
|
|
|
|
}
|
2019-09-04 10:48:05 +02:00
|
|
|
// send back the accept
|
|
|
|
if err := c.Accept(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-07 19:44:33 +02:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
}
|