FlareSolverr

package module
v0.0.0-...-37ada51 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2025 License: MIT Imports: 8 Imported by: 3

README

FlareSolverr-go

README.md generated by VSCode Copilot

基于 Go 的 FlareSolverr 客户端,封装了 request.get / request.post、会话管理(sessions.list/create/destroy)等常用命令,并提供便捷的返回结构体与辅助方法。

模块路径:github.com/Miuzarte/FlareSolverr-go

要求

  • Go 1.25+
  • 运行中的 FlareSolverr 服务端(默认示例使用 http://127.0.0.1:8191/v1

安装

go get github.com/Miuzarte/FlareSolverr-go

快速开始

package main

import (
	"fmt"
	fs "github.com/Miuzarte/FlareSolverr-go"
)

func main() {
	client := fs.NewClient("http://127.0.0.1:8191/v1")

	// GET 简单示例
	resp, err := client.Get("https://example.com", nil)
	if err != nil {
		panic(err)
	}
	fmt.Println("Status:", resp.Solution.Status)
	fmt.Println("URL:", resp.Solution.URL)
	fmt.Println("UA:", resp.Solution.UserAgent)
	fmt.Println("Body length:", len(resp.Solution.Response))
}

用法示例

基础 GET
resp, err := client.Get("https://example.com", nil)

带参数:

params := map[string]any{
	fs.PARAM_MAX_TIMEOUT: 60000,             // 毫秒
	fs.PARAM_RETURN_SCREENSHOT: false,       // 返回截图 base64
	fs.PARAM_RETURN_ONLY_COOKIES: false,     // 仅返回 cookies
	fs.PARAM_WAIT_IN_SECONDS: 0,             // 等待页面稳定(动态内容)
}
resp, err := client.Get("https://example.com", params)
基础 POST

postDataapplication/x-www-form-urlencoded 的原始字符串:

params := map[string]any{
	fs.PARAM_MAX_TIMEOUT: 60000,
}
resp, err := client.Post("https://httpbin.org/post", "a=b&c=d", params)
代理与会话
params := map[string]any{
	fs.PARAM_PROXY: map[string]any{
		"url": "http://127.0.0.1:7890",
		"username":  "testuser",
		"password":  "testpass",
	},
}

// 预创建会话(可复用浏览器上下文)
_ = client.SessionsCreate("mysess", params)

// 会话内请求
params[fs.PARAM_SESSION] = "mysess"
resp, err := client.Get("https://example.com", params)

// 列出/销毁会话
list, _ := client.SessionsList()
_ = client.SessionsDestroy("mysess")
sol, _ := client.Get("https://example.com", nil)
httpCookies := resp.Solution.Cookies.ToHttpCookies() // []*http.Cookie
原始 Submit 接口

如需发送自定义命令或完全掌控参数,可直接使用 Submit

resp, err := client.Submit(fs.CMD_REQUEST_GET, map[string]any{
	fs.PARAM_URL:             "https://example.com",
	fs.PARAM_MAX_TIMEOUT:     60000,
	fs.PARAM_RETURN_SCREENSHOT: true,
})
if err != nil {
	// 注意:当 FlareSolverr 返回非 ok 状态时,err 为 Response.Message,且可同时读取 resp 获取详细信息
	fmt.Println("request error:", err)
}
fmt.Println("status:", resp.Status)
fmt.Println("session:", resp.Session)

参数与常量

  • 命令(Cmd):

    • fs.CMD_REQUEST_GET
    • fs.CMD_REQUEST_POST
    • fs.CMD_SESSIONS_LIST
    • fs.CMD_SESSIONS_CREATE
    • fs.CMD_SESSIONS_DESTROY
  • 参数键(Param,map[string]any 的 key):

    • fs.PARAM_URL(string)
    • fs.PARAM_SESSION(string)
    • fs.PARAM_SESSION_TTL_MINUTES(int)
    • fs.PARAM_MAX_TIMEOUT(int,毫秒)
    • fs.PARAM_COOKIES([]Cookie)
    • fs.PARAM_RETURN_ONLY_COOKIES(bool)
    • fs.PARAM_RETURN_SCREENSHOT(bool)
    • fs.PARAM_PROXY(map[string]any:包含 url、可选 usernamepassword
    • fs.PARAM_WAIT_IN_SECONDS(int)
    • fs.PARAM_POST_DATA(string)

返回类型

  • Response:包含 StatusMessageVersion、时间戳、Session/SessionsSolution 等。
  • Solution:页面 URL、HTTP StatusCookiesUserAgentHeadersResponse(HTML 文本)、Screenshot(base64 PNG)。

错误处理约定:

  • HTTP 层非 200 会返回 error(内容为 HTTP 状态)。
  • FlareSolverr 返回 status != "ok" 时:同时返回 *ResponseerrorerrorResponse.Message)。

设计细节

  • Request 时内部会克隆你传入的 params(使用 Go 1.20+ 的 maps.Clone),避免修改调用方 map。
  • Submit 采用 JSON 序列化并通过 Content-Type: application/json POST 到服务端。

许可

遵循与上游相同的开源许可(见仓库 LICENSE)。

Documentation

Index

Constants

View Source
const RESP_STATUS_OK = "ok"

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Endpoint string
}

