-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
154 lines (129 loc) · 2.82 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
package main
import (
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"path"
"strings"
"github.com/oschwald/geoip2-golang"
)
// Lookup ip or hostname
func Lookup(lookup string) {
var ciso string
var cname string
var mmdb string
var output []string
var response string
var ipraw string
// convert to ip if hostname
addresses, err := net.LookupHost(lookup)
if len(addresses) > 0 {
Verbose(fmt.Sprintf("Ip search for: %s", addresses[0]))
ipraw = addresses[0]
} else {
fmt.Println("Error:", err)
os.Exit(1)
}
fi, err := os.Stat(dataDir)
if err != nil {
fmt.Println("Error: Directory does not exist", dataDir)
os.Exit(1)
}
switch mode := fi.Mode(); {
case mode.IsDir(): // if dataDir is dir, append GeoLite2-Country.mmdb
mmdb = path.Join(dataDir, "GeoLite2-Country.mmdb")
case mode.IsRegular():
mmdb = dataDir
}
Verbose(fmt.Sprintf("Opening %s", mmdb))
db, err := geoip2.Open(mmdb)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
defer db.Close()
ip := net.ParseIP(ipraw)
record, err := db.Country(ip)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
if record.Traits.IsAnonymousProxy {
Verbose("Anonymous IP detected")
ciso = "A1"
cname = "Anonymous Proxy"
} else {
ciso = record.Country.IsoCode
cname = record.Country.Names["en"]
}
if country || iso {
if iso && ciso != "" {
output = append(output, ciso)
}
if country && cname != "" {
output = append(output, cname)
}
response = fmt.Sprintf(strings.Join(output, ", "))
} else {
if ciso == "" {
response = "GeoIP Country Edition: IP Address not found"
} else {
response = fmt.Sprintf("GeoIP Country Edition: %s, %s", ciso, cname)
}
}
fmt.Println(response)
}
// DownloadToFile downloads a URL to a file
func DownloadToFile(filepath string, uri string) error {
debugURI := uri
u, err := url.Parse(uri)
if err != nil {
return err
}
q := u.Query()
if q.Get("license_key") != "" {
// remove key from debug
debugURI = strings.Replace(debugURI, q.Get("license_key"), "xxxxxxxxxx", -1)
}
Verbose(fmt.Sprintf("Downloading %s", debugURI))
// Get the data
resp, err := http.Get(uri)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
// IsFile returns if a path is a file
func isFile(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) || !info.Mode().IsRegular() {
return false
}
return true
}
// IsDir returns whether a path is a directory
func isDir(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) || !info.IsDir() {
return false
}
return true
}
// Verbose displays debug information with `-v`
func Verbose(m string) {
if verboseOutput {
fmt.Println(m)
}
}