replaced deprecated 'io/ioutil' with 'io'
Some checks failed
Run Integration Tests / test (push) Failing after 39s

This commit is contained in:
partisan 2025-01-11 22:06:52 +01:00
parent 851b93bed5
commit 24c7a09479
3 changed files with 26 additions and 11 deletions

View file

@ -5,7 +5,7 @@ import (
"crypto/rand" "crypto/rand"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"time" "time"
) )
@ -65,7 +65,10 @@ func sendMessage(serverAddr string, msg Message) error {
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
}
return fmt.Errorf("server error: %s", body) return fmt.Errorf("server error: %s", body)
} }

View file

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -21,12 +21,14 @@ func PerformBraveTextSearch(query, safe, lang string, offset int) ([]TextSearchR
req, err := http.NewRequest("GET", searchURL, nil) req, err := http.NewRequest("GET", searchURL, nil)
if err != nil { if err != nil {
printWarn("Error creating request: %v", err)
return nil, 0, fmt.Errorf("creating request: %v", err) return nil, 0, fmt.Errorf("creating request: %v", err)
} }
// Set headers including User-Agent // Set headers including User-Agent
TextUserAgent, err := GetUserAgent("Text-Search") TextUserAgent, err := GetUserAgent("Text-Search")
if err != nil { if err != nil {
printWarn("Error generating User-Agent: %v", err)
return nil, 0, err return nil, 0, err
} }
req.Header.Set("User-Agent", TextUserAgent) req.Header.Set("User-Agent", TextUserAgent)
@ -35,19 +37,22 @@ func PerformBraveTextSearch(query, safe, lang string, offset int) ([]TextSearchR
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
printWarn("Error performing request: %v", err)
return nil, 0, fmt.Errorf("performing request: %v", err) return nil, 0, fmt.Errorf("performing request: %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
// Read the response body // Read the response body
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
printWarn("Error reading response body: %v", err)
return nil, 0, fmt.Errorf("reading response body: %v", err) return nil, 0, fmt.Errorf("reading response body: %v", err)
} }
// Parse the response body // Parse the response body
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(body))) doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(body)))
if err != nil { if err != nil {
printErr("Error parsing response body: %v", err)
return nil, 0, fmt.Errorf("parsing response body: %v", err) return nil, 0, fmt.Errorf("parsing response body: %v", err)
} }
@ -71,8 +76,10 @@ func PerformBraveTextSearch(query, safe, lang string, offset int) ([]TextSearchR
// Return an error if no results are found // Return an error if no results are found
if len(results) == 0 { if len(results) == 0 {
printDebug("No results found for query: %s", query)
return nil, duration, fmt.Errorf("no results found") return nil, duration, fmt.Errorf("no results found")
} }
printDebug("Search completed successfully for query: %s, found %d results", query, len(results))
return results, duration, nil return results, duration, nil
} }

View file

@ -3,8 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"log"
"net/http" "net/http"
"time" "time"
) )
@ -23,7 +22,13 @@ func fetchInstances() ([]Instance, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
XNGUserAgent, err := GetUserAgent("Text-Search-XNG")
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", XNGUserAgent)
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
@ -35,7 +40,7 @@ func fetchInstances() ([]Instance, error) {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -53,20 +58,20 @@ func validateInstance(instance Instance) bool {
client := &http.Client{Timeout: 10 * time.Second} client := &http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/search?q=test&categories=general&language=en&safe_search=1&page=1&format=json", instance.URL), nil) req, err := http.NewRequest("GET", fmt.Sprintf("%s/search?q=test&categories=general&language=en&safe_search=1&page=1&format=json", instance.URL), nil)
if err != nil { if err != nil {
log.Printf("Error creating request for URL: %s, Error: %v", instance.URL, err) printWarn("Error creating request for URL: %s, Error: %v", instance.URL, err)
return false return false
} }
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36") req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Printf("Error performing request for URL: %s, Error: %v", instance.URL, err) printWarn("Error performing request for URL: %s, Error: %v", instance.URL, err)
return false return false
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
log.Printf("Instance validation failed for URL: %s, StatusCode: %d", instance.URL, resp.StatusCode) printWarn("Instance validation failed for URL: %s, StatusCode: %d", instance.URL, resp.StatusCode)
return false return false
} }