Jake Hillion
5066f8a823
All checks were successful
continuous-integration/drone/push Build is passing
137 lines
2.6 KiB
Go
137 lines
2.6 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"mpbl3p/crypto"
|
|
"mpbl3p/crypto/sharedkey"
|
|
"mpbl3p/proxy"
|
|
"mpbl3p/tcp"
|
|
"mpbl3p/tun"
|
|
"mpbl3p/udp"
|
|
"mpbl3p/udp/congestion"
|
|
"time"
|
|
)
|
|
|
|
func (c Configuration) Build() (*proxy.Proxy, error) {
|
|
p := proxy.NewProxy(0)
|
|
|
|
var g func() proxy.MacGenerator
|
|
var v func() proxy.MacVerifier
|
|
|
|
switch c.Host.Crypto {
|
|
case "None":
|
|
g = func() proxy.MacGenerator { return crypto.None{} }
|
|
v = func() proxy.MacVerifier { return crypto.None{} }
|
|
case "Blake2s":
|
|
key, err := base64.StdEncoding.DecodeString(c.Host.SharedKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := sharedkey.NewBlake2s(key); err != nil {
|
|
return nil, err
|
|
}
|
|
g = func() proxy.MacGenerator {
|
|
g, _ := sharedkey.NewBlake2s(key)
|
|
return g
|
|
}
|
|
v = func() proxy.MacVerifier {
|
|
v, _ := sharedkey.NewBlake2s(key)
|
|
return v
|
|
}
|
|
}
|
|
|
|
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, g, v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
case "UDP":
|
|
err := buildUdp(p, peer, g, v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
return p, nil
|
|
}
|
|
|
|
func buildTcp(p *proxy.Proxy, peer Peer, v func() proxy.MacGenerator, g func() proxy.MacVerifier) 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, v())
|
|
p.AddProducer(f, g())
|
|
|
|
return nil
|
|
}
|
|
|
|
err := tcp.NewListener(p, fmt.Sprintf("%s:%d", peer.LocalHost, peer.LocalPort), g, v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func buildUdp(p *proxy.Proxy, peer Peer, v func() proxy.MacGenerator, g func() proxy.MacVerifier) 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),
|
|
crypto.None{},
|
|
crypto.None{},
|
|
c(),
|
|
time.Duration(peer.KeepAlive)*time.Second,
|
|
)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p.AddConsumer(f, v())
|
|
p.AddProducer(f, g())
|
|
|
|
return nil
|
|
}
|
|
|
|
err := udp.NewListener(p, fmt.Sprintf("%s:%d", peer.LocalHost, peer.LocalPort), g, v, c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|