dissertation-2-code/config/builder.go
Jake Hillion fda177d8a8
Some checks failed
continuous-integration/drone/push Build is failing
logging and removing buffers
2020-11-01 20:41:42 +00:00

70 lines
1.1 KiB
Go

package config
import (
"fmt"
"mpbl3p/proxy"
"mpbl3p/tcp"
"mpbl3p/tun"
)
// TODO: Delete this code as soon as an alternative is available
type UselessMac struct{}
func (UselessMac) CodeLength() int {
return 0
}
func (UselessMac) Generate([]byte) []byte {
return nil
}
func (u UselessMac) Verify([]byte, []byte) error {
return nil
}
func (c Configuration) Build() (*proxy.Proxy, error) {
p := proxy.NewProxy(0)
p.Generator = UselessMac{}
ss, err := tun.NewTun("nc%d", 1500)
if err != nil {
return nil, err
}
p.Source = ss
p.Sink = ss
for _, peer := range c.Peers {
switch peer.Method {
case "TCP":
err := buildTcp(p, peer)
if err != nil {
return nil, err
}
}
}
return p, nil
}
func buildTcp(p *proxy.Proxy, peer Peer) error {
if peer.RemoteHost != "" {
f, err := tcp.InitiateFlow(
fmt.Sprintf("%s:", peer.LocalHost),
fmt.Sprintf("%s:%d", peer.RemoteHost, peer.RemotePort),
)
p.AddConsumer(f)
p.AddProducer(f, UselessMac{})
return err
}
err := tcp.NewListener(p, fmt.Sprintf("%s:%d", peer.LocalHost, peer.LocalPort), UselessMac{})
if err != nil {
return err
}
return nil
}