-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotdb.go
82 lines (65 loc) · 1.47 KB
/
dotdb.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
package dns
import (
"database/sql"
"time"
"github.com/u6du/db"
"github.com/u6du/ex"
)
var Db = db.Db(
"dns/dot",
`CREATE TABLE "dot" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"host" TEXT NOT NULL UNIQUE,
"delay" INTEGER NOT NULL DEFAULT 0);
CREATE INDEX "dot.delay" ON "dot" ("delay" ASC);`,
"INSERT INTO dot(host) values (?)",
"dns.rubyfish.cn",
"dot-jp.blahdns.com",
"dns.google",
"security-filter-dns.cleanbrowsing.org",
"dot.securedns.eu",
"sdns.233py.com",
"ndns.233py.com",
"dns.quad9.net",
"wdns.233py.com",
"dot-de.blahdns.com",
"1dot1dot1dot1.cloudflare-dns.com",
"dns.brahma.world",
)
func DotTxt(name string, verify func(string) bool) (txt *string) {
Db.With(func(db *sql.DB) {
c, err := db.Query("select id,host from dot order by delay asc")
ex.Panic(err)
var id uint
var nameserver string
var costIdLi [][2]uint
defer func() {
for _, costId := range costIdLi {
_, err = db.Exec("UPDATE dot SET delay=? WHERE id=?", costId[0], costId[1])
ex.Warn(err)
}
}()
for c.Next() {
ex.Warn(c.Scan(&id, &nameserver))
start := time.Now()
txt = DotLookupTxt(name, nameserver, 2)
cost := uint(time.Since(start).Nanoseconds() / 1000000)
var verified bool
if txt == nil {
cost += 20000
verified = false
} else {
verified = verify(*txt)
if !verified {
cost += 10000
}
}
costIdLi = append(costIdLi, [2]uint{cost, id})
if verified {
ex.Close(c)
return
}
}
})
return txt
}