control_tester.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package nebula
  4. import (
  5. "net"
  6. "github.com/google/gopacket"
  7. "github.com/google/gopacket/layers"
  8. )
  9. // WaitForTypeByIndex will pipe all messages from this control device into the pipeTo control device
  10. // returning after a message matching the criteria has been piped
  11. func (c *Control) WaitForType(msgType NebulaMessageType, subType NebulaMessageSubType, pipeTo *Control) {
  12. h := &Header{}
  13. for {
  14. p := c.f.outside.Get(true)
  15. if err := h.Parse(p.Data); err != nil {
  16. panic(err)
  17. }
  18. pipeTo.InjectUDPPacket(p)
  19. if h.Type == msgType && h.Subtype == subType {
  20. return
  21. }
  22. }
  23. }
  24. // WaitForTypeByIndex is similar to WaitForType except it adds an index check
  25. // Useful if you have many nodes communicating and want to wait to find a specific nodes packet
  26. func (c *Control) WaitForTypeByIndex(toIndex uint32, msgType NebulaMessageType, subType NebulaMessageSubType, pipeTo *Control) {
  27. h := &Header{}
  28. for {
  29. p := c.f.outside.Get(true)
  30. if err := h.Parse(p.Data); err != nil {
  31. panic(err)
  32. }
  33. pipeTo.InjectUDPPacket(p)
  34. if h.RemoteIndex == toIndex && h.Type == msgType && h.Subtype == subType {
  35. return
  36. }
  37. }
  38. }
  39. // InjectLightHouseAddr will push toAddr into the local lighthouse cache for the vpnIp
  40. // This is necessary if you did not configure static hosts or are not running a lighthouse
  41. func (c *Control) InjectLightHouseAddr(vpnIp net.IP, toAddr *net.UDPAddr) {
  42. c.f.lightHouse.Lock()
  43. remoteList := c.f.lightHouse.unlockedGetRemoteList(ip2int(vpnIp))
  44. remoteList.Lock()
  45. defer remoteList.Unlock()
  46. c.f.lightHouse.Unlock()
  47. iVpnIp := ip2int(vpnIp)
  48. if v4 := toAddr.IP.To4(); v4 != nil {
  49. remoteList.unlockedPrependV4(iVpnIp, NewIp4AndPort(v4, uint32(toAddr.Port)))
  50. } else {
  51. remoteList.unlockedPrependV6(iVpnIp, NewIp6AndPort(toAddr.IP, uint32(toAddr.Port)))
  52. }
  53. }
  54. // GetFromTun will pull a packet off the tun side of nebula
  55. func (c *Control) GetFromTun(block bool) []byte {
  56. return c.f.inside.(*Tun).Get(block)
  57. }
  58. // GetFromUDP will pull a udp packet off the udp side of nebula
  59. func (c *Control) GetFromUDP(block bool) *UdpPacket {
  60. return c.f.outside.Get(block)
  61. }
  62. func (c *Control) GetUDPTxChan() <-chan *UdpPacket {
  63. return c.f.outside.txPackets
  64. }
  65. func (c *Control) GetTunTxChan() <-chan []byte {
  66. return c.f.inside.(*Tun).txPackets
  67. }
  68. // InjectUDPPacket will inject a packet into the udp side of nebula
  69. func (c *Control) InjectUDPPacket(p *UdpPacket) {
  70. c.f.outside.Send(p)
  71. }
  72. // InjectTunUDPPacket puts a udp packet on the tun interface. Using UDP here because it's a simpler protocol
  73. func (c *Control) InjectTunUDPPacket(toIp net.IP, toPort uint16, fromPort uint16, data []byte) {
  74. ip := layers.IPv4{
  75. Version: 4,
  76. TTL: 64,
  77. Protocol: layers.IPProtocolUDP,
  78. SrcIP: c.f.inside.CidrNet().IP,
  79. DstIP: toIp,
  80. }
  81. udp := layers.UDP{
  82. SrcPort: layers.UDPPort(fromPort),
  83. DstPort: layers.UDPPort(toPort),
  84. }
  85. err := udp.SetNetworkLayerForChecksum(&ip)
  86. if err != nil {
  87. panic(err)
  88. }
  89. buffer := gopacket.NewSerializeBuffer()
  90. opt := gopacket.SerializeOptions{
  91. ComputeChecksums: true,
  92. FixLengths: true,
  93. }
  94. err = gopacket.SerializeLayers(buffer, opt, &ip, &udp, gopacket.Payload(data))
  95. if err != nil {
  96. panic(err)
  97. }
  98. c.f.inside.(*Tun).Send(buffer.Bytes())
  99. }
  100. func (c *Control) GetUDPAddr() string {
  101. return c.f.outside.addr.String()
  102. }
  103. func (c *Control) KillPendingTunnel(vpnIp net.IP) bool {
  104. hostinfo, ok := c.f.handshakeManager.pendingHostMap.Hosts[ip2int(vpnIp)]
  105. if !ok {
  106. return false
  107. }
  108. c.f.handshakeManager.pendingHostMap.DeleteHostInfo(hostinfo)
  109. return true
  110. }