2020-10-24 17:44:14 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-03-30 20:57:53 +01:00
|
|
|
"context"
|
2020-11-29 22:06:38 +00:00
|
|
|
"encoding/base64"
|
2020-10-24 17:44:14 +01:00
|
|
|
"fmt"
|
2020-11-29 22:06:38 +00:00
|
|
|
"mpbl3p/crypto"
|
|
|
|
"mpbl3p/crypto/sharedkey"
|
2020-10-24 18:27:31 +01:00
|
|
|
"mpbl3p/proxy"
|
2021-05-13 21:56:25 +01:00
|
|
|
"mpbl3p/replay"
|
2020-10-24 18:27:31 +01:00
|
|
|
"mpbl3p/tcp"
|
2020-11-26 18:55:29 +00:00
|
|
|
"mpbl3p/udp"
|
2020-11-26 22:15:22 +00:00
|
|
|
"mpbl3p/udp/congestion"
|
2021-04-14 17:07:59 +01:00
|
|
|
"mpbl3p/udp/congestion/newreno"
|
2020-11-26 22:15:22 +00:00
|
|
|
"time"
|
2020-10-24 17:44:14 +01:00
|
|
|
)
|
|
|
|
|
2021-03-30 20:57:53 +01:00
|
|
|
func (c Configuration) Build(ctx context.Context, source proxy.Source, sink proxy.Sink) (*proxy.Proxy, error) {
|
2020-11-01 20:41:42 +00:00
|
|
|
p := proxy.NewProxy(0)
|
2020-11-29 22:06:38 +00:00
|
|
|
|
2021-05-13 21:56:25 +01:00
|
|
|
var gs []func() proxy.MacGenerator
|
|
|
|
var vs []func() proxy.MacVerifier
|
|
|
|
|
|
|
|
if c.Host.ReplayProtection {
|
|
|
|
rp := replay.NewAntiReplay()
|
|
|
|
gs = append(gs, func() proxy.MacGenerator { return rp })
|
|
|
|
vs = append(vs, func() proxy.MacVerifier { return rp })
|
|
|
|
}
|
2020-11-29 22:06:38 +00:00
|
|
|
|
|
|
|
switch c.Host.Crypto {
|
|
|
|
case "None":
|
2021-05-13 21:56:25 +01:00
|
|
|
gs = append(gs, func() proxy.MacGenerator { return crypto.None{} })
|
|
|
|
vs = append(vs, func() proxy.MacVerifier { return crypto.None{} })
|
2020-11-29 22:06:38 +00:00
|
|
|
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
|
|
|
|
}
|
2021-05-13 21:56:25 +01:00
|
|
|
gs = append(gs, func() proxy.MacGenerator {
|
2020-11-29 22:06:38 +00:00
|
|
|
g, _ := sharedkey.NewBlake2s(key)
|
|
|
|
return g
|
2021-05-13 21:56:25 +01:00
|
|
|
})
|
|
|
|
vs = append(vs, func() proxy.MacVerifier {
|
2020-11-29 22:06:38 +00:00
|
|
|
v, _ := sharedkey.NewBlake2s(key)
|
|
|
|
return v
|
2021-05-13 21:56:25 +01:00
|
|
|
})
|
2020-11-29 22:06:38 +00:00
|
|
|
}
|
2020-10-24 17:44:14 +01:00
|
|
|
|
2021-03-26 11:58:03 +00:00
|
|
|
p.Source = source
|
|
|
|
p.Sink = sink
|
2020-10-25 15:36:34 +00:00
|
|
|
|
2020-10-24 17:44:14 +01:00
|
|
|
for _, peer := range c.Peers {
|
|
|
|
switch peer.Method {
|
|
|
|
case "TCP":
|
2021-05-13 21:56:25 +01:00
|
|
|
if err := buildTcp(ctx, p, peer, gs, vs); err != nil {
|
2020-10-24 17:44:14 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-26 18:55:29 +00:00
|
|
|
case "UDP":
|
2021-05-13 21:56:25 +01:00
|
|
|
if err := buildUdp(ctx, p, peer, gs, vs); err != nil {
|
2020-11-26 18:55:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-24 17:44:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:50:02 +00:00
|
|
|
return p, nil
|
2020-10-24 17:44:14 +01:00
|
|
|
}
|
|
|
|
|
2021-05-13 21:56:25 +01:00
|
|
|
func buildTcp(
|
|
|
|
ctx context.Context,
|
|
|
|
p *proxy.Proxy,
|
|
|
|
peer Peer,
|
|
|
|
gs []func() proxy.MacGenerator,
|
|
|
|
vs []func() proxy.MacVerifier,
|
|
|
|
) error {
|
2021-02-27 18:30:41 +00:00
|
|
|
var laddr func() string
|
|
|
|
if peer.LocalPort == 0 {
|
|
|
|
laddr = func() string { return fmt.Sprintf("%s:", peer.GetLocalHost()) }
|
|
|
|
} else {
|
|
|
|
laddr = func() string { return fmt.Sprintf("%s:%d", peer.GetLocalHost(), peer.LocalPort) }
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:50:02 +00:00
|
|
|
if peer.RemoteHost != "" {
|
2021-05-13 22:29:04 +01:00
|
|
|
f, err := tcp.InitiateFlow(laddr, fmt.Sprintf("%s:%d", peer.RemoteHost, peer.RemotePort), initialiseVerifiers(vs), initialiseGenerators(gs))
|
2020-10-24 17:44:14 +01:00
|
|
|
|
2020-11-26 18:55:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-09 18:50:00 +01:00
|
|
|
if !peer.DisableConsumer {
|
2021-05-13 21:56:25 +01:00
|
|
|
p.AddConsumer(ctx, f)
|
2021-04-09 18:50:00 +01:00
|
|
|
}
|
|
|
|
if !peer.DisableProducer {
|
2021-05-13 21:56:25 +01:00
|
|
|
p.AddProducer(ctx, f)
|
2021-04-09 18:50:00 +01:00
|
|
|
}
|
2020-11-01 16:50:02 +00:00
|
|
|
|
2020-11-26 18:55:29 +00:00
|
|
|
return nil
|
2020-10-24 17:44:14 +01:00
|
|
|
}
|
|
|
|
|
2021-05-13 21:56:25 +01:00
|
|
|
err := tcp.NewListener(ctx, p, laddr(), vs, gs, !peer.DisableConsumer, !peer.DisableProducer)
|
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
|
|
|
}
|
2020-11-26 18:55:29 +00:00
|
|
|
|
2021-05-13 21:56:25 +01:00
|
|
|
func buildUdp(
|
|
|
|
ctx context.Context,
|
|
|
|
p *proxy.Proxy,
|
|
|
|
peer Peer,
|
|
|
|
gs []func() proxy.MacGenerator,
|
|
|
|
vs []func() proxy.MacVerifier,
|
|
|
|
) error {
|
2021-02-27 18:30:41 +00:00
|
|
|
var laddr func() string
|
|
|
|
if peer.LocalPort == 0 {
|
|
|
|
laddr = func() string { return fmt.Sprintf("%s:", peer.GetLocalHost()) }
|
|
|
|
} else {
|
|
|
|
laddr = func() string { return fmt.Sprintf("%s:%d", peer.GetLocalHost(), peer.LocalPort) }
|
|
|
|
}
|
|
|
|
|
2020-11-27 20:17:59 +00:00
|
|
|
var c func() udp.Congestion
|
2020-11-26 22:26:01 +00:00
|
|
|
switch peer.Congestion {
|
|
|
|
case "None":
|
2020-11-29 22:06:38 +00:00
|
|
|
c = func() udp.Congestion { return congestion.NewNone() }
|
2020-11-26 22:26:01 +00:00
|
|
|
default:
|
|
|
|
fallthrough
|
|
|
|
case "NewReno":
|
2021-04-14 17:07:59 +01:00
|
|
|
c = func() udp.Congestion { return newreno.NewNewReno() }
|
2020-11-26 22:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 18:55:29 +00:00
|
|
|
if peer.RemoteHost != "" {
|
|
|
|
f, err := udp.InitiateFlow(
|
2021-02-27 18:30:41 +00:00
|
|
|
laddr,
|
2020-11-26 18:55:29 +00:00
|
|
|
fmt.Sprintf("%s:%d", peer.RemoteHost, peer.RemotePort),
|
2021-05-13 22:29:04 +01:00
|
|
|
initialiseVerifiers(vs),
|
|
|
|
initialiseGenerators(gs),
|
2020-11-27 20:17:59 +00:00
|
|
|
c(),
|
2020-11-26 22:15:22 +00:00
|
|
|
time.Duration(peer.KeepAlive)*time.Second,
|
2020-11-26 18:55:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-09 18:50:00 +01:00
|
|
|
if !peer.DisableConsumer {
|
2021-05-13 21:56:25 +01:00
|
|
|
p.AddConsumer(ctx, f)
|
2021-04-09 18:50:00 +01:00
|
|
|
}
|
|
|
|
if !peer.DisableProducer {
|
2021-05-13 21:56:25 +01:00
|
|
|
p.AddProducer(ctx, f)
|
2021-04-09 18:50:00 +01:00
|
|
|
}
|
2020-11-26 18:55:29 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:56:25 +01:00
|
|
|
err := udp.NewListener(ctx, p, laddr(), vs, gs, c, !peer.DisableConsumer, !peer.DisableProducer)
|
2020-11-26 18:55:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-13 21:56:25 +01:00
|
|
|
|
2021-05-13 22:29:04 +01:00
|
|
|
func initialiseVerifiers(vs []func() proxy.MacVerifier) (out []proxy.MacVerifier) {
|
2021-05-13 21:56:25 +01:00
|
|
|
out = make([]proxy.MacVerifier, len(vs))
|
|
|
|
for i, v := range vs {
|
|
|
|
out[i] = v()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 22:29:04 +01:00
|
|
|
func initialiseGenerators(gs []func() proxy.MacGenerator) (out []proxy.MacGenerator) {
|
2021-05-13 21:56:25 +01:00
|
|
|
out = make([]proxy.MacGenerator, len(gs))
|
|
|
|
for i, g := range gs {
|
|
|
|
out[i] = g()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|