extractor

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: MPL-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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"`
}

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)

SetFetcherConfig 设置 Fetcher 配置(代理和 User-Agent)

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