-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathips.go
More file actions
126 lines (116 loc) · 2.73 KB
/
ips.go
File metadata and controls
126 lines (116 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"io/ioutil"
"log"
"strconv"
"strings"
"time"
)
func readFromFile(filePath string, outputChannel chan string) int {
log.Printf("Reading IPs from %v...\n", filePath)
fileData, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatalf("Unable to read file: %v\n", err.Error())
return 1
}
ips := strings.Split(strings.TrimSpace(string(fileData)), "\n")
ok, index := validateIps(ips)
if !ok {
log.Fatalf("The IP address on line %v is invalid!\n", index+1)
return 1
}
log.Printf("Successfully read %v IP addresses from file\n", len(ips))
for _, ip := range ips {
if strings.TrimSpace(ip) != "" {
outputChannel <- ip
}
}
time.Sleep(100 * time.Millisecond)
outputChannel <- "end"
return 0
}
func generateIps(startingIp string, outputChannel chan string) int {
log.Printf("Generating IPs from %v...\n", startingIp)
ok, _ := validateIps([]string{startingIp})
if !ok {
log.Println("The starting IP address you specified is invalid!")
return 1
}
segments := strings.Split(startingIp, ".")
segmentA, _ := strconv.Atoi(segments[0])
segmentB, _ := strconv.Atoi(segments[1])
segmentC, _ := strconv.Atoi(segments[2])
segmentD, _ := strconv.Atoi(segments[3])
for {
serverIp := fmt.Sprintf("%v.%v.%v.%v", segmentA, segmentB, segmentC, segmentD)
outputChannel <- serverIp
segmentD += 1
if segmentD > 255 {
segmentD = 0
segmentC += 1
if segmentC > 255 {
segmentC = 0
segmentB += 1
log.Printf("Scanning IP range %v.%v.*.*...\n", segmentA, segmentB)
if segmentB > 255 {
segmentB = 0
segmentA += 1
if segmentA > 255 {
break
}
}
}
}
}
time.Sleep(100 * time.Millisecond)
outputChannel <- "end"
return 0
}
func validateIps(ips []string) (bool, int) {
for index, ip := range ips {
if strings.TrimSpace(ip) == "" {
continue
}
if strings.Contains(ip, ":") {
addressSegments := strings.Split(ip, ":")
ip = addressSegments[0]
port := addressSegments[1]
segments := strings.Split(ip, ".")
if len(segments) != 4 {
return false, index
}
for _, segment := range segments {
number, err := strconv.Atoi(segment)
if err != nil {
return false, index
}
if number < 0 || number > 255 {
return false, index
}
}
number, err := strconv.ParseUint(port, 10, 16)
if err != nil {
return false, index
}
if number < 0 || number > 65535 {
return false, index
}
} else {
segments := strings.Split(ip, ".")
if len(segments) != 4 {
return false, index
}
for _, segment := range segments {
number, err := strconv.Atoi(segment)
if err != nil {
return false, index
}
if number < 0 || number > 255 {
return false, index
}
}
}
}
return true, -1
}