api

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package api 提供 FANBOX 的认证请求传输、Cloudflare challenge 恢复和资源 locator 缓存。

本包不承载内容语义:路由与响应映射由 sdk/fanbox 持有。

包的组成:Client 由 New 构造,持有 cookie、传输和 solver 状态;Options 与 FlareSolverrOptions 是配置,Locator 是已校验的媒体地址,MediaRequest 约束 资源读取可用的条件头;protocol.go、identity.go、resource.go 提供校验与身份解析。

Index

Constants

View Source
const (
	// WebBaseURL 指定 FANBOX 网页入口。
	WebBaseURL = "https://www.fanbox.cc/"
	// APIBaseURL 指定 FANBOX API 入口;sdk/fanbox 用它拼接 api.go 中的路由常量。
	APIBaseURL = "https://api.fanbox.cc/"
	// PostListPageLimit 指定 FANBOX 列表路由使用的 upstream 页大小参数,续页仍由服务端
	// 返回的 URL 决定。
	PostListPageLimit = 10
)
View Source
const (
	PathCreatorGet           = "creator.get"
	PathCreatorListFollowing = "creator.listFollowing"
	PathPlanListSupporting   = "plan.listSupporting"
	PathTagGetFeatured       = "tag.getFeatured"
	PathPostInfo             = "post.info"
	PathPostListHome         = "post.listHome"
	PathPostListCreator      = "post.listCreator"
	PathPostListTagged       = "post.listTagged"
	PathPostListSupporting   = "post.listSupporting"
)

PathCreatorGet 等常量是 FANBOX API 路径,相对 APIBaseURL。

View Source
const (
	ParamCreatorID = "creatorId"
	ParamPostID    = "postId"
	ParamTag       = "tag"
	ParamLimit     = "limit"
)

ParamCreatorID 等常量是 FANBOX query 参数名。

View Source
const DownloadsHost = "downloads.fanbox.cc"

DownloadsHost 标识需要 FANBOXSESSID Cookie 的一手 FANBOX 附件主机。该主机的资源带 凭据,必须经 OpenResource 打开;Pixiv CDN 与其他允许的媒体主机是公开链接。

View Source
const Referer = WebBaseURL

Referer 保存 FANBOX 媒体请求需要的非 secret Referer。

Variables

View Source
var (
	// ErrForbidden 表示 FANBOX 拒绝访问(非 Cloudflare challenge)。
	ErrForbidden = errors.New("fanbox: forbidden")
	// ErrChallenge 表示 403 响应体或 header 表明触发了 Cloudflare challenge。
	ErrChallenge = errors.New("fanbox: challenge required")
	// ErrNotAuthenticated 表示会话缺失或过期(401)。
	ErrNotAuthenticated = errors.New("fanbox: not authenticated")
	// ErrInvalidOption 表示显式 FANBOX 连接选项格式错误;公开 SDK 把它映射为
	// InvalidArgument,且不回显原值。
	ErrInvalidOption = errors.New("fanbox: invalid option")
	// ErrMalformedResponse 表示 API JSON 或身份 metadata 无法解析。
	ErrMalformedResponse = errors.New("fanbox: malformed response")
)

ErrForbidden 等 sentinel 是传输层的分类错误;公开 SDK 层依据它们做进一步分类。

View Source
var (
	// ErrSolverUnavailable 表示控制请求无法到达配置的 FlareSolverr 服务,区别于服务明确
	// 报告求解失败。
	ErrSolverUnavailable = errors.New("fanbox: FlareSolverr service unavailable")
	// ErrSolverFailed 表示 FlareSolverr 有响应,但没有解开匿名首页 challenge。
	ErrSolverFailed = errors.New("fanbox: FlareSolverr could not solve challenge")
	// ErrMalformedSolverResponse 表示响应不满足只接受 clearance 的状态契约。
	ErrMalformedSolverResponse = errors.New("fanbox: malformed FlareSolverr response")
)

Functions

func RequiresCredentials

func RequiresCredentials(rawURL string) bool

RequiresCredentials 报告 rawURL 是否指向需要凭据的 FANBOX 附件主机。解析失败时返回 false,让调用方把无法解析的 locator 当作公开链接,后续由主机 allowlist 拒绝。

func ValidateAPIURL

func ValidateAPIURL(rawURL string) error

