Jake Hillion
55cd39f2b3
All checks were successful
continuous-integration/drone/push Build is passing
112 lines
2.0 KiB
Go
112 lines
2.0 KiB
Go
package tcp
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"io"
|
|
"mpbl3p/proxy"
|
|
"net"
|
|
)
|
|
|
|
var ErrNotEnoughBytes = errors.New("not enough bytes")
|
|
|
|
type Conn interface {
|
|
Read(b []byte) (n int, err error)
|
|
Write(b []byte) (n int, err error)
|
|
}
|
|
|
|
type InitiatedFlow struct {
|
|
Local string
|
|
Remote string
|
|
|
|
Flow
|
|
}
|
|
|
|
type Flow struct {
|
|
conn Conn
|
|
}
|
|
|
|
func InitiateFlow(local, remote string) (InitiatedFlow, error) {
|
|
f := InitiatedFlow{
|
|
Local: local,
|
|
Remote: remote,
|
|
}
|
|
|
|
if err := f.Reconnect(); err != nil {
|
|
return InitiatedFlow{}, err
|
|
}
|
|
|
|
return f, nil
|
|
}
|
|
|
|
func (f *InitiatedFlow) Reconnect() error {
|
|
localAddr, err := net.ResolveTCPAddr("tcp", f.Local)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
remoteAddr, err := net.ResolveTCPAddr("tcp", f.Remote)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
f.conn, err = net.DialTCP("tcp", localAddr, remoteAddr)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (f *Flow) IsAlive() bool {
|
|
// TODO: Implement this
|
|
return true
|
|
}
|
|
|
|
func (f *Flow) Consume(p proxy.Packet, g proxy.MacGenerator) error {
|
|
data := p.Marshal(g)
|
|
return f.consumeMarshalled(data)
|
|
}
|
|
|
|
func (f *Flow) consumeMarshalled(data []byte) error {
|
|
prefixedData := make([]byte, len(data)+4)
|
|
binary.LittleEndian.PutUint32(prefixedData, uint32(len(data)))
|
|
copy(prefixedData[4:], data)
|
|
|
|
_, err := f.conn.Write(prefixedData)
|
|
return err
|
|
}
|
|
|
|
func (f *Flow) Produce(v proxy.MacVerifier) (proxy.Packet, error) {
|
|
data, err := f.produceMarshalled()
|
|
if err != nil {
|
|
return proxy.Packet{}, err
|
|
}
|
|
|
|
return proxy.UnmarshalPacket(data, v)
|
|
}
|
|
|
|
func (f *Flow) produceMarshalled() ([]byte, error) {
|
|
lengthBytes := make([]byte, 4)
|
|
if n, err := io.LimitReader(f.conn, 4).Read(lengthBytes); err != nil {
|
|
return nil, err
|
|
} else if n != 4 {
|
|
return nil, ErrNotEnoughBytes
|
|
}
|
|
|
|
length := binary.LittleEndian.Uint32(lengthBytes)
|
|
dataBytes := make([]byte, length)
|
|
|
|
var read uint32
|
|
for read < length {
|
|
if n, err := io.LimitReader(f.conn, int64(length-read)).Read(dataBytes); err != nil {
|
|
return nil, err
|
|
} else {
|
|
read += uint32(n)
|
|
}
|
|
}
|
|
|
|
return dataBytes, nil
|
|
}
|