From 2f8ee2164d3e2d4551983c25d1b4db3d7554faa4 Mon Sep 17 00:00:00 2001 From: Pachakutiq <101460915+PACHAKUTlQ@users.noreply.github.com> Date: Thu, 10 Apr 2025 18:07:57 +0800 Subject: [PATCH] fix: Fix serve mode not supporting raw mode --- cmd/serve.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/cmd/serve.go b/cmd/serve.go index 0b4e632..8e8cb96 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "time" "github.com/karust/openserp/baidu" @@ -9,8 +10,48 @@ import ( "github.com/karust/openserp/yandex" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "golang.org/x/time/rate" ) +// rawEngine implements SearchEngine interface for raw HTTP requests +type rawEngine struct { + name string +} + +func (r *rawEngine) Search(q core.Query) ([]core.SearchResult, error) { + // Inject proxy settings from config + q.ProxyURL = config.App.ProxyURL + q.Insecure = config.App.Insecure + + switch r.name { + case "google": + return google.Search(q) + case "yandex": + return yandex.Search(q) + case "baidu": + return baidu.Search(q) + default: + return nil, fmt.Errorf("unsupported engine: %s", r.name) + } +} + +func (r *rawEngine) SearchImage(q core.Query) ([]core.SearchResult, error) { + return nil, fmt.Errorf("image search is not supported in raw mode for %s", r.name) +} + +func (r *rawEngine) Name() string { + return r.name +} + +func (r *rawEngine) IsInitialized() bool { + return true +} + +func (r *rawEngine) GetRateLimiter() *rate.Limiter { + // Use default rate limiter for raw requests + return rate.NewLimiter(rate.Every(time.Second), 5) +} + var serveCMD = &cobra.Command{ Use: "serve", Aliases: []string{"listen"}, @@ -20,6 +61,17 @@ var serveCMD = &cobra.Command{ } func serve(cmd *cobra.Command, args []string) { + if config.App.IsRawRequests { + logrus.Warn("Browserless results are very inconsistent or may not even work!") + serv := core.NewServer(config.App.Host, config.App.Port, + &rawEngine{name: "google"}, + &rawEngine{name: "yandex"}, + &rawEngine{name: "baidu"}, + ) + serv.Listen() + return + } + opts := core.BrowserOpts{ IsHeadless: !config.App.IsBrowserHead, // Disable headless if browser head mode is set IsLeakless: config.App.IsLeakless, @@ -37,6 +89,7 @@ func serve(cmd *cobra.Command, args []string) { browser, err := core.NewBrowser(opts) if err != nil { logrus.Error(err) + return } yand := yandex.New(*browser, config.YandexConfig)