ValidateAPIURL 按原生传输使用的 HTTPS 与 FANBOX 主机策略校验 URL;端点族把服务端续页 交给传输前先调用它。

func ValidateMediaURL

func ValidateMediaURL(rawURL string) error

ValidateMediaURL 只按媒体主机策略校验,不发起请求;sdk/fanbox 用它校验每个对外暴露 的 locator。

func ValidateURL

func ValidateURL(rawURL string) error

ValidateURL 只校验 FANBOX 资源 locator,不打开请求。

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 持有 FANBOX 传输、会话 Cookie、solver 状态和本次进程内的 locator 缓存。

格式化与日志输出不会带出 Cookie;只关闭由本 client 创建的 HTTP 连接。

func New

func New(sessionValue string, options Options) (*Client, error)

New 构造一次传输运行时,不发起网络请求;sessionValue 只接受 FANBOXSESSID 的值。

func (*Client) CacheLocator

func (c *Client) CacheLocator(ref string, locator Locator)

CacheLocator 记录 ref 对应的最新媒体 locator,供后续同 ref 的资源读取使用。

func (*Client) CachedLocator

func (c *Client) CachedLocator(ref string) (Locator, bool)

CachedLocator 返回本次 client 生命周期内为 ref 记录的 locator。

func (*Client) CloseIdleConnections

func (c *Client) CloseIdleConnections()

CloseIdleConnections 只关闭由本 client 创建的 HTTP 连接,注入的 client 由调用方管理。

func (*Client) CurrentUser

func (s *Client) CurrentUser(ctx context.Context) (Identity, error)

CurrentUser 用 FANBOX 首页验证会话,只解析账号导入与状态查询需要的安全身份字段。

func (*Client) GetJSON

func (c *Client) GetJSON(ctx context.Context, path string, query url.Values, target any) error

GetJSON 向 APIBaseURL 下的 path 发起一次带会话的请求,并把 JSON 解码到 target。

func (*Client) GetJSONURL

func (s *Client) GetJSONURL(ctx context.Context, endpoint string, target any) error

GetJSONURL 向服务端提供的续页 URL 发起一次带会话的请求;URL 在进入传输前先按 FANBOX 主机策略校验。

func (*Client) GoString

func (*Client) GoString() string

GoString 同 Client.String,用于 %#v。

func (*Client) OpenMedia

func (s *Client) OpenMedia(ctx context.Context, mediaURL string, request MediaRequest) (*http.Response, error)

OpenMedia 提供带条件头的窄资源请求面,不允许调用方传任意头或凭据。

func (*Client) String

func (*Client) String() string

String 只输出类型摘要,调试格式化不会打印 Cookie。

func (*Client) VerifiedUserID

func (c *Client) VerifiedUserID(ctx context.Context) (int64, error)

VerifiedUserID 返回已认证的 FANBOX user id,每个 client 只验证一次。id 非秘密, 可以绑定进按身份生成的 cursor;session cookie 不出传输层。

type FlareSolverrOptions

type FlareSolverrOptions struct {
	URL      string
	ProxyURL string
}

FlareSolverrOptions 保存可选 challenge 恢复路径使用的两个独立地址;solver 状态只留在 单个 Client 上。

type Identity

type Identity struct {
	UserID        int64
	DisplayName   string
	CreatorID     string
	CreatorStatus string
	IsCreator     bool
}

Identity 保存从 FANBOX 首页 metadata 解析出的非敏感账号摘要;session Cookie 不会进入 该值。

type Locator

type Locator struct {
	URL      string
	Filename string
}

Locator 保存已通过媒体策略校验的 URL 及其附件文件名。

type MediaRequest

type MediaRequest struct {
	Method          string
	Range           string
	IfNoneMatch     string
	IfModifiedSince string
	IfRange         string
}

MediaRequest 保存 FANBOX 资源读取允许的请求面:只有 method 与条件头,调用方无法向产品 传输注入任意头或凭据。

type Options

type Options struct {
	HTTPClient   *http.Client
	ProxyURL     string
	UserAgent    string
	FlareSolverr *FlareSolverrOptions
	// Logger 记录请求结果和挑战恢复决策;nil 时不输出。
	Logger *slog.Logger
}

Options 提供 FANBOX 原生传输与 solver 的显式配置;FlareSolverr 为 nil 时不启用 solver。

Jump to

Keyboard shortcuts

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