fetcher

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MPL-2.0 Imports: 23 Imported by: ? 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

Index

Constants

View Source
const DefaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36"

DefaultUserAgent 默认 User-Agent(模拟 Chrome,不暴露工具标识)

View Source
const (
	// MaxBodySize 最大响应体大小 100MB
	MaxBodySize = 100 * 1024 * 1024
)

Variables

View Source
var ChromeProfile = browserProfiles[0]

ChromeProfile 固定 Chrome 画像(--no-random-tls、或用户自定义 UA 时使用)

View Source
var ErrBodyTooLarge = errors.New("response body exceeds max size limit")

ErrBodyTooLarge 响应体超过限制

Functions

func GetTempDir

func GetTempDir() string

GetTempDir 获取临时目录

func NormalizeOrigin

func NormalizeOrigin(url string) string

NormalizeOrigin 规范化 origin 部分(Windows 安全) https://test.com:8080 → https_test.com_8080

func NormalizePathForFile

func NormalizePathForFile(path string) string

NormalizePathForFile 规范化路径用于文件名(Windows 安全) /aa/bb/static/js/app.js → aa_bb_static_js_app.js

Types

type BrowserProfile added in v0.6.3

type BrowserProfile struct {
	Name          string
	HelloID       utls.ClientHelloID
	UserAgent     string
	SecChUA       string // 空 = 非 Chromium 内核,不发送 Sec-CH-* 系列头
	SecChPlatform string
}

BrowserProfile 一个「自洽」的浏览器画像:TLS 指纹与请求头(UA / Sec-CH-*) 同源。此前随机模式只随机 TLS 指纹、UA 恒为 Chrome,等于部分连接处于 「TLS 说 Safari、UA 说 Chrome」的自相矛盾状态而被 CDN/WAF 识别; 真实站点实测固定 Chrome 比随机多找回 10+ 个 JS 文件(svelte.dev 73→83)。

type CacheConfig

type CacheConfig struct {
	Enable  bool
	BaseDir string // 缓存根目录,如 /tmp/ejfkdev/dj
	// WriteEnabled 控制"写入"行为。--cache=false 时 Enable=false 但 WriteEnabled=true,
	// 即每次都走网络下载(不读缓存),但下载后仍保存到磁盘。
	WriteEnabled bool
	// OutputDir 非空时,所有 Save 操作额外写一份到此目录(不带域名层级)。
	// 用途: -o/--output 选项,将文件导出到用户指定目录。
	OutputDir string
}

CacheConfig 缓存配置

func (*CacheConfig) CanWrite added in v0.5.2

func (c *CacheConfig) CanWrite() bool

CanWrite 返回是否允许写入缓存(Enable 模式或 save-only 模式均允许)。 用于统一守卫所有 Save 操作:只要允许写入就执行。

func (*CacheConfig) GetCachePath

func (c *CacheConfig) GetCachePath(baseURL, subDir, urlPath string) string

GetCachePath 获取缓存文件路径 baseURL: https://test.com:8080/aa subDir: "js" 或 "source_map" urlPath: /static/js/app.js 返回: /tmp/ejfkdev/dj/https_test.com_8080_aa/js/static_js_app.js

func (*CacheConfig) GetCacheRoot

func (c *CacheConfig) GetCacheRoot(baseURL string) string

GetCacheRoot 获取缓存根目录 输入: https://test.com:8080/aa 输出: /tmp/ejfkdev/dj/https_test.com_8080_aa

func (*CacheConfig) GetSourcesDir added in v0.4.0

func (c *CacheConfig) GetSourcesDir(baseURL string) string

GetSourcesDir 获取还原源码的根目录路径

func (*CacheConfig) LoadFromCache added in v0.4.0

func (c *CacheConfig) LoadFromCache(baseURL, subDir, urlPath string) ([]byte, bool)

LoadFromCache 读取缓存内容(通用方法,按 subDir + urlPath 定位)。 路径计算与 SaveToCache 完全一致(复用 GetCachePath),确保读写路径对应。 文件不存在、为空、或读取失败时返回 (nil, false)。

func (*CacheConfig) LoadHTML added in v0.4.0

func (c *CacheConfig) LoadHTML(baseURL string) ([]byte, bool)

LoadHTML 读取缓存的起始页 HTML(固定文件名 web.html)。 用于第二次运行同一站点时跳过起始页的网络下载。

func (*CacheConfig) LoadMetadata added in v0.4.0

func (c *CacheConfig) LoadMetadata(baseURL string) ([]byte, bool)

LoadMetadata 读取缓存的站点元数据 meta.json。 返回原始 JSON 字节;文件不存在或读取失败返回 (nil, false)。 用于第二次运行时从缓存恢复已发现的 JS URL 列表,跳过网络发现流程。

func (*CacheConfig) LoadSourceMap added in v0.4.0

func (c *CacheConfig) LoadSourceMap(baseURL, jsRelPath string) ([]byte, bool)

LoadSourceMap 读取缓存的 source map 内容。 jsRelPath 是关联 JS 的相对路径(与 SaveToCache 写入时的 urlPath 参数一致, 即 getRelativePath(jsURL, baseURL) 的结果,带前导 /)。 内部会拼接 ".map" 后缀并经 NormalizePathForFile 拍平,与 saveSourceMapToCache 写入路径一致。

func (*CacheConfig) SaveDataURI

