This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcid_contact_checker.go
112 lines (97 loc) · 2.13 KB
/
cid_contact_checker.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
package onion
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
var cidContactUrl = "https://cid.contact/cid/%s"
type CidContactChecker struct {
client http.Client
mismatches []string
}
type CidContactOutput struct {
Status int
Response string
IsDagHouse bool
IsPinata bool
}
func NewCidContactChecker(mismatches []string) *CidContactChecker {
return &CidContactChecker{
client: http.Client{
Transport: &http.Transport{
MaxConnsPerHost: 1000,
MaxIdleConnsPerHost: 1000,
MaxIdleConns: 1000,
},
Timeout: 3 * time.Minute,
},
mismatches: mismatches,
}
}
func (klm *CidContactChecker) Check() {
type Summary struct {
NotFoundOnCidContact int
DAGHouseCid int
PinataCid int
Others int
}
sum := Summary{}
for _, path := range klm.mismatches {
// check cid.contact
cid := ParseCidFromPath(path)
var cc *CidContactOutput
var err error
for {
cc, err = klm.GetCidContactResponse(cid)
if err != nil {
time.Sleep(1 * time.Second)
continue
}
break
}
if cc.Status == http.StatusNotFound {
sum.NotFoundOnCidContact++
} else if cc.IsDagHouse {
sum.DAGHouseCid++
} else if cc.IsPinata {
sum.PinataCid++
} else {
sum.Others++
}
}
fmt.Println("\n--- cid.contact Summary of mismatches---")
bz, err := json.MarshalIndent(sum, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(bz))
}
func (klm *CidContactChecker) GetCidContactResponse(cid string) (*CidContactOutput, error) {
resp, err := klm.client.Get(fmt.Sprintf(cidContactUrl, cid))
if err != nil {
return nil, err
}
defer resp.Body.Close()
out := &CidContactOutput{
Status: resp.StatusCode,
}
if resp.StatusCode != http.StatusOK {
return out, nil
}
if resp.StatusCode == http.StatusOK {
bz, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
out.Response = string(bz)
if strings.Contains(out.Response, "dag.w3s") || strings.Contains(out.Response, "dag.house") {
out.IsDagHouse = true
} else if strings.Contains(out.Response, "pinata.cloud") {
out.IsPinata = true
}
}
return out, nil
}