Don't process unless connected, and only fire loopback messages back up the loopback

This commit is contained in:
Asim Aslam 2019-08-28 23:12:22 +01:00
parent a4f5772555
commit 6ab86c9e57
3 changed files with 82 additions and 33 deletions

View File

@ -49,8 +49,18 @@ type tun struct {
type link struct { type link struct {
transport.Socket transport.Socket
id string // unique id of this link e.g uuid
loopback bool // which we define for ourselves
id string
// whether its a loopback connection
loopback bool
// whether its actually connected
// dialled side sets it to connected
// after sending the message. the
// listener waits for the connect
connected bool
// the last time we received a keepalive
// on this link from the remote side
lastKeepAlive time.Time lastKeepAlive time.Time
} }
@ -190,9 +200,25 @@ func (t *tun) process() {
log.Debugf("No links to send to") log.Debugf("No links to send to")
} }
for node, link := range t.links { for node, link := range t.links {
// if the link is not connected skip it
if !link.connected {
log.Debugf("Link for node %s not connected", node)
continue
}
// if the link was a loopback accepted connection
// and the message is being sent outbound via
// a dialled connection don't use this link
if link.loopback && msg.outbound { if link.loopback && msg.outbound {
continue continue
} }
// if the message was being returned by the loopback listener
// send it back up the loopback link only
if msg.loopback && !link.loopback {
continue
}
log.Debugf("Sending %+v to %s", newMsg, node) log.Debugf("Sending %+v to %s", newMsg, node)
if err := link.Send(newMsg); err != nil { if err := link.Send(newMsg); err != nil {
log.Debugf("Tunnel error sending %+v to %s: %v", newMsg, node, err) log.Debugf("Tunnel error sending %+v to %s: %v", newMsg, node, err)
@ -209,15 +235,22 @@ func (t *tun) process() {
// process incoming messages // process incoming messages
func (t *tun) listen(link *link) { func (t *tun) listen(link *link) {
// remove the link on exit
defer func() {
log.Debugf("Tunnel deleting connection from %s", link.Remote())
t.Lock()
delete(t.links, link.Remote())
t.Unlock()
}()
// let us know if its a loopback
var loopback bool
for { for {
// process anything via the net interface // process anything via the net interface
msg := new(transport.Message) msg := new(transport.Message)
err := link.Recv(msg) if err := link.Recv(msg); err != nil {
if err != nil {
log.Debugf("Tunnel link %s receive error: %#v", link.Remote(), err) log.Debugf("Tunnel link %s receive error: %#v", link.Remote(), err)
t.Lock()
delete(t.links, link.Remote())
t.Unlock()
return return
} }
@ -232,11 +265,18 @@ func (t *tun) listen(link *link) {
// are we connecting to ourselves? // are we connecting to ourselves?
if token == t.token { if token == t.token {
t.Lock()
link.loopback = true link.loopback = true
t.Unlock() loopback = true
} }
// set as connected
link.connected = true
// save the link once connected
t.Lock()
t.links[link.Remote()] = link
t.Unlock()
// nothing more to do // nothing more to do
continue continue
case "close": case "close":
@ -258,6 +298,11 @@ func (t *tun) listen(link *link) {
continue continue
} }
// if its not connected throw away the link
if !link.connected {
return
}
// strip message header // strip message header
delete(msg.Header, "Micro-Tunnel") delete(msg.Header, "Micro-Tunnel")
@ -283,8 +328,10 @@ func (t *tun) listen(link *link) {
var s *socket var s *socket
var exists bool var exists bool
// If its a loopback connection then we've enabled link direction
// listening side is used for listening, the dialling side for dialling
switch { switch {
case link.loopback: case loopback:
s, exists = t.getSocket(id, "listener") s, exists = t.getSocket(id, "listener")
default: default:
// get the socket based on the tunnel id and session // get the socket based on the tunnel id and session
@ -298,6 +345,7 @@ func (t *tun) listen(link *link) {
s, exists = t.getSocket(id, "listener") s, exists = t.getSocket(id, "listener")
} }
} }
// bail if no socket has been found // bail if no socket has been found
if !exists { if !exists {
log.Debugf("Tunnel skipping no socket exists") log.Debugf("Tunnel skipping no socket exists")
@ -337,9 +385,10 @@ func (t *tun) listen(link *link) {
// construct the internal message // construct the internal message
imsg := &message{ imsg := &message{
id: id, id: id,
session: session, session: session,
data: tmsg, data: tmsg,
loopback: loopback,
} }
// append to recv backlog // append to recv backlog
@ -399,13 +448,14 @@ func (t *tun) setupLink(node string) (*link, error) {
return nil, err return nil, err
} }
// save the link // create a new link
id := uuid.New().String()
link := &link{ link := &link{
Socket: c, Socket: c,
id: id, id: uuid.New().String(),
// we made the outbound connection
// and sent the connect message
connected: true,
} }
t.links[node] = link
// process incoming messages // process incoming messages
go t.listen(link) go t.listen(link)
@ -430,25 +480,16 @@ func (t *tun) connect() error {
// accept inbound connections // accept inbound connections
err := l.Accept(func(sock transport.Socket) { err := l.Accept(func(sock transport.Socket) {
log.Debugf("Tunnel accepted connection from %s", sock.Remote()) log.Debugf("Tunnel accepted connection from %s", sock.Remote())
// save the link
id := uuid.New().String() // create a new link
t.Lock()
link := &link{ link := &link{
Socket: sock, Socket: sock,
id: id, id: uuid.New().String(),
} }
t.links[sock.Remote()] = link
t.Unlock()
// delete the link // listen for inbound messages.
defer func() { // only save the link once connected.
log.Debugf("Tunnel deleting connection from %s", sock.Remote()) // we do this inside liste
t.Lock()
delete(t.links, sock.Remote())
t.Unlock()
}()
// listen for inbound messages
t.listen(link) t.listen(link)
}) })
@ -473,6 +514,7 @@ func (t *tun) connect() error {
log.Debugf("Tunnel failed to establish node link to %s: %v", node, err) log.Debugf("Tunnel failed to establish node link to %s: %v", node, err)
continue continue
} }
// save the link // save the link
t.links[node] = link t.links[node] = link
} }

View File

@ -41,6 +41,8 @@ func (t *tunListener) process() {
id: m.id, id: m.id,
// the session id // the session id
session: m.session, session: m.session,
// is loopback conn
loopback: m.loopback,
// close chan // close chan
closed: make(chan bool), closed: make(chan bool),
// recv called by the acceptor // recv called by the acceptor

View File

@ -25,8 +25,10 @@ type socket struct {
recv chan *message recv chan *message
// wait until we have a connection // wait until we have a connection
wait chan bool wait chan bool
// outbound marks the socket as outbound // outbound marks the socket as outbound dialled connection
outbound bool outbound bool
// lookback marks the socket as a loopback on the inbound
loopback bool
} }
// message is sent over the send channel // message is sent over the send channel
@ -37,6 +39,8 @@ type message struct {
session string session string
// outbound marks the message as outbound // outbound marks the message as outbound
outbound bool outbound bool
// loopback marks the message intended for loopback
loopback bool
// transport data // transport data
data *transport.Message data *transport.Message
} }
@ -80,6 +84,7 @@ func (s *socket) Send(m *transport.Message) error {
id: s.id, id: s.id,
session: s.session, session: s.session,
outbound: s.outbound, outbound: s.outbound,
loopback: s.loopback,
data: data, data: data,
} }
log.Debugf("Appending %+v to send backlog", msg) log.Debugf("Appending %+v to send backlog", msg)