-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.go
159 lines (146 loc) · 3.41 KB
/
utils.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"archive/zip"
"bytes"
"encoding/csv"
"errors"
"fmt"
"io"
"net"
"os"
"path"
"strings"
"time"
"unicode"
"github.com/fatih/color"
"golang.org/x/net/html/charset"
)
func removeSpace(s string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, s)
}
func getFileFromZip(archive []*zip.File, filename string) (*zip.File, error) {
for _, file := range archive {
if strings.Contains(file.Name, filename) {
return file, nil
}
}
return nil, errors.New("file not found")
}
func openMapFile(outputDir, filename string) (*os.File, error) {
filepath := path.Join(outputDir, filename)
return os.Create(filepath)
}
func printMessage(module, message, status string) {
var statusMesage string
switch status {
case "OK":
if Config.LogLevel > 0 {
return
}
statusMesage = color.GreenString(status)
case "WARN":
if Config.LogLevel > 1 {
return
}
statusMesage = color.YellowString(status)
case "FAIL":
statusMesage = color.RedString(status)
default:
if Config.LogLevel > 1 {
return
}
statusMesage = color.BlueString(status)
}
fmt.Printf("%-15s | %-60s [%s]\n", module, message, statusMesage)
}
func getIPRange(ipver int, network string) string {
if ipver == 4 {
_, ipnet, err := net.ParseCIDR(network)
if err != nil {
return ""
}
ipb := make(net.IP, net.IPv4len)
copy(ipb, ipnet.IP)
for i, v := range ipb {
ipb[i] = v | ^ipnet.Mask[i]
}
return fmt.Sprintf("%s-%s", ipnet.IP, ipb)
}
if strings.Contains(network, ":") && strings.Contains(network, "/") {
return network
}
return ""
}
// Convert uint to net.IP
func int2ip(ipnr int64) net.IP {
var bytes [4]byte
bytes[0] = byte(ipnr & 0xFF)
bytes[1] = byte((ipnr >> 8) & 0xFF)
bytes[2] = byte((ipnr >> 16) & 0xFF)
bytes[3] = byte((ipnr >> 24) & 0xFF)
return net.IPv4(bytes[3], bytes[2], bytes[1], bytes[0])
}
func readCSVDatabase(archive []*zip.File, filename string, dbType string, comma rune, windowsEncoding bool) chan []string {
yield := make(chan []string)
go func() {
defer close(yield)
file, err := getFileFromZip(archive, filename)
if err != nil {
printMessage(dbType, fmt.Sprintf("%s %s", filename, err.Error()), "FAIL")
return
}
fp, err := file.Open()
if err != nil {
printMessage(dbType, fmt.Sprintf("Can't open %s", filename), "FAIL")
yield <- nil
}
defer fp.Close()
var r *csv.Reader
if windowsEncoding {
utf8, err := charset.NewReader(fp, "text/csv; charset=windows-1251")
if err != nil {
printMessage(dbType, fmt.Sprintf("%s not in cp1251", filename), "FAIL")
yield <- nil
}
r = csv.NewReader(utf8)
} else {
r = csv.NewReader(fp)
}
r.Comma, r.LazyQuotes = comma, true
for {
record, err := r.Read()
// Stop at EOF.
if err == io.EOF {
break
}
if err != nil {
printMessage(dbType, fmt.Sprintf("Can't read line from %s", filename), "WARN")
continue
}
yield <- record
}
}()
return yield
}
func convertTZToOffset(t time.Time, tz string) string {
location, err := time.LoadLocation(tz)
if err != nil {
return ""
}
_, offset := t.In(location).Zone()
return fmt.Sprintf("UTC%+d", offset/3600)
}
// Unpack zip file and return slice of zip.File`s
func Unpack(response []byte) ([]*zip.File, error) {
zipReader, err := zip.NewReader(bytes.NewReader(response), int64(len(response)))
var file []*zip.File
if err == nil {
file = zipReader.File
}
return file, err
}