| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500 |
- //go:build e2e_testing
- // +build e2e_testing
- package e2e
- import (
- "net/netip"
- "slices"
- "testing"
- "time"
- "github.com/google/gopacket"
- "github.com/google/gopacket/layers"
- "github.com/slackhq/nebula"
- "github.com/slackhq/nebula/cert"
- "github.com/slackhq/nebula/cert_test"
- "github.com/slackhq/nebula/e2e/router"
- "github.com/slackhq/nebula/header"
- "github.com/slackhq/nebula/udp"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.yaml.in/yaml/v3"
- )
- func BenchmarkHotPath(b *testing.B) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Put their info in our lighthouse
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Start the servers
- myControl.Start()
- theirControl.Start()
- r := router.NewR(b, myControl, theirControl)
- r.CancelFlowLogs()
- assertTunnel(b, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- b.ResetTimer()
- for n := 0; n < b.N; n++ {
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- _ = r.RouteForAllUntilTxTun(theirControl)
- }
- myControl.Stop()
- theirControl.Stop()
- }
- func BenchmarkHotPathRelay(b *testing.B) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(b, myControl, relayControl, theirControl)
- r.CancelFlowLogs()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- assertTunnel(b, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- b.ResetTimer()
- for n := 0; n < b.N; n++ {
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- _ = r.RouteForAllUntilTxTun(theirControl)
- }
- myControl.Stop()
- theirControl.Stop()
- relayControl.Stop()
- }
- func TestGoodHandshake(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Put their info in our lighthouse
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Start the servers
- myControl.Start()
- theirControl.Start()
- t.Log("Send a udp packet through to begin standing up the tunnel, this should come out the other side")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- t.Log("Have them consume my stage 0 packet. They have a tunnel now")
- theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
- t.Log("Get their stage 1 packet so that we can play with it")
- stage1Packet := theirControl.GetFromUDP(true)
- t.Log("I consume a garbage packet with a proper nebula header for our tunnel")
- // this should log a statement and get ignored, allowing the real handshake packet to complete the tunnel
- badPacket := stage1Packet.Copy()
- badPacket.Data = badPacket.Data[:len(badPacket.Data)-header.Len]
- myControl.InjectUDPPacket(badPacket)
- t.Log("Have me consume their real stage 1 packet. I have a tunnel now")
- myControl.InjectUDPPacket(stage1Packet)
- t.Log("Wait until we see my cached packet come through")
- myControl.WaitForType(1, 0, theirControl)
- t.Log("Make sure our host infos are correct")
- assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
- t.Log("Get that cached packet and make sure it looks right")
- myCachedPacket := theirControl.GetFromTun(true)
- assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- t.Log("Do a bidirectional tunnel test")
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestGoodHandshakeNoOverlap(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "2001::69/24", nil) //look ma, cross-stack!
- // Put their info in our lighthouse
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Start the servers
- myControl.Start()
- theirControl.Start()
- empty := []byte{}
- t.Log("do something to cause a handshake")
- myControl.GetF().SendMessageToVpnAddr(header.Test, header.MessageNone, theirVpnIpNet[0].Addr(), empty, empty, empty)
- t.Log("Have them consume my stage 0 packet. They have a tunnel now")
- theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
- t.Log("Get their stage 1 packet")
- stage1Packet := theirControl.GetFromUDP(true)
- t.Log("Have me consume their stage 1 packet. I have a tunnel now")
- myControl.InjectUDPPacket(stage1Packet)
- t.Log("Wait until we see a test packet come through to make sure we give the tunnel time to complete")
- myControl.WaitForType(header.Test, 0, theirControl)
- t.Log("Make sure our host infos are correct")
- assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestWrongResponderHandshake(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.100/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.99/24", nil)
- evilControl, evilVpnIp, evilUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "evil", "10.128.0.2/24", nil)
- // Put the evil udp addr in for their vpn Ip, this is a case of being lied to by the lighthouse.
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), evilUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl, evilControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- evilControl.Start()
- t.Log("Start the handshake process, we will route until we see the evil tunnel closed")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- h := &header.H{}
- r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
- err := h.Parse(p.Data)
- if err != nil {
- panic(err)
- }
- if h.Type == header.CloseTunnel && p.To == evilUdpAddr {
- return router.RouteAndExit
- }
- return router.KeepRouting
- })
- t.Log("Evil tunnel is closed, inject the correct udp addr for them")
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- pendingHi := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), true)
- assert.NotContains(t, pendingHi.RemoteAddrs, evilUdpAddr)
- t.Log("Route until we see the cached packet")
- r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
- err := h.Parse(p.Data)
- if err != nil {
- panic(err)
- }
- if p.To == theirUdpAddr && h.Type == 1 {
- return router.RouteAndExit
- }
- return router.KeepRouting
- })
- t.Log("My cached packet should be received by them")
- myCachedPacket := theirControl.GetFromTun(true)
- assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- t.Log("Test the tunnel with them")
- assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Flush all packets from all controllers")
- r.FlushAll()
- t.Log("Ensure ensure I don't have any hostinfo artifacts from evil")
- assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), true), "My pending hostmap should not contain evil")
- assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), false), "My main hostmap should not contain evil")
- r.RenderHostmaps("Final hostmaps", myControl, theirControl, evilControl)
- t.Log("Success!")
- myControl.Stop()
- theirControl.Stop()
- }
- func TestWrongResponderHandshakeStaticHostMap(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.99/24", nil)
- evilControl, evilVpnIp, evilUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "evil", "10.128.0.2/24", nil)
- o := m{
- "static_host_map": m{
- theirVpnIpNet[0].Addr().String(): []string{evilUdpAddr.String()},
- },
- }
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.100/24", o)
- // Put the evil udp addr in for their vpn addr, this is a case of a remote at a static entry changing its vpn addr.
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), evilUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl, evilControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- evilControl.Start()
- t.Log("Start the handshake process, we will route until we see the evil tunnel closed")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- h := &header.H{}
- r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
- err := h.Parse(p.Data)
- if err != nil {
- panic(err)
- }
- if h.Type == header.CloseTunnel && p.To == evilUdpAddr {
- return router.RouteAndExit
- }
- return router.KeepRouting
- })
- t.Log("Evil tunnel is closed, inject the correct udp addr for them")
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- pendingHi := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), true)
- assert.NotContains(t, pendingHi.RemoteAddrs, evilUdpAddr)
- t.Log("Route until we see the cached packet")
- r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
- err := h.Parse(p.Data)
- if err != nil {
- panic(err)
- }
- if p.To == theirUdpAddr && h.Type == 1 {
- return router.RouteAndExit
- }
- return router.KeepRouting
- })
- t.Log("My cached packet should be received by them")
- myCachedPacket := theirControl.GetFromTun(true)
- assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- t.Log("Test the tunnel with them")
- assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Flush all packets from all controllers")
- r.FlushAll()
- t.Log("Ensure ensure I don't have any hostinfo artifacts from evil")
- assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), true), "My pending hostmap should not contain evil")
- assert.Nil(t, myControl.GetHostInfoByVpnAddr(evilVpnIp[0].Addr(), false), "My main hostmap should not contain evil")
- //NOTE: if evil lost the handshake race it may still have a tunnel since me would reject the handshake since the tunnel is complete
- r.RenderHostmaps("Final hostmaps", myControl, theirControl, evilControl)
- t.Log("Success!")
- myControl.Stop()
- theirControl.Stop()
- }
- func TestStage1Race(t *testing.T) {
- // This tests ensures that two hosts handshaking with each other at the same time will allow traffic to flow
- // But will eventually collapse down to a single tunnel
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Put their info in our lighthouse and vice versa
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake to start on both me and them")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- theirControl.InjectTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them"))
- t.Log("Get both stage 1 handshake packets")
- myHsForThem := myControl.GetFromUDP(true)
- theirHsForMe := theirControl.GetFromUDP(true)
- r.Log("Now inject both stage 1 handshake packets")
- r.InjectUDPPacket(theirControl, myControl, theirHsForMe)
- r.InjectUDPPacket(myControl, theirControl, myHsForThem)
- r.Log("Route until they receive a message packet")
- myCachedPacket := r.RouteForAllUntilTxTun(theirControl)
- assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.Log("Their cached packet should be received by me")
- theirCachedPacket := r.RouteForAllUntilTxTun(myControl)
- assertUdpPacket(t, []byte("Hi from them"), theirCachedPacket, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
- r.Log("Do a bidirectional tunnel test")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- myHostmapHosts := myControl.ListHostmapHosts(false)
- myHostmapIndexes := myControl.ListHostmapIndexes(false)
- theirHostmapHosts := theirControl.ListHostmapHosts(false)
- theirHostmapIndexes := theirControl.ListHostmapIndexes(false)
- // We should have two tunnels on both sides
- assert.Len(t, myHostmapHosts, 1)
- assert.Len(t, theirHostmapHosts, 1)
- assert.Len(t, myHostmapIndexes, 2)
- assert.Len(t, theirHostmapIndexes, 2)
- r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
- r.Log("Spin until connection manager tears down a tunnel")
- for len(myControl.GetHostmap().Indexes)+len(theirControl.GetHostmap().Indexes) > 2 {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Connection manager hasn't ticked yet")
- time.Sleep(time.Second)
- }
- myFinalHostmapHosts := myControl.ListHostmapHosts(false)
- myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
- theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
- theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
- // We should only have a single tunnel now on both sides
- assert.Len(t, myFinalHostmapHosts, 1)
- assert.Len(t, theirFinalHostmapHosts, 1)
- assert.Len(t, myFinalHostmapIndexes, 1)
- assert.Len(t, theirFinalHostmapIndexes, 1)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestUncleanShutdownRaceLoser(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- r.Log("Trigger a handshake from me to them")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- p := r.RouteForAllUntilTxTun(theirControl)
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.Log("Nuke my hostmap")
- myHostmap := myControl.GetHostmap()
- myHostmap.Hosts = map[netip.Addr]*nebula.HostInfo{}
- myHostmap.Indexes = map[uint32]*nebula.HostInfo{}
- myHostmap.RemoteIndexes = map[uint32]*nebula.HostInfo{}
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me again"))
- p = r.RouteForAllUntilTxTun(theirControl)
- assertUdpPacket(t, []byte("Hi from me again"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.Log("Assert the tunnel works")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- r.Log("Wait for the dead index to go away")
- start := len(theirControl.GetHostmap().Indexes)
- for {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- if len(theirControl.GetHostmap().Indexes) < start {
- break
- }
- time.Sleep(time.Second)
- }
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- }
- func TestUncleanShutdownRaceWinner(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- r.Log("Trigger a handshake from me to them")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- p := r.RouteForAllUntilTxTun(theirControl)
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- r.Log("Nuke my hostmap")
- theirHostmap := theirControl.GetHostmap()
- theirHostmap.Hosts = map[netip.Addr]*nebula.HostInfo{}
- theirHostmap.Indexes = map[uint32]*nebula.HostInfo{}
- theirHostmap.RemoteIndexes = map[uint32]*nebula.HostInfo{}
- theirControl.InjectTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them again"))
- p = r.RouteForAllUntilTxTun(myControl)
- assertUdpPacket(t, []byte("Hi from them again"), p, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("Derp hostmaps", myControl, theirControl)
- r.Log("Assert the tunnel works")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- r.Log("Wait for the dead index to go away")
- start := len(myControl.GetHostmap().Indexes)
- for {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- if len(myControl.GetHostmap().Indexes) < start {
- break
- }
- time.Sleep(time.Second)
- }
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- }
- func TestRelays(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- p := r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
- }
- func TestRelaysDontCareAboutIps(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay ", "2001::9999/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- p := r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
- }
- func TestReestablishRelays(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- p := r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- t.Log("Ensure packet traversal from them to me via the relay")
- theirControl.InjectTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them"))
- p = r.RouteForAllUntilTxTun(myControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from them"), p, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), 80, 80)
- // If we break the relay's connection to 'them', 'me' needs to detect and recover the connection
- r.Log("Close the tunnel")
- relayControl.CloseTunnel(theirVpnIpNet[0].Addr(), true)
- start := len(myControl.GetHostmap().Indexes)
- curIndexes := len(myControl.GetHostmap().Indexes)
- for curIndexes >= start {
- curIndexes = len(myControl.GetHostmap().Indexes)
- r.Logf("Wait for the dead index to go away:start=%v indexes, current=%v indexes", start, curIndexes)
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me should fail"))
- r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
- return router.RouteAndExit
- })
- time.Sleep(2 * time.Second)
- }
- r.Log("Dead index went away. Woot!")
- r.RenderHostmaps("Me removed hostinfo", myControl, relayControl, theirControl)
- // Next packet should re-establish a relayed connection and work just great.
- t.Logf("Assert the tunnel...")
- for {
- t.Log("RouteForAllUntilTxTun")
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- p = r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- packet := gopacket.NewPacket(p, layers.LayerTypeIPv4, gopacket.Lazy)
- v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
- if slices.Compare(v4.SrcIP, myVpnIpNet[0].Addr().AsSlice()) != 0 {
- t.Logf("SrcIP is unexpected...this is not the packet I'm looking for. Keep looking")
- continue
- }
- if slices.Compare(v4.DstIP, theirVpnIpNet[0].Addr().AsSlice()) != 0 {
- t.Logf("DstIP is unexpected...this is not the packet I'm looking for. Keep looking")
- continue
- }
- udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
- if udp == nil {
- t.Log("Not a UDP packet. This is not the packet I'm looking for. Keep looking")
- continue
- }
- data := packet.ApplicationLayer()
- if data == nil {
- t.Log("No data found in packet. This is not the packet I'm looking for. Keep looking.")
- continue
- }
- if string(data.Payload()) != "Hi from me" {
- t.Logf("Unexpected payload: '%v', keep looking", string(data.Payload()))
- continue
- }
- t.Log("I found my lost packet. I am so happy.")
- break
- }
- t.Log("Assert the tunnel works the other way, too")
- for {
- t.Log("RouteForAllUntilTxTun")
- theirControl.InjectTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them"))
- p = r.RouteForAllUntilTxTun(myControl)
- r.Log("Assert the tunnel works")
- packet := gopacket.NewPacket(p, layers.LayerTypeIPv4, gopacket.Lazy)
- v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
- if slices.Compare(v4.DstIP, myVpnIpNet[0].Addr().AsSlice()) != 0 {
- t.Logf("Dst is unexpected...this is not the packet I'm looking for. Keep looking")
- continue
- }
- if slices.Compare(v4.SrcIP, theirVpnIpNet[0].Addr().AsSlice()) != 0 {
- t.Logf("SrcIP is unexpected...this is not the packet I'm looking for. Keep looking")
- continue
- }
- udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
- if udp == nil {
- t.Log("Not a UDP packet. This is not the packet I'm looking for. Keep looking")
- continue
- }
- data := packet.ApplicationLayer()
- if data == nil {
- t.Log("No data found in packet. This is not the packet I'm looking for. Keep looking.")
- continue
- }
- if string(data.Payload()) != "Hi from them" {
- t.Logf("Unexpected payload: '%v', keep looking", string(data.Payload()))
- continue
- }
- t.Log("I found my lost packet. I am so happy.")
- break
- }
- r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
- }
- func TestStage1RaceRelays(t *testing.T) {
- //NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- theirControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- theirControl.InjectRelays(myVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- relayControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- r.Log("Get a tunnel between me and relay")
- assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
- r.Log("Get a tunnel between them and relay")
- assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
- r.Log("Trigger a handshake from both them and me via relay to them and me")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- theirControl.InjectTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them"))
- r.Log("Wait for a packet from them to me")
- p := r.RouteForAllUntilTxTun(myControl)
- _ = p
- r.FlushAll()
- myControl.Stop()
- theirControl.Stop()
- relayControl.Stop()
- }
- func TestStage1RaceRelays2(t *testing.T) {
- //NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- theirControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- theirControl.InjectRelays(myVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- relayControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- r.Log("Get a tunnel between me and relay")
- assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
- r.Log("Get a tunnel between them and relay")
- assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
- r.Log("Trigger a handshake from both them and me via relay to them and me")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- theirControl.InjectTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them"))
- //r.RouteUntilAfterMsgType(myControl, header.Control, header.MessageNone)
- //r.RouteUntilAfterMsgType(theirControl, header.Control, header.MessageNone)
- r.Log("Wait for a packet from them to me; myControl")
- r.RouteForAllUntilTxTun(myControl)
- r.Log("Wait for a packet from them to me; theirControl")
- r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- t.Log("Wait until we remove extra tunnels")
- t.Logf("Waiting for hostinfos to be removed... myControl=%d theirControl=%d relayControl=%d",
- len(myControl.GetHostmap().Indexes),
- len(theirControl.GetHostmap().Indexes),
- len(relayControl.GetHostmap().Indexes),
- )
- hostInfos := len(myControl.GetHostmap().Indexes) + len(theirControl.GetHostmap().Indexes) + len(relayControl.GetHostmap().Indexes)
- retries := 60
- for hostInfos > 6 && retries > 0 {
- hostInfos = len(myControl.GetHostmap().Indexes) + len(theirControl.GetHostmap().Indexes) + len(relayControl.GetHostmap().Indexes)
- t.Logf("Waiting for hostinfos to be removed... myControl=%d theirControl=%d relayControl=%d",
- len(myControl.GetHostmap().Indexes),
- len(theirControl.GetHostmap().Indexes),
- len(relayControl.GetHostmap().Indexes),
- )
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Connection manager hasn't ticked yet")
- time.Sleep(time.Second)
- retries--
- }
- r.Log("Assert the tunnel works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- myControl.Stop()
- theirControl.Stop()
- relayControl.Stop()
- }
- func TestRehandshakingRelays(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- p := r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
- // When I update the certificate for the relay, both me and them will have 2 host infos for the relay,
- // and the main host infos will not have any relay state to handle the me<->relay<->them tunnel.
- r.Log("Renew relay certificate and spin until me and them sees it")
- _, _, myNextPrivKey, myNextPEM := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "relay", time.Now(), time.Now().Add(5*time.Minute), relayVpnIpNet, nil, []string{"new group"})
- caB, err := ca.MarshalPEM()
- if err != nil {
- panic(err)
- }
- relayConfig.Settings["pki"] = m{
- "ca": string(caB),
- "cert": string(myNextPEM),
- "key": string(myNextPrivKey),
- }
- rc, err := yaml.Marshal(relayConfig.Settings)
- require.NoError(t, err)
- relayConfig.ReloadConfigString(string(rc))
- for {
- r.Log("Assert the tunnel works between myVpnIpNet and relayVpnIpNet")
- assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
- c := myControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
- if len(c.Cert.Groups()) != 0 {
- // We have a new certificate now
- r.Log("Certificate between my and relay is updated!")
- break
- }
- time.Sleep(time.Second)
- }
- for {
- r.Log("Assert the tunnel works between theirVpnIpNet and relayVpnIpNet")
- assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
- c := theirControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
- if len(c.Cert.Groups()) != 0 {
- // We have a new certificate now
- r.Log("Certificate between their and relay is updated!")
- break
- }
- time.Sleep(time.Second)
- }
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
- // We should have two hostinfos on all sides
- for len(myControl.GetHostmap().Indexes) != 2 {
- t.Logf("Waiting for myControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(myControl.GetHostmap().Indexes))
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("myControl hostinfos got cleaned up!")
- for len(theirControl.GetHostmap().Indexes) != 2 {
- t.Logf("Waiting for theirControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(theirControl.GetHostmap().Indexes))
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("theirControl hostinfos got cleaned up!")
- for len(relayControl.GetHostmap().Indexes) != 2 {
- t.Logf("Waiting for relayControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(relayControl.GetHostmap().Indexes))
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("relayControl hostinfos got cleaned up!")
- }
- func TestRehandshakingRelaysPrimary(t *testing.T) {
- // This test is the same as TestRehandshakingRelays but one of the terminal types is a primary swap winner
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.128/24", m{"relay": m{"use_relays": true}})
- relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(cert.Version1, ca, caKey, "relay ", "10.128.0.1/24", m{"relay": m{"am_relay": true}})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
- // Teach my how to get to the relay and that their can be reached via the relay
- myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
- myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
- relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, relayControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- relayControl.Start()
- theirControl.Start()
- t.Log("Trigger a handshake from me to them via the relay")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- p := r.RouteForAllUntilTxTun(theirControl)
- r.Log("Assert the tunnel works")
- assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
- r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
- // When I update the certificate for the relay, both me and them will have 2 host infos for the relay,
- // and the main host infos will not have any relay state to handle the me<->relay<->them tunnel.
- r.Log("Renew relay certificate and spin until me and them sees it")
- _, _, myNextPrivKey, myNextPEM := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "relay", time.Now(), time.Now().Add(5*time.Minute), relayVpnIpNet, nil, []string{"new group"})
- caB, err := ca.MarshalPEM()
- if err != nil {
- panic(err)
- }
- relayConfig.Settings["pki"] = m{
- "ca": string(caB),
- "cert": string(myNextPEM),
- "key": string(myNextPrivKey),
- }
- rc, err := yaml.Marshal(relayConfig.Settings)
- require.NoError(t, err)
- relayConfig.ReloadConfigString(string(rc))
- for {
- r.Log("Assert the tunnel works between myVpnIpNet and relayVpnIpNet")
- assertTunnel(t, myVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), myControl, relayControl, r)
- c := myControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
- if len(c.Cert.Groups()) != 0 {
- // We have a new certificate now
- r.Log("Certificate between my and relay is updated!")
- break
- }
- time.Sleep(time.Second)
- }
- for {
- r.Log("Assert the tunnel works between theirVpnIpNet and relayVpnIpNet")
- assertTunnel(t, theirVpnIpNet[0].Addr(), relayVpnIpNet[0].Addr(), theirControl, relayControl, r)
- c := theirControl.GetHostInfoByVpnAddr(relayVpnIpNet[0].Addr(), false)
- if len(c.Cert.Groups()) != 0 {
- // We have a new certificate now
- r.Log("Certificate between their and relay is updated!")
- break
- }
- time.Sleep(time.Second)
- }
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
- // We should have two hostinfos on all sides
- for len(myControl.GetHostmap().Indexes) != 2 {
- t.Logf("Waiting for myControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(myControl.GetHostmap().Indexes))
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("myControl hostinfos got cleaned up!")
- for len(theirControl.GetHostmap().Indexes) != 2 {
- t.Logf("Waiting for theirControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(theirControl.GetHostmap().Indexes))
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("theirControl hostinfos got cleaned up!")
- for len(relayControl.GetHostmap().Indexes) != 2 {
- t.Logf("Waiting for relayControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(relayControl.GetHostmap().Indexes))
- r.Log("Assert the relay tunnel still works")
- assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
- r.Log("yupitdoes")
- time.Sleep(time.Second)
- }
- t.Logf("relayControl hostinfos got cleaned up!")
- }
- func TestRehandshaking(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.2/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.1/24", nil)
- // Put their info in our lighthouse and vice versa
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- t.Log("Stand up a tunnel between me and them")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
- r.Log("Renew my certificate and spin until their sees it")
- _, _, myNextPrivKey, myNextPEM := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "me", time.Now(), time.Now().Add(5*time.Minute), myVpnIpNet, nil, []string{"new group"})
- caB, err := ca.MarshalPEM()
- if err != nil {
- panic(err)
- }
- myConfig.Settings["pki"] = m{
- "ca": string(caB),
- "cert": string(myNextPEM),
- "key": string(myNextPrivKey),
- }
- rc, err := yaml.Marshal(myConfig.Settings)
- require.NoError(t, err)
- myConfig.ReloadConfigString(string(rc))
- for {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
- if len(c.Cert.Groups()) != 0 {
- // We have a new certificate now
- break
- }
- time.Sleep(time.Second)
- }
- r.Log("Got the new cert")
- // Flip their firewall to only allowing the new group to catch the tunnels reverting incorrectly
- rc, err = yaml.Marshal(theirConfig.Settings)
- require.NoError(t, err)
- var theirNewConfig m
- require.NoError(t, yaml.Unmarshal(rc, &theirNewConfig))
- theirFirewall := theirNewConfig["firewall"].(map[string]any)
- theirFirewall["inbound"] = []m{{
- "proto": "any",
- "port": "any",
- "group": "new group",
- }}
- rc, err = yaml.Marshal(theirNewConfig)
- require.NoError(t, err)
- theirConfig.ReloadConfigString(string(rc))
- r.Log("Spin until there is only 1 tunnel")
- for len(myControl.GetHostmap().Indexes)+len(theirControl.GetHostmap().Indexes) > 2 {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Connection manager hasn't ticked yet")
- time.Sleep(time.Second)
- }
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- myFinalHostmapHosts := myControl.ListHostmapHosts(false)
- myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
- theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
- theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
- // Make sure the correct tunnel won
- c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
- assert.Contains(t, c.Cert.Groups(), "new group")
- // We should only have a single tunnel now on both sides
- assert.Len(t, myFinalHostmapHosts, 1)
- assert.Len(t, theirFinalHostmapHosts, 1)
- assert.Len(t, myFinalHostmapIndexes, 1)
- assert.Len(t, theirFinalHostmapIndexes, 1)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestRehandshakingLoser(t *testing.T) {
- // The purpose of this test is that the race loser renews their certificate and rehandshakes. The final tunnel
- // Should be the one with the new certificate
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version1, ca, caKey, "me ", "10.128.0.2/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.1/24", nil)
- // Put their info in our lighthouse and vice versa
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- myControl.Start()
- theirControl.Start()
- t.Log("Stand up a tunnel between me and them")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
- theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
- r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
- r.Log("Renew their certificate and spin until mine sees it")
- _, _, theirNextPrivKey, theirNextPEM := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "them", time.Now(), time.Now().Add(5*time.Minute), theirVpnIpNet, nil, []string{"their new group"})
- caB, err := ca.MarshalPEM()
- if err != nil {
- panic(err)
- }
- theirConfig.Settings["pki"] = m{
- "ca": string(caB),
- "cert": string(theirNextPEM),
- "key": string(theirNextPrivKey),
- }
- rc, err := yaml.Marshal(theirConfig.Settings)
- require.NoError(t, err)
- theirConfig.ReloadConfigString(string(rc))
- for {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- theirCertInMe := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
- if slices.Contains(theirCertInMe.Cert.Groups(), "their new group") {
- break
- }
- time.Sleep(time.Second)
- }
- // Flip my firewall to only allowing the new group to catch the tunnels reverting incorrectly
- rc, err = yaml.Marshal(myConfig.Settings)
- require.NoError(t, err)
- var myNewConfig m
- require.NoError(t, yaml.Unmarshal(rc, &myNewConfig))
- theirFirewall := myNewConfig["firewall"].(map[string]any)
- theirFirewall["inbound"] = []m{{
- "proto": "any",
- "port": "any",
- "group": "their new group",
- }}
- rc, err = yaml.Marshal(myNewConfig)
- require.NoError(t, err)
- myConfig.ReloadConfigString(string(rc))
- r.Log("Spin until there is only 1 tunnel")
- for len(myControl.GetHostmap().Indexes)+len(theirControl.GetHostmap().Indexes) > 2 {
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- t.Log("Connection manager hasn't ticked yet")
- time.Sleep(time.Second)
- }
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- myFinalHostmapHosts := myControl.ListHostmapHosts(false)
- myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
- theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
- theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
- // Make sure the correct tunnel won
- theirCertInMe := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
- assert.Contains(t, theirCertInMe.Cert.Groups(), "their new group")
- // We should only have a single tunnel now on both sides
- assert.Len(t, myFinalHostmapHosts, 1)
- assert.Len(t, theirFinalHostmapHosts, 1)
- assert.Len(t, myFinalHostmapIndexes, 1)
- assert.Len(t, theirFinalHostmapIndexes, 1)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestRaceRegression(t *testing.T) {
- // This test forces stage 1, stage 2, stage 1 to be received by me from them
- // We had a bug where we were not finding the duplicate handshake and responding to the final stage 1 which
- // caused a cross-linked hostinfo
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
- // Put their info in our lighthouse
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
- // Start the servers
- myControl.Start()
- theirControl.Start()
- //them rx stage:1 initiatorIndex=642843150 responderIndex=0
- //me rx stage:1 initiatorIndex=120607833 responderIndex=0
- //them rx stage:1 initiatorIndex=642843150 responderIndex=0
- //me rx stage:2 initiatorIndex=642843150 responderIndex=3701775874
- //me rx stage:1 initiatorIndex=120607833 responderIndex=0
- //them rx stage:2 initiatorIndex=120607833 responderIndex=4209862089
- t.Log("Start both handshakes")
- myControl.InjectTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- theirControl.InjectTunUDPPacket(myVpnIpNet[0].Addr(), 80, theirVpnIpNet[0].Addr(), 80, []byte("Hi from them"))
- t.Log("Get both stage 1")
- myStage1ForThem := myControl.GetFromUDP(true)
- theirStage1ForMe := theirControl.GetFromUDP(true)
- t.Log("Inject them in a special way")
- theirControl.InjectUDPPacket(myStage1ForThem)
- myControl.InjectUDPPacket(theirStage1ForMe)
- theirControl.InjectUDPPacket(myStage1ForThem)
- t.Log("Get both stage 2")
- myStage2ForThem := myControl.GetFromUDP(true)
- theirStage2ForMe := theirControl.GetFromUDP(true)
- t.Log("Inject them in a special way again")
- myControl.InjectUDPPacket(theirStage2ForMe)
- myControl.InjectUDPPacket(theirStage1ForMe)
- theirControl.InjectUDPPacket(myStage2ForThem)
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- t.Log("Flush the packets")
- r.RouteForAllUntilTxTun(myControl)
- r.RouteForAllUntilTxTun(theirControl)
- r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
- t.Log("Make sure the tunnel still works")
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- myControl.Stop()
- theirControl.Stop()
- }
- func TestV2NonPrimaryWithLighthouse(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh ", "10.128.0.1/24, ff::1/64", m{"lighthouse": m{"am_lighthouse": true}})
- o := m{
- "static_host_map": m{
- lhVpnIpNet[1].Addr().String(): []string{lhUdpAddr.String()},
- },
- "lighthouse": m{
- "hosts": []string{lhVpnIpNet[1].Addr().String()},
- "local_allow_list": m{
- // Try and block our lighthouse updates from using the actual addresses assigned to this computer
- // If we start discovering addresses the test router doesn't know about then test traffic cant flow
- "10.0.0.0/24": true,
- "::/0": false,
- },
- },
- }
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.2/24, ff::2/64", o)
- theirControl, theirVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "10.128.0.3/24, ff::3/64", o)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, lhControl, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- lhControl.Start()
- myControl.Start()
- theirControl.Start()
- t.Log("Stand up an ipv6 tunnel between me and them")
- assert.True(t, myVpnIpNet[1].Addr().Is6())
- assert.True(t, theirVpnIpNet[1].Addr().Is6())
- assertTunnel(t, myVpnIpNet[1].Addr(), theirVpnIpNet[1].Addr(), myControl, theirControl, r)
- lhControl.Stop()
- myControl.Stop()
- theirControl.Stop()
- }
- func TestV2NonPrimaryWithOffNetLighthouse(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh ", "2001::1/64", m{"lighthouse": m{"am_lighthouse": true}})
- o := m{
- "static_host_map": m{
- lhVpnIpNet[0].Addr().String(): []string{lhUdpAddr.String()},
- },
- "lighthouse": m{
- "hosts": []string{lhVpnIpNet[0].Addr().String()},
- "local_allow_list": m{
- // Try and block our lighthouse updates from using the actual addresses assigned to this computer
- // If we start discovering addresses the test router doesn't know about then test traffic cant flow
- "10.0.0.0/24": true,
- "::/0": false,
- },
- },
- }
- myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.2/24, ff::2/64", o)
- theirControl, theirVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "them", "10.128.0.3/24, ff::3/64", o)
- // Build a router so we don't have to reason who gets which packet
- r := router.NewR(t, lhControl, myControl, theirControl)
- defer r.RenderFlow()
- // Start the servers
- lhControl.Start()
- myControl.Start()
- theirControl.Start()
- t.Log("Stand up an ipv6 tunnel between me and them")
- assert.True(t, myVpnIpNet[1].Addr().Is6())
- assert.True(t, theirVpnIpNet[1].Addr().Is6())
- assertTunnel(t, myVpnIpNet[1].Addr(), theirVpnIpNet[1].Addr(), myControl, theirControl, r)
- lhControl.Stop()
- myControl.Stop()
- theirControl.Stop()
- }
- func TestLighthouseUpdateOnReload(t *testing.T) {
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- // Create the lighthouse
- lhControl, lhVpnIpNet, lhUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "lh", "10.128.0.1/24", m{"lighthouse": m{"am_lighthouse": true}})
- // Create a client with NO lighthouse configured and a long update interval.
- // The initial SendUpdate at startup will be a no-op since no lighthouses are known.
- myControl, myVpnIpNet, _, myConfig := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.2/24", m{
- "lighthouse": m{
- "interval": 600,
- "local_allow_list": m{
- "10.0.0.0/24": true,
- "::/0": false,
- },
- },
- })
- r := router.NewR(t, lhControl, myControl)
- defer r.RenderFlow()
- lhControl.Start()
- myControl.Start()
- // Drain any startup packets (there should be none meaningful)
- r.FlushAll()
- // Verify lighthouse has no knowledge of the client
- assert.Nil(t, lhControl.QueryLighthouse(myVpnIpNet[0].Addr()))
- // Build a new config that adds the lighthouse
- newSettings := make(m)
- for k, v := range myConfig.Settings {
- newSettings[k] = v
- }
- newSettings["static_host_map"] = m{
- lhVpnIpNet[0].Addr().String(): []any{lhUdpAddr.String()},
- }
- newSettings["lighthouse"] = m{
- "hosts": []any{lhVpnIpNet[0].Addr().String()},
- "interval": 600,
- "local_allow_list": m{
- "10.0.0.0/24": true,
- "::/0": false,
- },
- }
- newCfg, err := yaml.Marshal(newSettings)
- require.NoError(t, err)
- // Reload the config. The lighthouse.hosts change triggers TriggerUpdate,
- // which wakes the update worker. It calls SendUpdate, initiating a
- // handshake to the new lighthouse and caching the HostUpdateNotification.
- require.NoError(t, myConfig.ReloadConfigString(string(newCfg)))
- // Route until the lighthouse receives the HostUpdateNotification.
- // This covers: handshake stage 1, stage 2, then the cached update.
- done := make(chan struct{})
- go func() {
- r.RouteForAllUntilAfterMsgTypeTo(lhControl, header.LightHouse, 0)
- close(done)
- }()
- select {
- case <-done:
- case <-time.After(5 * time.Second):
- t.Fatal("timed out waiting for lighthouse update after config reload")
- }
- // Verify lighthouse now has the client's addresses
- assert.NotNil(t, lhControl.QueryLighthouse(myVpnIpNet[0].Addr()))
- r.RenderHostmaps("Final hostmaps", lhControl, myControl)
- lhControl.Stop()
- myControl.Stop()
- }
- func TestGoodHandshakeUnsafeDest(t *testing.T) {
- unsafePrefix := "192.168.6.0/24"
- ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
- theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServerWithUdpAndUnsafeNetworks(cert.Version2, ca, caKey, "spooky", "10.128.0.2/24", netip.MustParseAddrPort("10.64.0.2:4242"), unsafePrefix, nil)
- route := m{"route": unsafePrefix, "via": theirVpnIpNet[0].Addr().String()}
- myCfg := m{
- "tun": m{
- "unsafe_routes": []m{route},
- },
- }
- myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(cert.Version2, ca, caKey, "me", "10.128.0.1/24", myCfg)
- t.Logf("my config %v", myConfig)
- // Put their info in our lighthouse
- myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
- spookyDest := netip.MustParseAddr("192.168.6.4")
- // Start the servers
- myControl.Start()
- theirControl.Start()
- t.Log("Send a udp packet through to begin standing up the tunnel, this should come out the other side")
- myControl.InjectTunUDPPacket(spookyDest, 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me"))
- t.Log("Have them consume my stage 0 packet. They have a tunnel now")
- theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
- t.Log("Get their stage 1 packet so that we can play with it")
- stage1Packet := theirControl.GetFromUDP(true)
- t.Log("I consume a garbage packet with a proper nebula header for our tunnel")
- // this should log a statement and get ignored, allowing the real handshake packet to complete the tunnel
- badPacket := stage1Packet.Copy()
- badPacket.Data = badPacket.Data[:len(badPacket.Data)-header.Len]
- myControl.InjectUDPPacket(badPacket)
- t.Log("Have me consume their real stage 1 packet. I have a tunnel now")
- myControl.InjectUDPPacket(stage1Packet)
- t.Log("Wait until we see my cached packet come through")
- myControl.WaitForType(1, 0, theirControl)
- t.Log("Make sure our host infos are correct")
- assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet, theirVpnIpNet, myControl, theirControl)
- t.Log("Get that cached packet and make sure it looks right")
- myCachedPacket := theirControl.GetFromTun(true)
- assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet[0].Addr(), spookyDest, 80, 80)
- //reply
- theirControl.InjectTunUDPPacket(myVpnIpNet[0].Addr(), 80, spookyDest, 80, []byte("Hi from the spookyman"))
- //wait for reply
- theirControl.WaitForType(1, 0, myControl)
- theirCachedPacket := myControl.GetFromTun(true)
- assertUdpPacket(t, []byte("Hi from the spookyman"), theirCachedPacket, spookyDest, myVpnIpNet[0].Addr(), 80, 80)
- t.Log("Do a bidirectional tunnel test")
- r := router.NewR(t, myControl, theirControl)
- defer r.RenderFlow()
- assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
- r.RenderHostmaps("Final hostmaps", myControl, theirControl)
- myControl.Stop()
- theirControl.Stop()
- }
|