ipscan.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. fmt.Println("OK", host)
  52. hosts = append(hosts, host)
  53. }(thisIp)
  54. }
  55. //Wait until all go routine done
  56. wg.Wait()
  57. sortByIP(hosts)
  58. return hosts, nil
  59. }
  60. func ScanCIDRRange(cidr string) ([]*DiscoveredHost, error) {
  61. _, ipNet, err := net.ParseCIDR(cidr)
  62. if err != nil {
  63. return nil, err
  64. }
  65. ip := ipNet.IP.To4()
  66. startIP := net.IPv4(ip[0], ip[1], ip[2], 1).String()
  67. endIP := net.IPv4(ip[0], ip[1], ip[2], 254).String()
  68. return ScanIpRange(startIP, endIP)
  69. }
  70. func inc(ip net.IP) {
  71. for j := len(ip) - 1; j >= 0; j-- {
  72. ip[j]++
  73. if ip[j] > 0 {
  74. break
  75. }
  76. }
  77. }
  78. func sortByIP(discovered []*DiscoveredHost) {
  79. sort.Slice(discovered, func(i, j int) bool {
  80. return discovered[i].IP < discovered[j].IP
  81. })
  82. }
  83. func (host *DiscoveredHost) CheckPing() error {
  84. // ping the host and set the ping time in milliseconds
  85. pinger, err := ping.NewPinger(host.IP)
  86. if err != nil {
  87. return err
  88. }
  89. pinger.Count = 4
  90. pinger.Timeout = time.Second
  91. pinger.SetPrivileged(true) // This line may help on some systems
  92. pinger.Run()
  93. stats := pinger.Statistics()
  94. if stats.PacketsRecv == 0 {
  95. return fmt.Errorf("Host unreachable for " + host.IP)
  96. }
  97. host.Ping = int(stats.AvgRtt.Milliseconds())
  98. return nil
  99. }
  100. func (host *DiscoveredHost) CheckHostname() {
  101. // lookup the hostname for the IP address
  102. names, err := net.LookupAddr(host.IP)
  103. fmt.Println(names, err)
  104. if err == nil && len(names) > 0 {
  105. host.Hostname = names[0]
  106. }
  107. }
  108. func (host *DiscoveredHost) CheckPort(protocol string, port int, detected *bool) {
  109. // try to connect to the specified port on the host
  110. conn, err := net.DialTimeout("tcp", net.JoinHostPort(host.IP, strconv.Itoa(port)), 1*time.Second)
  111. if err == nil {
  112. conn.Close()
  113. *detected = true
  114. }
  115. }
  116. func (host *DiscoveredHost) ScanPorts(startPort, endPort int) []int {
  117. var openPorts []int
  118. for port := startPort; port <= endPort; port++ {
  119. target := fmt.Sprintf("%s:%d", host.IP, port)
  120. conn, err := net.DialTimeout("tcp", target, time.Millisecond*500)
  121. if err == nil {
  122. conn.Close()
  123. openPorts = append(openPorts, port)
  124. }
  125. }
  126. return openPorts
  127. }