Merge branch 'develop' into freebsd
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
commit
b8428d4643
114
tcp/flow.go
114
tcp/flow.go
@ -1,6 +1,7 @@
|
|||||||
package tcp
|
package tcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -37,16 +38,51 @@ func (f *InitiatedFlow) String() string {
|
|||||||
type Flow struct {
|
type Flow struct {
|
||||||
conn Conn
|
conn Conn
|
||||||
isAlive bool
|
isAlive bool
|
||||||
|
|
||||||
|
toConsume, produced chan []byte
|
||||||
|
consumeErrors, produceErrors chan error
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFlow() Flow {
|
||||||
|
return Flow{
|
||||||
|
toConsume: make(chan []byte),
|
||||||
|
produced: make(chan []byte),
|
||||||
|
consumeErrors: make(chan error),
|
||||||
|
produceErrors: make(chan error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFlowConn(conn Conn) Flow {
|
||||||
|
f := Flow{
|
||||||
|
conn: conn,
|
||||||
|
isAlive: true,
|
||||||
|
|
||||||
|
toConsume: make(chan []byte),
|
||||||
|
produced: make(chan []byte),
|
||||||
|
consumeErrors: make(chan error),
|
||||||
|
produceErrors: make(chan error),
|
||||||
|
}
|
||||||
|
|
||||||
|
go f.produceMarshalled()
|
||||||
|
go f.consumeMarshalled()
|
||||||
|
|
||||||
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Flow) String() string {
|
func (f Flow) String() string {
|
||||||
return fmt.Sprintf("TcpInbound{%v -> %v}", f.conn.RemoteAddr(), f.conn.LocalAddr())
|
return fmt.Sprintf("TcpInbound{%v -> %v}", f.conn.RemoteAddr(), f.conn.LocalAddr())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Flow) IsAlive() bool {
|
||||||
|
return f.isAlive
|
||||||
|
}
|
||||||
|
|
||||||
func InitiateFlow(local func() string, remote string) (*InitiatedFlow, error) {
|
func InitiateFlow(local func() string, remote string) (*InitiatedFlow, error) {
|
||||||
f := InitiatedFlow{
|
f := InitiatedFlow{
|
||||||
Local: local,
|
Local: local,
|
||||||
Remote: remote,
|
Remote: remote,
|
||||||
|
|
||||||
|
Flow: NewFlow(),
|
||||||
}
|
}
|
||||||
|
|
||||||
return &f, nil
|
return &f, nil
|
||||||
@ -77,6 +113,10 @@ func (f *InitiatedFlow) Reconnect() error {
|
|||||||
|
|
||||||
f.conn = conn
|
f.conn = conn
|
||||||
f.isAlive = true
|
f.isAlive = true
|
||||||
|
|
||||||
|
go f.produceMarshalled()
|
||||||
|
go f.consumeMarshalled()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,36 +134,27 @@ func (f *InitiatedFlow) Produce(v proxy.MacVerifier) (proxy.Packet, error) {
|
|||||||
return f.Flow.Produce(v)
|
return f.Flow.Produce(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Flow) IsAlive() bool {
|
func (f *Flow) Consume(p proxy.Packet, g proxy.MacGenerator) error {
|
||||||
return f.isAlive
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Flow) Consume(p proxy.Packet, g proxy.MacGenerator) (err error) {
|
|
||||||
if !f.isAlive {
|
if !f.isAlive {
|
||||||
return shared.ErrDeadConnection
|
return shared.ErrDeadConnection
|
||||||
}
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-f.consumeErrors:
|
||||||
|
f.isAlive = false
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
marshalled := p.Marshal()
|
marshalled := p.Marshal()
|
||||||
data := proxy.AppendMac(marshalled, g)
|
data := proxy.AppendMac(marshalled, g)
|
||||||
|
|
||||||
err = f.consumeMarshalled(data)
|
|
||||||
if err != nil {
|
|
||||||
f.isAlive = false
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Flow) consumeMarshalled(data []byte) error {
|
|
||||||
prefixedData := make([]byte, len(data)+4)
|
prefixedData := make([]byte, len(data)+4)
|
||||||
binary.LittleEndian.PutUint32(prefixedData, uint32(len(data)))
|
binary.LittleEndian.PutUint32(prefixedData, uint32(len(data)))
|
||||||
copy(prefixedData[4:], data)
|
copy(prefixedData[4:], data)
|
||||||
|
|
||||||
err := f.conn.SetWriteDeadline(time.Now().Add(5 * time.Second))
|
f.toConsume <- prefixedData
|
||||||
if err != nil {
|
return nil
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = f.conn.Write(prefixedData)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Flow) Produce(v proxy.MacVerifier) (proxy.Packet, error) {
|
func (f *Flow) Produce(v proxy.MacVerifier) (proxy.Packet, error) {
|
||||||
@ -131,8 +162,11 @@ func (f *Flow) Produce(v proxy.MacVerifier) (proxy.Packet, error) {
|
|||||||
return nil, shared.ErrDeadConnection
|
return nil, shared.ErrDeadConnection
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := f.produceMarshalled()
|
var data []byte
|
||||||
if err != nil {
|
|
||||||
|
select {
|
||||||
|
case data = <-f.produced:
|
||||||
|
case err := <-f.produceErrors:
|
||||||
f.isAlive = false
|
f.isAlive = false
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -145,12 +179,34 @@ func (f *Flow) Produce(v proxy.MacVerifier) (proxy.Packet, error) {
|
|||||||
return proxy.SimplePacket(b), nil
|
return proxy.SimplePacket(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Flow) produceMarshalled() ([]byte, error) {
|
func (f *Flow) consumeMarshalled() {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Flow) produceMarshalled() {
|
||||||
|
buf := bufio.NewReader(f.conn)
|
||||||
|
|
||||||
|
for {
|
||||||
lengthBytes := make([]byte, 4)
|
lengthBytes := make([]byte, 4)
|
||||||
if n, err := io.LimitReader(f.conn, 4).Read(lengthBytes); err != nil {
|
if n, err := io.LimitReader(buf, 4).Read(lengthBytes); err != nil {
|
||||||
return nil, err
|
f.produceErrors <- err
|
||||||
|
return
|
||||||
} else if n != 4 {
|
} else if n != 4 {
|
||||||
return nil, shared.ErrNotEnoughBytes
|
f.produceErrors <- shared.ErrNotEnoughBytes
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
length := binary.LittleEndian.Uint32(lengthBytes)
|
length := binary.LittleEndian.Uint32(lengthBytes)
|
||||||
@ -158,12 +214,14 @@ func (f *Flow) produceMarshalled() ([]byte, error) {
|
|||||||
|
|
||||||
var read uint32
|
var read uint32
|
||||||
for read < length {
|
for read < length {
|
||||||
if n, err := io.LimitReader(f.conn, int64(length-read)).Read(dataBytes[read:]); err != nil {
|
if n, err := io.LimitReader(buf, int64(length-read)).Read(dataBytes[read:]); err != nil {
|
||||||
return nil, err
|
f.produceErrors <- err
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
read += uint32(n)
|
read += uint32(n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return dataBytes, nil
|
f.produced <- dataBytes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ func TestFlow_Consume(t *testing.T) {
|
|||||||
t.Run("Length", func(t *testing.T) {
|
t.Run("Length", func(t *testing.T) {
|
||||||
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
||||||
|
|
||||||
flowA := Flow{conn: testConn.SideA(), isAlive: true}
|
flowA := NewFlowConn(testConn.SideA())
|
||||||
|
|
||||||
err := flowA.Consume(testPacket, testMac)
|
err := flowA.Consume(testPacket, testMac)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
@ -41,7 +41,7 @@ func TestFlow_Produce(t *testing.T) {
|
|||||||
t.Run("Length", func(t *testing.T) {
|
t.Run("Length", func(t *testing.T) {
|
||||||
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
||||||
|
|
||||||
flowA := Flow{conn: testConn.SideA(), isAlive: true}
|
flowA := NewFlowConn(testConn.SideA())
|
||||||
|
|
||||||
_, err := testConn.SideB().Write(testMarshalled)
|
_, err := testConn.SideB().Write(testMarshalled)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
@ -54,7 +54,7 @@ func TestFlow_Produce(t *testing.T) {
|
|||||||
t.Run("Value", func(t *testing.T) {
|
t.Run("Value", func(t *testing.T) {
|
||||||
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
testConn := mocks.NewMockPerfectBiStreamConn(100)
|
||||||
|
|
||||||
flowA := Flow{conn: testConn.SideA(), isAlive: true}
|
flowA := NewFlowConn(testConn.SideA())
|
||||||
|
|
||||||
_, err := testConn.SideB().Write(testMarshalled)
|
_, err := testConn.SideB().Write(testMarshalled)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
@ -24,7 +24,7 @@ func NewListener(p *proxy.Proxy, local string, v func() proxy.MacVerifier, g fun
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f := Flow{conn: conn, isAlive: true}
|
f := NewFlowConn(conn)
|
||||||
|
|
||||||
log.Printf("received new tcp connection: %v\n", f)
|
log.Printf("received new tcp connection: %v\n", f)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user