extractor

package
v0.5.9 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MPL-2.0 Imports: 17 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

This section is empty.

Variables

This section is empty.

Functions

func DecodeContent added in v0.5.0

func DecodeContent(content string) string

DecodeContent 对文本进行常见编码的"机械式"还原,目的是让被编码的 JS URL 字符串(如 "https:\/\/foo.com\/bar.js"、"%2Fpath%2Fchunk.js"、"\u003Cscript src=...") 变成普通可被 URL 正则直接命中的字符串。

还原的编码类型(顺序很重要:先做 URL 编码 → JS 转义 → Unicode → HTML 实体):

  1. URL 编码: %xx → 字符(仅在 %xx 出现频率较高时整段 QueryUnescape,避免误改哈希)
  2. JS 字符串字面量转义: \/ \" \' \\ \n \r \t
  3. Unicode 转义: \uXXXX → 字符(BMP 范围)
  4. HTML 实体: < > & " ' / 等

注意:

  • 本函数不解释 JS 语义(不解析字符串拼接、不执行函数)
  • 输入可能是 JS 字符串字面量、HTML 文本、或其他文本,统一做"最坏情况"还原
  • 如果内容不含任何目标编码模式,函数应极快返回(避免无谓的字符串分配)
  • 不抛错(编码可能不完整),失败时静默跳过该步骤

典型调用方:UniversalURLPlugin 等需要在做正则匹配前对原始 JS/HTML 文本 做"机械式"字符还原的下游插件。

func ExpandComboLoader added in v0.5.6

func ExpandComboLoader(rawURL string) []string

ExpandComboLoader 展开 CDN combo-loader URL(含 `??` 语法)。 支持 WordPress 和 阿里云 CDN 的 combo-loader 格式:

如果不含 `??` 或展开失败,返回 url(原样)。

func FormatOutput

func FormatOutput(format OutputFormat, result *OutputResult) string

FormatOutput 格式化输出

func FormatTextSummary added in v0.4.0

func FormatTextSummary(result *OutputResult) string

FormatTextSummary 生成 text 格式的末尾汇总信息。 text 模式下 JS URL 是实时流式打印的,此函数用于在末尾追加统计摘要, 让用户一眼看到 source map 和源码还原情况。

func GetBaseURL

func GetBaseURL(u string) string

