2020-10-24 17:44:14 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-10-24 18:27:31 +01:00
|
|
|
"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
|
2020-10-26 11:07:27 +00:00
|
|
|
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) {
|
|
|
|
p := proxy.Proxy{}
|
2020-10-25 21:44:56 +00:00
|
|
|
p.Generator = UselessMac{}
|
2020-10-24 17:44:14 +01:00
|
|
|
|
2020-10-25 15:36:34 +00:00
|
|
|
ss, err := tun.NewTun("nc%d", 1500)
|
|
|
|
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 {
|
2020-10-25 21:44:56 +00:00
|
|
|
p.Verifiers = append(p.Verifiers, UselessMac{})
|
2020-10-24 17:44:14 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|