-
Notifications
You must be signed in to change notification settings - Fork 5
/
onezero.go
114 lines (104 loc) · 2.35 KB
/
onezero.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
package weiss
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
)
type OneZeroReq struct {
name string
}
type OneZeroRes struct {
Status int `json:"Status"`
TC bool `json:"TC"`
RD bool `json:"RD"`
RA bool `json:"RA"`
AD bool `json:"AD"`
CD bool `json:"CD"`
Question []Question `json:"Question"`
Answer []Answer `json:"Answer"`
}
type Question struct {
Name string `json:"name"`
Type int `json:"type"`
}
type Answer struct {
Type int `json:"type"`
TTL int `json:"TTL"`
Data string `json:"data"`
}
var (
hardcodeIpMap = make(map[string]string)
PIXIV_API_IP = "210.140.131.199"
)
func init() {
hardcodeIpMap["app-api.pixiv.net"] = "210.140.131.199"
hardcodeIpMap["oauth.secure.pixiv.net"] = "210.140.131.199"
}
func (oneZeroReq *OneZeroReq) fetch(doh string) (*OneZeroRes, error) {
if doh == "" {
doh = "doh.dns.sb"
}
url := fmt.Sprintf("https://%s/dns-query?ct=application/dns-json&name=%s&type=A&do=false&cd=false", doh, oneZeroReq.name)
log.Print(url)
v, ok := hardcodeIpMap[oneZeroReq.name]
answer := &OneZeroRes{}
if ok {
answer.Answer = []Answer{{Data: v, TTL: 50, Type: 1}}
return answer, nil
}
var body []byte
var err error
for i := 0; i < 3; i++ {
body, err = request(url)
if err == nil {
break
} else if i == 2 {
body, err = request(strings.ReplaceAll(url, doh, "dns.adguard.com"))
}
}
err = json.Unmarshal(body, answer)
if err != nil {
answer.Answer = []Answer{{Data: PIXIV_API_IP, TTL: 50, Type: 1}}
return answer, err
}
for i := range answer.Answer {
print(answer.Answer[i].Data)
if strings.Contains(answer.Answer[i].Data, "104") {
answer.Answer[i].Data = PIXIV_API_IP
}
}
return answer, err
}
func request(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body, err
}
func (oneZeroReq *OneZeroReq) PrePare(doh string) (*string, error) {
res, err := oneZeroReq.fetch(doh)
if err != nil {
return nil, err
}
for _, item := range res.Answer {
if item.Type != 1 {
continue
}
conn, err := net.Dial("tcp", item.Data+":443")
if err != nil {
continue
}
OneZeroCache.Data[oneZeroReq.name] = item.Data
conn.Close()
return &item.Data, nil
break
}
return nil, err
}