2019-08-06 12:45:25 +02:00
|
|
|
package tunnel
|
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
import (
|
2019-11-25 20:56:00 +02:00
|
|
|
"encoding/hex"
|
2019-08-07 19:44:33 +02:00
|
|
|
"errors"
|
2019-08-30 21:05:00 +02:00
|
|
|
"io"
|
2019-09-04 10:48:05 +02:00
|
|
|
"time"
|
2019-08-06 12:45:25 +02:00
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
"github.com/micro/go-micro/transport"
|
2019-08-11 19:11:33 +02:00
|
|
|
"github.com/micro/go-micro/util/log"
|
2019-08-07 19:44:33 +02:00
|
|
|
)
|
2019-08-06 12:45:25 +02:00
|
|
|
|
2019-08-30 21:05:00 +02:00
|
|
|
// session is our pseudo session for transport.Socket
|
|
|
|
type session struct {
|
2019-09-03 16:56:37 +02:00
|
|
|
// the tunnel id
|
|
|
|
tunnel string
|
2019-08-30 21:05:00 +02:00
|
|
|
// the channel name
|
|
|
|
channel string
|
2019-08-07 19:44:33 +02:00
|
|
|
// the session id based on Micro.Tunnel-Session
|
|
|
|
session string
|
2019-11-25 20:56:00 +02:00
|
|
|
// token is the session token
|
|
|
|
token string
|
2019-08-07 19:44:33 +02:00
|
|
|
// closed
|
|
|
|
closed chan bool
|
|
|
|
// remote addr
|
|
|
|
remote string
|
|
|
|
// local addr
|
|
|
|
local string
|
|
|
|
// send chan
|
|
|
|
send chan *message
|
|
|
|
// recv chan
|
|
|
|
recv chan *message
|
|
|
|
// wait until we have a connection
|
|
|
|
wait chan bool
|
2019-09-04 10:48:05 +02:00
|
|
|
// if the discovery worked
|
|
|
|
discovered bool
|
|
|
|
// if the session was accepted
|
|
|
|
accepted bool
|
2019-08-30 21:05:00 +02:00
|
|
|
// outbound marks the session as outbound dialled connection
|
2019-08-14 18:14:39 +02:00
|
|
|
outbound bool
|
2019-08-30 21:05:00 +02:00
|
|
|
// lookback marks the session as a loopback on the inbound
|
2019-08-29 00:12:22 +02:00
|
|
|
loopback bool
|
2019-10-15 16:40:04 +02:00
|
|
|
// mode of the connection
|
|
|
|
mode Mode
|
2019-09-04 10:48:05 +02:00
|
|
|
// the timeout
|
|
|
|
timeout time.Duration
|
2019-08-29 13:42:27 +02:00
|
|
|
// the link on which this message was received
|
|
|
|
link string
|
2019-08-30 21:05:00 +02:00
|
|
|
// the error response
|
|
|
|
errChan chan error
|
2019-08-06 12:45:25 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
// message is sent over the send channel
|
|
|
|
type message struct {
|
2019-08-29 13:42:27 +02:00
|
|
|
// type of message
|
|
|
|
typ string
|
2019-08-07 19:44:33 +02:00
|
|
|
// tunnel id
|
2019-09-03 16:56:37 +02:00
|
|
|
tunnel string
|
2019-08-30 21:05:00 +02:00
|
|
|
// channel name
|
|
|
|
channel string
|
2019-08-07 19:44:33 +02:00
|
|
|
// the session id
|
|
|
|
session string
|
2019-08-14 18:14:39 +02:00
|
|
|
// outbound marks the message as outbound
|
|
|
|
outbound bool
|
2019-08-29 00:12:22 +02:00
|
|
|
// loopback marks the message intended for loopback
|
|
|
|
loopback bool
|
2019-10-15 16:40:04 +02:00
|
|
|
// mode of the connection
|
|
|
|
mode Mode
|
2019-08-29 13:42:27 +02:00
|
|
|
// the link to send the message on
|
|
|
|
link string
|
2019-08-07 19:44:33 +02:00
|
|
|
// transport data
|
|
|
|
data *transport.Message
|
2019-08-30 21:05:00 +02:00
|
|
|
// the error channel
|
|
|
|
errChan chan error
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 21:05:00 +02:00
|
|
|
func (s *session) Remote() string {
|
2019-08-07 19:44:33 +02:00
|
|
|
return s.remote
|
|
|
|
}
|
|
|
|
|
2019-08-30 21:05:00 +02:00
|
|
|
func (s *session) Local() string {
|
2019-08-07 19:44:33 +02:00
|
|
|
return s.local
|
2019-08-06 12:45:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-11 21:07:43 +02:00
|
|
|
func (s *session) Link() string {
|
|
|
|
return s.link
|
|
|
|
}
|
|
|
|
|
2019-08-30 21:05:00 +02:00
|
|
|
func (s *session) Id() string {
|
|
|
|
return s.session
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 21:05:00 +02:00
|
|
|
func (s *session) Channel() string {
|
|
|
|
return s.channel
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 13:18:37 +02:00
|
|
|
// newMessage creates a new message based on the session
|
2019-09-04 13:16:31 +02:00
|
|
|
func (s *session) newMessage(typ string) *message {
|
|
|
|
return &message{
|
2019-10-15 16:40:04 +02:00
|
|
|
typ: typ,
|
|
|
|
tunnel: s.tunnel,
|
|
|
|
channel: s.channel,
|
|
|
|
session: s.session,
|
|
|
|
outbound: s.outbound,
|
|
|
|
loopback: s.loopback,
|
|
|
|
mode: s.mode,
|
|
|
|
link: s.link,
|
|
|
|
errChan: s.errChan,
|
2019-09-04 10:48:05 +02:00
|
|
|
}
|
2019-09-04 13:16:31 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 15:16:22 +02:00
|
|
|
// waitFor waits for the message type required until the timeout specified
|
2019-10-25 15:22:38 +02:00
|
|
|
func (s *session) waitFor(msgType string, timeout time.Duration) (*message, error) {
|
2019-10-25 15:16:22 +02:00
|
|
|
now := time.Now()
|
|
|
|
|
2019-10-25 16:41:37 +02:00
|
|
|
after := func(timeout time.Duration) time.Duration {
|
2019-10-25 15:16:22 +02:00
|
|
|
d := time.Since(now)
|
|
|
|
// dial timeout minus time since
|
|
|
|
wait := timeout - d
|
|
|
|
|
|
|
|
if wait < time.Duration(0) {
|
|
|
|
return time.Duration(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wait
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for the message type
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-s.recv:
|
|
|
|
// ignore what we don't want
|
|
|
|
if msg.typ != msgType {
|
|
|
|
log.Debugf("Tunnel received non %s message in waiting for %s", msg.typ, msgType)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// got the message
|
2019-10-25 15:22:38 +02:00
|
|
|
return msg, nil
|
2019-10-25 16:41:37 +02:00
|
|
|
case <-time.After(after(timeout)):
|
2019-10-25 15:22:38 +02:00
|
|
|
return nil, ErrDialTimeout
|
2019-10-25 15:16:22 +02:00
|
|
|
case <-s.closed:
|
2019-10-25 15:22:38 +02:00
|
|
|
return nil, io.EOF
|
2019-10-25 15:16:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discover attempts to discover the link for a specific channel
|
|
|
|
func (s *session) Discover() error {
|
|
|
|
// create a new discovery message for this channel
|
|
|
|
msg := s.newMessage("discover")
|
|
|
|
msg.mode = Broadcast
|
|
|
|
msg.outbound = true
|
|
|
|
msg.link = ""
|
|
|
|
|
|
|
|
// send the discovery message
|
|
|
|
s.send <- msg
|
|
|
|
|
|
|
|
// set time now
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
after := func() time.Duration {
|
|
|
|
d := time.Since(now)
|
|
|
|
// dial timeout minus time since
|
|
|
|
wait := s.timeout - d
|
|
|
|
if wait < time.Duration(0) {
|
|
|
|
return time.Duration(0)
|
|
|
|
}
|
|
|
|
return wait
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait to hear back about the sent message
|
|
|
|
select {
|
|
|
|
case <-time.After(after()):
|
|
|
|
return ErrDialTimeout
|
|
|
|
case err := <-s.errChan:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// set a new dialTimeout
|
|
|
|
dialTimeout := after()
|
|
|
|
|
|
|
|
// set a shorter delay for multicast
|
|
|
|
if s.mode != Unicast {
|
|
|
|
// shorten this
|
|
|
|
dialTimeout = time.Millisecond * 500
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for announce
|
2019-10-25 15:22:38 +02:00
|
|
|
_, err = s.waitFor("announce", dialTimeout)
|
2019-10-25 15:16:22 +02:00
|
|
|
|
|
|
|
// if its multicast just go ahead because this is best effort
|
|
|
|
if s.mode != Unicast {
|
|
|
|
s.discovered = true
|
|
|
|
s.accepted = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set discovered
|
|
|
|
s.discovered = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-04 13:16:31 +02:00
|
|
|
// Open will fire the open message for the session. This is called by the dialler.
|
|
|
|
func (s *session) Open() error {
|
|
|
|
// create a new message
|
|
|
|
msg := s.newMessage("open")
|
2019-09-04 10:48:05 +02:00
|
|
|
|
|
|
|
// send open message
|
|
|
|
s.send <- msg
|
|
|
|
|
|
|
|
// wait for an error response for send
|
|
|
|
select {
|
|
|
|
case err := <-msg.errChan:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-s.closed:
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
2019-10-15 16:40:04 +02:00
|
|
|
// don't wait on multicast/broadcast
|
2019-10-15 16:55:08 +02:00
|
|
|
if s.mode == Multicast {
|
2019-09-04 10:48:05 +02:00
|
|
|
s.accepted = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// now wait for the accept
|
2019-10-25 15:22:38 +02:00
|
|
|
msg, err := s.waitFor("accept", s.timeout)
|
|
|
|
if err != nil {
|
2019-10-25 15:16:22 +02:00
|
|
|
return err
|
2019-09-04 10:48:05 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 15:16:22 +02:00
|
|
|
// set to accepted
|
|
|
|
s.accepted = true
|
|
|
|
// set link
|
|
|
|
s.link = msg.link
|
|
|
|
|
2019-09-04 10:48:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-04 12:58:03 +02:00
|
|
|
// Accept sends the accept response to an open message from a dialled connection
|
2019-09-04 10:48:05 +02:00
|
|
|
func (s *session) Accept() error {
|
2019-09-04 13:16:31 +02:00
|
|
|
msg := s.newMessage("accept")
|
2019-09-04 10:48:05 +02:00
|
|
|
|
|
|
|
// send the accept message
|
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
return io.EOF
|
|
|
|
case s.send <- msg:
|
2019-10-07 19:29:49 +02:00
|
|
|
// no op here
|
2019-09-04 10:48:05 +02:00
|
|
|
}
|
|
|
|
|
2019-10-15 16:40:04 +02:00
|
|
|
// don't wait on multicast/broadcast
|
2019-10-15 16:55:08 +02:00
|
|
|
if s.mode == Multicast {
|
2019-10-15 16:40:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-04 10:48:05 +02:00
|
|
|
// wait for send response
|
|
|
|
select {
|
|
|
|
case err := <-s.errChan:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-s.closed:
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-04 12:58:03 +02:00
|
|
|
// Announce sends an announcement to notify that this session exists. This is primarily used by the listener.
|
|
|
|
func (s *session) Announce() error {
|
2019-09-04 13:16:31 +02:00
|
|
|
msg := s.newMessage("announce")
|
|
|
|
// we don't need an error back
|
|
|
|
msg.errChan = nil
|
2019-09-05 16:16:11 +02:00
|
|
|
// announce to all
|
2019-10-15 16:40:04 +02:00
|
|
|
msg.mode = Broadcast
|
2019-09-04 13:16:31 +02:00
|
|
|
// we don't need the link
|
|
|
|
msg.link = ""
|
2019-09-04 12:58:03 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case s.send <- msg:
|
|
|
|
return nil
|
|
|
|
case <-s.closed:
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send is used to send a message
|
2019-08-30 21:05:00 +02:00
|
|
|
func (s *session) Send(m *transport.Message) error {
|
2019-08-07 19:44:33 +02:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
2019-09-04 10:48:05 +02:00
|
|
|
return io.EOF
|
2019-08-07 19:44:33 +02:00
|
|
|
default:
|
|
|
|
// no op
|
|
|
|
}
|
2019-08-11 19:11:33 +02:00
|
|
|
|
2019-11-25 17:34:41 +02:00
|
|
|
// encrypt the transport message payload
|
2019-11-25 20:56:00 +02:00
|
|
|
body, err := Encrypt(m.Body, s.token+s.channel+s.session)
|
2019-11-25 17:34:41 +02:00
|
|
|
if err != nil {
|
2019-11-25 20:56:00 +02:00
|
|
|
log.Debugf("failed to encrypt message body: %v", err)
|
2019-11-25 17:34:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-11 19:11:33 +02:00
|
|
|
// make copy
|
|
|
|
data := &transport.Message{
|
|
|
|
Header: make(map[string]string),
|
2019-11-25 17:34:41 +02:00
|
|
|
Body: body,
|
2019-08-11 19:11:33 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 20:56:00 +02:00
|
|
|
// encrypt all the headers
|
2019-08-11 19:11:33 +02:00
|
|
|
for k, v := range m.Header {
|
2019-11-25 20:56:00 +02:00
|
|
|
// encrypt the transport message payload
|
|
|
|
val, err := Encrypt([]byte(v), s.token+s.channel+s.session)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("failed to encrypt message header %s: %v", k, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// hex encode the encrypted header value
|
|
|
|
data.Header[k] = hex.EncodeToString(val)
|
2019-08-11 19:11:33 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 13:16:31 +02:00
|
|
|
// create a new message
|
|
|
|
msg := s.newMessage("session")
|
|
|
|
// set the data
|
|
|
|
msg.data = data
|
2019-09-04 10:48:05 +02:00
|
|
|
|
2019-09-04 13:16:31 +02:00
|
|
|
// if multicast don't set the link
|
2019-10-15 16:55:08 +02:00
|
|
|
if s.mode == Multicast {
|
2019-09-04 13:16:31 +02:00
|
|
|
msg.link = ""
|
2019-09-04 10:48:05 +02:00
|
|
|
}
|
|
|
|
|
2019-11-03 18:12:17 +02:00
|
|
|
log.Tracef("Appending %+v to send backlog", msg)
|
2019-09-04 12:58:03 +02:00
|
|
|
// send the actual message
|
2019-08-11 19:11:33 +02:00
|
|
|
s.send <- msg
|
2019-08-30 21:05:00 +02:00
|
|
|
|
|
|
|
// wait for an error response
|
|
|
|
select {
|
|
|
|
case err := <-msg.errChan:
|
|
|
|
return err
|
|
|
|
case <-s.closed:
|
|
|
|
return io.EOF
|
|
|
|
}
|
2019-08-06 12:45:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 12:58:03 +02:00
|
|
|
// Recv is used to receive a message
|
2019-08-30 21:05:00 +02:00
|
|
|
func (s *session) Recv(m *transport.Message) error {
|
2019-12-03 10:11:36 +02:00
|
|
|
var msg *message
|
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
2019-08-30 21:05:00 +02:00
|
|
|
return errors.New("session is closed")
|
2019-08-07 19:44:33 +02:00
|
|
|
// recv from backlog
|
2019-12-03 10:11:36 +02:00
|
|
|
case msg = <-s.recv:
|
|
|
|
}
|
2019-08-30 21:05:00 +02:00
|
|
|
|
|
|
|
// check the error if one exists
|
|
|
|
select {
|
|
|
|
case err := <-msg.errChan:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2019-11-25 20:56:00 +02:00
|
|
|
//log.Tracef("Received %+v from recv backlog", msg)
|
|
|
|
log.Debugf("Received %+v from recv backlog", msg)
|
2019-11-25 17:34:41 +02:00
|
|
|
|
|
|
|
// decrypt the received payload using the token
|
2019-11-25 20:56:00 +02:00
|
|
|
body, err := Decrypt(msg.data.Body, s.token+s.channel+s.session)
|
2019-11-25 17:34:41 +02:00
|
|
|
if err != nil {
|
2019-11-25 20:56:00 +02:00
|
|
|
log.Debugf("failed to decrypt message body: %v", err)
|
2019-11-25 17:34:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg.data.Body = body
|
|
|
|
|
2019-11-25 20:56:00 +02:00
|
|
|
// encrypt all the headers
|
|
|
|
for k, v := range msg.data.Header {
|
|
|
|
// hex decode the header values
|
|
|
|
h, err := hex.DecodeString(v)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("failed to decode message header %s: %v", k, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// encrypt the transport message payload
|
|
|
|
val, err := Decrypt([]byte(h), s.token+s.channel+s.session)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("failed to decrypt message header %s: %v", k, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// hex encode the encrypted header value
|
|
|
|
msg.data.Header[k] = string(val)
|
|
|
|
}
|
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
// set message
|
|
|
|
*m = *msg.data
|
|
|
|
// return nil
|
|
|
|
return nil
|
2019-08-06 12:45:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 12:58:03 +02:00
|
|
|
// Close closes the session by sending a close message
|
2019-08-30 21:05:00 +02:00
|
|
|
func (s *session) Close() error {
|
2019-08-07 19:44:33 +02:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
// no op
|
|
|
|
default:
|
|
|
|
close(s.closed)
|
2019-09-04 10:48:05 +02:00
|
|
|
|
|
|
|
// append to backlog
|
2019-09-04 13:16:31 +02:00
|
|
|
msg := s.newMessage("close")
|
|
|
|
// no error response on close
|
|
|
|
msg.errChan = nil
|
2019-09-04 10:48:05 +02:00
|
|
|
|
|
|
|
// send the close message
|
|
|
|
select {
|
|
|
|
case s.send <- msg:
|
|
|
|
default:
|
|
|
|
}
|
2019-08-07 19:44:33 +02:00
|
|
|
}
|
2019-09-04 10:48:05 +02:00
|
|
|
|
2019-08-07 19:44:33 +02:00
|
|
|
return nil
|
2019-08-06 12:45:25 +02:00
|
|
|
}
|