reordering, formatting, some tests

This commit is contained in:
Jake Hillion 2020-10-25 15:36:34 +00:00
parent eef63a09b8
commit 5fb5a55f79
11 changed files with 94 additions and 32 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
config.ini config.ini
Makefile Makefile
logs/
# Created by https://www.toptal.com/developers/gitignore/api/intellij+all,go # Created by https://www.toptal.com/developers/gitignore/api/intellij+all,go
# Edit at https://www.toptal.com/developers/gitignore?templates=intellij+all,go # Edit at https://www.toptal.com/developers/gitignore?templates=intellij+all,go

View File

@ -26,6 +26,14 @@ func (c Configuration) Build() (*proxy.Proxy, error) {
p := proxy.Proxy{} p := proxy.Proxy{}
p.Generator = UselessMac(false) p.Generator = UselessMac(false)
ss, err := tun.NewTun("nc%d", 1500)
if err != nil {
return nil, err
}
p.Source = ss
p.Sink = ss
for _, peer := range c.Peers { for _, peer := range c.Peers {
p.Verifiers = append(p.Verifiers, UselessMac(true)) p.Verifiers = append(p.Verifiers, UselessMac(true))
@ -40,14 +48,6 @@ func (c Configuration) Build() (*proxy.Proxy, error) {
} }
} }
ss, err := tun.NewTun("nc%d", 1500)
if err != nil {
return nil, err
}
p.Source = ss
p.Sink = ss
return &p, nil return &p, nil
} }

View File

