-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (54 loc) · 1.37 KB
/
main.go
File metadata and controls
66 lines (54 loc) · 1.37 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/user"
"strings"
"github.com/jmoiron/jsonq"
)
func main() {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
file := usr.HomeDir + "/.config/ipdb.txt"
API_KEY, err := os.ReadFile(file)
if err != nil {
fmt.Println(err)
fmt.Println("Please put your API_KEY in", file)
os.Exit(0)
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
client := http.Client{}
req, err := http.NewRequest("GET", "https://api.abuseipdb.com/api/v2/check?ipAddress="+scanner.Text(), nil)
if err != nil {
fmt.Println(err)
os.Exit(0)
}
req.Header = http.Header{
"Key": []string{strings.TrimRight(string(API_KEY), "\n")},
"Content-Type": []string{"application/json"},
}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
os.Exit(0)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
data := map[string]interface{}{}
dec := json.NewDecoder(strings.NewReader(string(body)))
dec.Decode(&data)
jq := jsonq.NewQuery(data)
countryCode, err := jq.String("data", "countryCode")
abuseScore, err := jq.Int("data", "abuseConfidenceScore")
totalReports, err := jq.Int("data", "totalReports")
fmt.Print("[", scanner.Text(), "] Code: ", countryCode, " | Score: ", abuseScore, " | Total Reports: ", totalReports, "\n")
}
}