dissertation-2-code/tcp/flow.go

255 lines
4.6 KiB
Go
Raw Normal View History

package tcp
import (
2021-01-23 18:39:01 +00:00
"bufio"
2021-03-30 20:57:53 +01:00
"context"
"encoding/binary"
2020-11-26 19:18:07 +00:00
"fmt"
"io"
"mpbl3p/proxy"
2020-11-01 18:18:37 +00:00
"mpbl3p/shared"
"net"
2020-11-01 17:16:43 +00:00
"sync"
2020-11-02 17:24:15 +00:00
"time"
)
2020-10-25 21:44:56 +00:00
type Conn interface {
Read(b []byte) (n int, err error)
Write(b []byte) (n int, err error)
2020-11-02 17:24:15 +00:00
SetWriteDeadline(time.Time) error
2020-11-26 19:18:07 +00:00
// For printing
LocalAddr() net.Addr
RemoteAddr() net.Addr
2020-10-25 21:44:56 +00:00
}
2020-11-01 16:50:02 +00:00
type InitiatedFlow struct {
2021-01-23 17:39:52 +00:00
Local func() string
2020-10-24 17:44:14 +01:00
Remote string
2020-11-01 17:16:43 +00:00
mu sync.RWMutex
2020-11-01 16:50:02 +00:00
Flow
}
2020-11-26 19:18:07 +00:00
func (f *InitiatedFlow) String() string {
2021-01-23 17:43:02 +00:00
return fmt.Sprintf("TcpOutbound{%v -> %v}", f.Local(), f.Remote)
2020-11-26 19:18:07 +00:00
}
2020-11-01 16:50:02 +00:00
type Flow struct {
2020-11-01 18:18:37 +00:00
conn Conn
isAlive bool
2021-01-23 18:24:04 +00:00
toConsume, produced chan []byte
consumeErrors, produceErrors chan error
generators []proxy.MacGenerator
verifiers []proxy.MacVerifier
2021-01-23 18:24:04 +00:00
}
func NewFlow(vs []proxy.MacVerifier, gs []proxy.MacGenerator) Flow {
2021-01-23 18:24:04 +00:00
return Flow{
2021-05-13 21:56:57 +01:00
verifiers: vs,
generators: gs,
2021-01-23 18:24:04 +00:00
toConsume: make(chan []byte),
produced: make(chan []byte),
consumeErrors: make(chan error),
produceErrors: make(chan error),
}
}
func NewFlowConn(ctx context.Context, conn Conn, vs []proxy.MacVerifier, gs []proxy.MacGenerator) Flow {
2021-01-23 18:24:04 +00:00
f := Flow{
conn: conn,
isAlive: true,
generators: gs,
verifiers: vs,
2021-01-23 18:24:04 +00:00
toConsume: make(chan []byte),
produced: make(chan []byte),
consumeErrors: make(chan error),
produceErrors: make(chan error),
}
2021-05-11 21:43:47 +01:00
go f.produceMarshalled(ctx)
go f.consumeMarshalled(ctx)
2021-01-23 18:24:04 +00:00
return f
}
2020-11-26 19:18:07 +00:00
func (f Flow) String() string {
return fmt.Sprintf("TcpInbound{%v -> %v}", f.conn.RemoteAddr(), f.conn.LocalAddr())
}
2021-01-23 18:24:04 +00:00
func (f *Flow) IsAlive() bool {
return f.isAlive
}
func InitiateFlow(local func() string, remote string, vs []proxy.MacVerifier, gs []proxy.MacGenerator) (*InitiatedFlow, error) {
2020-11-01 16:50:02 +00:00
f := InitiatedFlow{
2020-10-25 15:36:34 +00:00
Local: local,
2020-10-24 17:44:14 +01:00
Remote: remote,
2021-01-23 18:24:04 +00:00
Flow: NewFlow(vs, gs),
2020-10-24 17:44:14 +01:00
}
2020-11-01 17:16:43 +00:00
return &f, nil
2020-10-24 17:44:14 +01:00
}
2021-05-11 21:43:47 +01:00
func (f *InitiatedFlow) Reconnect(ctx context.Context) error {
2020-11-01 17:16:43 +00:00
f.mu.Lock()
defer f.mu.Unlock()
2020-11-01 18:18:37 +00:00
if f.isAlive {
2020-11-01 17:16:43 +00:00
return nil
}
2021-01-23 17:39:52 +00:00
localAddr, err := net.ResolveTCPAddr("tcp", f.Local())
if err != nil {
2020-10-24 17:44:14 +01:00
return err
}
2020-10-24 17:44:14 +01:00
remoteAddr, err := net.ResolveTCPAddr("tcp", f.Remote)
if err != nil {
return err
}
2020-11-03 20:56:51 +00:00
conn, err := net.DialTCP("tcp", localAddr, remoteAddr)
if err != nil {
return err
}
2020-10-24 17:44:14 +01:00
2021-01-25 19:38:39 +00:00
if err := conn.SetWriteBuffer(0); err != nil {
2021-01-23 20:52:37 +00:00
return err
}
2020-11-05 13:32:47 +00:00
f.conn = conn
2020-11-01 18:18:37 +00:00
f.isAlive = true
2021-01-23 18:24:04 +00:00
2021-05-11 21:43:47 +01:00
go f.produceMarshalled(ctx)
go f.consumeMarshalled(ctx)
2021-01-23 18:24:04 +00:00
2020-10-24 17:44:14 +01:00
return nil
}
func (f *InitiatedFlow) Consume(ctx context.Context, p proxy.Packet) error {
2020-11-01 17:16:43 +00:00
f.mu.RLock()
defer f.mu.RUnlock()
return f.Flow.Consume(ctx, p)
}
func (f *InitiatedFlow) Produce(ctx context.Context) (proxy.Packet, error) {
2020-11-26 22:10:37 +00:00
f.mu.RLock()
defer f.mu.RUnlock()
return f.Flow.Produce(ctx)
2020-11-26 22:10:37 +00:00
}
func (f *Flow) Consume(ctx context.Context, p proxy.Packet) error {
2020-11-01 18:18:37 +00:00
if !f.isAlive {
return shared.ErrDeadConnection
}
2021-03-23 19:38:34 +00:00
select {
case err := <-f.consumeErrors:
f.isAlive = false
return err
default:
}
data := p.Marshal()
for _, g := range f.generators {
data = proxy.AppendMac(data, g)
}
2020-11-26 18:55:29 +00:00
prefixedData := make([]byte, len(data)+4)
binary.LittleEndian.PutUint32(prefixedData, uint32(len(data)))
2020-10-25 21:44:56 +00:00
copy(prefixedData[4:], data)
2021-03-30 20:57:53 +01:00
select {
case f.toConsume <- prefixedData:
case <-ctx.Done():
return ctx.Err()
}
2021-03-23 19:38:34 +00:00
return nil
}
func (f *Flow) Produce(ctx context.Context) (proxy.Packet, error) {
2020-11-01 18:18:37 +00:00
if !f.isAlive {
2020-11-25 19:35:31 +00:00
return nil, shared.ErrDeadConnection
2020-11-01 18:18:37 +00:00
}
2021-01-23 18:24:04 +00:00
var data []byte
select {
2021-03-30 20:57:53 +01:00
case <-ctx.Done():
return nil, ctx.Err()
2021-01-23 18:24:04 +00:00
case data = <-f.produced:
case err := <-f.produceErrors:
2020-11-01 18:18:37 +00:00
f.isAlive = false
2020-11-25 19:35:31 +00:00
return nil, err
2020-10-24 17:44:14 +01:00
}
for _, v := range f.verifiers {
var err error
data, err = proxy.StripMac(data, v)
if err != nil {
return nil, err
}
2020-11-26 18:55:29 +00:00
}
return proxy.SimplePacket(data), nil
2020-10-24 17:44:14 +01:00
}
2021-05-11 21:43:47 +01:00
func (f *Flow) consumeMarshalled(ctx context.Context) {
2021-01-23 18:24:04 +00:00
for {
data := <-f.toConsume
err := f.conn.SetWriteDeadline(time.Now().Add(5 * time.Second))
if err != nil {
f.consumeErrors <- err
return
}
_, err = f.conn.Write(data)
if err != nil {
f.consumeErrors <- err
return
}
}
2021-01-23 18:24:04 +00:00
}
2021-05-11 21:43:47 +01:00
func (f *Flow) produceMarshalled(ctx context.Context) {
2021-01-23 20:46:06 +00:00
buf := bufio.NewReader(f.conn)
2021-01-23 18:39:01 +00:00
2021-01-23 18:24:04 +00:00
for {
lengthBytes := make([]byte, 4)
2021-01-23 18:39:01 +00:00
if n, err := io.LimitReader(buf, 4).Read(lengthBytes); err != nil {
2021-01-23 18:24:04 +00:00
f.produceErrors <- err
return
} else if n != 4 {
f.produceErrors <- shared.ErrNotEnoughBytes
return
}
2020-11-01 16:50:02 +00:00
2021-01-23 18:24:04 +00:00
length := binary.LittleEndian.Uint32(lengthBytes)
dataBytes := make([]byte, length)
var read uint32
for read < length {
2021-01-23 18:39:01 +00:00
if n, err := io.LimitReader(buf, int64(length-read)).Read(dataBytes[read:]); err != nil {
2021-01-23 18:24:04 +00:00
f.produceErrors <- err
return
} else {
read += uint32(n)
}
2020-11-01 16:50:02 +00:00
}
2021-01-23 18:24:04 +00:00
f.produced <- dataBytes
}
2020-10-25 15:36:34 +00:00
}