dissertation-2-code/config/builder.go

75 lines
1.3 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 bool
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.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))
switch peer.Method {
case "TCP":
prod, cons, err := buildTcp(peer)
if err != nil {
return nil, err
}
p.Producers = append(p.Producers, prod)
p.Consumers = append(p.Consumers, cons)
}
}
return &p, nil
}
func buildTcp(p Peer) (proxy.Producer, proxy.Consumer, error) {
if p.RemoteHost != "" {
f, err := tcp.NewFlow(
fmt.Sprintf("%s:", p.LocalHost),
fmt.Sprintf("%s:%d", p.RemoteHost, p.RemotePort),
)
if err != nil {
return nil, nil, err
}
return &f, &f, nil
}
r, err := tcp.NewReceiver(fmt.Sprintf("%s:%d", p.LocalHost, p.LocalPort))
if err != nil {
return nil, nil, err
}
return r, r, nil
}