bare for use

This commit is contained in:
Sense T 2022-09-09 06:31:47 +00:00
parent 94b80580fa
commit 3bf499a5dc
5 changed files with 99 additions and 36 deletions

View File

@ -4,6 +4,7 @@ type Connection struct {
RX <-chan *DataFrame RX <-chan *DataFrame
TX chan<- *DataFrame TX chan<- *DataFrame
ID ID ID ID
closed bool
} }
func (c *Connection) Read(p []byte) (int, error) { 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 { func (c *Connection) Close() error {
defer close(c.TX) defer close(c.TX)
if !c.closed {
c.TX <- &DataFrame{
ID: c.ID,
Type: TypeClosed,
}
}
c.closed = true
return nil return nil
} }

2
go.mod
View File

@ -1,3 +1,5 @@
module git.sense-t.eu.org/senset/tunnel module git.sense-t.eu.org/senset/tunnel
go 1.19 go 1.19
require github.com/google/uuid v1.3.0

2
go.sum Normal file
View 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=

View File

@ -1,6 +1,7 @@
package tunnel package tunnel
import ( import (
"errors"
"io" "io"
) )
@ -11,6 +12,8 @@ type Manager struct {
outgoing chan *DataFrame outgoing chan *DataFrame
accept chan *DataFrame accept chan *DataFrame
closed chan bool closed chan bool
newConnection chan *Connection
delConnection chan *Connection
} }
func NewManager(tun io.ReadWriter) *Manager { func NewManager(tun io.ReadWriter) *Manager {
@ -20,6 +23,8 @@ func NewManager(tun io.ReadWriter) *Manager {
incoming: make(chan *DataFrame, 1024), incoming: make(chan *DataFrame, 1024),
outgoing: make(chan *DataFrame, 1024), outgoing: make(chan *DataFrame, 1024),
accept: make(chan *DataFrame, 1024), accept: make(chan *DataFrame, 1024),
newConnection: make(chan *Connection),
delConnection: make(chan *Connection),
} }
} }
@ -30,8 +35,18 @@ func (m *Manager) Run() {
return return
case df := <-m.incoming: case df := <-m.incoming:
go df.Encode(m.Tunnel) 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: default:
go func() { go m.onReceive()
}
}
}
func (m *Manager) onReceive() {
df := new(DataFrame) df := new(DataFrame)
df.Decode(m.Tunnel) df.Decode(m.Tunnel)
switch df.Type { switch df.Type {
@ -40,8 +55,7 @@ func (m *Manager) Run() {
case TypeClosed: case TypeClosed:
connection, ok := m.Connections[df.ID] connection, ok := m.Connections[df.ID]
if ok { if ok {
connection.Close() m.delConnection <- connection
delete(m.Connections, df.ID)
} }
case TypeNormal: case TypeNormal:
connection, ok := m.Connections[df.ID] connection, ok := m.Connections[df.ID]
@ -50,14 +64,44 @@ func (m *Manager) Run() {
m.outgoing <- df m.outgoing <- df
} }
} }
}()
}
}
} }
func (m *Manager) Connect() 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
func (m *Manager) Accept() 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 { func (m *Manager) Close() error {
defer close(m.closed) defer close(m.closed)

View File

@ -1,4 +1,11 @@
package tunnel package tunnel
import "github.com/google/uuid"
type ID string type ID string
func NewID() ID {
return ID(uuid.NewString())
}
type Type uint8 type Type uint8