lighthouse_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. package nebula
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "net/netip"
  6. "testing"
  7. "github.com/gaissmai/bart"
  8. "github.com/slackhq/nebula/cert"
  9. "github.com/slackhq/nebula/config"
  10. "github.com/slackhq/nebula/header"
  11. "github.com/slackhq/nebula/test"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. "go.yaml.in/yaml/v3"
  15. )
  16. func TestOldIPv4Only(t *testing.T) {
  17. // This test ensures our new ipv6 enabled LH protobuf IpAndPorts works with the old style to enable backwards compatibility
  18. b := []byte{8, 129, 130, 132, 80, 16, 10}
  19. var m V4AddrPort
  20. err := m.Unmarshal(b)
  21. require.NoError(t, err)
  22. ip := netip.MustParseAddr("10.1.1.1")
  23. bp := ip.As4()
  24. assert.Equal(t, binary.BigEndian.Uint32(bp[:]), m.GetAddr())
  25. }
  26. func Test_lhStaticMapping(t *testing.T) {
  27. l := test.NewLogger()
  28. myVpnNet := netip.MustParsePrefix("10.128.0.1/16")
  29. nt := new(bart.Lite)
  30. nt.Insert(myVpnNet)
  31. cs := &CertState{
  32. myVpnNetworks: []netip.Prefix{myVpnNet},
  33. myVpnNetworksTable: nt,
  34. }
  35. lh1 := "10.128.0.2"
  36. c := config.NewC(l)
  37. c.Settings["lighthouse"] = map[string]any{"hosts": []any{lh1}}
  38. c.Settings["static_host_map"] = map[string]any{lh1: []any{"1.1.1.1:4242"}}
  39. _, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
  40. require.NoError(t, err)
  41. lh2 := "10.128.0.3"
  42. c = config.NewC(l)
  43. c.Settings["lighthouse"] = map[string]any{"hosts": []any{lh1, lh2}}
  44. c.Settings["static_host_map"] = map[string]any{lh1: []any{"100.1.1.1:4242"}}
  45. _, err = NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
  46. require.EqualError(t, err, "lighthouse 10.128.0.3 does not have a static_host_map entry")
  47. }
  48. func TestReloadLighthouseInterval(t *testing.T) {
  49. l := test.NewLogger()
  50. myVpnNet := netip.MustParsePrefix("10.128.0.1/16")
  51. nt := new(bart.Lite)
  52. nt.Insert(myVpnNet)
  53. cs := &CertState{
  54. myVpnNetworks: []netip.Prefix{myVpnNet},
  55. myVpnNetworksTable: nt,
  56. }
  57. lh1 := "10.128.0.2"
  58. c := config.NewC(l)
  59. c.Settings["lighthouse"] = map[string]any{
  60. "hosts": []any{lh1},
  61. "interval": "1s",
  62. }
  63. c.Settings["static_host_map"] = map[string]any{lh1: []any{"1.1.1.1:4242"}}
  64. lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
  65. require.NoError(t, err)
  66. lh.ifce = &mockEncWriter{}
  67. // The first one routine is kicked off by main.go currently, lets make sure that one dies
  68. require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 5"))
  69. assert.Equal(t, int64(5), lh.interval.Load())
  70. // Subsequent calls are killed off by the LightHouse.Reload function
  71. require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 10"))
  72. assert.Equal(t, int64(10), lh.interval.Load())
  73. // If this completes then nothing is stealing our reload routine
  74. require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 11"))
  75. assert.Equal(t, int64(11), lh.interval.Load())
  76. }
  77. func BenchmarkLighthouseHandleRequest(b *testing.B) {
  78. l := test.NewLogger()
  79. myVpnNet := netip.MustParsePrefix("10.128.0.1/0")
  80. nt := new(bart.Lite)
  81. nt.Insert(myVpnNet)
  82. cs := &CertState{
  83. myVpnNetworks: []netip.Prefix{myVpnNet},
  84. myVpnNetworksTable: nt,
  85. }
  86. c := config.NewC(l)
  87. lh, err := NewLightHouseFromConfig(b.Context(), l, c, cs, nil, nil)
  88. require.NoError(b, err)
  89. hAddr := netip.MustParseAddrPort("4.5.6.7:12345")
  90. hAddr2 := netip.MustParseAddrPort("4.5.6.7:12346")
  91. vpnIp3 := netip.MustParseAddr("0.0.0.3")
  92. lh.addrMap[vpnIp3] = NewRemoteList([]netip.Addr{vpnIp3}, nil)
  93. lh.addrMap[vpnIp3].unlockedSetV4(
  94. vpnIp3,
  95. vpnIp3,
  96. []*V4AddrPort{
  97. netAddrToProtoV4AddrPort(hAddr.Addr(), hAddr.Port()),
  98. netAddrToProtoV4AddrPort(hAddr2.Addr(), hAddr2.Port()),
  99. },
  100. func(netip.Addr, *V4AddrPort) bool { return true },
  101. )
  102. rAddr := netip.MustParseAddrPort("1.2.2.3:12345")
  103. rAddr2 := netip.MustParseAddrPort("1.2.2.3:12346")
  104. vpnIp2 := netip.MustParseAddr("0.0.0.3")
  105. lh.addrMap[vpnIp2] = NewRemoteList([]netip.Addr{vpnIp2}, nil)
  106. lh.addrMap[vpnIp2].unlockedSetV4(
  107. vpnIp3,
  108. vpnIp3,
  109. []*V4AddrPort{
  110. netAddrToProtoV4AddrPort(rAddr.Addr(), rAddr.Port()),
  111. netAddrToProtoV4AddrPort(rAddr2.Addr(), rAddr2.Port()),
  112. },
  113. func(netip.Addr, *V4AddrPort) bool { return true },
  114. )
  115. mw := &mockEncWriter{}
  116. hi := []netip.Addr{vpnIp2}
  117. b.Run("notfound", func(b *testing.B) {
  118. lhh := lh.NewRequestHandler()
  119. req := &NebulaMeta{
  120. Type: NebulaMeta_HostQuery,
  121. Details: &NebulaMetaDetails{
  122. OldVpnAddr: 4,
  123. V4AddrPorts: nil,
  124. },
  125. }
  126. p, err := req.Marshal()
  127. require.NoError(b, err)
  128. for n := 0; n < b.N; n++ {
  129. lhh.HandleRequest(rAddr, hi, p, mw)
  130. }
  131. })
  132. b.Run("found", func(b *testing.B) {
  133. lhh := lh.NewRequestHandler()
  134. req := &NebulaMeta{
  135. Type: NebulaMeta_HostQuery,
  136. Details: &NebulaMetaDetails{
  137. OldVpnAddr: 3,
  138. V4AddrPorts: nil,
  139. },
  140. }
  141. p, err := req.Marshal()
  142. require.NoError(b, err)
  143. for n := 0; n < b.N; n++ {
  144. lhh.HandleRequest(rAddr, hi, p, mw)
  145. }
  146. })
  147. }
  148. func TestLighthouse_Memory(t *testing.T) {
  149. l := test.NewLogger()
  150. myUdpAddr0 := netip.MustParseAddrPort("10.0.0.2:4242")
  151. myUdpAddr1 := netip.MustParseAddrPort("192.168.0.2:4242")
  152. myUdpAddr2 := netip.MustParseAddrPort("172.16.0.2:4242")
  153. myUdpAddr3 := netip.MustParseAddrPort("100.152.0.2:4242")
  154. myUdpAddr4 := netip.MustParseAddrPort("24.15.0.2:4242")
  155. myUdpAddr5 := netip.MustParseAddrPort("192.168.0.2:4243")
  156. myUdpAddr6 := netip.MustParseAddrPort("192.168.0.2:4244")
  157. myUdpAddr7 := netip.MustParseAddrPort("192.168.0.2:4245")
  158. myUdpAddr8 := netip.MustParseAddrPort("192.168.0.2:4246")
  159. myUdpAddr9 := netip.MustParseAddrPort("192.168.0.2:4247")
  160. myUdpAddr10 := netip.MustParseAddrPort("192.168.0.2:4248")
  161. myUdpAddr11 := netip.MustParseAddrPort("192.168.0.2:4249")
  162. myVpnIp := netip.MustParseAddr("10.128.0.2")
  163. theirUdpAddr0 := netip.MustParseAddrPort("10.0.0.3:4242")
  164. theirUdpAddr1 := netip.MustParseAddrPort("192.168.0.3:4242")
  165. theirUdpAddr2 := netip.MustParseAddrPort("172.16.0.3:4242")
  166. theirUdpAddr3 := netip.MustParseAddrPort("100.152.0.3:4242")
  167. theirUdpAddr4 := netip.MustParseAddrPort("24.15.0.3:4242")
  168. theirVpnIp := netip.MustParseAddr("10.128.0.3")
  169. c := config.NewC(l)
  170. c.Settings["lighthouse"] = map[string]any{"am_lighthouse": true}
  171. c.Settings["listen"] = map[string]any{"port": 4242}
  172. myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
  173. nt := new(bart.Lite)
  174. nt.Insert(myVpnNet)
  175. cs := &CertState{
  176. myVpnNetworks: []netip.Prefix{myVpnNet},
  177. myVpnNetworksTable: nt,
  178. }
  179. lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
  180. lh.ifce = &mockEncWriter{}
  181. require.NoError(t, err)
  182. lhh := lh.NewRequestHandler()
  183. // Test that my first update responds with just that
  184. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr1, myUdpAddr2}, lhh)
  185. r := newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  186. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr2)
  187. // Ensure we don't accumulate addresses
  188. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr3}, lhh)
  189. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  190. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr3)
  191. // Grow it back to 2
  192. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr1, myUdpAddr4}, lhh)
  193. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  194. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4)
  195. // Update a different host and ask about it
  196. newLHHostUpdate(theirUdpAddr0, theirVpnIp, []netip.AddrPort{theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4}, lhh)
  197. r = newLHHostRequest(theirUdpAddr0, theirVpnIp, theirVpnIp, lhh)
  198. assertIp4InArray(t, r.msg.Details.V4AddrPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4)
  199. // Have both hosts ask about the other
  200. r = newLHHostRequest(theirUdpAddr0, theirVpnIp, myVpnIp, lhh)
  201. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4)
  202. r = newLHHostRequest(myUdpAddr0, myVpnIp, theirVpnIp, lhh)
  203. assertIp4InArray(t, r.msg.Details.V4AddrPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4)
  204. // Make sure we didn't get changed
  205. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  206. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4)
  207. // Ensure proper ordering and limiting
  208. // Send 12 addrs, get 10 back, the last 2 removed, allowing the duplicate to remain (clients dedupe)
  209. newLHHostUpdate(
  210. myUdpAddr0,
  211. myVpnIp,
  212. []netip.AddrPort{
  213. myUdpAddr1,
  214. myUdpAddr2,
  215. myUdpAddr3,
  216. myUdpAddr4,
  217. myUdpAddr5,
  218. myUdpAddr5, //Duplicated on purpose
  219. myUdpAddr6,
  220. myUdpAddr7,
  221. myUdpAddr8,
  222. myUdpAddr9,
  223. myUdpAddr10,
  224. myUdpAddr11, // This should get cut
  225. }, lhh)
  226. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  227. assertIp4InArray(
  228. t,
  229. r.msg.Details.V4AddrPorts,
  230. myUdpAddr1, myUdpAddr2, myUdpAddr3, myUdpAddr4, myUdpAddr5, myUdpAddr5, myUdpAddr6, myUdpAddr7, myUdpAddr8, myUdpAddr9,
  231. )
  232. // Make sure we won't add ips in our vpn network
  233. bad1 := netip.MustParseAddrPort("10.128.0.99:4242")
  234. bad2 := netip.MustParseAddrPort("10.128.0.100:4242")
  235. good := netip.MustParseAddrPort("1.128.0.99:4242")
  236. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{bad1, bad2, good}, lhh)
  237. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  238. assertIp4InArray(t, r.msg.Details.V4AddrPorts, good)
  239. }
  240. func TestLighthouse_reload(t *testing.T) {
  241. l := test.NewLogger()
  242. c := config.NewC(l)
  243. c.Settings["lighthouse"] = map[string]any{"am_lighthouse": true}
  244. c.Settings["listen"] = map[string]any{"port": 4242}
  245. myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
  246. nt := new(bart.Lite)
  247. nt.Insert(myVpnNet)
  248. cs := &CertState{
  249. myVpnNetworks: []netip.Prefix{myVpnNet},
  250. myVpnNetworksTable: nt,
  251. }
  252. lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
  253. require.NoError(t, err)
  254. nc := map[string]any{
  255. "static_host_map": map[string]any{
  256. "10.128.0.2": []any{"1.1.1.1:4242"},
  257. },
  258. }
  259. rc, err := yaml.Marshal(nc)
  260. require.NoError(t, err)
  261. c.ReloadConfigString(string(rc))
  262. err = lh.reload(c, false)
  263. require.NoError(t, err)
  264. }
  265. func newLHHostRequest(fromAddr netip.AddrPort, myVpnIp, queryVpnIp netip.Addr, lhh *LightHouseHandler) testLhReply {
  266. req := &NebulaMeta{
  267. Type: NebulaMeta_HostQuery,
  268. Details: &NebulaMetaDetails{},
  269. }
  270. if queryVpnIp.Is4() {
  271. bip := queryVpnIp.As4()
  272. req.Details.OldVpnAddr = binary.BigEndian.Uint32(bip[:])
  273. } else {
  274. req.Details.VpnAddr = netAddrToProtoAddr(queryVpnIp)
  275. }
  276. b, err := req.Marshal()
  277. if err != nil {
  278. panic(err)
  279. }
  280. filter := NebulaMeta_HostQueryReply
  281. w := &testEncWriter{
  282. metaFilter: &filter,
  283. }
  284. lhh.HandleRequest(fromAddr, []netip.Addr{myVpnIp}, b, w)
  285. return w.lastReply
  286. }
  287. func newLHHostUpdate(fromAddr netip.AddrPort, vpnIp netip.Addr, addrs []netip.AddrPort, lhh *LightHouseHandler) {
  288. req := &NebulaMeta{
  289. Type: NebulaMeta_HostUpdateNotification,
  290. Details: &NebulaMetaDetails{},
  291. }
  292. if vpnIp.Is4() {
  293. bip := vpnIp.As4()
  294. req.Details.OldVpnAddr = binary.BigEndian.Uint32(bip[:])
  295. } else {
  296. req.Details.VpnAddr = netAddrToProtoAddr(vpnIp)
  297. }
  298. for _, v := range addrs {
  299. if v.Addr().Is4() {
  300. req.Details.V4AddrPorts = append(req.Details.V4AddrPorts, netAddrToProtoV4AddrPort(v.Addr(), v.Port()))
  301. } else {
  302. req.Details.V6AddrPorts = append(req.Details.V6AddrPorts, netAddrToProtoV6AddrPort(v.Addr(), v.Port()))
  303. }
  304. }
  305. b, err := req.Marshal()
  306. if err != nil {
  307. panic(err)
  308. }
  309. w := &testEncWriter{}
  310. lhh.HandleRequest(fromAddr, []netip.Addr{vpnIp}, b, w)
  311. }
  312. type testLhReply struct {
  313. nebType header.MessageType
  314. nebSubType header.MessageSubType
  315. vpnIp netip.Addr
  316. msg *NebulaMeta
  317. }
  318. type testEncWriter struct {
  319. lastReply testLhReply
  320. metaFilter *NebulaMeta_MessageType
  321. protocolVersion cert.Version
  322. }
  323. func (tw *testEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
  324. }
  325. func (tw *testEncWriter) Handshake(vpnIp netip.Addr) {
  326. }
  327. func (tw *testEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, _, _ []byte) {
  328. msg := &NebulaMeta{}
  329. err := msg.Unmarshal(p)
  330. if tw.metaFilter == nil || msg.Type == *tw.metaFilter {
  331. tw.lastReply = testLhReply{
  332. nebType: t,
  333. nebSubType: st,
  334. vpnIp: hostinfo.vpnAddrs[0],
  335. msg: msg,
  336. }
  337. }
  338. if err != nil {
  339. panic(err)
  340. }
  341. }
  342. func (tw *testEncWriter) SendMessageToVpnAddr(t header.MessageType, st header.MessageSubType, vpnIp netip.Addr, p, _, _ []byte) {
  343. msg := &NebulaMeta{}
  344. err := msg.Unmarshal(p)
  345. if tw.metaFilter == nil || msg.Type == *tw.metaFilter {
  346. tw.lastReply = testLhReply{
  347. nebType: t,
  348. nebSubType: st,
  349. vpnIp: vpnIp,
  350. msg: msg,
  351. }
  352. }
  353. if err != nil {
  354. panic(err)
  355. }
  356. }
  357. func (tw *testEncWriter) GetHostInfo(vpnIp netip.Addr) *HostInfo {
  358. return nil
  359. }
  360. func (tw *testEncWriter) GetCertState() *CertState {
  361. return &CertState{initiatingVersion: tw.protocolVersion}
  362. }
  363. // assertIp4InArray asserts every address in want is at the same position in have and that the lengths match
  364. func assertIp4InArray(t *testing.T, have []*V4AddrPort, want ...netip.AddrPort) {
  365. if !assert.Len(t, have, len(want)) {
  366. return
  367. }
  368. for k, w := range want {
  369. h := protoV4AddrPortToNetAddrPort(have[k])
  370. if !(h == w) {
  371. assert.Fail(t, fmt.Sprintf("Response did not contain: %v at %v, found %v", w, k, h))
  372. }
  373. }
  374. }
  375. func Test_findNetworkUnion(t *testing.T) {
  376. var out netip.Addr
  377. var ok bool
  378. tenDot := netip.MustParsePrefix("10.0.0.0/8")
  379. oneSevenTwo := netip.MustParsePrefix("172.16.0.0/16")
  380. fe80 := netip.MustParsePrefix("fe80::/8")
  381. fc00 := netip.MustParsePrefix("fc00::/7")
  382. a1 := netip.MustParseAddr("10.0.0.1")
  383. afe81 := netip.MustParseAddr("fe80::1")
  384. //simple
  385. out, ok = findNetworkUnion([]netip.Prefix{tenDot}, []netip.Addr{a1})
  386. assert.True(t, ok)
  387. assert.Equal(t, out, a1)
  388. //mixed lengths
  389. out, ok = findNetworkUnion([]netip.Prefix{tenDot}, []netip.Addr{a1, afe81})
  390. assert.True(t, ok)
  391. assert.Equal(t, out, a1)
  392. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo}, []netip.Addr{a1})
  393. assert.True(t, ok)
  394. assert.Equal(t, out, a1)
  395. //mixed family
  396. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{a1})
  397. assert.True(t, ok)
  398. assert.Equal(t, out, a1)
  399. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{a1, afe81})
  400. assert.True(t, ok)
  401. assert.Equal(t, out, a1)
  402. //ordering
  403. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{afe81, a1})
  404. assert.True(t, ok)
  405. assert.Equal(t, out, a1)
  406. out, ok = findNetworkUnion([]netip.Prefix{fe80, tenDot, oneSevenTwo}, []netip.Addr{afe81, a1})
  407. assert.True(t, ok)
  408. assert.Equal(t, out, afe81)
  409. //some mismatches
  410. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{afe81})
  411. assert.True(t, ok)
  412. assert.Equal(t, out, afe81)
  413. out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fe80}, []netip.Addr{a1, afe81})
  414. assert.True(t, ok)
  415. assert.Equal(t, out, afe81)
  416. //falsey cases
  417. out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fe80}, []netip.Addr{a1})
  418. assert.False(t, ok)
  419. out, ok = findNetworkUnion([]netip.Prefix{fc00, fe80}, []netip.Addr{a1})
  420. assert.False(t, ok)
  421. out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fc00}, []netip.Addr{a1, afe81})
  422. assert.False(t, ok)
  423. out, ok = findNetworkUnion([]netip.Prefix{fc00}, []netip.Addr{a1, afe81})
  424. assert.False(t, ok)
  425. }
  426. func TestLighthouse_Dont_Delete_Static_Hosts(t *testing.T) {
  427. l := test.NewLogger()
  428. myUdpAddr2 := netip.MustParseAddrPort("1.2.3.4:4242")
  429. testSameHostNotStatic := netip.MustParseAddr("10.128.0.41")
  430. testStaticHost := netip.MustParseAddr("10.128.0.42")
  431. //myVpnIp := netip.MustParseAddr("10.128.0.2")
  432. c := config.NewC(l)
  433. lh1 := "10.128.0.2"
  434. c.Settings["lighthouse"] = map[string]any{
  435. "hosts": []any{lh1},
  436. "interval": "1s",
  437. }
  438. c.Settings["listen"] = map[string]any{"port": 4242}
  439. c.Settings["static_host_map"] = map[string]any{
  440. lh1: []any{"1.1.1.1:4242"},
  441. "10.128.0.42": []any{"1.2.3.4:4242"},
  442. }
  443. myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
  444. nt := new(bart.Lite)
  445. nt.Insert(myVpnNet)
  446. cs := &CertState{
  447. myVpnNetworks: []netip.Prefix{myVpnNet},
  448. myVpnNetworksTable: nt,
  449. }
  450. lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
  451. require.NoError(t, err)
  452. lh.ifce = &mockEncWriter{}
  453. //test that we actually have the static entry:
  454. out := lh.Query(testStaticHost)
  455. assert.NotNil(t, out)
  456. assert.Equal(t, out.vpnAddrs[0], testStaticHost)
  457. out.Rebuild([]netip.Prefix{}) //why tho
  458. assert.Equal(t, out.addrs[0], myUdpAddr2)
  459. //bolt on a lower numbered primary IP
  460. am := lh.unlockedGetRemoteList([]netip.Addr{testStaticHost})
  461. am.vpnAddrs = []netip.Addr{testSameHostNotStatic, testStaticHost}
  462. lh.addrMap[testSameHostNotStatic] = am
  463. out.Rebuild([]netip.Prefix{}) //???
  464. //test that we actually have the static entry:
  465. out = lh.Query(testStaticHost)
  466. assert.NotNil(t, out)
  467. assert.Equal(t, out.vpnAddrs[0], testSameHostNotStatic)
  468. assert.Equal(t, out.vpnAddrs[1], testStaticHost)
  469. assert.Equal(t, out.addrs[0], myUdpAddr2)
  470. //test that we actually have the static entry for BOTH:
  471. out2 := lh.Query(testSameHostNotStatic)
  472. assert.Same(t, out2, out)
  473. //now do the delete
  474. lh.DeleteVpnAddrs([]netip.Addr{testSameHostNotStatic, testStaticHost})
  475. //verify
  476. out = lh.Query(testSameHostNotStatic)
  477. assert.NotNil(t, out)
  478. if out == nil {
  479. t.Fatal("expected non-nil query for the static host")
  480. }
  481. assert.Equal(t, out.vpnAddrs[0], testSameHostNotStatic)
  482. assert.Equal(t, out.vpnAddrs[1], testStaticHost)
  483. assert.Equal(t, out.addrs[0], myUdpAddr2)
  484. }
  485. func TestLighthouse_DeletesWork(t *testing.T) {
  486. l := test.NewLogger()
  487. myUdpAddr2 := netip.MustParseAddrPort("1.2.3.4:4242")
  488. testHost := netip.MustParseAddr("10.128.0.42")
  489. c := config.NewC(l)
  490. lh1 := "10.128.0.2"
  491. c.Settings["lighthouse"] = map[string]any{
  492. "hosts": []any{lh1},
  493. "interval": "1s",
  494. }
  495. c.Settings["listen"] = map[string]any{"port": 4242}
  496. c.Settings["static_host_map"] = map[string]any{
  497. lh1: []any{"1.1.1.1:4242"},
  498. }
  499. myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
  500. nt := new(bart.Lite)
  501. nt.Insert(myVpnNet)
  502. cs := &CertState{
  503. myVpnNetworks: []netip.Prefix{myVpnNet},
  504. myVpnNetworksTable: nt,
  505. }
  506. lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
  507. require.NoError(t, err)
  508. lh.ifce = &mockEncWriter{}
  509. //insert the host
  510. am := lh.unlockedGetRemoteList([]netip.Addr{testHost})
  511. am.vpnAddrs = []netip.Addr{testHost}
  512. am.addrs = []netip.AddrPort{myUdpAddr2}
  513. lh.addrMap[testHost] = am
  514. am.Rebuild([]netip.Prefix{}) //???
  515. //test that we actually have the entry:
  516. out := lh.Query(testHost)
  517. assert.NotNil(t, out)
  518. assert.Equal(t, out.vpnAddrs[0], testHost)
  519. out.Rebuild([]netip.Prefix{}) //why tho
  520. assert.Equal(t, out.addrs[0], myUdpAddr2)
  521. //now do the delete
  522. lh.DeleteVpnAddrs([]netip.Addr{testHost})
  523. //verify
  524. out = lh.Query(testHost)
  525. assert.Nil(t, out)
  526. }