-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipchecker.go
More file actions
76 lines (74 loc) · 1.58 KB
/
ipchecker.go
File metadata and controls
76 lines (74 loc) · 1.58 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
67
68
69
70
71
72
73
74
75
76
package main
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strings"
)
func isIpValid(site string, ip string, keyName string, value []string, blacklist bool) bool {
// check if ip is domain or ip
_ip := net.ParseIP(ip)
if _ip == nil { //domain
ips, err := net.LookupIP(ip)
if err != nil {
fmt.Println("Unable to resolve server name:", err)
return false
} else {
if ips[0] != nil {
ip = ips[0].To16().String()
if ip == "" {
fmt.Println("Unable to convert server name:", err)
return false
}
}
}
} //ip
request := strings.Builder{}
request.WriteString(site)
request.WriteString(ip)
resp, err := http.Get(request.String())
if err != nil {
fmt.Println("Unable to connect to ip checker server:", err)
return false
} else if resp.StatusCode == 200 {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Unable to read ip info:", err)
return false
} else {
var answer map[string]interface{}
err := json.Unmarshal([]byte(body), &answer)
if err != nil {
fmt.Println("Error while unmarshalling JSON:", err)
return false
}
for key, val := range answer {
if key == keyName {
j := len(value)
for i := range j {
if val == value[i] {
if blacklist {
return false
} else {
fmt.Println("Country ", val)
return true
}
}
}
if blacklist {
fmt.Println("Country ", val)
return true
} else {
return false
}
}
}
}
} else {
fmt.Println("Service unavailable :", err)
}
return false
}