bare for use
This commit is contained in:
parent
94b80580fa
commit
3bf499a5dc
@ -1,9 +1,10 @@
|
||||
package tunnel
|
||||
|
||||
type Connection struct {
|
||||
RX <-chan *DataFrame
|
||||
TX chan<- *DataFrame
|
||||
ID ID
|
||||
RX <-chan *DataFrame
|
||||
TX chan<- *DataFrame
|
||||
ID ID
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (c *Connection) Read(p []byte) (int, error) {
|
||||
@ -30,5 +31,12 @@ func (c *Connection) Write(p []byte) (int, error) {
|
||||
|
||||
func (c *Connection) Close() error {
|
||||
defer close(c.TX)
|
||||
if !c.closed {
|
||||
c.TX <- &DataFrame{
|
||||
ID: c.ID,
|
||||
Type: TypeClosed,
|
||||
}
|
||||
}
|
||||
c.closed = true
|
||||
return nil
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
||||
module git.sense-t.eu.org/senset/tunnel
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/google/uuid v1.3.0
|
||||
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
110
manager.go
110
manager.go
@ -1,25 +1,30 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
Tunnel io.ReadWriter
|
||||
Connections map[ID]*Connection
|
||||
incoming chan *DataFrame
|
||||
outgoing chan *DataFrame
|
||||
accept chan *DataFrame
|
||||
closed chan bool
|
||||
Tunnel io.ReadWriter
|
||||
Connections map[ID]*Connection
|
||||
incoming chan *DataFrame
|
||||
outgoing chan *DataFrame
|
||||
accept chan *DataFrame
|
||||
closed chan bool
|
||||
newConnection chan *Connection
|
||||
delConnection chan *Connection
|
||||
}
|
||||
|
||||
func NewManager(tun io.ReadWriter) *Manager {
|
||||
return &Manager{
|
||||
Tunnel: tun,
|
||||
Connections: make(map[ID]*Connection),
|
||||
incoming: make(chan *DataFrame, 1024),
|
||||
outgoing: make(chan *DataFrame, 1024),
|
||||
accept: make(chan *DataFrame, 1024),
|
||||
Tunnel: tun,
|
||||
Connections: make(map[ID]*Connection),
|
||||
incoming: make(chan *DataFrame, 1024),
|
||||
outgoing: make(chan *DataFrame, 1024),
|
||||
accept: make(chan *DataFrame, 1024),
|
||||
newConnection: make(chan *Connection),
|
||||
delConnection: make(chan *Connection),
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,34 +35,73 @@ func (m *Manager) Run() {
|
||||
return
|
||||
case df := <-m.incoming:
|
||||
go df.Encode(m.Tunnel)
|
||||
case connection := <-m.newConnection:
|
||||
m.Connections[connection.ID] = connection
|
||||
case connection := <-m.delConnection:
|
||||
connection.Close()
|
||||
delete(m.Connections, connection.ID)
|
||||
default:
|
||||
go func() {
|
||||
df := new(DataFrame)
|
||||
df.Decode(m.Tunnel)
|
||||
switch df.Type {
|
||||
case TypeRequest:
|
||||
m.accept <- df
|
||||
case TypeClosed:
|
||||
connection, ok := m.Connections[df.ID]
|
||||
if ok {
|
||||
connection.Close()
|
||||
delete(m.Connections, df.ID)
|
||||
}
|
||||
case TypeNormal:
|
||||
connection, ok := m.Connections[df.ID]
|
||||
if ok {
|
||||
connection.RX = m.outgoing
|
||||
m.outgoing <- df
|
||||
}
|
||||
}
|
||||
}()
|
||||
go m.onReceive()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) Connect()
|
||||
func (m *Manager) onReceive() {
|
||||
df := new(DataFrame)
|
||||
df.Decode(m.Tunnel)
|
||||
switch df.Type {
|
||||
case TypeRequest:
|
||||
m.accept <- df
|
||||
case TypeClosed:
|
||||
connection, ok := m.Connections[df.ID]
|
||||
if ok {
|
||||
m.delConnection <- connection
|
||||
}
|
||||
case TypeNormal:
|
||||
connection, ok := m.Connections[df.ID]
|
||||
if ok {
|
||||
connection.RX = m.outgoing
|
||||
m.outgoing <- df
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) Accept()
|
||||
func (m *Manager) Connect() (*Connection, error) {
|
||||
df := &DataFrame{
|
||||
ID: NewID(),
|
||||
Type: TypeRequest,
|
||||
}
|
||||
connection := &Connection{
|
||||
ID: df.ID,
|
||||
TX: m.incoming,
|
||||
}
|
||||
m.newConnection <- connection
|
||||
m.incoming <- df
|
||||
|
||||
df = <-connection.RX
|
||||
if df.Type != TypeConnected {
|
||||
|
||||
return nil, errors.New("failed to connect")
|
||||
}
|
||||
return connection, nil
|
||||
}
|
||||
|
||||
func (m *Manager) Accept() (*Connection, error) {
|
||||
df := <-m.accept
|
||||
if df.Type != TypeRequest {
|
||||
return nil, errors.New("failed to accept")
|
||||
}
|
||||
connection := &Connection{
|
||||
ID: df.ID,
|
||||
TX: m.incoming,
|
||||
}
|
||||
m.newConnection <- connection
|
||||
|
||||
df.Type = TypeConnected
|
||||
m.incoming <- df
|
||||
|
||||
return connection, nil
|
||||
}
|
||||
|
||||
func (m *Manager) Close() error {
|
||||
defer close(m.closed)
|
||||
|
Reference in New Issue
Block a user