-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (59 loc) · 1.43 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
71
72
73
package main
// geoUR : read ips from stdin, display for Country and ASN
import (
"fmt"
"os"
)
// History :
// v1.0 combine geoip2 Country and ASN,
// v1.2 testing functions as parameter
// v1.3 Better json output
// v1.4 correcting for & ' and \ caracters
//
// it doesn't resolve ip anymore
// use "| adnsresfilter -ua" if resolution is needed
//
var (
maxrequests = 128
// Version given by git tag via Makefile
Version = "v1.4"
)
// printVersionUsage only print Version and Usage anq exit
func printVersionUsage() {
fmt.Printf("geoUR (%s) build with MaxMind free edition db :\n\t- %s\n\t- %s\n\n",
Version, AssetCountryName, AssetASNname)
fmt.Printf("Usage: geoUR ip or stdin input\nuse BULKFORMAT env for JSON, SED format")
os.Exit(0)
}
func main() {
bulkformat := getenv("BULKFORMAT", "")
var printfunc FuncStringtoString
switch bulkformat {
case "JSON":
printfunc = parseandprintJSON
case "SED":
printfunc = parseandprintSED
default:
printfunc = parseandprint
}
switch {
case len(os.Args) == 1:
// Read stdin
readandprintbulk(printfunc)
case (len(os.Args) == 2) && (os.Args[1] == "-h" || os.Args[1] == "-v"):
printVersionUsage()
case len(os.Args) == 2:
// Single input as parameter
fmt.Printf("%s\n", parseandprint(os.Args[1]))
default:
printVersionUsage()
}
os.Exit(0)
}
func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}