2019-08-06 13:45:25 +03:00
|
|
|
package tunnel
|
|
|
|
|
2019-08-07 20:44:33 +03:00
|
|
|
import (
|
|
|
|
"errors"
|
2019-08-30 22:05:00 +03:00
|
|
|
"io"
|
2019-08-06 13:45:25 +03:00
|
|
|
|
2019-08-07 20:44:33 +03:00
|
|
|
"github.com/micro/go-micro/transport"
|
2019-08-11 20:11:33 +03:00
|
|
|
"github.com/micro/go-micro/util/log"
|
2019-08-07 20:44:33 +03:00
|
|
|
)
|
2019-08-06 13:45:25 +03:00
|
|
|
|
2019-08-30 22:05:00 +03:00
|
|
|
// session is our pseudo session for transport.Socket
|
|
|
|
type session struct {
|
|
|
|
// unique id based on the remote tunnel id
|
2019-08-07 20:44:33 +03:00
|
|
|
id string
|
2019-08-30 22:05:00 +03:00
|
|
|
// the channel name
|
|
|
|
channel string
|
2019-08-07 20:44:33 +03:00
|
|
|
// the session id based on Micro.Tunnel-Session
|
|
|
|
session string
|
|
|
|
// 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-08-30 22:05:00 +03:00
|
|
|
// outbound marks the session as outbound dialled connection
|
2019-08-14 19:14:39 +03:00
|
|
|
outbound bool
|
2019-08-30 22:05:00 +03:00
|
|
|
// lookback marks the session as a loopback on the inbound
|
2019-08-29 01:12:22 +03:00
|
|
|
loopback bool
|
2019-08-29 14:42:27 +03:00
|
|
|
// the link on which this message was received
|
|
|
|
link string
|
2019-08-30 22:05:00 +03:00
|
|
|
// the error response
|
|
|
|
errChan chan error
|
2019-08-06 13:45:25 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 20:44:33 +03:00
|
|
|
// message is sent over the send channel
|
|
|
|
type message struct {
|
2019-08-29 14:42:27 +03:00
|
|
|
// type of message
|
|
|
|
typ string
|
2019-08-07 20:44:33 +03:00
|
|
|
// tunnel id
|
|
|
|
id string
|
2019-08-30 22:05:00 +03:00
|
|
|
// channel name
|
|
|
|
channel string
|
2019-08-07 20:44:33 +03:00
|
|
|
// the session id
|
|
|
|
session string
|
2019-08-14 19:14:39 +03:00
|
|
|
// outbound marks the message as outbound
|
|
|
|
outbound bool
|
2019-08-29 01:12:22 +03:00
|
|
|
// loopback marks the message intended for loopback
|
|
|
|
loopback bool
|
2019-08-29 14:42:27 +03:00
|
|
|
// the link to send the message on
|
|
|
|
link string
|
2019-08-07 20:44:33 +03:00
|
|
|
// transport data
|
|
|
|
data *transport.Message
|
2019-08-30 22:05:00 +03:00
|
|
|
// the error channel
|
|
|
|
errChan chan error
|
2019-08-07 20:44:33 +03:00
|
|
|
}
|
|
|
|
|
2019-08-30 22:05:00 +03:00
|
|
|
func (s *session) Remote() string {
|
2019-08-07 20:44:33 +03:00
|
|
|
return s.remote
|
|
|
|
}
|
|
|
|
|
2019-08-30 22:05:00 +03:00
|
|
|
func (s *session) Local() string {
|
2019-08-07 20:44:33 +03:00
|
|
|
return s.local
|
2019-08-06 13:45:25 +03:00
|
|
|
}
|
|
|
|
|
2019-08-30 22:05:00 +03:00
|
|
|
func (s *session) Id() string {
|
|
|
|
return s.session
|
2019-08-07 20:44:33 +03:00
|
|
|
}
|
|
|
|
|
2019-08-30 22:05:00 +03:00
|
|
|
func (s *session) Channel() string {
|
|
|
|
return s.channel
|
2019-08-07 20:44:33 +03:00
|
|
|
}
|
|
|
|
|
2019-08-30 22:05:00 +03:00
|
|
|
func (s *session) Send(m *transport.Message) error {
|
2019-08-07 20:44:33 +03:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
2019-08-30 22:05:00 +03:00
|
|
|
return errors.New("session is closed")
|
2019-08-07 20:44:33 +03:00
|
|
|
default:
|
|
|
|
// no op
|
|
|
|
}
|
2019-08-11 20:11:33 +03:00
|
|
|
|
|
|
|
// make copy
|
|
|
|
data := &transport.Message{
|
|
|
|
Header: make(map[string]string),
|
|
|
|
Body: m.Body,
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range m.Header {
|
|
|
|
data.Header[k] = v
|
|
|
|
}
|
|
|
|
|
2019-08-07 20:44:33 +03:00
|
|
|
// append to backlog
|
2019-08-14 19:14:39 +03:00
|
|
|
msg := &message{
|
2019-08-29 14:42:27 +03:00
|
|
|
typ: "message",
|
2019-08-14 19:14:39 +03:00
|
|
|
id: s.id,
|
2019-08-30 22:05:00 +03:00
|
|
|
channel: s.channel,
|
2019-08-14 19:14:39 +03:00
|
|
|
session: s.session,
|
|
|
|
outbound: s.outbound,
|
2019-08-29 01:12:22 +03:00
|
|
|
loopback: s.loopback,
|
2019-08-14 19:14:39 +03:00
|
|
|
data: data,
|
2019-08-29 14:42:27 +03:00
|
|
|
// specify the link on which to send this
|
2019-08-30 22:05:00 +03:00
|
|
|
// it will be blank for dialled sessions
|
2019-08-29 14:42:27 +03:00
|
|
|
link: s.link,
|
2019-08-30 22:05:00 +03:00
|
|
|
// error chan
|
|
|
|
errChan: s.errChan,
|
2019-08-14 19:14:39 +03:00
|
|
|
}
|
2019-08-11 20:11:33 +03:00
|
|
|
log.Debugf("Appending %+v to send backlog", msg)
|
|
|
|
s.send <- msg
|
2019-08-30 22:05:00 +03:00
|
|
|
|
|
|
|
// wait for an error response
|
|
|
|
select {
|
|
|
|
case err := <-msg.errChan:
|
|
|
|
return err
|
|
|
|
case <-s.closed:
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
2019-08-06 13:45:25 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-30 22:05:00 +03:00
|
|
|
func (s *session) Recv(m *transport.Message) error {
|
2019-08-07 20:44:33 +03:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
2019-08-30 22:05:00 +03:00
|
|
|
return errors.New("session is closed")
|
2019-08-07 20:44:33 +03:00
|
|
|
default:
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
// recv from backlog
|
|
|
|
msg := <-s.recv
|
2019-08-30 22:05:00 +03:00
|
|
|
|
|
|
|
// check the error if one exists
|
|
|
|
select {
|
|
|
|
case err := <-msg.errChan:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2019-08-11 20:11:33 +03:00
|
|
|
log.Debugf("Received %+v from recv backlog", msg)
|
2019-08-07 20:44:33 +03:00
|
|
|
// set message
|
|
|
|
*m = *msg.data
|
|
|
|
// return nil
|
|
|
|
return nil
|
2019-08-06 13:45:25 +03:00
|
|
|
}
|
|
|
|
|
2019-08-30 22:05:00 +03:00
|
|
|
// Close closes the session
|
|
|
|
func (s *session) Close() error {
|
2019-08-07 20:44:33 +03:00
|
|
|
select {
|
|
|
|
case <-s.closed:
|
|
|
|
// no op
|
|
|
|
default:
|
|
|
|
close(s.closed)
|
|
|
|
}
|
|
|
|
return nil
|
2019-08-06 13:45:25 +03:00
|
|
|
}
|