micro/tunnel/default.go

1140 lines
24 KiB
Go
Raw Normal View History

2019-08-05 21:41:48 +03:00
package tunnel
import (
2019-08-07 20:44:33 +03:00
"errors"
"math/rand"
2019-08-30 22:05:00 +03:00
"strings"
2019-08-05 21:41:48 +03:00
"sync"
"time"
2019-08-05 21:41:48 +03:00
2019-08-07 20:44:33 +03:00
"github.com/google/uuid"
2019-08-05 21:41:48 +03:00
"github.com/micro/go-micro/transport"
2019-08-07 20:44:33 +03:00
"github.com/micro/go-micro/util/log"
2019-08-05 21:41:48 +03:00
)
var (
// DiscoverTime sets the time at which we fire discover messages
DiscoverTime = 60 * time.Second
// KeepAliveTime defines time interval we send keepalive messages to outbound links
KeepAliveTime = 30 * time.Second
// ReconnectTime defines time interval we periodically attempt to reconnect dead links
ReconnectTime = 5 * time.Second
)
2019-08-07 20:44:33 +03:00
// tun represents a network tunnel
2019-08-05 21:41:48 +03:00
type tun struct {
2019-08-07 20:44:33 +03:00
options Options
2019-08-05 21:41:48 +03:00
sync.RWMutex
2019-08-07 20:44:33 +03:00
2019-08-30 22:05:00 +03:00
// the unique id for this tunnel
id string
// tunnel token for authentication
token string
2019-08-07 20:44:33 +03:00
// to indicate if we're connected or not
2019-08-05 21:41:48 +03:00
connected bool
2019-08-07 20:44:33 +03:00
// the send channel for all messages
send chan *message
// close channel
closed chan bool
2019-08-30 22:05:00 +03:00
// a map of sessions based on Micro-Tunnel-Channel
sessions map[string]*session
2019-08-07 20:44:33 +03:00
// outbound links
links map[string]*link
// listener
listener transport.Listener
2019-08-05 21:41:48 +03:00
}
2019-08-07 20:44:33 +03:00
// create new tunnel on top of a link
func newTunnel(opts ...Option) *tun {
options := DefaultOptions()
2019-08-05 21:41:48 +03:00
for _, o := range opts {
o(&options)
}
2019-08-07 20:44:33 +03:00
return &tun{
2019-08-30 22:05:00 +03:00
options: options,
id: options.Id,
token: options.Token,
send: make(chan *message, 128),
closed: make(chan bool),
sessions: make(map[string]*session),
links: make(map[string]*link),
2019-08-05 21:41:48 +03:00
}
2019-08-07 20:44:33 +03:00
}
2019-08-05 21:41:48 +03:00
2019-08-14 15:26:23 +03:00
// Init initializes tunnel options
func (t *tun) Init(opts ...Option) error {
t.Lock()
defer t.Unlock()
2019-08-14 15:26:23 +03:00
for _, o := range opts {
o(&t.options)
}
return nil
}
2019-08-30 22:05:00 +03:00
// getSession returns a session from the internal session map.
// It does this based on the Micro-Tunnel-Channel and Micro-Tunnel-Session
func (t *tun) getSession(channel, session string) (*session, bool) {
// get the session
2019-08-07 20:44:33 +03:00
t.RLock()
2019-08-30 22:05:00 +03:00
s, ok := t.sessions[channel+session]
2019-08-07 20:44:33 +03:00
t.RUnlock()
return s, ok
2019-08-05 21:41:48 +03:00
}
func (t *tun) delSession(channel, session string) {
t.Lock()
delete(t.sessions, channel+session)
t.Unlock()
}
// listChannels returns a list of listening channels
func (t *tun) listChannels() []string {
t.RLock()
defer t.RUnlock()
var channels []string
for _, session := range t.sessions {
if session.session != "listener" {
continue
}
channels = append(channels, session.channel)
}
return channels
}
2019-08-30 22:05:00 +03:00
// newSession creates a new session and saves it
func (t *tun) newSession(channel, sessionId string) (*session, bool) {
// new session
s := &session{
2019-09-03 17:56:37 +03:00
tunnel: t.id,
2019-08-30 22:05:00 +03:00
channel: channel,
session: sessionId,
2019-08-07 20:44:33 +03:00
closed: make(chan bool),
recv: make(chan *message, 128),
send: t.send,
wait: make(chan bool),
2019-08-30 22:05:00 +03:00
errChan: make(chan error, 1),
2019-08-07 20:44:33 +03:00
}
2019-08-30 22:05:00 +03:00
// save session
2019-08-07 20:44:33 +03:00
t.Lock()
2019-08-30 22:05:00 +03:00
_, ok := t.sessions[channel+sessionId]
2019-08-07 20:44:33 +03:00
if ok {
2019-08-30 22:05:00 +03:00
// session already exists
2019-08-07 20:44:33 +03:00
t.Unlock()
return nil, false
}
2019-08-30 22:05:00 +03:00
t.sessions[channel+sessionId] = s
2019-08-07 20:44:33 +03:00
t.Unlock()
2019-08-30 22:05:00 +03:00
// return session
2019-08-07 20:44:33 +03:00
return s, true
2019-08-05 21:41:48 +03:00
}
2019-08-07 20:44:33 +03:00
// TODO: use tunnel id as part of the session
2019-08-30 22:05:00 +03:00
func (t *tun) newSessionId() string {
2019-08-07 20:44:33 +03:00
return uuid.New().String()
2019-08-06 13:45:25 +03:00
}
func (t *tun) announce(channel, session string, link *link) {
// create the "announce" response message for a discover request
msg := &transport.Message{
Header: map[string]string{
"Micro-Tunnel": "announce",
"Micro-Tunnel-Id": t.id,
"Micro-Tunnel-Channel": channel,
"Micro-Tunnel-Session": session,
"Micro-Tunnel-Link": link.id,
"Micro-Tunnel-Token": t.token,
},
}
// if no channel is present we've been asked to discover all channels
if len(channel) == 0 {
// get the list of channels
channels := t.listChannels()
// if there are no channels continue
if len(channels) == 0 {
return
}
// create a list of channels as comma separated list
channel = strings.Join(channels, ",")
// set channels as header
msg.Header["Micro-Tunnel-Channel"] = channel
} else {
// otherwise look for a single channel mapping
// looking for existing mapping as a listener
_, exists := t.getSession(channel, "listener")
if !exists {
return
}
}
log.Debugf("Tunnel sending announce for discovery of channel(s) %s", channel)
// send back the announcement
if err := link.Send(msg); err != nil {
log.Debugf("Tunnel failed to send announcement for channel(s) %s message: %v", channel, err)
}
}
// monitor monitors outbound links and attempts to reconnect to the failed ones
func (t *tun) monitor() {
reconnect := time.NewTicker(ReconnectTime)
defer reconnect.Stop()
for {
select {
case <-t.closed:
return
case <-reconnect.C:
t.RLock()
var delLinks []string
// check the link status and purge dead links
for node, link := range t.links {
// check link status
switch link.Status() {
case "closed":
delLinks = append(delLinks, node)
case "error":
delLinks = append(delLinks, node)
}
}
t.RUnlock()
// delete the dead links
if len(delLinks) > 0 {
t.Lock()
for _, node := range delLinks {
2019-09-25 14:36:07 +03:00
log.Debugf("Tunnel deleting dead link for %s", node)
link := t.links[node]
link.Close()
delete(t.links, node)
}
t.Unlock()
}
// check current link status
2019-09-02 14:05:47 +03:00
var connect []string
// build list of unknown nodes to connect to
t.RLock()
for _, node := range t.options.Nodes {
if _, ok := t.links[node]; !ok {
2019-09-02 14:05:47 +03:00
connect = append(connect, node)
}
}
t.RUnlock()
for _, node := range connect {
// create new link
link, err := t.setupLink(node)
if err != nil {
log.Debugf("Tunnel failed to setup node link to %s: %v", node, err)
continue
}
2019-09-02 14:05:47 +03:00
// save the link
t.Lock()
t.links[node] = link
t.Unlock()
}
}
}
}
2019-08-30 22:05:00 +03:00
// process outgoing messages sent by all local sessions
2019-08-07 20:44:33 +03:00
func (t *tun) process() {
// manage the send buffer
2019-08-30 22:05:00 +03:00
// all pseudo sessions throw everything down this
2019-08-07 20:44:33 +03:00
for {
select {
case msg := <-t.send:
newMsg := &transport.Message{
Header: make(map[string]string),
2019-08-07 20:44:33 +03:00
}
// set the data
if msg.data != nil {
for k, v := range msg.data.Header {
newMsg.Header[k] = v
}
newMsg.Body = msg.data.Body
2019-08-08 14:45:37 +03:00
}
2019-08-20 19:20:21 +03:00
// set message head
2019-08-29 14:42:27 +03:00
newMsg.Header["Micro-Tunnel"] = msg.typ
2019-08-20 19:20:21 +03:00
2019-08-07 20:44:33 +03:00
// set the tunnel id on the outgoing message
2019-09-03 17:56:37 +03:00
newMsg.Header["Micro-Tunnel-Id"] = msg.tunnel
2019-08-07 20:44:33 +03:00
2019-08-30 22:05:00 +03:00
// set the tunnel channel on the outgoing message
newMsg.Header["Micro-Tunnel-Channel"] = msg.channel
2019-08-07 20:44:33 +03:00
// set the session id
newMsg.Header["Micro-Tunnel-Session"] = msg.session
2019-08-07 20:44:33 +03:00
// set the tunnel token
newMsg.Header["Micro-Tunnel-Token"] = t.token
2019-08-07 20:44:33 +03:00
// send the message via the interface
t.RLock()
2019-08-29 14:42:27 +03:00
2019-08-11 20:11:33 +03:00
if len(t.links) == 0 {
log.Debugf("No links to send message type: %s channel: %s", msg.typ, msg.channel)
2019-08-11 20:11:33 +03:00
}
2019-08-29 14:42:27 +03:00
2019-08-30 22:05:00 +03:00
var sent bool
var err error
var sendTo []*link
2019-08-30 22:05:00 +03:00
// build the list of links ot send to
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)
2019-08-30 22:05:00 +03:00
err = errors.New("link not connected")
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
2019-08-14 19:14:39 +03:00
if link.loopback && msg.outbound {
2019-08-30 22:05:00 +03:00
err = errors.New("link is loopback")
2019-08-14 19:14:39 +03:00
continue
}
// if the message was being returned by the loopback listener
// send it back up the loopback link only
if msg.loopback && !link.loopback {
2019-08-30 22:05:00 +03:00
err = errors.New("link is not loopback")
continue
}
// check the multicast mappings
if msg.multicast {
link.RLock()
_, ok := link.channels[msg.channel]
link.RUnlock()
// channel mapping not found in link
if !ok {
continue
}
2019-09-04 20:46:20 +03:00
} else {
// if we're picking the link check the id
// this is where we explicitly set the link
// in a message received via the listen method
if len(msg.link) > 0 && link.id != msg.link {
err = errors.New("link not found")
continue
}
}
// add to link list
sendTo = append(sendTo, link)
}
t.RUnlock()
// send the message
for _, link := range sendTo {
2019-08-29 14:42:27 +03:00
// send the message via the current link
log.Debugf("Sending %+v to %s", newMsg, link.Remote())
2019-08-30 22:05:00 +03:00
if errr := link.Send(newMsg); errr != nil {
log.Debugf("Tunnel error sending %+v to %s: %v", newMsg, link.Remote(), errr)
2019-08-30 22:05:00 +03:00
err = errors.New(errr.Error())
t.delLink(link.Remote())
continue
}
2019-08-30 22:05:00 +03:00
// is sent
sent = true
// keep sending broadcast messages
if msg.broadcast || msg.multicast {
continue
}
// break on unicast
break
2019-08-07 20:44:33 +03:00
}
2019-08-29 14:42:27 +03:00
var gerr error
2019-08-30 22:05:00 +03:00
// set the error if not sent
2019-08-30 22:05:00 +03:00
if !sent {
gerr = err
}
// skip if its not been set
if msg.errChan == nil {
continue
}
2019-08-30 22:05:00 +03:00
// return error non blocking
select {
case msg.errChan <- gerr:
default:
}
2019-08-07 20:44:33 +03:00
case <-t.closed:
return
}
}
2019-08-05 21:41:48 +03:00
}
func (t *tun) delLink(remote string) {
t.Lock()
defer t.Unlock()
// get the link
for id, link := range t.links {
if link.id != remote {
continue
}
// close and delete
2019-09-13 03:40:47 +03:00
log.Debugf("Tunnel deleting link node: %s remote: %s", id, link.Remote())
link.Close()
delete(t.links, id)
}
}
2019-08-07 20:44:33 +03:00
// process incoming messages
2019-08-14 19:14:39 +03:00
func (t *tun) listen(link *link) {
// remove the link on exit
defer func() {
t.delLink(link.Remote())
}()
// let us know if its a loopback
var loopback bool
2019-08-07 20:44:33 +03:00
for {
// process anything via the net interface
msg := new(transport.Message)
if err := link.Recv(msg); err != nil {
log.Debugf("Tunnel link %s receive error: %#v", link.Remote(), err)
2019-08-07 20:44:33 +03:00
return
}
2019-08-30 22:05:00 +03:00
// always ensure we have the correct auth token
// TODO: segment the tunnel based on token
// e.g use it as the basis
token := msg.Header["Micro-Tunnel-Token"]
if token != t.token {
log.Debugf("Tunnel link %s received invalid token %s", token)
return
}
2019-09-03 17:56:37 +03:00
// message type
mtype := msg.Header["Micro-Tunnel"]
// the tunnel id
id := msg.Header["Micro-Tunnel-Id"]
// the tunnel channel
channel := msg.Header["Micro-Tunnel-Channel"]
// the session id
sessionId := msg.Header["Micro-Tunnel-Session"]
// if its not connected throw away the link
// the first message we process needs to be connect
if !link.connected && mtype != "connect" {
log.Debugf("Tunnel link %s not connected", link.id)
return
}
2019-09-03 17:56:37 +03:00
switch mtype {
case "connect":
log.Debugf("Tunnel link %s received connect message", link.Remote())
2019-08-30 22:05:00 +03:00
2019-09-04 20:46:20 +03:00
link.Lock()
// are we connecting to ourselves?
2019-08-30 22:05:00 +03:00
if id == t.id {
2019-08-14 19:14:39 +03:00
link.loopback = true
loopback = true
}
2019-08-20 19:20:21 +03:00
// set to remote node
2019-09-13 03:12:49 +03:00
link.id = link.Remote()
// set as connected
link.connected = true
2019-09-04 20:46:20 +03:00
link.Unlock()
// save the link once connected
t.Lock()
t.links[link.Remote()] = link
t.Unlock()
// send back a discovery
go t.announce("", "", link)
2019-08-20 19:20:21 +03:00
// nothing more to do
continue
case "close":
// TODO: handle the close message
// maybe report io.EOF or kill the link
// close the link entirely
if len(channel) == 0 {
log.Debugf("Tunnel link %s received close message", link.Remote())
return
}
// the entire listener was closed so remove it from the mapping
if sessionId == "listener" {
link.Lock()
delete(link.channels, channel)
link.Unlock()
continue
}
// try get the dialing socket
s, exists := t.getSession(channel, sessionId)
if exists {
// close and continue
s.Close()
continue
}
// otherwise its a session mapping of sorts
case "keepalive":
log.Debugf("Tunnel link %s received keepalive", link.Remote())
2019-09-13 03:12:49 +03:00
link.Lock()
2019-08-29 14:42:27 +03:00
// save the keepalive
link.lastKeepAlive = time.Now()
2019-09-13 03:12:49 +03:00
link.Unlock()
continue
// a new connection dialled outbound
case "open":
2019-09-04 20:46:20 +03:00
log.Debugf("Tunnel link %s received open %s %s", link.id, channel, sessionId)
// we just let it pass through to be processed
// an accept returned by the listener
case "accept":
2019-09-04 20:46:20 +03:00
s, exists := t.getSession(channel, sessionId)
// we don't need this
if exists && s.multicast {
s.accepted = true
continue
}
if exists && s.accepted {
continue
}
// a continued session
2019-09-03 17:56:37 +03:00
case "session":
2019-08-20 19:20:21 +03:00
// process message
log.Debugf("Received %+v from %s", msg, link.Remote())
// an announcement of a channel listener
case "announce":
// process the announcement
channels := strings.Split(channel, ",")
// update mapping in the link
link.Lock()
for _, channel := range channels {
link.channels[channel] = time.Now()
}
link.Unlock()
// this was an announcement not intended for anything
if sessionId == "listener" || sessionId == "" {
continue
}
// get the session that asked for the discovery
s, exists := t.getSession(channel, sessionId)
if exists {
// don't bother it's already discovered
if s.discovered {
continue
}
// send the announce back to the caller
s.recv <- &message{
typ: "announce",
tunnel: id,
channel: channel,
session: sessionId,
link: link.id,
}
}
continue
case "discover":
// send back an announcement
go t.announce(channel, sessionId, link)
continue
2019-08-20 19:20:21 +03:00
default:
// blackhole it
continue
2019-08-07 20:44:33 +03:00
}
2019-08-30 22:05:00 +03:00
// strip tunnel message header
for k, _ := range msg.Header {
if strings.HasPrefix(k, "Micro-Tunnel") {
delete(msg.Header, k)
}
}
2019-08-20 19:20:21 +03:00
2019-08-07 20:44:33 +03:00
// if the session id is blank there's nothing we can do
// TODO: check this is the case, is there any reason
// why we'd have a blank session? Is the tunnel
// used for some other purpose?
2019-08-30 22:05:00 +03:00
if len(channel) == 0 || len(sessionId) == 0 {
2019-08-07 20:44:33 +03:00
continue
}
2019-08-30 22:05:00 +03:00
var s *session
2019-08-07 20:44:33 +03:00
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 {
case loopback, mtype == "open":
2019-08-30 22:05:00 +03:00
s, exists = t.getSession(channel, "listener")
// only return accept to the session
case mtype == "accept":
log.Debugf("Received accept message for %s %s", channel, sessionId)
s, exists = t.getSession(channel, sessionId)
if exists && s.accepted {
continue
}
default:
2019-08-30 22:05:00 +03:00
// get the session 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
2019-08-30 22:05:00 +03:00
s, exists = t.getSession(channel, sessionId)
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
2019-08-30 22:05:00 +03:00
s, exists = t.getSession(channel, "listener")
}
2019-08-07 20:44:33 +03:00
}
// bail if no session or listener has been found
2019-08-07 20:44:33 +03:00
if !exists {
log.Debugf("Tunnel skipping no session %s %s exists", channel, sessionId)
2019-08-07 20:44:33 +03:00
// drop it, we don't care about
// messages we don't know about
continue
}
2019-08-30 22:05:00 +03:00
// is the session closed?
2019-08-07 20:44:33 +03:00
select {
case <-s.closed:
// closed
2019-08-30 22:05:00 +03:00
delete(t.sessions, channel)
2019-08-07 20:44:33 +03:00
continue
default:
// process
}
log.Debugf("Tunnel using channel %s session %s", s.channel, s.session)
2019-08-30 22:05:00 +03:00
// is the session new?
2019-08-07 20:44:33 +03:00
select {
2019-08-30 22:05:00 +03:00
// if its new the session is actually blocked waiting
2019-08-07 20:44:33 +03:00
// for a connection. so we check if its waiting.
case <-s.wait:
// if its waiting e.g its new then we close it
default:
2019-08-30 22:05:00 +03:00
// set remote address of the session
2019-08-07 20:44:33 +03:00
s.remote = msg.Header["Remote"]
close(s.wait)
}
// construct a new transport message
tmsg := &transport.Message{
Header: msg.Header,
Body: msg.Body,
}
// construct the internal message
imsg := &message{
2019-09-03 17:56:37 +03:00
tunnel: id,
typ: mtype,
2019-08-30 22:05:00 +03:00
channel: channel,
session: sessionId,
data: tmsg,
2019-08-29 14:42:27 +03:00
link: link.id,
loopback: loopback,
2019-08-30 22:05:00 +03:00
errChan: make(chan error, 1),
2019-08-07 20:44:33 +03:00
}
// append to recv backlog
// we don't block if we can't pass it on
select {
case s.recv <- imsg:
default:
}
}
2019-08-05 21:41:48 +03:00
}
// discover sends channel discover requests periodically
func (t *tun) discover(link *link) {
tick := time.NewTicker(DiscoverTime)
defer tick.Stop()
for {
select {
case <-tick.C:
// send a discovery message to all links
if err := link.Send(&transport.Message{
Header: map[string]string{
"Micro-Tunnel": "discover",
"Micro-Tunnel-Id": t.id,
"Micro-Tunnel-Token": t.token,
},
}); err != nil {
log.Debugf("Tunnel failed to send discover to link %s: %v", link.id, err)
}
2019-09-13 03:12:49 +03:00
case <-link.closed:
return
case <-t.closed:
return
}
}
}
// keepalive periodically sends keepalive messages to link
func (t *tun) keepalive(link *link) {
keepalive := time.NewTicker(KeepAliveTime)
defer keepalive.Stop()
for {
select {
case <-t.closed:
return
2019-09-13 03:12:49 +03:00
case <-link.closed:
return
case <-keepalive.C:
// send keepalive message
log.Debugf("Tunnel sending keepalive to link: %v", link.Remote())
if err := link.Send(&transport.Message{
Header: map[string]string{
"Micro-Tunnel": "keepalive",
2019-08-30 22:05:00 +03:00
"Micro-Tunnel-Id": t.id,
"Micro-Tunnel-Token": t.token,
},
}); err != nil {
log.Debugf("Error sending keepalive to link %v: %v", link.Remote(), err)
t.delLink(link.Remote())
return
}
}
}
}
2019-08-15 21:24:24 +03:00
// setupLink connects to node and returns link if successful
// It returns error if the link failed to be established
func (t *tun) setupLink(node string) (*link, error) {
2019-08-29 18:58:07 +03:00
log.Debugf("Tunnel setting up link: %s", node)
c, err := t.options.Transport.Dial(node)
if err != nil {
log.Debugf("Tunnel failed to connect to %s: %v", node, err)
return nil, err
}
log.Debugf("Tunnel connected to %s", node)
// send the first connect message
if err := c.Send(&transport.Message{
Header: map[string]string{
"Micro-Tunnel": "connect",
2019-08-30 22:05:00 +03:00
"Micro-Tunnel-Id": t.id,
"Micro-Tunnel-Token": t.token,
},
}); err != nil {
return nil, err
}
// create a new link
2019-08-29 14:42:27 +03:00
link := newLink(c)
// set link id to remote side
link.id = c.Remote()
2019-08-29 14:42:27 +03:00
// we made the outbound connection
// and sent the connect message
link.connected = true
// process incoming messages
go t.listen(link)
// start keepalive monitor
go t.keepalive(link)
// discover things on the remote side
go t.discover(link)
return link, nil
}
2019-08-14 15:26:23 +03:00
// connect the tunnel to all the nodes and listen for incoming tunnel connections
2019-08-07 20:44:33 +03:00
func (t *tun) connect() error {
l, err := t.options.Transport.Listen(t.options.Address)
if err != nil {
return err
}
// save the listener
t.listener = l
go func() {
// accept inbound connections
err := l.Accept(func(sock transport.Socket) {
2019-08-08 15:15:30 +03:00
log.Debugf("Tunnel accepted connection from %s", sock.Remote())
// create a new link
2019-08-29 14:42:27 +03:00
link := newLink(sock)
2019-08-07 20:44:33 +03:00
2019-09-13 03:12:49 +03:00
// start keepalive monitor
go t.keepalive(link)
// discover things on the remote side
go t.discover(link)
// listen for inbound messages.
// only save the link once connected.
// we do this inside liste
2019-08-14 19:14:39 +03:00
t.listen(link)
2019-08-07 20:44:33 +03:00
})
2019-08-29 14:42:27 +03:00
t.RLock()
defer t.RUnlock()
2019-08-07 20:44:33 +03:00
// still connected but the tunnel died
if err != nil && t.connected {
log.Logf("Tunnel listener died: %v", err)
}
}()
for _, node := range t.options.Nodes {
2019-08-08 15:15:30 +03:00
// skip zero length nodes
if len(node) == 0 {
continue
}
// connect to node and return link
2019-08-15 21:24:24 +03:00
link, err := t.setupLink(node)
2019-08-07 20:44:33 +03:00
if err != nil {
log.Debugf("Tunnel failed to establish node link to %s: %v", node, err)
2019-08-07 20:44:33 +03:00
continue
}
2019-08-07 20:44:33 +03:00
// save the link
2019-09-13 03:12:49 +03:00
t.links[node] = link
2019-08-07 20:44:33 +03:00
}
// process outbound messages to be sent
// process sends to all links
go t.process()
// monitor links
go t.monitor()
2019-08-05 21:41:48 +03:00
return nil
}
2019-08-14 15:26:23 +03:00
// Connect the tunnel
func (t *tun) Connect() error {
t.Lock()
defer t.Unlock()
// already connected
if t.connected {
return nil
}
// send the connect message
if err := t.connect(); err != nil {
return err
}
// set as connected
t.connected = true
// create new close channel
t.closed = make(chan bool)
return nil
}
2019-08-07 20:44:33 +03:00
func (t *tun) close() error {
2019-09-04 20:46:20 +03:00
// close all the sessions
for id, s := range t.sessions {
s.Close()
delete(t.sessions, id)
}
2019-08-07 20:44:33 +03:00
// close all the links
for node, link := range t.links {
2019-08-07 20:44:33 +03:00
link.Send(&transport.Message{
Header: map[string]string{
"Micro-Tunnel": "close",
2019-08-30 22:05:00 +03:00
"Micro-Tunnel-Id": t.id,
"Micro-Tunnel-Token": t.token,
2019-08-07 20:44:33 +03:00
},
})
link.Close()
delete(t.links, node)
2019-08-07 20:44:33 +03:00
}
// close the listener
2019-09-05 17:23:19 +03:00
// this appears to be blocking
return t.listener.Close()
2019-08-05 21:41:48 +03:00
}
2019-08-21 14:55:10 +03:00
func (t *tun) Address() string {
t.RLock()
defer t.RUnlock()
if !t.connected {
return t.options.Address
}
return t.listener.Addr()
}
2019-08-07 20:44:33 +03:00
// Close the tunnel
func (t *tun) Close() error {
t.Lock()
defer t.Unlock()
if !t.connected {
return nil
}
log.Debug("Tunnel closing")
2019-08-05 21:41:48 +03:00
select {
case <-t.closed:
2019-08-07 20:44:33 +03:00
return nil
2019-08-05 21:41:48 +03:00
default:
2019-08-07 20:44:33 +03:00
close(t.closed)
t.connected = false
// send a close message
// we don't close the link
// just the tunnel
return t.close()
2019-08-05 21:41:48 +03:00
}
2019-08-07 20:44:33 +03:00
return nil
2019-08-05 21:41:48 +03:00
}
2019-08-07 20:44:33 +03:00
// Dial an address
func (t *tun) Dial(channel string, opts ...DialOption) (Session, error) {
2019-08-30 22:05:00 +03:00
log.Debugf("Tunnel dialing %s", channel)
c, ok := t.newSession(channel, t.newSessionId())
2019-08-07 20:44:33 +03:00
if !ok {
2019-08-30 22:05:00 +03:00
return nil, errors.New("error dialing " + channel)
2019-08-07 20:44:33 +03:00
}
// set remote
2019-08-30 22:05:00 +03:00
c.remote = channel
2019-08-07 20:44:33 +03:00
// set local
c.local = "local"
2019-08-30 22:05:00 +03:00
// outbound session
2019-08-14 19:14:39 +03:00
c.outbound = true
2019-08-07 20:44:33 +03:00
// get opts
options := DialOptions{
Timeout: DefaultDialTimeout,
}
for _, o := range opts {
o(&options)
}
// set the multicast option
c.multicast = options.Multicast
// set the dial timeout
c.timeout = options.Timeout
2019-09-04 20:46:20 +03:00
now := time.Now()
after := func() time.Duration {
d := time.Since(now)
// dial timeout minus time since
wait := options.Timeout - d
if wait < time.Duration(0) {
return time.Duration(0)
}
return wait
}
var links []string
// non multicast so we need to find the link
t.RLock()
for _, link := range t.links {
2019-09-11 21:57:41 +03:00
// use the link specified it its available
if id := options.Link; len(id) > 0 && link.id != id {
continue
}
link.RLock()
_, ok := link.channels[channel]
link.RUnlock()
// we have at least one channel mapping
if ok {
c.discovered = true
links = append(links, link.id)
}
}
t.RUnlock()
2019-09-11 21:57:41 +03:00
// link not found
if len(links) == 0 && len(options.Link) > 0 {
return nil, ErrLinkNotFound
}
2019-09-05 09:41:19 +03:00
// discovered so set the link if not multicast
// TODO: pick the link efficiently based
2019-09-04 22:19:53 +03:00
// on link status and saturation.
2019-09-05 09:41:19 +03:00
if c.discovered && !c.multicast {
// set the link
i := rand.Intn(len(links))
c.link = links[i]
}
// shit fuck
if !c.discovered {
// create a new discovery message for this channel
2019-09-04 14:16:31 +03:00
msg := c.newMessage("discover")
msg.broadcast = true
msg.outbound = true
msg.link = ""
// send the discovery message
t.send <- msg
select {
2019-09-04 20:46:20 +03:00
case <-time.After(after()):
return nil, ErrDialTimeout
case err := <-c.errChan:
if err != nil {
return nil, err
}
}
var err error
2019-09-05 19:40:41 +03:00
// set a dialTimeout
dialTimeout := after()
// set a shorter delay for multicast
if c.multicast {
// shorten this
dialTimeout = time.Millisecond * 500
}
// wait for announce
select {
case msg := <-c.recv:
if msg.typ != "announce" {
err = ErrDiscoverChan
}
2019-09-05 19:40:41 +03:00
case <-time.After(dialTimeout):
err = ErrDialTimeout
}
2019-09-05 19:40:41 +03:00
// if its multicast just go ahead because this is best effort
if c.multicast {
c.discovered = true
2019-09-05 19:40:41 +03:00
c.accepted = true
return c, nil
}
2019-09-05 19:40:41 +03:00
// otherwise return an error
if err != nil {
return nil, err
}
2019-09-05 19:40:41 +03:00
// set discovered to true
c.discovered = true
}
// a unicast session so we call "open" and wait for an "accept"
// try to open the session
err := c.Open()
if err != nil {
// delete the session
t.delSession(c.channel, c.session)
return nil, err
}
2019-08-07 20:44:33 +03:00
return c, nil
}
// Accept a connection on the address
2019-08-30 22:05:00 +03:00
func (t *tun) Listen(channel string) (Listener, error) {
log.Debugf("Tunnel listening on %s", channel)
2019-08-30 22:05:00 +03:00
// create a new session by hashing the address
c, ok := t.newSession(channel, "listener")
2019-08-07 20:44:33 +03:00
if !ok {
2019-08-30 22:05:00 +03:00
return nil, errors.New("already listening on " + channel)
2019-08-07 20:44:33 +03:00
}
delFunc := func() {
t.delSession(channel, "listener")
}
2019-08-07 20:44:33 +03:00
// set remote. it will be replaced by the first message received
c.remote = "remote"
// set local
2019-08-30 22:05:00 +03:00
c.local = channel
2019-08-07 20:44:33 +03:00
tl := &tunListener{
2019-08-30 22:05:00 +03:00
channel: channel,
2019-08-07 20:44:33 +03:00
// the accept channel
2019-08-30 22:05:00 +03:00
accept: make(chan *session, 128),
2019-08-07 20:44:33 +03:00
// the channel to close
closed: make(chan bool),
// tunnel closed channel
tunClosed: t.closed,
2019-08-30 22:05:00 +03:00
// the listener session
session: c,
// delete session
delFunc: delFunc,
2019-08-07 20:44:33 +03:00
}
// this kicks off the internal message processor
2019-08-30 22:05:00 +03:00
// for the listener so it can create pseudo sessions
2019-08-07 20:44:33 +03:00
// per session if they do not exist or pass messages
// to the existign sessions
go tl.process()
// announces the listener channel to others
go tl.announce()
2019-08-07 20:44:33 +03:00
// return the listener
return tl, nil
2019-08-05 21:41:48 +03:00
}
2019-08-20 19:21:35 +03:00
2019-09-04 17:41:57 +03:00
func (t *tun) Links() []Link {
t.RLock()
defer t.RUnlock()
var links []Link
for _, link := range t.links {
links = append(links, link)
}
return links
}
2019-08-20 19:21:35 +03:00
func (t *tun) String() string {
return "mucp"
}