ipscan.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package ipscan
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net"
  6. "sort"
  7. "strconv"
  8. "sync"
  9. "time"
  10. "github.com/go-ping/ping"
  11. )
  12. /*
  13. IP Scanner
  14. This module scan the given network range and return a list
  15. of nearby nodes.
  16. */
  17. type DiscoveredHost struct {
  18. IP string
  19. Ping int
  20. Hostname string
  21. HttpPortDetected bool
  22. HttpsPortDetected bool
  23. }
  24. // Scan an IP range given the start and ending ip address
  25. func ScanIpRange(start, end string) ([]*DiscoveredHost, error) {
  26. ipStart := net.ParseIP(start)
  27. ipEnd := net.ParseIP(end)
  28. if ipStart == nil || ipEnd == nil {
  29. return nil, fmt.Errorf("Invalid IP address")
  30. }
  31. if bytes.Compare(ipStart, ipEnd) > 0 {
  32. return nil, fmt.Errorf("Invalid IP range")
  33. }
  34. var wg sync.WaitGroup
  35. hosts := make([]*DiscoveredHost, 0)
  36. for ip := ipStart; bytes.Compare(ip, ipEnd) <= 0; inc(ip) {
  37. wg.Add(1)
  38. thisIp := ip.String()
  39. go func(thisIp string) {
  40. defer wg.Done()
  41. host := &DiscoveredHost{IP: thisIp}
  42. if err := host.CheckPing(); err != nil {
  43. // skip if the host is unreachable
  44. host.Ping = -1
  45. hosts = append(hosts, host)
  46. return
  47. }
  48. host.CheckHostname()
  49. host.CheckPort("http", 80, &host.HttpPortDetected)
  50. host.CheckPort("https", 443, &host.HttpsPortDetected)
  51. hosts = append(hosts, host)
  52. }(thisIp)
  53. }
  54. //Wait until all go routine done
  55. wg.Wait()
  56. sortByIP(hosts)
  57. return hosts, nil
  58. }
  59. func ScanCIDRRange(cidr string) ([]*DiscoveredHost, error) {
  60. _, ipNet, err := net.ParseCIDR(cidr)
  61. if err != nil {
  62. return nil, err
  63. }
  64. ip := ipNet.IP.To4()
  65. startIP := net.IPv4(ip[0], ip[1], ip[2], 1).String()
  66. endIP := net.IPv4(ip[0], ip[1], ip[2], 254).String()
  67. return ScanIpRange(startIP, endIP)
  68. }
  69. func inc(ip net.IP) {
  70. for j := len(ip) - 1; j >= 0; j-- {
  71. ip[j]++
  72. if ip[j] > 0 {
  73. break
  74. }
  75. }
  76. }
  77. func sortByIP(discovered []*DiscoveredHost) {
  78. sort.Slice(discovered, func(i, j int) bool {
  79. return discovered[i].IP < discovered[j].IP
  80. })
  81. }
  82. func (host *DiscoveredHost) CheckPing() error {
  83. // ping the host and set the ping time in milliseconds
  84. pinger, err := ping.NewPinger(host.IP)
  85. if err != nil {
  86. return err
  87. }
  88. pinger.Count = 4
  89. pinger.Timeout = time.Second
  90. pinger.SetPrivileged(true) // This line may help on some systems
  91. pinger.Run()
  92. stats := pinger.Statistics()
  93. if stats.PacketsRecv == 0 {
  94. return fmt.Errorf("Host unreachable for " + host.IP)
  95. }
  96. host.Ping = int(stats.AvgRtt.Milliseconds())
  97. return nil
  98. }
  99. func (host *DiscoveredHost) CheckHostname() {
  100. // lookup the hostname for the IP address
  101. names, err := net.LookupAddr(host.IP)
  102. //fmt.Println(names, err)
  103. if err == nil && len(names) > 0 {
  104. host.Hostname = names[0]
  105. }
  106. }
  107. func (host *DiscoveredHost) CheckPort(protocol string, port int, detected *bool) {
  108. // try to connect to the specified port on the host
  109. conn, err := net.DialTimeout("tcp", net.JoinHostPort(host.IP, strconv.Itoa(port)), 1*time.Second)
  110. if err == nil {
  111. conn.Close()
  112. *detected = true
  113. }
  114. }
  115. func (host *DiscoveredHost) ScanPorts(startPort, endPort int) []int {
  116. var openPorts []int
  117. for port := startPort; port <= endPort; port++ {
  118. target := fmt.Sprintf("%s:%d", host.IP, port)
  119. conn, err := net.DialTimeout("tcp", target, time.Millisecond*500)
  120. if err == nil {
  121. conn.Close()
  122. openPorts = append(openPorts, port)
  123. }
  124. }
  125. return openPorts
  126. }