dissertation-2-code/config/builder.go

74 lines
1.2 KiB
Go
Raw Normal View History

2020-10-24 17:44:14 +01:00
package config
import (
"fmt"
"mpbl3p/proxy"
"mpbl3p/tcp"
"mpbl3p/tun"
2020-10-24 17:44:14 +01:00
)
// TODO: Delete this code as soon as an alternative is available
type UselessMac struct{}
2020-10-24 17:44:14 +01:00
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) {
2020-11-01 20:41:42 +00:00
p := proxy.NewProxy(0)
2020-10-25 21:44:56 +00:00
p.Generator = UselessMac{}
2020-10-24 17:44:14 +01:00
2020-11-02 17:24:15 +00:00
if c.Host.InterfaceName == "" {
c.Host.InterfaceName = "nc%d"
}
ss, err := tun.NewTun(c.Host.InterfaceName, 1500)
2020-10-25 15:36:34 +00:00
if err != nil {
return nil, err
}
p.Source = ss
p.Sink = ss
2020-10-24 17:44:14 +01:00
for _, peer := range c.Peers {
switch peer.Method {
case "TCP":
2020-11-01 16:50:02 +00:00
err := buildTcp(p, peer)
2020-10-24 17:44:14 +01:00
if err != nil {
return nil, err
}
}
}
2020-11-01 16:50:02 +00:00
return p, nil
2020-10-24 17:44:14 +01:00
}
2020-11-01 16:50:02 +00:00
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),
2020-10-24 17:44:14 +01:00
)
2020-11-01 17:16:43 +00:00
p.AddConsumer(f)
p.AddProducer(f, UselessMac{})
2020-11-01 16:50:02 +00:00
2020-11-01 20:41:42 +00:00
return err
2020-10-24 17:44:14 +01:00
}
2020-11-01 16:50:02 +00:00
err := tcp.NewListener(p, fmt.Sprintf("%s:%d", peer.LocalHost, peer.LocalPort), UselessMac{})
2020-10-24 17:44:14 +01:00
if err != nil {
2020-11-01 16:50:02 +00:00
return err
2020-10-24 17:44:14 +01:00
}
2020-11-01 16:50:02 +00:00
return nil
2020-10-24 17:44:14 +01:00
}