Skip to content

Commit

Permalink
feat: 添加ip代理池,避免使用同一个ip并发请求
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxuan6 committed Nov 25, 2024
1 parent f1bddb3 commit bb3a423
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
22 changes: 19 additions & 3 deletions deeplx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
"github.com/avast/retry-go"
"github.com/tidwall/gjson"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
)

var (
wg sync.WaitGroup

targetUrls = make([]string, 0)
urls = []string{"https://deeplx.mingming.dev/translate"}
)
Expand Down Expand Up @@ -44,10 +47,23 @@ func Translate(text, sourceLang, targetLang string) *Response {
requestParams := bytes.Buffer{}
requestParams.WriteString(`{"text":"` + text + `","source_lang":"` + sourceLang + `","target_lang":"` + targetLang + `"}`)

transport := &http.Transport{}
if proxyUrl := getProxyUrl(); proxyUrl != "" {
proxy, errs := url.Parse(proxyUrl)
if errs == nil {
transport.Proxy = http.ProxyURL(proxy)
}
}

httpClient := &http.Client{
Transport: transport,
Timeout: 3 * time.Second,
}

var body []byte
err := retry.Do(
func() error {
response, err := client.Post(fetchUri(), "application/json", strings.NewReader(requestParams.String()))
response, err := httpClient.Post(fetchUri(), "application/json", strings.NewReader(requestParams.String()))
defer func() {
_ = response.Body.Close()
}()
Expand Down Expand Up @@ -76,7 +92,7 @@ func Translate(text, sourceLang, targetLang string) *Response {
}

func TranslateByDeeplx(text, sourceLang, targetLang string) *Response {
result, err := translate.TranslateByDeepLX(sourceLang, targetLang, text, "", "")
result, err := translate.TranslateByDeepLX(sourceLang, targetLang, text, "", getProxyUrl())
if err != nil {
return &Response{
Code: 500,
Expand Down
43 changes: 40 additions & 3 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"strings"
Expand Down Expand Up @@ -31,7 +32,7 @@ func fetchUri() string {
wg.Wait()
}

randomIndex := randomNum()
randomIndex := randomNum(urls)
if randomIndex <= len(urls) {
return urls[randomIndex]
}
Expand Down Expand Up @@ -81,7 +82,43 @@ func checkUrlVerify(url string, wg *sync.WaitGroup) {
}
}

func randomNum() int {
urlsLen := len(urls)
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, line)
}
})

if len(proxyUrls) < 1 {
return ""
}

return proxyUrls[randomNum(proxyUrls)]
}

0 comments on commit bb3a423

Please sign in to comment.