2021-10-21 23:24:11 +02:00
|
|
|
//go:build e2e_testing
|
2021-03-29 21:29:20 +02:00
|
|
|
// +build e2e_testing
|
|
|
|
|
2021-11-11 23:37:29 +01:00
|
|
|
package overlay
|
2021-03-29 21:29:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-12 18:19:28 +01:00
|
|
|
"github.com/slackhq/nebula/cidr"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
2021-03-29 21:29:20 +02:00
|
|
|
)
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
type TestTun struct {
|
2021-11-12 18:19:28 +01:00
|
|
|
Device string
|
|
|
|
Cidr *net.IPNet
|
|
|
|
Routes []Route
|
|
|
|
cidrTree *cidr.Tree4
|
|
|
|
l *logrus.Logger
|
2021-03-29 21:29:20 +02:00
|
|
|
|
|
|
|
rxPackets chan []byte // Packets to receive into nebula
|
2021-11-11 23:37:29 +01:00
|
|
|
TxPackets chan []byte // Packets transmitted outside by nebula
|
2021-03-29 21:29:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 18:19:28 +01:00
|
|
|
func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, _ int, routes []Route, _ int, _ bool) (*TestTun, error) {
|
|
|
|
cidrTree, err := makeCidrTree(routes, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
return &TestTun{
|
2021-11-12 18:19:28 +01:00
|
|
|
Device: deviceName,
|
|
|
|
Cidr: cidr,
|
|
|
|
Routes: routes,
|
|
|
|
cidrTree: cidrTree,
|
|
|
|
l: l,
|
|
|
|
rxPackets: make(chan []byte, 1),
|
|
|
|
TxPackets: make(chan []byte, 1),
|
2021-03-29 21:29:20 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 18:19:28 +01:00
|
|
|
func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []Route, _ int) (*TestTun, error) {
|
2021-03-29 21:29:20 +02:00
|
|
|
return nil, fmt.Errorf("newTunFromFd not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send will place a byte array onto the receive queue for nebula to consume
|
|
|
|
// These are unencrypted ip layer frames destined for another nebula node.
|
|
|
|
// packets should exit the udp side, capture them with udpConn.Get
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *TestTun) Send(packet []byte) {
|
|
|
|
t.l.WithField("dataLen", len(packet)).Info("Tun receiving injected packet")
|
|
|
|
t.rxPackets <- packet
|
2021-03-29 21:29:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get will pull an unencrypted ip layer frame from the transmit queue
|
|
|
|
// nebula meant to send this message to some application on the local system
|
|
|
|
// packets were ingested from the udp side, you can send them with udpConn.Send
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *TestTun) Get(block bool) []byte {
|
2021-03-29 21:29:20 +02:00
|
|
|
if block {
|
2021-11-12 17:47:36 +01:00
|
|
|
return <-t.TxPackets
|
2021-03-29 21:29:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2021-11-12 17:47:36 +01:00
|
|
|
case p := <-t.TxPackets:
|
2021-03-29 21:29:20 +02:00
|
|
|
return p
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//********************************************************************************************************************//
|
|
|
|
// Below this is boilerplate implementation to make nebula actually work
|
|
|
|
//********************************************************************************************************************//
|
|
|
|
|
2021-11-12 18:19:28 +01:00
|
|
|
func (t *TestTun) RouteFor(ip iputil.VpnIp) iputil.VpnIp {
|
|
|
|
r := t.cidrTree.MostSpecificContains(ip)
|
|
|
|
if r != nil {
|
|
|
|
return r.(iputil.VpnIp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *TestTun) Activate() error {
|
2021-03-29 21:29:20 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *TestTun) CidrNet() *net.IPNet {
|
|
|
|
return t.Cidr
|
2021-03-29 21:29:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *TestTun) DeviceName() string {
|
|
|
|
return t.Device
|
2021-03-29 21:29:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *TestTun) Write(b []byte) (n int, err error) {
|
|
|
|
return len(b), t.WriteRaw(b)
|
2021-03-29 21:29:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *TestTun) Close() error {
|
|
|
|
close(t.rxPackets)
|
2021-03-29 21:29:20 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *TestTun) WriteRaw(b []byte) error {
|
2021-03-29 21:29:20 +02:00
|
|
|
packet := make([]byte, len(b), len(b))
|
|
|
|
copy(packet, b)
|
2021-11-12 17:47:36 +01:00
|
|
|
t.TxPackets <- packet
|
2021-03-29 21:29:20 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *TestTun) Read(b []byte) (int, error) {
|
|
|
|
p := <-t.rxPackets
|
2021-03-29 21:29:20 +02:00
|
|
|
copy(b, p)
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 17:47:36 +01:00
|
|
|
func (t *TestTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
|
2021-03-29 21:29:20 +02:00
|
|
|
return nil, fmt.Errorf("TODO: multiqueue not implemented")
|
|
|
|
}
|