local.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. GNU GENERAL PUBLIC LICENSE
  3. Version 3, 29 June 2007
  4. kyoketsu, a Client-To-Client Network Enumeration System
  5. Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
  6. Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  7. Everyone is permitted to copy and distribute verbatim copies
  8. of this license document, but changing it is not allowed.
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. See the GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package kyoketsu
  21. import (
  22. "fmt"
  23. "log"
  24. "net"
  25. "net/netip"
  26. "strconv"
  27. "strings"
  28. )
  29. type NetworkInterfaceNotFound struct{ Passed string }
  30. // Implementing error interface
  31. func (n *NetworkInterfaceNotFound) Error() string {
  32. return fmt.Sprintf("Interface: '%s' not found.", n.Passed)
  33. }
  34. type IpSubnetMapper struct {
  35. Ipv4s []net.IP `json:"addresses"`
  36. NetworkAddr net.IP
  37. Current net.IP
  38. Mask int
  39. }
  40. /*
  41. Get the next IPv4 address of the address specified in the 'addr' argument,
  42. :param addr: the address to get the next address of
  43. */
  44. func getNextAddr(addr string) string {
  45. parsed, err := netip.ParseAddr(addr)
  46. if err != nil {
  47. log.Fatal("failed while parsing address in getNextAddr() ", err, "\n")
  48. }
  49. return parsed.Next().String()
  50. }
  51. /*
  52. get the network address of the ip address in 'addr' with the subnet mask from 'cidr'
  53. :param addr: the ipv4 address to get the network address of
  54. :param cidr: the CIDR notation of the subbet
  55. */
  56. func getNetwork(addr string, cidr int) string {
  57. addr = fmt.Sprintf("%s/%v", addr, cidr)
  58. ip, net, err := net.ParseCIDR(addr)
  59. if err != nil {
  60. log.Fatal("failed whilst attempting to parse cidr in getNetwork() ", err, "\n")
  61. }
  62. return ip.Mask(net.Mask).String()
  63. }
  64. /*
  65. Recursive function to get all of the IPv4 addresses for each IPv4 network that the host is on
  66. :param ipmap: a pointer to an IpSubnetMapper struct which contains domain details such as
  67. the subnet mask, the original network mask, and the current IP address used in the
  68. recursive function
  69. :param max: This is safety feature to prevent stack overflows, so you can manually set the depth to
  70. call the function
  71. */
  72. func addressRecurse(ipmap *IpSubnetMapper) {
  73. next := getNextAddr(ipmap.Current.String())
  74. nextNet := getNetwork(next, ipmap.Mask)
  75. currentNet := ipmap.NetworkAddr.String()
  76. if nextNet != currentNet {
  77. return
  78. }
  79. ipmap.Current = net.ParseIP(next)
  80. ipmap.Ipv4s = append(ipmap.Ipv4s, net.ParseIP(next))
  81. addressRecurse(ipmap)
  82. }
  83. /*
  84. Get all of the IPv4 addresses in the network that 'addr' belongs to. YOU MUST PASS THE ADDRESS WITH CIDR NOTATION
  85. i.e. '192.168.50.1/24'
  86. :param addr: the ipv4 address to use for subnet discovery
  87. */
  88. func GetNetworkAddresses(addr string) (*IpSubnetMapper, error) {
  89. ipmap := &IpSubnetMapper{Ipv4s: []net.IP{}}
  90. ip, net, err := net.ParseCIDR(addr)
  91. if err != nil {
  92. return nil, err
  93. }
  94. mask, err := strconv.Atoi(strings.Split(addr, "/")[1])
  95. if err != nil {
  96. return nil, err
  97. }
  98. ipmap.NetworkAddr = ip.Mask(net.Mask)
  99. ipmap.Mask = mask
  100. ipmap.Current = ip.Mask(net.Mask)
  101. addressRecurse(ipmap)
  102. return ipmap, nil
  103. }
  104. type PromptEntry struct {
  105. HostAddress string
  106. NetworkAddress string
  107. Cidr int
  108. SubnetMask string
  109. InterfaceName string
  110. }
  111. type TuiSelectionFeed struct {
  112. Choice []PromptEntry
  113. }
  114. func RetrieveLocalAddresses() (TuiSelectionFeed, error) {
  115. var tuidata TuiSelectionFeed
  116. ifaces, err := net.Interfaces()
  117. if err != nil {
  118. return tuidata, err
  119. }
  120. for i := range ifaces {
  121. fmt.Printf("%+v\n", ifaces[i])
  122. }
  123. return tuidata, nil
  124. }