Documentation
¶
Index ¶
- Constants
- Variables
- func GetTempDir() string
- func NormalizeOrigin(url string) string
- func NormalizePathForFile(path string) string
- type CacheConfig
- func (c *CacheConfig) GetCachePath(baseURL, subDir, urlPath string) string
- func (c *CacheConfig) GetCacheRoot(baseURL string) string
- func (c *CacheConfig) GetSourcesDir(baseURL string) string
- func (c *CacheConfig) LoadFromCache(baseURL, subDir, urlPath string) ([]byte, bool)
- func (c *CacheConfig) LoadHTML(baseURL string) ([]byte, bool)
- func (c *CacheConfig) LoadMetadata(baseURL string) ([]byte, bool)
- func (c *CacheConfig) LoadSourceMap(baseURL, jsRelPath string) ([]byte, bool)
- func (c *CacheConfig) SaveDataURI(baseURL, urlPath, dataURI string) (string, []byte, error)
- func (c *CacheConfig) SaveMetadata(baseURL, urlPath string, metadata []byte) error
- func (c *CacheConfig) SaveSourceFile(baseURL, sourcePath string, content []byte) (string, error)
- func (c *CacheConfig) SaveToCache(baseURL, subDir, urlPath string, content []byte) error
- type FetchResult
- type Fetcher
- func (f *Fetcher) Fetch(rawURL string) ([]byte, error)
- func (f *Fetcher) FetchWithHeaders(rawURL string, headers map[string]string) (*FetchResult, error)
- func (f *Fetcher) FetchWithStatus(rawURL string) (*FetchResult, error)
- func (f *Fetcher) FetchWithStatusHead(rawURL string) (*FetchResult, error)
- func (f *Fetcher) SetCookies(targetURL string, cookies []*http.Cookie) error
- func (f *Fetcher) SetExtraHeaders(headers map[string]string)
- func (f *Fetcher) SetUserAgent(ua string)
- type FetcherConfig
Constants ¶
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,不暴露工具标识)
const (
// MaxBodySize 最大响应体大小 100MB
MaxBodySize = 100 * 1024 * 1024
)
Variables ¶
var ErrBodyTooLarge = errors.New("response body exceeds max size limit")
ErrBodyTooLarge 响应体超过限制
Functions ¶
func NormalizeOrigin ¶
NormalizeOrigin 规范化 origin 部分(Windows 安全) https://test.com:8080 → https_test.com_8080
func NormalizePathForFile ¶
NormalizePathForFile 规范化路径用于文件名(Windows 安全) /aa/bb/static/js/app.js → aa_bb_static_js_app.js
Types ¶
type CacheConfig ¶
CacheConfig 缓存配置
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 响应头
}
FetchResult 包含内容和状态码
type Fetcher ¶
type Fetcher struct {
// contains filtered or unexported fields
}
Fetcher HTTP 下载器
func NewFetcherWithConfig ¶
func NewFetcherWithConfig(cfg FetcherConfig) (*Fetcher, error)
NewFetcherWithConfig 创建下载器,支持 uTLS 指纹伪装、Cookie Jar 和代理
func (*Fetcher) FetchWithHeaders ¶ added in v0.3.1
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 是否存在
func (*Fetcher) SetCookies ¶ added in v0.3.0
SetCookies 向 Fetcher 的 Cookie Jar 中注入 cookie
func (*Fetcher) SetExtraHeaders ¶ added in v0.3.5
SetExtraHeaders 设置自定义请求头(用于 --header/-H) 多次调用会合并;同 key 后设置的值会覆盖先前的值。 自定义头在浏览器默认头之后应用,因此可以覆盖 User-Agent、Accept 等。
func (*Fetcher) SetUserAgent ¶
SetUserAgent 设置自定义 User-Agent
type FetcherConfig ¶ added in v0.3.0
type FetcherConfig struct {
Proxy string // 代理 URL (http/https/socks5)
UseUTLS bool // 启用 uTLS TLS 指纹伪装
}
FetcherConfig Fetcher 配置