func (c *CacheConfig) SaveDataURI(baseURL, urlPath, dataURI string) (string, []byte, error)

SaveDataURI 保存 data URI 内容到缓存 dataURI: data:application/json;base64,... 或 data:application/json,... baseURL: 来源页面的 base URL urlPath: 来源 JS 的路径 返回保存的路径和解码后的原始内容(content 用于后续 source map 还原)

func (*CacheConfig) SaveMetadata

func (c *CacheConfig) SaveMetadata(baseURL, urlPath string, metadata []byte) error

SaveMetadata 保存元数据 JSON 到缓存 如果 urlPath 为空,保存到根目录的 meta.json

func (*CacheConfig) SaveSourceFile added in v0.4.0

func (c *CacheConfig) SaveSourceFile(baseURL, sourcePath string, content []byte) (string, error)

SaveSourceFile 保存还原出的原始源码文件到缓存,保留 sources 路径的目录层级。

与 SaveToCache 不同,本方法不拍平路径,而是按 sourcePath 的 / 分隔符创建子目录, 以还原原始项目结构。路径安全由调用方(sourcemap.NormalizeSourcePath)保证, 此处再做一次 filepath.Clean + ".." 段校验作为最后防线。

baseURL: 站点 URL(用于定位缓存根目录) sourcePath: 规范化后的源码相对路径(如 src/App.jsx),必须是相对路径 content: 文件内容 返回写入的完整文件路径

func (*CacheConfig) SaveToCache

func (c *CacheConfig) SaveToCache(baseURL, subDir, urlPath string, content []byte) error

SaveToCache 保存内容到缓存

type FetchResult

type FetchResult struct {
	Content     []byte
	StatusCode  int
	ContentType string
	Headers     http.Header // HTTP 响应头
	FinalURL    string      // 重定向后的最终 URL(与原始 URL 相同时为空)
}

FetchResult 包含内容和状态码

type Fetcher

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

Fetcher HTTP 下载器

func NewFetcher

func NewFetcher() *Fetcher

NewFetcher 创建下载器(使用默认配置)

func NewFetcherWithConfig

func NewFetcherWithConfig(cfg FetcherConfig) (*Fetcher, error)

NewFetcherWithConfig 创建下载器,支持 uTLS 指纹伪装、Cookie Jar 和代理

func (*Fetcher) Fetch

func (f *Fetcher) Fetch(rawURL string) ([]byte, error)

Fetch 获取 URL 内容

func (*Fetcher) FetchWithHeaders added in v0.3.1

func (f *Fetcher) FetchWithHeaders(rawURL string, headers map[string]string) (*FetchResult, error)

FetchWithHeaders 使用自定义请求头获取 URL 内容

func (*Fetcher) FetchWithStatus

func (f *Fetcher) FetchWithStatus(rawURL string) (*FetchResult, error)

FetchWithStatus 获取 URL 内容和状态码

func (*Fetcher) FetchWithStatusHead

func (f *Fetcher) FetchWithStatusHead(rawURL string) (*FetchResult, error)

FetchWithStatusHead 使用 HEAD 请求探测 URL 是否存在。 与 GET 走同一浏览器画像(此前 HEAD 用 Go 默认请求头,容易被 WAF 单独拦)。

func (*Fetcher) SetConcurrency added in v0.5.21

func (f *Fetcher) SetConcurrency(n int)

SetConcurrency 设置全局 HTTP 并发上限(所有请求共享)。 n<1 时取 1,n>256 时取 256。

func (*Fetcher) SetCookies added in v0.3.0

func (f *Fetcher) SetCookies(targetURL string, cookies []*http.Cookie) error

SetCookies 向 Fetcher 的 Cookie Jar 中注入 cookie

func (*Fetcher) SetExtraHeaders added in v0.3.5

func (f *Fetcher) SetExtraHeaders(headers map[string]string)

SetExtraHeaders 设置自定义请求头(用于 --header/-H) 多次调用会合并;同 key 后设置的值会覆盖先前的值。 自定义头在浏览器默认头之后应用,因此可以覆盖 User-Agent、Accept 等。

func (*Fetcher) SetUserAgent

func (f *Fetcher) SetUserAgent(ua string)

SetUserAgent 设置自定义 User-Agent

type FetcherConfig added in v0.3.0

type FetcherConfig struct {
	Proxy          string             // 代理 URL (http/https/socks5)
	UseUTLS        bool               // 启用 uTLS TLS 指纹伪装
	TLSFingerprint TLSFingerprintMode // uTLS 指纹模式(随机/Chrome)
	RequestTimeout time.Duration      // 单个 HTTP 请求超时(默认 10s)
}

FetcherConfig Fetcher 配置

type TLSFingerprintMode added in v0.5.5

type TLSFingerprintMode int

TLSFingerprintMode 控制 uTLS 指纹模式

const (
	// TLSFingerprintChrome 固定 Chrome 指纹与配套请求头(默认)。
	// 实测:随机指纹在 CDN/WAF 站点上会被按 JA3 拦截(真实站点 arco.design
	// 随机得 11 个 JS、固定 Chrome 得 469 个),因此默认即 Chrome 仿真。
	TLSFingerprintChrome TLSFingerprintMode = iota
	// TLSFingerprintRandom 每次请求随机挑选一个自洽浏览器画像(--random-tls)
	TLSFingerprintRandom
)

Jump to

Keyboard shortcuts

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