local.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package kyoketsu
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "net/netip"
  7. "strings"
  8. )
  9. type AllAddress struct {
  10. Addr []net.IP `json:"addresses"`
  11. }
  12. type NetworkInterfaceNotFound struct{ Passed string }
  13. // Implementing error interface
  14. func (n *NetworkInterfaceNotFound) Error() string {
  15. return fmt.Sprintf("Interface: '%s' not found.", n.Passed)
  16. }
  17. func getNextAddr(addr net.IP) (net.IP, error) {
  18. next, err := netip.ParseAddr(addr.String())
  19. if err != nil {
  20. return nil, err
  21. }
  22. return net.ParseIP(next.Next().String()), nil
  23. }
  24. /*
  25. Recursive function to get all of the IPv4 addresses for each IPv4 network that the host is on
  26. :param addr: the address to recursively find the next address for
  27. :param out: a pointer to a struct containing a list of addresses
  28. */
  29. func addressRecurse(ipmap *IpSubnetMapper, max int) {
  30. if len(ipmap.Ipv4s) > max {
  31. return
  32. }
  33. next, err := getNextAddr(ipmap.NetworkAddr)
  34. if err != nil {
  35. log.Println(err)
  36. return
  37. }
  38. ip, net, err := net.ParseCIDR(next.String())
  39. if err != nil {
  40. log.Println(err)
  41. return
  42. }
  43. if ip.Mask(net.Mask).String() != ipmap.NetworkAddr.String() {
  44. return
  45. }
  46. ipmap.Ipv4s = append(ipmap.Ipv4s, next)
  47. addressRecurse(ipmap, max)
  48. }
  49. /*
  50. Retrieve the address of a specific interface
  51. :param name: the name of the interface to get the address of
  52. */
  53. func getAddressByInterface(name string) ([]net.Addr, error) {
  54. interfaces, err := net.Interfaces()
  55. if err != nil {
  56. return nil, err
  57. }
  58. for idx := range interfaces {
  59. if interfaces[idx].Name == name {
  60. return interfaces[idx].Addrs()
  61. }
  62. }
  63. return nil, &NetworkInterfaceNotFound{Passed: name}
  64. }
  65. /*
  66. Utilized a recursive function to find all addresses in the address space that the host belongs.
  67. Returns a pointer to an AllAddresses struct who has a list of net.IP structs inside
  68. */
  69. func GetAllAddresses(name string, maxDepth int) (*AllAddress, error) {
  70. addresses, err := getAddressByInterface(name)
  71. if err != nil {
  72. return nil, err
  73. }
  74. out := &AllAddress{}
  75. for idx := range addresses {
  76. ip := net.ParseIP(strings.Split(addresses[idx].String(), "/")[0])
  77. root, err := netip.ParseAddr(ip.Mask(ip.DefaultMask()).String())
  78. if err != nil {
  79. continue
  80. }
  81. if root.IsLoopback() {
  82. continue
  83. }
  84. // addressRecurse(ip, ip, out, maxDepth)
  85. }
  86. return out, nil
  87. }
  88. /*
  89. Utilized a recursive function to find all addresses in the address space that the host belongs.
  90. Returns a pointer to an AllAddresses struct who has a list of net.IP structs inside
  91. */
  92. func GetAllRemoteAddresses(addrs []string, maxDepth int) (*AllAddress, error) {
  93. out := &AllAddress{}
  94. var addresses []net.IP
  95. for i := range addrs {
  96. ip, _, err := net.ParseCIDR(addrs[i])
  97. if err != nil {
  98. return nil, err
  99. }
  100. addresses = append(addresses, ip)
  101. }
  102. for idx := range addresses {
  103. ip := net.ParseIP(strings.Split(addresses[idx].String(), "/")[0])
  104. root, err := netip.ParseAddr(ip.Mask(ip.DefaultMask()).String())
  105. if err != nil {
  106. continue
  107. }
  108. if root.IsLoopback() {
  109. continue
  110. }
  111. // addressRecurse(ip, ip, out, maxDepth)
  112. }
  113. return out, nil
  114. }
  115. type IpSubnetMapper struct {
  116. Ipv4s []net.IP
  117. NetworkAddr net.IP
  118. Mask net.IPMask
  119. }
  120. func RefactorGetAllRemAddr(addr string) (*AllAddress, error) {
  121. // out := &AllAddress{}
  122. ipmap := &IpSubnetMapper{Ipv4s: []net.IP{}}
  123. ip, net, err := net.ParseCIDR(addr)
  124. if err != nil {
  125. return nil, err
  126. }
  127. ipmap.NetworkAddr = ip.Mask(net.Mask)
  128. ipmap.Mask = ip.DefaultMask()
  129. fmt.Printf("%+v\n", ip.Mask(net.Mask))
  130. fmt.Println(ip.DefaultMask())
  131. fmt.Printf("%s\n", net.IP.DefaultMask())
  132. addressRecurse(ipmap, 2000)
  133. return nil, nil
  134. }