first commit to draft up a way for Sending messages to loopback

This commit is contained in:
Milos Gajdos
2019-08-13 20:11:23 +01:00
parent 7ce0305db4
commit 750267b308

View File

@@ -17,6 +17,9 @@ type tun struct {
sync.RWMutex sync.RWMutex
// tunnel token
token string
// to indicate if we're connected or not // to indicate if we're connected or not
connected bool connected bool
@@ -50,6 +53,7 @@ func newTunnel(opts ...Option) *tun {
return &tun{ return &tun{
options: options, options: options,
token: uuid.New().String(),
send: make(chan *message, 128), send: make(chan *message, 128),
closed: make(chan bool), closed: make(chan bool),
sockets: make(map[string]*socket), sockets: make(map[string]*socket),
@@ -144,7 +148,7 @@ func (t *tun) process() {
} }
// process incoming messages // process incoming messages
func (t *tun) listen(link transport.Socket, listener bool) { func (t *tun) listen(link transport.Socket) {
for { for {
// process anything via the net interface // process anything via the net interface
msg := new(transport.Message) msg := new(transport.Message)
@@ -154,11 +158,24 @@ func (t *tun) listen(link transport.Socket, listener bool) {
return return
} }
var loopback bool
switch msg.Header["Micro-Tunnel"] { switch msg.Header["Micro-Tunnel"] {
case "connect", "close": case "connect":
// TODO: handle the connect/close message // TODO: handle the connect message
// maybe used to create the dial/listen sockets // check the Micro-Tunnel-Token
// or report io.EOF or maybe to kill the link token, ok := msg.Header["Micro-Tunnel-Token"]
if !ok {
// no token found; bailing
continue
}
// are we connecting to ourselves?
if token == t.token {
loopback = true
}
case "close":
// TODO: handle the close message
// maybe report io.EOF or kill the link
continue continue
} }
@@ -182,18 +199,17 @@ func (t *tun) listen(link transport.Socket, listener bool) {
var exists bool var exists bool
log.Debugf("Received %+v from %s", msg, link.Remote()) log.Debugf("Received %+v from %s", msg, link.Remote())
// get the socket based on the tunnel id and session
// this could be something we dialed in which case
// we have a session for it otherwise its a listener
s, exists = t.getSocket(id, session)
if !exists {
// try get it based on just the tunnel id
// the assumption here is that a listener
// has no session but its set a listener session
s, exists = t.getSocket(id, "listener")
}
// no socket in existence switch {
case loopback:
s, exists = t.getSocket(id, "listener")
default:
// get the socket based on the tunnel id and session
// this could be something we dialed in which case
// we have a session for it otherwise its a listener
s, exists = t.getSocket(id, session)
}
// 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")
// drop it, we don't care about // drop it, we don't care about
@@ -277,7 +293,7 @@ func (t *tun) connect() error {
}() }()
// listen for inbound messages // listen for inbound messages
t.listen(sock, true) t.listen(sock)
}) })
t.Lock() t.Lock()
@@ -306,14 +322,15 @@ func (t *tun) connect() error {
if err := c.Send(&transport.Message{ if err := c.Send(&transport.Message{
Header: map[string]string{ Header: map[string]string{
"Micro-Tunnel": "connect", "Micro-Tunnel": "connect",
"Micro-Tunnel-Token": t.token,
}, },
}); err != nil { }); err != nil {
continue continue
} }
// process incoming messages // process incoming messages
go t.listen(c, false) go t.listen(c)
// save the link // save the link
id := uuid.New().String() id := uuid.New().String()