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) {
|
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-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 {
|
|
|
|
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
|
|
|
}
|