GetBaseURL 获取 URL 的基础部分 (scheme://host)

func GetDirFromURL

func GetDirFromURL(u string) string

GetDirFromURL 获取 URL 的目录部分

func IsAbsoluteURL

func IsAbsoluteURL(s string) bool

IsAbsoluteURL 判断是否为完整 URL

func NormalizeURL

func NormalizeURL(rawURL string) string

NormalizeURL 规范化 URL,去除双斜杠、清理路径、清除 fragment

func ResolveRelativePath

func ResolveRelativePath(baseURL, relative string) string

ResolveRelativePath 解析相对路径为绝对路径

Types

type AnalyzeInput

type AnalyzeInput struct {
	SourceURL   string      // 当前内容来源 URL,绝对路径
	ContentType ContentType // "html" | "js" | "json"
	Content     []byte      // 资源内容
	Headers     http.Header // HTTP 响应头(用于 source map 等场景)
}

AnalyzeInput 插件分析的上下文输入

type CacheDirs

type CacheDirs struct {
	JS        string `json:"js"`
	SourceMap string `json:"sourceMap,omitempty"`
	Source    string `json:"source,omitempty"`
	HTML      string `json:"html,omitempty"`
}

CacheDirs 缓存目录

type CacheDirsMetadata added in v0.4.0

type CacheDirsMetadata struct {
	// HTML 站点 HTML 缓存文件路径
	HTML string `json:"html,omitempty"`
	// JS JS 文件缓存目录
	JS string `json:"js,omitempty"`
	// SourceMap source map 文件缓存目录
	SourceMap string `json:"source_map,omitempty"`
	// Sources 还原出的原始源码目录
	Sources string `json:"sources,omitempty"`
}

CacheDirsMetadata 缓存目录元数据,记录各资源类型在磁盘上的落盘位置

type ContentType

type ContentType string

ContentType 类型安全常量

const (
	ContentTypeHTML   ContentType = "html"
	ContentTypeJS     ContentType = "js"
	ContentTypeJSON   ContentType = "json"
	ContentTypeFlight ContentType = "flight" // Next.js RSC Flight 数据
)

type DiscoveredJS

type DiscoveredJS struct {
	// URL 发现的 JS URL
	URL string
	// FromURL 发现该 JS 的来源 URL(分析的是哪个 HTML/JS 文件)
	FromURL string
	// FromPlugin 发现该 JS 的插件名称
	FromPlugin string
	// IsInline 是否来自内联脚本
	IsInline bool
}

DiscoveredJS 发现的 JS 详细信息

type InlineScript

type InlineScript struct {
	SourceURL string // 来源 HTML 的 URL
	Index     int    // 脚本在 HTML 中的顺序索引
	Content   []byte // 脚本内容
}

InlineScript 行内脚本内容

type Intermediate

type Intermediate struct {
	URL  string      // 资源的绝对 URL
	Type ContentType // 期望的资源类型
}

Intermediate 发现的中间配置资源

type JSMetadata

type JSMetadata struct {
	// URL 完整的 JS URL
	URL string `json:"url"`
	// LocalPath 本地文件路径
	LocalPath string `json:"local_path"`
	// SourceURL 发现该 JS 的来源 URL
	SourceURL string `json:"source_url"`
	// IsInline 是否来自内联脚本
	IsInline bool `json:"is_inline"`
	// SourceMapURL 关联的 source map URL(如果有)
	SourceMapURL string `json:"source_map_url,omitempty"`
	// SourceMapPath 本地 source map 文件路径(如果有)
	SourceMapPath string `json:"source_map_path,omitempty"`
	// SourcesRestored 是否已从 source map 还原出原始源码
	SourcesRestored bool `json:"sources_restored,omitempty"`
	// RestoredSourceCount 从该 JS 的 source map 还原出的源码文件数量
	RestoredSourceCount int `json:"restored_source_count,omitempty"`
	// RestoredSources 从该 JS 的 source map 还原出的源码文件相对路径列表
	// 路径相对于 meta.json 所在目录(即缓存根目录)
	RestoredSources []string `json:"restored_sources,omitempty"`
	// FromPlugin 发现该 JS 的插件名称
	FromPlugin string `json:"from_plugin,omitempty"`
	// DiscoveredAt 发现时间(Unix timestamp)
	DiscoveredAt int64 `json:"discovered_at"`
}

JSMetadata JS 文件的元数据信息

type KnowledgeBase

type KnowledgeBase struct {

	// PublicPaths 发现的 publicPath 列表
	PublicPaths []string

	// PrependURLs 用于拼接在文件名前面的 URL 列表
	PrependURLs []string

	// SeenURLs 已尝试过的 URL(无论成功/404)
	SeenURLs map[string]bool

	// SeenFragments 已探测过的 JS 路径片段
	SeenFragments map[string]bool

	// KnownPaths 已知存在的 JS 路径(用于优先拼接)
	KnownPaths []string
	// contains filtered or unexported fields
}

KnowledgeBase 全局知识库

func GetKnowledgeFromContext

func GetKnowledgeFromContext(ctx context.Context) *KnowledgeBase

GetKnowledgeFromContext 从 context 中获取 KnowledgeBase

func NewKnowledgeBase

func NewKnowledgeBase() *KnowledgeBase

NewKnowledgeBase 创建知识库

func (*KnowledgeBase) AddKnownPath

func (kb *KnowledgeBase) AddKnownPath(paths ...string)

AddKnownPath 添加已知存在的 JS 路径

func (*KnowledgeBase) AddPrependURL

func (kb *KnowledgeBase) AddPrependURL(urls ...string)

AddPrependURL 添加用于拼接的 URL

func (*KnowledgeBase) AddPublicPath

func (kb *KnowledgeBase) AddPublicPath(paths ...string)

AddPublicPath 添加 publicPath

func (*KnowledgeBase) GetKnownPaths

func (kb *KnowledgeBase) GetKnownPaths() []string

GetKnownPaths 获取所有已知路径

func (*KnowledgeBase) GetPrependURLs

func (kb *KnowledgeBase) GetPrependURLs() []string

GetPrependURLs 获取所有用于拼接的 URL

func (*KnowledgeBase) GetPublicPaths

func (kb *KnowledgeBase) GetPublicPaths() []string

GetPublicPaths 获取所有 publicPath

func (*KnowledgeBase) IsSeenFragment

func (kb *KnowledgeBase) IsSeenFragment(fragment string) bool

IsSeenFragment 检查片段是否已探测

func (*KnowledgeBase) IsSeenURL

func (kb *KnowledgeBase) IsSeenURL(url string) bool

IsSeenURL 检查 URL 是否已尝试

func (*KnowledgeBase) JSHasSourceMap

func (kb *KnowledgeBase) JSHasSourceMap(url string) bool

JSHasSourceMap 检查 JS 是否有 source map

func (*KnowledgeBase) MarkSeenFragment

func (kb *KnowledgeBase) MarkSeenFragment(fragment string)

MarkSeenFragment 标记片段已探测

func (*KnowledgeBase) MarkSeenURL

func (kb *KnowledgeBase) MarkSeenURL(url string)

MarkSeenURL 标记 URL 已尝试

func (*KnowledgeBase) SetJSHasSourceMap

func (kb *KnowledgeBase) SetJSHasSourceMap(url string, has bool)

SetJSHasSourceMap 标记 JS 是否有 source map

type OutputFormat

type OutputFormat string

OutputFormat 输出格式

const (
	FormatText OutputFormat = "text"
	FormatJSON OutputFormat = "json"
	FormatMD   OutputFormat = "md"
)

type OutputResult

type OutputResult struct {
	Summary    Summary    `json:"summary"`
	JSURLs     []string   `json:"jsURLs"`
	CacheBase  string     `json:"cacheBase,omitempty"`
	CacheDirs  *CacheDirs `json:"cacheDirs,omitempty"`
	OutputDir  string     `json:"outputDir,omitempty"`
	OutputDirs *CacheDirs `json:"outputDirs,omitempty"`
}

OutputResult 输出结果结构

type Pipeline

type Pipeline struct {

	// 调试模式
	Debug bool
	// contains filtered or unexported fields
}

Pipeline 主流程调度器

func NewPipeline

func NewPipeline(reg *PluginRegistry) *Pipeline

NewPipeline 创建 Pipeline

func (*Pipeline) GetFoundCh

func (p *Pipeline) GetFoundCh() <-chan string

GetFoundCh 获取发现 JS URL 的 channel

func (*Pipeline) GetKnowledge

func (p *Pipeline) GetKnowledge() *KnowledgeBase

GetKnowledge 获取知识库(用于测试)

func (*Pipeline) GetOutputResult

func (p *Pipeline) GetOutputResult() *OutputResult

GetOutputResult 获取格式化输出结果

func (*Pipeline) Run

func (p *Pipeline) Run(ctx context.Context, startURL string) ([]string, error)

Run 执行主流程

func (*Pipeline) SetBaseURL

func (p *Pipeline) SetBaseURL(url string)

SetBaseURL 设置起始 URL

func (*Pipeline) SetBrowserCookies added in v0.3.0

func (p *Pipeline) SetBrowserCookies(targetURL string, cookies []*http.Cookie) error

SetBrowserCookies 注入浏览器 cookie 到 Fetcher

func (*Pipeline) SetCacheConfig

func (p *Pipeline) SetCacheConfig(cfg *fetcher.CacheConfig)

SetCacheConfig 设置缓存配置

func (*Pipeline) SetExtraHeaders added in v0.3.5

func (p *Pipeline) SetExtraHeaders(headers map[string]string)

SetExtraHeaders 设置自定义 HTTP 请求头

func (*Pipeline) SetFetcherConfig

func (p *Pipeline) SetFetcherConfig(proxy, userAgent string, fpMode fetcher.TLSFingerprintMode, reqTimeout time.Duration)

SetFetcherConfig 设置 Fetcher 配置(代理、User-Agent、TLS 指纹模式和请求超时)

type Plugin

type Plugin interface {
	// Name 插件的唯一标识名称
	Name() string

	// Precheck 快速过滤,返回 true 才执行 Analyze
	Precheck(ctx context.Context, input *AnalyzeInput) bool

	// Analyze 正式分析逻辑
	Analyze(ctx context.Context, input *AnalyzeInput) (*Result, error)
}

Plugin 核心插件接口

type PluginRegistry

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

PluginRegistry 插件注册中心

func NewPluginRegistry

func NewPluginRegistry() *PluginRegistry

NewPluginRegistry 创建插件注册中心

func (*PluginRegistry) GetAll

func (r *PluginRegistry) GetAll() []Plugin

GetAll 获取所有插件

func (*PluginRegistry) Register

func (r *PluginRegistry) Register(p Plugin)

Register 注册插件

type RSCProbe added in v0.3.1

type RSCProbe struct {
	URL     string            // 要发送 RSC 请求的 URL
	Headers map[string]string // 请求头(如 RSC: 1)
}

RSCProbe 需要发送 RSC 请求的探测目标

type Result

type Result struct {
	// FromPlugin 来源插件名称
	FromPlugin string

	// URLs 明确且完整的 JS URL,直接加入下载队列
	URLs []DiscoveredJS

	// ProbeTargets 提取到的路径片段,待主流程探测
	ProbeTargets []DiscoveredJS

	// PublicPaths 发现的 publicPath 路径
	PublicPaths []string

	// PrependURLs 用于拼接在文件名前面的 URL(通常是 protocol-relative URL)
	PrependURLs []string

	// Intermediates 需要主流程去下载并重新分发的配置文件
	Intermediates []Intermediate

	// InlineScripts 行内脚本内容,需要主流程分析
	InlineScripts []InlineScript

	// RSCProbes 需要发送 RSC 请求的探测目标(Next.js Flight 数据)
	RSCProbes []RSCProbe
}

Result 插件分析的返回结果

type SiteMetadata

type SiteMetadata struct {
	// URLs 所有发现的 JS URL 信息
	URLs []JSMetadata `json:"urls"`
	// PrependURLs 探测时使用的 PrependURLs(拼接用的 CDN 前缀)
	PrependURLs []string `json:"prepend_urls"`
	// PublicPaths 探测时使用的 PublicPaths
	PublicPaths []string `json:"public_paths"`
	// CacheDirs 各资源类型的缓存目录路径
	CacheDirs *CacheDirsMetadata `json:"cache_dirs,omitempty"`
	// DiscoveredAt 探测开始时间
	DiscoveredAt int64 `json:"discovered_at"`
}

SiteMetadata 站点元数据(所有信息保存到一个 JSON 文件)

type Summary

type Summary struct {
	JSCount        int `json:"jsCount"`
	SourceMapCount int `json:"sourceMapCount,omitempty"`
	SourceCount    int `json:"sourceCount,omitempty"`
}

Summary 统计摘要

func (Summary) HasSourceMap added in v0.4.0

func (s Summary) HasSourceMap() bool

HasSourceMap 是否发现 source map

func (Summary) SourcesRestored added in v0.4.0

func (s Summary) SourcesRestored() bool

SourcesRestored 是否成功还原出源码

Jump to

Keyboard shortcuts

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