2021-10-21 15:24:11 -06:00
|
|
|
//go:build e2e_testing
|
2021-03-29 13:29:20 -06:00
|
|
|
// +build e2e_testing
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
package udp
|
2021-03-29 13:29:20 -06:00
|
|
|
|
|
|
|
import (
|
2023-07-26 11:52:14 -06:00
|
|
|
"io"
|
2024-07-31 09:18:56 -06:00
|
|
|
"net/netip"
|
2023-07-26 11:52:14 -06:00
|
|
|
"sync/atomic"
|
2021-03-29 13:29:20 -06:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
|
|
|
"github.com/slackhq/nebula/header"
|
2021-03-29 13:29:20 -06:00
|
|
|
)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
type Packet struct {
|
2024-07-31 09:18:56 -06:00
|
|
|
To netip.AddrPort
|
|
|
|
From netip.AddrPort
|
|
|
|
Data []byte
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func (u *Packet) Copy() *Packet {
|
|
|
|
n := &Packet{
|
2024-07-31 09:18:56 -06:00
|
|
|
To: u.To,
|
|
|
|
From: u.From,
|
|
|
|
Data: make([]byte, len(u.Data)),
|
2021-03-31 12:36:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
copy(n.Data, u.Data)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2023-06-14 09:48:52 -06:00
|
|
|
type TesterConn struct {
|
2024-07-31 09:18:56 -06:00
|
|
|
Addr netip.AddrPort
|
2021-03-29 13:29:20 -06:00
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
RxPackets chan *Packet // Packets to receive into nebula
|
|
|
|
TxPackets chan *Packet // Packets transmitted outside by nebula
|
2021-03-29 13:29:20 -06:00
|
|
|
|
2023-07-26 11:52:14 -06:00
|
|
|
closed atomic.Bool
|
|
|
|
l *logrus.Logger
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func NewListener(l *logrus.Logger, ip netip.Addr, port int, _ bool, _ int) (Conn, error) {
|
2023-06-14 09:48:52 -06:00
|
|
|
return &TesterConn{
|
2024-07-31 09:18:56 -06:00
|
|
|
Addr: netip.AddrPortFrom(ip, uint16(port)),
|
2022-06-27 11:33:29 -06:00
|
|
|
RxPackets: make(chan *Packet, 10),
|
|
|
|
TxPackets: make(chan *Packet, 10),
|
2021-03-29 13:29:20 -06:00
|
|
|
l: l,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send will place a UdpPacket onto the receive queue for nebula to consume
|
|
|
|
// this is an encrypted packet or a handshake message in most cases
|
|
|
|
// packets were transmitted from another nebula node, you can send them with Tun.Send
|
2023-06-14 09:48:52 -06:00
|
|
|
func (u *TesterConn) Send(packet *Packet) {
|
2023-07-26 11:52:14 -06:00
|
|
|
if u.closed.Load() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
h := &header.H{}
|
2021-04-14 12:50:09 -06:00
|
|
|
if err := h.Parse(packet.Data); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-03-30 10:09:20 -06:00
|
|
|
if u.l.Level >= logrus.DebugLevel {
|
2022-09-01 08:44:58 -06:00
|
|
|
u.l.WithField("header", h).
|
2024-07-31 09:18:56 -06:00
|
|
|
WithField("udpAddr", packet.From).
|
2022-09-01 08:44:58 -06:00
|
|
|
WithField("dataLen", len(packet.Data)).
|
2023-03-13 11:35:14 -06:00
|
|
|
Debug("UDP receiving injected packet")
|
2022-09-01 08:44:58 -06:00
|
|
|
}
|
2021-11-03 19:54:04 -06:00
|
|
|
u.RxPackets <- packet
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get will pull a UdpPacket from the transmit queue
|
|
|
|
// nebula meant to send this message on the network, it will be encrypted
|
|
|
|
// packets were ingested from the tun side (in most cases), you can send them with Tun.Send
|
2023-06-14 09:48:52 -06:00
|
|
|
func (u *TesterConn) Get(block bool) *Packet {
|
2021-03-29 13:29:20 -06:00
|
|
|
if block {
|
2021-11-03 19:54:04 -06:00
|
|
|
return <-u.TxPackets
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2021-11-03 19:54:04 -06:00
|
|
|
case p := <-u.TxPackets:
|
2021-03-29 13:29:20 -06:00
|
|
|
return p
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//********************************************************************************************************************//
|
|
|
|
// Below this is boilerplate implementation to make nebula actually work
|
|
|
|
//********************************************************************************************************************//
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func (u *TesterConn) WriteTo(b []byte, addr netip.AddrPort) error {
|
2023-07-26 11:52:14 -06:00
|
|
|
if u.closed.Load() {
|
|
|
|
return io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
p := &Packet{
|
2024-07-31 09:18:56 -06:00
|
|
|
Data: make([]byte, len(b), len(b)),
|
|
|
|
From: u.Addr,
|
|
|
|
To: addr,
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
copy(p.Data, b)
|
2021-11-03 19:54:04 -06:00
|
|
|
u.TxPackets <- p
|
2021-03-29 13:29:20 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-23 21:02:10 -06:00
|
|
|
func (u *TesterConn) ListenOut(r EncReader) {
|
2021-03-29 13:29:20 -06:00
|
|
|
for {
|
2022-09-01 08:44:58 -06:00
|
|
|
p, ok := <-u.RxPackets
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2024-10-23 21:02:10 -06:00
|
|
|
r(p.From, p.Data)
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 09:48:52 -06:00
|
|
|
func (u *TesterConn) ReloadConfig(*config.C) {}
|
2021-03-29 13:29:20 -06:00
|
|
|
|
2023-06-14 09:48:52 -06:00
|
|
|
func NewUDPStatsEmitter(_ []Conn) func() {
|
2021-03-29 13:29:20 -06:00
|
|
|
// No UDP stats for non-linux
|
|
|
|
return func() {}
|
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func (u *TesterConn) LocalAddr() (netip.AddrPort, error) {
|
2021-11-03 19:54:04 -06:00
|
|
|
return u.Addr, nil
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
2023-06-14 09:48:52 -06:00
|
|
|
func (u *TesterConn) Rebind() error {
|
2021-03-29 13:29:20 -06:00
|
|
|
return nil
|
|
|
|
}
|
2023-07-10 11:43:48 -06:00
|
|
|
|
|
|
|
func (u *TesterConn) Close() error {
|
2023-07-26 11:52:14 -06:00
|
|
|
if u.closed.CompareAndSwap(false, true) {
|
|
|
|
close(u.RxPackets)
|
|
|
|
close(u.TxPackets)
|
|
|
|
}
|
2023-07-10 11:43:48 -06:00
|
|
|
return nil
|
|
|
|
}
|