Jake Hillion
ff4ce07b05
All checks were successful
continuous-integration/drone/push Build is passing
131 lines
2.2 KiB
Go
131 lines
2.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"mpbl3p/proxy"
|
|
"mpbl3p/tcp"
|
|
"mpbl3p/tun"
|
|
"mpbl3p/udp"
|
|
"mpbl3p/udp/congestion"
|
|
"time"
|
|
)
|
|
|
|
// 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{}
|
|
|
|
if c.Host.InterfaceName == "" {
|
|
c.Host.InterfaceName = "nc%d"
|
|
}
|
|
|
|
ss, err := tun.NewTun(c.Host.InterfaceName, 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
|
|
}
|
|
case "UDP":
|
|
err := buildUdp(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),
|
|
)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p.AddConsumer(f)
|
|
p.AddProducer(f, UselessMac{})
|
|
|
|
return nil
|
|
}
|
|
|
|
err := tcp.NewListener(p, fmt.Sprintf("%s:%d", peer.LocalHost, peer.LocalPort), UselessMac{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func buildUdp(p *proxy.Proxy, peer Peer) error {
|
|
var c func() udp.Congestion
|
|
switch peer.Congestion {
|
|
case "None":
|
|
c = func() udp.Congestion {return congestion.NewNone()}
|
|
default:
|
|
fallthrough
|
|
case "NewReno":
|
|
c = func() udp.Congestion {return congestion.NewNewReno()}
|
|
}
|
|
|
|
if peer.RemoteHost != "" {
|
|
f, err := udp.InitiateFlow(
|
|
fmt.Sprintf("%s:", peer.LocalHost),
|
|
fmt.Sprintf("%s:%d", peer.RemoteHost, peer.RemotePort),
|
|
UselessMac{},
|
|
UselessMac{},
|
|
c(),
|
|
time.Duration(peer.KeepAlive)*time.Second,
|
|
)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p.AddConsumer(f)
|
|
p.AddProducer(f, UselessMac{})
|
|
|
|
return nil
|
|
}
|
|
|
|
err := udp.NewListener(
|
|
p,
|
|
fmt.Sprintf("%s:%d", peer.LocalHost, peer.LocalPort),
|
|
UselessMac{},
|
|
UselessMac{},
|
|
c,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|