func NewClient

func NewClient(endpoint string) *Client

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string, params map[string]any) (*Response, error)

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, postData string, params map[string]any) (*Response, error)

func (*Client) RequestGet

func (c *Client) RequestGet(ctx context.Context, url string, params map[string]any) (*Response, error)

params:

(*)url
session
session_ttl_minutes
maxTimeout: 60000
cookies: [{"name": "cookie1", "value": "value1"}, {"name": "cookie2", "value": "value2"}]
returnOnlyCookies: false
returnScreenshot: false
proxy: {"url": "http://127.0.0.1:7890", "username": "testuser", "password": "testpass"}
waitInSeconds: 0 // Useful to allow it to load dynamic content.

func (*Client) RequestPost

func (c *Client) RequestPost(ctx context.Context, url string, postData string, params map[string]any) (*Response, error)

params:

(*)url
postData: "a=b&c=d" // application/x-www-form-urlencoded
// other params same as [Client.RequestGet]

func (*Client) SessionsCreate

func (c *Client) SessionsCreate(ctx context.Context, session string, params map[string]any) error

params:

(*)session
proxy: {"url": "http://127.0.0.1:7890", "username": "testuser", "password": "testpass"}

func (*Client) SessionsDestroy

func (c *Client) SessionsDestroy(ctx context.Context, session string) error

params:

(*)session

func (*Client) SessionsList

func (c *Client) SessionsList(ctx context.Context) ([]string, error)

func (*Client) Submit

func (c *Client) Submit(ctx context.Context, cmd string, params map[string]any) (*Response, error)

Submit 直接提交命令和参数至 FlareSolverr, 返回 Response 结构体

type Cmd

type Cmd = string
const (
	CMD_REQUEST_GET      Cmd = "request.get"
	CMD_REQUEST_POST     Cmd = "request.post"
	CMD_SESSIONS_LIST    Cmd = "sessions.list"
	CMD_SESSIONS_CREATE  Cmd = "sessions.create"
	CMD_SESSIONS_DESTROY Cmd = "sessions.destroy"
)
type Cookie struct {
	Name     string `json:"name"`
	Value    string `json:"value"`
	Path     string `json:"path"`
	Domain   string `json:"domain"`
	Expiry   int64  `json:"expiry"`
	Secure   bool   `json:"secure"`
	HttpOnly bool   `json:"httpOnly"`
	SameSite string `json:"sameSite"`
}

func (*Cookie) ToHttpCookie

func (c *Cookie) ToHttpCookie() *http.Cookie

type Cookies

type Cookies []Cookie

func (Cookies) ToHttpCookies

func (cs Cookies) ToHttpCookies() []*http.Cookie

type Param

type Param = string
const (
	PARAM_CMD                 Param = "cmd" // [CMD_REQUEST_GET] | [CMD_REQUEST_POST] ...
	PARAM_URL                 Param = "url"
	PARAM_SESSION             Param = "session"
	PARAM_SESSION_TTL_MINUTES Param = "session_ttl_minutes" // int
	PARAM_MAX_TIMEOUT         Param = "maxTimeout"          // int
	PARAM_COOKIES             Param = "cookies"
	PARAM_RETURN_ONLY_COOKIES Param = "returnOnlyCookies" // bool
	PARAM_RETURN_SCREENSHOT   Param = "returnScreenshot"  // bool
	PARAM_PROXY               Param = "proxy"
	PARAM_WAIT_IN_SECONDS     Param = "waitInSeconds" // int
	PARAM_POST_DATA           Param = "postData"      // string // application/x-www-form-urlencoded
)

type RespBase

type RespBase struct {
	Status         string `json:"status"`
	Message        string `json:"message"`
	StartTimestamp int64  `json:"startTimestamp"`
	EndTimestamp   int64  `json:"endTimestamp"`
	Version        string `json:"version"`
}

type Response

type Response struct {
	RespBase
	Session  string    `json:"session"`
	Sessions []string  `json:"sessions"` // sessions.list
	Solution *Solution `json:"solution"`
}

type Solution

type Solution struct {
	Url        string            `json:"url"`
	Status     int               `json:"status"`
	Cookies    Cookies           `json:"cookies"`
	UserAgent  string            `json:"userAgent"`
	Headers    map[string]string `json:"headers"`
	Response   string            `json:"response"`
	Screenshot string            `json:"screenshot"` // base64-encoded PNG
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL