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
Makefile
logs/
# Created by https://www.toptal.com/developers/gitignore/api/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.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 {
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
}

View File

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

1
go.mod
View File

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

12
main.go
View File

@ -1,7 +1,7 @@
package main
import (
"fmt"
"log"
"mpbl3p/config"
"os"
"os/signal"
@ -9,23 +9,25 @@ import (
)
func main() {
fmt.Println("loading config...")
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
log.Println("loading config...")
c, err := config.LoadConfig("config.ini")
if err != nil {
panic(err)
}
fmt.Println("building config...")
log.Println("building config...")
p, err := c.Build()
if err != nil {
panic(err)
}
fmt.Println("starting...")
log.Println("starting...")
p.Start()
fmt.Println("running")
log.Println("running")
signals := make(chan os.Signal)
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
func NewPacket(data []byte) Packet {
return Packet{
Data: data,
Data: data,
timestamp: time.Now(),
}
}
@ -29,7 +29,7 @@ func UnmarshalPacket(raw []byte, verifier MacVerifier) (Packet, error) {
}
p := Packet{
Data: data[:8],
Data: 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
import "fmt"
import (
"log"
)
type Producer interface {
IsAlive() bool
@ -44,6 +46,7 @@ func (p *Proxy) Start() {
panic(err)
return
} else {
log.Println("proxy received packet from source")
p.proxyChan <- packet
}
}
@ -55,23 +58,26 @@ func (p *Proxy) Start() {
go func() {
for c.IsAlive() {
if err := c.Consume(<-p.proxyChan, p.Generator); err != nil {
fmt.Println(err)
log.Println(err)
return
} else {
log.Println("proxy consumed a packet")
}
}
}()
}
for i, prod := range p.Producers {
i := i
verifier := p.Verifiers[i]
prod := prod
go func() {
for prod.IsAlive() {
if packet, err := prod.Produce(p.Verifiers[i]); err != nil {
fmt.Println(err)
if packet, err := prod.Produce(verifier); err != nil {
log.Println(err)
return
} else {
log.Println("proxy received a packet from a producer")
p.sinkChan <- packet
}
}
@ -84,6 +90,8 @@ func (p *Proxy) Start() {
if err := p.Sink.Sink(packet); err != nil {
panic(err)
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) {
f := Flow{
Local: local,
Local: local,
Remote: remote,
}
@ -67,7 +67,7 @@ func (f *Flow) Consume(p proxy.Packet, g proxy.MacGenerator) error {
func (f *Flow) consumeMarshalled(data []byte) error {
prefixedData := make([]byte, len(data)+4)
binary.LittleEndian.PutUint32(prefixedData, uint32(len(data)))
copy(prefixedData[4:], data)
copy(prefixedData[:len(prefixedData)-4], data)
_, err := f.conn.Write(prefixedData)
return err
@ -82,7 +82,7 @@ func (f *Flow) Produce(v proxy.MacVerifier) (proxy.Packet, error) {
return proxy.UnmarshalPacket(data, v)
}
func (f *Flow) produceMarshalled() ([]byte,error) {
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

View File

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