-
Notifications
You must be signed in to change notification settings - Fork 57
/
dnsleaktest.go
138 lines (108 loc) · 2.33 KB
/
dnsleaktest.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
)
var ApiDomain = "bash.ws"
type Block struct {
Ip string `json:"ip"`
Country string `json:"country"`
CountryName string `json:"country_name"`
Asn string `json:"asn"`
Type string `json:"type"`
}
func raiseError(err error) {
if err == nil {
return;
}
panic(err)
}
func getContent(url string) ([]byte) {
resp, err := http.Get(url)
if err != nil {
raiseError(fmt.Errorf("GET error: %v", err))
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
raiseError(fmt.Errorf("Status error: %v", resp.StatusCode))
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
raiseError(fmt.Errorf("Read body: %v", err))
}
return data
}
func getId() (string) {
return string(getContent(fmt.Sprintf("https://%s/id", ApiDomain)))
}
func fakePing(testId string) () {
var wg sync.WaitGroup
for i := 0; i <= 10; i++ {
urlPing := fmt.Sprintf("https://%d.%s.%s", i, testId, ApiDomain)
wg.Add(1)
go func(urlPing string) {
defer wg.Done()
http.Get(urlPing)
}(urlPing)
}
wg.Wait()
}
func getResult(testId string) ([]Block) {
// send GET request
data := getContent(fmt.Sprintf("https://%s/dnsleak/test/%s?json", ApiDomain, testId))
var xml []Block
err := json.Unmarshal(data, &xml)
raiseError(err)
return xml
}
func printResult(result []Block, Type string) {
for _, Block := range result {
if Block.Type != Type {
continue
}
if Block.Asn != "" {
fmt.Printf("%s [%s, %s]\n", Block.Ip, Block.CountryName, Block.Asn)
continue
}
if Block.CountryName != "" {
fmt.Printf("%s [%s]\n", Block.Ip, Block.CountryName)
continue
}
if Block.Ip != "" {
fmt.Printf("%s\n", Block.Ip)
}
}
}
func main() {
// get an id fo testing
testId := getId()
// ping fake domains
fakePing(testId)
// parse test result
result := getResult(testId)
// show the testing result
dns := 0
for _, Block := range result {
switch Block.Type {
case "dns":
dns++
}
}
fmt.Print("Your IP:\n")
printResult(result, "ip")
if dns == 0 {
fmt.Print("No DNS servers found\n")
} else {
if dns == 1 {
fmt.Printf("You use %d DNS server:\n", dns)
} else {
fmt.Printf("You use %d DNS servers:\n", dns)
}
printResult(result, "dns")
}
fmt.Print("Conclusion:\n")
printResult(result, "conclusion")
}