@ -7,8 +7,8 @@ import (
func LoadConfig(path string) (c Configuration, err error) { func LoadConfig(path string) (c Configuration, err error) {
var file *ini.File var file *ini.File
file, err = ini.LoadSources(ini.LoadOptions{ file, err = ini.LoadSources(ini.LoadOptions{
AllowShadows: true, AllowShadows: true,
AllowNonUniqueSections: true, AllowNonUniqueSections: true,
}, path) }, path)
if err != nil { if err != nil {
return return

1
go.mod
View File

@ -5,5 +5,6 @@ go 1.15
require ( require (
github.com/go-playground/validator/v10 v10.4.1 github.com/go-playground/validator/v10 v10.4.1
github.com/pkg/taptun v0.0.0-20160424131934-bbbd335672ab github.com/pkg/taptun v0.0.0-20160424131934-bbbd335672ab
github.com/stretchr/testify v1.4.0
gopkg.in/ini.v1 v1.62.0 gopkg.in/ini.v1 v1.62.0
) )

12
main.go
View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"fmt" "log"
"mpbl3p/config" "mpbl3p/config"
"os" "os"
"os/signal" "os/signal"
@ -9,23 +9,25 @@ import (
) )
func main() { func main() {
fmt.Println("loading config...") log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
log.Println("loading config...")
c, err := config.LoadConfig("config.ini") c, err := config.LoadConfig("config.ini")
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("building config...") log.Println("building config...")
p, err := c.Build() p, err := c.Build()
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("starting...") log.Println("starting...")
p.Start() p.Start()
fmt.Println("running") log.Println("running")
signals := make(chan os.Signal) signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT) signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)

View File

@ -13,7 +13,7 @@ type Packet struct {
// create a packet from the raw data of an IP packet // create a packet from the raw data of an IP packet
func NewPacket(data []byte) Packet { func NewPacket(data []byte) Packet {
return Packet{ return Packet{
Data: data, Data: data,
timestamp: time.Now(), timestamp: time.Now(),
} }
} }
@ -29,7 +29,7 @@ func UnmarshalPacket(raw []byte, verifier MacVerifier) (Packet, error) {
} }
p := Packet{ p := Packet{
Data: data[:8], Data: data[:len(data)-8],
} }
unixTime := int64(binary.LittleEndian.Uint64(data[len(data)-8:])) unixTime := int64(binary.LittleEndian.Uint64(data[len(data)-8:]))

50
proxy/packet_test.go Normal file
View File

@ -0,0 +1,50 @@
package proxy
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
type AlmostUselessMac bool
func (AlmostUselessMac) CodeLength() int {
return 4
}
func (AlmostUselessMac) Generate([]byte) []byte {
return []byte{'a', 'b', 'c', 'd'}
}
func (u AlmostUselessMac) Verify(_, sum []byte) error {
if !(sum[0] == 'a' && sum[1] == 'b' && sum[2] == 'c' && sum[3] == 'd') {
return ErrBadChecksum
}
return nil
}
func TestPacket_Marshal(t *testing.T) {
testContent := []byte("A test string is the content of this packet.")
testPacket := NewPacket(testContent)
testMac := AlmostUselessMac(false)
t.Run("Length", func(t *testing.T) {
marshalled := testPacket.Marshal(testMac)
assert.Len(t, marshalled, len(testContent)+8+4)
})
}
func TestUnmarshalPacket(t *testing.T) {
testContent := []byte("A test string is the content of this packet.")
testPacket := NewPacket(testContent)
testMac := AlmostUselessMac(false)
testMarshalled := testPacket.Marshal(testMac)
t.Run("Success", func(t *testing.T) {
content, err := UnmarshalPacket(testMarshalled, testMac)
require.Nil(t, err)
assert.Equal(t, testContent, content.Raw())
})
}

View File

@ -1,6 +1,8 @@
package proxy package proxy
import "fmt" import (
"log"
)
type Producer interface { type Producer interface {
IsAlive() bool IsAlive() bool
@ -44,6 +46,7 @@ func (p *Proxy) Start() {
panic(err) panic(err)
return return
} else { } else {
log.Println("proxy received packet from source")
p.proxyChan <- packet p.proxyChan <- packet
} }
} }
@ -55,23 +58,26 @@ func (p *Proxy) Start() {
go func() { go func() {
for c.IsAlive() { for c.IsAlive() {
if err := c.Consume(<-p.proxyChan, p.Generator); err != nil { if err := c.Consume(<-p.proxyChan, p.Generator); err != nil {
fmt.Println(err) log.Println(err)
return return
} else {
log.Println("proxy consumed a packet")
} }
} }
}() }()
} }
for i, prod := range p.Producers { for i, prod := range p.Producers {
i := i verifier := p.Verifiers[i]
prod := prod prod := prod
go func() { go func() {
for prod.IsAlive() { for prod.IsAlive() {
if packet, err := prod.Produce(p.Verifiers[i]); err != nil { if packet, err := prod.Produce(verifier); err != nil {
fmt.Println(err) log.Println(err)
return return
} else { } else {
log.Println("proxy received a packet from a producer")
p.sinkChan <- packet p.sinkChan <- packet
} }
} }
@ -84,7 +90,9 @@ func (p *Proxy) Start() {
if err := p.Sink.Sink(packet); err != nil { if err := p.Sink.Sink(packet); err != nil {
panic(err) panic(err)
return return
} else {
log.Println("proxy sent packet via sink")
} }
} }
}() }()
} }

View File

@ -19,7 +19,7 @@ type Flow struct {
func NewFlow(local, remote string) (Flow, error) { func NewFlow(local, remote string) (Flow, error) {
f := Flow{ f := Flow{
Local: local, Local: local,
Remote: remote, Remote: remote,
} }
@ -67,7 +67,7 @@ func (f *Flow) Consume(p proxy.Packet, g proxy.MacGenerator) error {
func (f *Flow) consumeMarshalled(data []byte) error { 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[:len(prefixedData)-4], data)
_, err := f.conn.Write(prefixedData) _, err := f.conn.Write(prefixedData)
return err return err
@ -82,7 +82,7 @@ func (f *Flow) Produce(v proxy.MacVerifier) (proxy.Packet, error) {
return proxy.UnmarshalPacket(data, v) return proxy.UnmarshalPacket(data, v)
} }
func (f *Flow) produceMarshalled() ([]byte,error) { func (f *Flow) produceMarshalled() ([]byte, error) {
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(f.conn, 4).Read(lengthBytes); err != nil {
return nil, err return nil, err
@ -99,4 +99,4 @@ func (f *Flow) produceMarshalled() ([]byte,error) {
} }
return dataBytes, nil return dataBytes, nil
} }

View File

@ -1,7 +1,7 @@
package tcp package tcp
import ( import (
"fmt" "log"
"mpbl3p/proxy" "mpbl3p/proxy"
"mpbl3p/utils" "mpbl3p/utils"
"net" "net"
@ -15,7 +15,7 @@ type Receiver struct {
} }
func NewReceiver(local string) (*Receiver, error) { func NewReceiver(local string) (*Receiver, error) {
r := Receiver{} r := Receiver{flows: make(map[int]Flow)}
laddr, err := net.ResolveTCPAddr("tcp", local) laddr, err := net.ResolveTCPAddr("tcp", local)
if err != nil { if err != nil {
@ -70,7 +70,7 @@ func (r *Receiver) addFlow(flow Flow) {
for flow.IsAlive() { for flow.IsAlive() {
d, err := flow.produceMarshalled() d, err := flow.produceMarshalled()
if err != nil { if err != nil {
fmt.Println(err) log.Println(err)
continue continue
} }
r.inPackets <- d r.inPackets <- d
@ -83,7 +83,7 @@ func (r *Receiver) addFlow(flow Flow) {
d := <-r.outPackets d := <-r.outPackets
err := flow.consumeMarshalled(d) err := flow.consumeMarshalled(d)
if err != nil { if err != nil {
fmt.Println(err) log.Println(err)
// continue // continue
} }
} }

View File

@ -10,4 +10,4 @@ func init() {
i += 1 i += 1
} }
}() }()
} }