Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7662ab4e08 | |||
| 8c2b78e30b | |||
| 6002e20080 | |||
| afc125cc1c | |||
| c385a6b1ea |
@@ -10,6 +10,11 @@ type Connection struct {
|
||||
TX chan<- *DataFrame
|
||||
ID ID
|
||||
closed bool
|
||||
logger Logger
|
||||
}
|
||||
|
||||
func (c *Connection) SetLogger(logger Logger) {
|
||||
c.logger = logger
|
||||
}
|
||||
|
||||
func (c *Connection) Read(p []byte) (int, error) {
|
||||
@@ -17,6 +22,7 @@ func (c *Connection) Read(p []byte) (int, error) {
|
||||
if !ok {
|
||||
return 0, c.Close()
|
||||
}
|
||||
c.logger.Trace("dataframe received: ", df)
|
||||
return df.Read(p)
|
||||
}
|
||||
|
||||
@@ -37,11 +43,13 @@ func (c *Connection) Write(p []byte) (int, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
c.logger.Trace("dataframe send: ", df)
|
||||
c.TX <- df
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (c *Connection) Close() error {
|
||||
defer c.logger.Tracef("connection closed")
|
||||
var lock sync.Mutex
|
||||
defer close(c.TX)
|
||||
if !c.closed {
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package tunnel
|
||||
|
||||
type Logger interface {
|
||||
Trace(args ...interface{})
|
||||
Tracef(format string, args ...interface{})
|
||||
|
||||
Debug(args ...interface{})
|
||||
Debugf(format string, args ...interface{})
|
||||
|
||||
Info(args ...interface{})
|
||||
Infof(format string, args ...interface{})
|
||||
|
||||
Error(args ...interface{})
|
||||
Errorf(format string, args ...interface{})
|
||||
|
||||
Fatal(args ...interface{})
|
||||
Fatalf(format string, args ...interface{})
|
||||
|
||||
Panic(args ...interface{})
|
||||
Panicf(format string, args ...interface{})
|
||||
}
|
||||
|
||||
type logger struct{}
|
||||
|
||||
func (l *logger) Trace(args ...interface{}) {}
|
||||
func (l *logger) Tracef(format string, args ...interface{}) {}
|
||||
func (l *logger) Debug(args ...interface{}) {}
|
||||
func (l *logger) Debugf(format string, args ...interface{}) {}
|
||||
func (l *logger) Info(args ...interface{}) {}
|
||||
func (l *logger) Infof(format string, args ...interface{}) {}
|
||||
func (l *logger) Error(args ...interface{}) {}
|
||||
func (l *logger) Errorf(format string, args ...interface{}) {}
|
||||
func (l *logger) Fatal(args ...interface{}) {}
|
||||
func (l *logger) Fatalf(format string, args ...interface{}) {}
|
||||
func (l *logger) Panic(args ...interface{}) {}
|
||||
func (l *logger) Panicf(format string, args ...interface{}) {}
|
||||
|
||||
var DefaultLogger = &logger{}
|
||||
+23
-6
@@ -13,6 +13,7 @@ type Manager struct {
|
||||
closed chan bool
|
||||
newConnection chan *Connection
|
||||
delConnection chan *Connection
|
||||
logger Logger
|
||||
}
|
||||
|
||||
func NewManager(tun io.ReadWriter) *Manager {
|
||||
@@ -24,22 +25,34 @@ func NewManager(tun io.ReadWriter) *Manager {
|
||||
accept: make(chan *DataFrame, 1024),
|
||||
newConnection: make(chan *Connection),
|
||||
delConnection: make(chan *Connection),
|
||||
closed: make(chan bool),
|
||||
logger: DefaultLogger,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) SetLogger(logger Logger) {
|
||||
m.logger = logger
|
||||
}
|
||||
|
||||
func (m *Manager) Run() {
|
||||
m.logger.Debug("manager run")
|
||||
onReceiveQueue := make(chan bool)
|
||||
for {
|
||||
select {
|
||||
case <-m.closed:
|
||||
return
|
||||
case df := <-m.incoming:
|
||||
m.logger.Trace("dataframe->tunnel: ", df)
|
||||
go df.Encode(m.Tunnel)
|
||||
case connection := <-m.newConnection:
|
||||
m.Connections[connection.ID] = connection
|
||||
m.logger.Tracef("connection '%s' registered", connection.ID)
|
||||
case connection := <-m.delConnection:
|
||||
connection.Close()
|
||||
delete(m.Connections, connection.ID)
|
||||
if !connection.closed {
|
||||
connection.Close()
|
||||
delete(m.Connections, connection.ID)
|
||||
m.logger.Tracef("connection '%s' unregistered", connection.ID)
|
||||
}
|
||||
case onReceiveQueue <- true:
|
||||
go m.onReceive(onReceiveQueue)
|
||||
}
|
||||
@@ -52,6 +65,8 @@ func (m *Manager) onReceive(ch chan bool) {
|
||||
if err := df.Decode(m.Tunnel); err != nil {
|
||||
return
|
||||
}
|
||||
m.logger.Trace("dataframe<-tunnel: ", df)
|
||||
|
||||
switch df.Type {
|
||||
case TypeRequest:
|
||||
m.accept <- df
|
||||
@@ -75,8 +90,9 @@ func (m *Manager) Connect() (*Connection, error) {
|
||||
Type: TypeRequest,
|
||||
}
|
||||
connection := &Connection{
|
||||
ID: df.ID,
|
||||
TX: m.incoming,
|
||||
ID: df.ID,
|
||||
TX: m.incoming,
|
||||
logger: DefaultLogger,
|
||||
}
|
||||
m.newConnection <- connection
|
||||
m.incoming <- df
|
||||
@@ -94,8 +110,9 @@ func (m *Manager) Accept() (*Connection, error) {
|
||||
return nil, WrongDataFrameTypeError{ShouldBe: TypeRequest}
|
||||
}
|
||||
connection := &Connection{
|
||||
ID: df.ID,
|
||||
TX: m.incoming,
|
||||
ID: df.ID,
|
||||
TX: m.incoming,
|
||||
logger: DefaultLogger,
|
||||
}
|
||||
m.newConnection <- connection
|
||||
|
||||
|
||||
Reference in New Issue
Block a user