app

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package app 提供 twist 的核心业务逻辑:浏览器管理、CDP 连接、规则引擎和请求拦截。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	Type         string            `json:"type"`
	Name         string            `json:"name,omitempty"`
	Value        any               `json:"value,omitempty"`
	Search       string            `json:"search,omitempty"`
	Replace      string            `json:"replace,omitempty"`
	ReplaceAll   bool              `json:"replaceAll,omitempty"`
	StatusCode   int               `json:"statusCode,omitempty"`
	Headers      map[string]string `json:"headers,omitempty"`
	Body         string            `json:"body,omitempty"`
	BodyEncoding string            `json:"bodyEncoding,omitempty"`
	Encoding     string            `json:"encoding,omitempty"`
	Patches      []JSONPatch       `json:"patches,omitempty"`
	Selector     string            `json:"selector,omitempty"`
}

Action 执行行为,支持 18 种类型(请求/响应/通用)。

func (*Action) UnmarshalJSON

func (a *Action) UnmarshalJSON(data []byte) error

type App

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

App 应用主结构,串联浏览器、CDP、规则引擎和拦截器的完整生命周期。

func New

func New(opts Options) *App

func (*App) Run

func (a *App) Run(ctx context.Context) error

Run 启动应用主流程:信号监听 → 列出目标或启动拦截。

func (*App) Shutdown

func (a *App) Shutdown()

Shutdown 清理信号监听资源。

type Browser

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

Browser 管理浏览器进程的启动、端口检测和停止。

func NewBrowser

func NewBrowser(launch bool, port int) *Browser

func (*Browser) IsLaunched

func (b *Browser) IsLaunched() bool

func (*Browser) Start

func (b *Browser) Start(ctx context.Context, browserType string, args []string, url string) error

Start 启动浏览器进程,非 launch 模式直接返回。 启动前检测端口占用,自动在常见路径查找可执行文件。

func (*Browser) Stop

func (b *Browser) Stop() error

Stop 终止浏览器进程。

type CDP

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

CDP 管理浏览器和页面级 WebSocket 连接,封装 CDP 协议操作。 浏览器级连接用于管理标签页,页面级连接用于拦截具体页面。

func NewCDP

func NewCDP(host string, port int, timeout int, verbose bool) *CDP

func (*CDP) AttachToTarget

func (c *CDP) AttachToTarget(ctx context.Context, targetID string) error

AttachToTarget 连接到指定标签页的 WebSocket,建立页面级连接。

func (*CDP) Close

func (c *CDP) Close() error

func (*CDP) CloseBrowser

func (c *CDP) CloseBrowser() error

func (*CDP) Connect

func (c *CDP) Connect(ctx context.Context) error

Connect 建立浏览器级 CDP 连接,等待浏览器就绪后 WebSocket 握手。

func (*CDP) DisableFetch

func (c *CDP) DisableFetch(ctx context.Context) error

func (*CDP) EnableFetch

func (c *CDP) EnableFetch(ctx context.Context) (fetch.RequestPausedClient, error)

func (*CDP) EnableNetwork

func (c *CDP) EnableNetwork(ctx context.Context) error

func (*CDP) ListTargets

func (c *CDP) ListTargets(ctx context.Context) ([]CDPTarget, error)

ListTargets 通过 HTTP /json 端点获取所有可调试目标。

func (*CDP) NavigateTo

func (c *CDP) NavigateTo(ctx context.Context, targetURL string) error

func (*CDP) NewTab

func (c *CDP) NewTab(ctx context.Context, targetURL string) (*CDPTarget, error)

func (*CDP) TargetClient

func (c *CDP) TargetClient() *cdp.Client

type CDPTarget

type CDPTarget struct {
	ID    string `json:"id"`
	Title string `json:"title"`
	URL   string `json:"url"`
	Type  string `json:"type"`
}

CDPTarget 浏览器标签页目标信息。

type Condition

type Condition struct {
	Type    string   `json:"type"`
	Name    string   `json:"name,omitempty"`
	Value   string   `json:"value,omitempty"`
	Values  []string `json:"values,omitempty"`
	Pattern string   `json:"pattern,omitempty"`
	Path    string   `json:"path,omitempty"`
}

Condition 匹配条件,支持 25 种类型(URL/方法/Header/Query/Cookie/Body)。

func (*Condition) UnmarshalJSON

func (c *Condition) UnmarshalJSON(data []byte) error

type Config

type Config struct {
	ID          string         `json:"id"`
	Name        string         `json:"name"`
	Version     string         `json:"version"`
	Description string         `json:"description,omitempty"`
	Settings    map[string]any `json:"settings,omitempty"`
	Rules       []Rule         `json:"rules"`
}

Config 规则配置文件根对象。

func LoadConfig

func LoadConfig(configFile string, configData []byte) (*Config, error)

LoadConfig 加载规则配置,支持文件路径和 []byte 数据两种来源。

type Intercept

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

Intercept 拦截引擎:Fetch 域事件循环、worker 池、规则匹配和 action 执行。

func NewIntercept

func NewIntercept(cdp *CDP, config *Config) *Intercept

func (*Intercept) Start

func (i *Intercept) Start(ctx context.Context) error

Start 启动拦截循环:开启 Network + Fetch 域,进入事件接收。

type JSONPatch

type JSONPatch struct {
	Op    string `json:"op"`
	Path  string `json:"path"`
	From  string `json:"from,omitempty"`
	Value any    `json:"value,omitempty"`
}

type Match

type Match struct {
	AllOf []Condition `json:"allOf,omitempty"`
	AnyOf []Condition `json:"anyOf,omitempty"`
}

type Options

type Options struct {
	Host          string
	Port          int
	Launch        bool
	LaunchBrowser string
	LaunchArgs    []string
	URL           string
	ConfigFile    string
	ConfigData    []byte
	ListTargets   bool
	Target        string
	Verbose       bool
	Timeout       int
}

Options 命令行解析后的运行参数。

type Rule

type Rule struct {
	ID       string   `json:"id"`
	Name     string   `json:"name"`
	Enabled  bool     `json:"enabled"`
	Priority int      `json:"priority"`
	Stage    string   `json:"stage"`
	Match    Match    `json:"match"`
	Actions  []Action `json:"actions"`
}

Rule 单条拦截规则,包含匹配条件和执行行为。

type Target

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

Target 标签页选择策略:ID 精确匹配 → 新建标签 → 首个 page 类型。

func NewTarget

func NewTarget(cdp *CDP) *Target

func (*Target) Select

func (t *Target) Select(ctx context.Context, targetID string, url string) (*CDPTarget, error)

Select 按 targetID/url/默认三种策略选择拦截目标。

Jump to

Keyboard shortcuts

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