-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
124 lines (102 loc) · 2.02 KB
/
url.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
package deeplx
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"sync"
"time"
)
func fetchUri() string {
if len(urls) < 1 {
var wgs sync.WaitGroup
wgs.Add(2)
for i, url := range []string{
"https://github-mirror.us.kg/https://github.com/ycvk/deeplx-local/blob/windows/url.txt",
"https://github-mirror.us.kg/https://github.com/xiaozhou26/serch_deeplx/blob/main/success.txt",
} {
go fetchUrls(&wgs, i, url)
}
wgs.Wait()
for _, url := range targetUrls {
wg.Add(1)
go checkUrlVerify(url, &wg)
}
wg.Wait()
}
randomIndex := randomNum(urls)
if randomIndex <= len(urls) {
return urls[randomIndex]
}
return urls[0]
}
var client = &http.Client{
Timeout: 3 * time.Second,
}
func fetchUrls(wg *sync.WaitGroup, k int, url string) {
defer wg.Done()
resp, err := client.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
r := bufio.NewReader(resp.Body)
for {
line, _, errs := r.ReadLine()
if errs == io.EOF {
break
}
newUrl := string(line)
if k == 1 {
newUrl = fmt.Sprintf("%s/translate", newUrl)
}
targetUrls = append(targetUrls, newUrl)
}
}
func checkUrlVerify(url string, wg *sync.WaitGroup) {
defer wg.Done()
resp, err := client.Get(strings.ReplaceAll(url, "/translate", ""))
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
urls = append(urls, url)
}
}
func randomNum(slices []string) int {
urlsLen := len(slices)
return rand.Intn(urlsLen)
}
var (
proxyUrls []string
once sync.Once
)
func getProxyUrl() string {
once.Do(func() {
res, err := client.Get("https://269900.xyz/fetch_http_all")
if err != nil {
return
}
defer res.Body.Close()
item, err := ioutil.ReadAll(res.Body)
if err != nil {
return
}
f := bufio.NewReader(strings.NewReader(string(item)))
for {
line, err := f.ReadString('\n')
if err != nil {
break
}
proxyUrls = append(proxyUrls, strings.Trim(line, ","))
}
})
if len(proxyUrls) < 1 {
return ""
}
return proxyUrls[randomNum(proxyUrls)]
}