From bb3a423800fed2a0e9b0e26f0d78d4bc381dd74e Mon Sep 17 00:00:00 2001 From: xiaoxuan6 <1527736751@qq.com> Date: Mon, 25 Nov 2024 14:15:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0ip=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E6=B1=A0=EF=BC=8C=E9=81=BF=E5=85=8D=E4=BD=BF=E7=94=A8=E5=90=8C?= =?UTF-8?q?=E4=B8=80=E4=B8=AAip=E5=B9=B6=E5=8F=91=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deeplx.go | 22 +++++++++++++++++++--- url.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/deeplx.go b/deeplx.go index 332edf2..b85e0ff 100644 --- a/deeplx.go +++ b/deeplx.go @@ -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"} ) @@ -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() }() @@ -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, diff --git a/url.go b/url.go index 8bf8c29..ddc1064 100644 --- a/url.go +++ b/url.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io" + "io/ioutil" "math/rand" "net/http" "strings" @@ -31,7 +32,7 @@ func fetchUri() string { wg.Wait() } - randomIndex := randomNum() + randomIndex := randomNum(urls) if randomIndex <= len(urls) { return urls[randomIndex] } @@ -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)] +}