-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (55 loc) · 1.17 KB
/
main.go
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
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strconv"
)
var (
startIP, finishIP uint64
)
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
if len(os.Args) == 2 {
cc := os.Args[1]
csvfile, err := os.Open("./db/IP2LOCATION-LITE-DB1.CSV")
checkErr(err)
r := csv.NewReader(csvfile)
file, err := os.OpenFile(cc+".txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Unable to create file:", err)
os.Exit(1)
}
defer file.Close()
for {
record, err := r.Read()
if err == io.EOF {
break
}
checkErr(err)
if record[2] == cc {
if s1, err := strconv.ParseUint(record[0], 10, 64); err == nil {
startIP = s1
}
if s2, err := strconv.ParseUint(record[1], 10, 64); err == nil {
finishIP = s2
}
for i := startIP; i < finishIP; i++ {
file.WriteString(inet_ntoa(i) + "\n")
}
}
}
} else {
fmt.Println("Something wrong,country code please use 2 letter format as sample 'UA' for Ukraine or 'US' for USA")
}
}
func inet_ntoa(ip uint64) string {
return fmt.Sprintf("%d.%d.%d.%d", byte(ip>>24), byte(ip>>16), byte(ip>>8),
byte(ip))
}