Skip to content

Commit

Permalink
Feat/Flaresolverr bugfixes (#21)
Browse files Browse the repository at this point in the history
* chg: fix: add pt-bt to audio detection

* chg: fix: add retry when flaresolverr response is without body

* chg: fix: add back under attack verification
  • Loading branch information
felipemarinho97 authored Dec 11, 2024
1 parent 5034a11 commit 339db28
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
58 changes: 55 additions & 3 deletions requester/flaresolverr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"sync"
)
Expand All @@ -19,6 +21,10 @@ type FlareSolverr struct {
initiated bool
}

var (
ErrListSessions = fmt.Errorf("failed to list sessions")
)

func NewFlareSolverr(url string, timeoutMilli int) *FlareSolverr {
poolSize := 5
httpClient := &http.Client{}
Expand Down Expand Up @@ -48,6 +54,14 @@ func (f *FlareSolverr) FillSessionPool() error {
// Pre-initialize the pool with existing sessions
sessions, err := f.ListSessions()
if err != nil {
// if fail to list sessions, it may not support the sessions.list command
// create new dumb sessions to fill the pool
if err == ErrListSessions {
for len(f.sessionPool) < cap(f.sessionPool) {
f.sessionPool <- "dumb-session"
}
return nil
}
fmt.Println("Failed to list existing FlareSolverr sessions:", err)
return err
} else {
Expand Down Expand Up @@ -134,6 +148,9 @@ func (f *FlareSolverr) ListSessions() ([]string, error) {
if err != nil {
return nil, err
}
if sessionsResponse["sessions"] == nil {
return nil, ErrListSessions
}

sessions := sessionsResponse["sessions"].([]interface{})
var sessionIDs []string
Expand Down Expand Up @@ -172,7 +189,7 @@ type Response struct {
} `json:"solution"`
}

func (f *FlareSolverr) Get(url string) (io.ReadCloser, error) {
func (f *FlareSolverr) Get(_url string) (io.ReadCloser, error) {
// Check if the FlareSolverr instance was initiated
if !f.initiated {
return io.NopCloser(bytes.NewReader([]byte(""))), nil
Expand All @@ -188,14 +205,15 @@ func (f *FlareSolverr) Get(url string) (io.ReadCloser, error) {

body := map[string]string{
"cmd": "request.get",
"url": url,
"url": _url,
"maxTimeout": fmt.Sprintf("%d", f.maxTimeout),
"session": session,
}
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1", f.url), bytes.NewBuffer(jsonBody))
if err != nil {
return nil, err
Expand Down Expand Up @@ -225,6 +243,40 @@ func (f *FlareSolverr) Get(url string) (io.ReadCloser, error) {
return nil, fmt.Errorf("under attack")
}

// Return the response body
// If the response body is empty but cookies are present, make a new request
if response.Solution.Response == "" && len(response.Solution.Cookies) > 0 {
// Create a new request with cookies
client := &http.Client{}
cookieJar, err := cookiejar.New(&cookiejar.Options{})
if err != nil {
return nil, err
}
for _, cookie := range response.Solution.Cookies {
cookieJar.SetCookies(&url.URL{Host: cookie.Domain}, []*http.Cookie{
{
Name: cookie.Name,
Value: cookie.Value,
Domain: cookie.Domain,
Path: cookie.Path,
},
})
}
client.Jar = cookieJar

secondReq, err := http.NewRequest("GET", _url, nil)
if err != nil {
return nil, err
}

secondResp, err := client.Do(secondReq)
if err != nil {
return nil, err
}

// Return the body of the second request
return secondResp.Body, nil
}

// Return the original response body
return io.NopCloser(bytes.NewReader([]byte(response.Solution.Response))), nil
}
8 changes: 7 additions & 1 deletion schema/audio.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package schema

import "strings"

type Audio string

const (
AudioPortuguese = "Português"
AudioPortuguese2 = "Portugues"
AudioPortuguese3 = "PT-BR"
AudioEnglish = "Inglês"
AudioEnglish2 = "Ingles"
AudioSpanish = "Espanhol"
Expand Down Expand Up @@ -33,6 +36,7 @@ const (
var AudioList = []Audio{
AudioPortuguese,
AudioPortuguese2,
AudioPortuguese3,
AudioEnglish,
AudioEnglish2,
AudioSpanish,
Expand Down Expand Up @@ -64,7 +68,7 @@ func (a Audio) String() string {

func GetAudioFromString(s string) *Audio {
for _, a := range AudioList {
if string(a) == s {
if strings.EqualFold(string(a), s) {
return &a
}
}
Expand All @@ -77,6 +81,8 @@ func (a Audio) toTag() string {
return "brazilian"
case AudioPortuguese2:
return "brazilian"
case AudioPortuguese3:
return "brazilian"
case AudioEnglish:
return "eng"
case AudioEnglish2:
Expand Down

0 comments on commit 339db28

Please sign in to comment.