-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaven-checking.yak
143 lines (109 loc) · 3.08 KB
/
maven-checking.yak
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
139
140
141
142
143
base = "~/Projects/maven-mirror"
if base.HasPrefix("~") {
base = base[2:]
base = file.Join("/Users/v1ll4n", base)
}
file.MkdirAll(base)~
proxy = "http://127.0.0.1:7890"
swg = sync.NewSizedWaitGroup(20)
fetchNext = (url, basePath) => {
for secondTime in 5 {
try {
return _fetchNext(url, basePath)
} catch e {
log.error("fetch error: %v", url)
sleep(1 + 1 * secondTime)
continue
}
}
}
_fetchNext = (url, basePath) => {
if file.IsExisted(basePath) {
results = []
for info in file.Ls(basePath) {
next = info.Name
if info.IsDir {
next = next.HasSuffix("/") ? next : (next + "/")
}
if next.Contains("maven-metadata.xml") {
println("Have %v in %v" % [next, url])
return []
}
if next.Contains("no-existed.txt") {
println("emtpy package for %v" % url)
return []
}
results.Push(next)
}
if results.Len() > 0 {
return results
}
} else {
println("not existed: %v" % basePath)
}
swg.Add()
println("start to find %v" % url)
rsp, req, err := poc.Get(
url,
poc.proxy(proxy),
poc.randomJA3(true),
poc.replaceUserAgent(`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36`),
poc.retryNotInStatusCode(200),
poc.retryTimes(10),
)
swg.Done()
if err != nil {
die(err)
}
println("finished to find %v" % url)
body = rsp.GetBody()
node = xpath.LoadHTMLDocument(body)~
results = xpath.QueryAll(node, `//*[@id="contents"]/child::a/@href`)~
return results.Map(i => f`${xpath.OutputHTML(i)}`)
}
recursive = (url, basePath) => {
defer recover()
if !file.IsExisted(basePath) {
file.MkdirAll(basePath)
}
// println("start to view url: %v" % url)
results = fetchNext(url, basePath)
next = []
haveMetadataXML = false
for i in results {
nextItem = i
if nextItem.Contains(`maven-metadata.xml`) {
haveMetadataXML = true
}
if nextItem == "../" {
continue
}
if !nextItem.HasSuffix("/") {
continue
}
next.Push(nextItem)
}
if haveMetadataXML {
file.Save(file.Join(basePath, `maven-metadata.xml`), "")
}
if next.Len() <= 0 {
file.Save(file.Join(basePath, "no-existed.txt"), time.Now())
}
for i in next {
newPath = file.Join(basePath, i)
file.MkdirAll(newPath)
}
if haveMetadataXML {
return
}
for info in next {
newUrl := str.UrlJoin(url, info)~
newPath := file.Join(basePath, info)
go func {
defer recover()
recursive(newUrl, newPath)
}
}
}
recursive("https://repo.maven.apache.org/maven2/", base)
swg.Wait()