common

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddProxy

func AddProxy(
	sub *generatedConfig, autotest bool,
	lazy bool, clashType model.ClashType, proxies ...proxy.Proxy,
)

func AppendRules

func AppendRules(sub *generatedConfig, rules ...string)

AppendRules 在规则尾部追加,但如果尾部已有 MATCH,则保持 MATCH 仍然是最后一条。

func AppendYAMLSequenceValue added in v1.1.3

func AppendYAMLSequenceValue(node *yaml.Node, value any) error

AppendYAMLSequenceValue 向 sequence 节点末尾追加一个元素。

func AppenddRuleProvider

func AppenddRuleProvider(
	sub *generatedConfig, providerName string, group string,
)

func EnsureYAMLMappingPath added in v1.1.3

func EnsureYAMLMappingPath(doc *yaml.Node, path string) (*yaml.Node, error)

EnsureYAMLMappingPath 确保某个路径最终是 mapping(YAML 对象)节点。

func EnsureYAMLSequencePath added in v1.1.3

func EnsureYAMLSequencePath(doc *yaml.Node, path string) (*yaml.Node, error)

EnsureYAMLSequencePath 确保某个路径最终是 sequence(YAML 数组)节点。 不存在时会自动创建,已存在但类型不匹配时返回错误。

func FetchSubscriptionFromAPI

func FetchSubscriptionFromAPI(url string, userAgent string, retryTimes int) ([]byte, error)

func FetchSubscriptionUserInfo

func FetchSubscriptionUserInfo(url string, userAgent string, retryTimes int) (string, error)

func FindYAMLSequenceMappingByStringField added in v1.1.3

func FindYAMLSequenceMappingByStringField(node *yaml.Node, field string, value string) *yaml.Node

FindYAMLSequenceMappingByStringField 在 YAML 数组中查找一个对象元素, 要求该对象存在指定字段且字段值等于目标字符串。

例如在 proxy-groups 里按 name 查找:

  • name: 节点选择 type: select

func GetContryName

func GetContryName(countryKey string) string

func GetYAMLPath added in v1.1.3

func GetYAMLPath(doc *yaml.Node, path string) (*yaml.Node, error)

GetYAMLPath 按 a.b.c 这种点路径向下查找节点。 当前实现只支持 mapping 之间的逐层下钻,不处理数组索引路径。

func HasYAMLPath added in v1.1.3

func HasYAMLPath(doc *yaml.Node, path string) bool

HasYAMLPath 判断某个点路径是否存在。 这里仅关心“是否找到节点”,不关心节点具体类型。

func IsErrorCode

func IsErrorCode(err error, code ErrorCode) bool

IsErrorCode checks if an error has a specific error code

func LoadSubscription

func LoadSubscription(url string, refresh bool, userAgent string, cacheExpire int64, retryTimes int) ([]byte, error)

func LoadTemplate

func LoadTemplate(templateName string) ([]byte, error)

LoadTemplate 只读取运行目录下的 templates 目录,防止其他文件内容泄漏

func MKDir

func MKDir(dir string) error

func MergeSubAndTemplate

func MergeSubAndTemplate(temp *generatedConfig, sub *generatedConfig, igcg bool)

MergeSubAndTemplate 把“模板侧需要参与计算的最小叠加层”和“本项目生成结果”合并。 它只处理本项目关心的运行期结构,不负责最终 YAML 输出。

func MkEssentialDir

func MkEssentialDir() error

func ParseYAMLDocument added in v1.1.3

func ParseYAMLDocument(data []byte) (*yaml.Node, error)

ParseYAMLDocument 把原始 YAML 解析成 DocumentNode, 并确保根内容最终是一个可写入的 mapping 节点。

func PrependRuleProvider

func PrependRuleProvider(
	sub *generatedConfig, providerName string, group string,
)

func PrependRules

func PrependRules(sub *generatedConfig, rules ...string)

PrependRules 用于在规则头部插入新规则。 这通常对应用户显式要求 prepend 的场景。

func RandomString

func RandomString(length int) string

func Request

func Request(retryTimes int) *resty.Client

func SetYAMLMappingField added in v1.1.3

func SetYAMLMappingField(node *yaml.Node, key string, value any) error

SetYAMLMappingField 在一个 mapping 节点里设置单个字段。 它等价于“在当前对象上写 key: value”。

func SetYAMLPath added in v1.1.3

func SetYAMLPath(doc *yaml.Node, path string, value any) error

SetYAMLPath 按点路径写入一个值;不存在的中间层会自动补成 mapping。 例如 a.b.c=1 会在缺失时依次创建 a 和 b 两层对象节点。

Types

type BuiltSub added in v1.1.3

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

BuiltSub 保存最终输出所需的完整 YAML 树。

这里刻意不再保存整份 typed 配置副本: - root 是整个转换流程的最终产物 - 所有常规输出都直接从 root 序列化 - nodeList 模式也从 root 中提取 proxies,而不是依赖额外状态

func BuildSub

func BuildSub(clashType model.ClashType, query model.ConvertConfig, template string, cacheExpire int64, retryTimes int) (
	*BuiltSub, error,
)

BuildSub 是当前配置转换链路的核心入口。

当前设计分为三层: 1. templateDoc:模板 YAML 的完整语法树,也是最终输出真源 2. generatedConfig:本项目运行期最小叠加层,只保存参与业务计算的字段 3. proxy.Proxy:节点解析后的 typed 模型,用于过滤、去重、重命名和输出

这个函数的目标不是“重建一整份 mihomo 配置”,而是: - 保留模板中绝大部分原始字段 - 只对 proxies / proxy-groups / rules / rule-providers 做定点 patch

func (*BuiltSub) MarshalNodeListYAML added in v1.1.3

func (b *BuiltSub) MarshalNodeListYAML() ([]byte, error)

MarshalNodeListYAML 从最终 YAML 树中提取 proxies 节点,构造 nodeList 模式输出。 这样 nodeList 也直接复用最终 root,而不是依赖额外的 typed struct 副本。

func (*BuiltSub) MarshalYAML added in v1.1.3

func (b *BuiltSub) MarshalYAML() (any, error)

MarshalYAML 让 BuiltSub 在输出时直接复用 patch 后的 YAML 树, 从而避免再次经过 struct round-trip 丢失未知字段。

type CommonError

type CommonError struct {
	Code    ErrorCode
	Message string
	Cause   error
}

CommonError represents a structured error type for the common package

func NewDatabaseConnectError

func NewDatabaseConnectError(cause error) *CommonError

Database errors

func NewDirAccessError

func NewDirAccessError(dirPath string, cause error) *CommonError

func NewDirCreationError

func NewDirCreationError(dirPath string, cause error) *CommonError

Directory errors

func NewError

func NewError(code ErrorCode, message string, cause error) *CommonError

NewError creates a new CommonError

func NewFileCreateError

func NewFileCreateError(filePath string, cause error) *CommonError

func NewFileNotFoundError

func NewFileNotFoundError(filePath string) *CommonError

File errors

func NewFileReadError

func NewFileReadError(filePath string, cause error) *CommonError

func NewFileWriteError

func NewFileWriteError(filePath string, cause error) *CommonError

func NewInvalidInputError

func NewInvalidInputError(paramName string, value string) *CommonError

func NewNetworkRequestError

func NewNetworkRequestError(url string, cause error) *CommonError

Network errors

func NewNetworkResponseError

func NewNetworkResponseError(message string, cause error) *CommonError

func NewRecordNotFoundError

func NewRecordNotFoundError(recordType string, id string) *CommonError

func NewRegexCompileError

func NewRegexCompileError(pattern string, cause error) *CommonError

Regex errors

func NewRegexInvalidError

func NewRegexInvalidError(paramName string, cause error) *CommonError

func NewSimpleError

func NewSimpleError(code ErrorCode, message string) *CommonError

NewSimpleError creates a new CommonError without a cause

func NewSubscriptionLoadError

func NewSubscriptionLoadError(url string, cause error) *CommonError

Subscription errors

func NewSubscriptionParseError

func NewSubscriptionParseError(data []byte, cause error) *CommonError

func NewTemplateLoadError

func NewTemplateLoadError(template string, cause error) *CommonError

Template errors

func NewTemplateParseError

func NewTemplateParseError(data []byte, cause error) *CommonError

func NewValidationError

func NewValidationError(field string, message string) *CommonError

Validation errors

func (*CommonError) Error

func (e *CommonError) Error() string

Error returns the string representation of the error

func (*CommonError) Is

func (e *CommonError) Is(target error) bool

Is allows error comparison

func (*CommonError) Unwrap

func (e *CommonError) Unwrap() error

Unwrap returns the underlying error

type ErrorCode

type ErrorCode string

ErrorCode represents different types of errors

const (
	// Directory operation errors
	ErrDirCreation ErrorCode = "DIRECTORY_CREATION_FAILED"
	ErrDirAccess   ErrorCode = "DIRECTORY_ACCESS_FAILED"

	// File operation errors
	ErrFileNotFound ErrorCode = "FILE_NOT_FOUND"
	ErrFileRead     ErrorCode = "FILE_READ_FAILED"
	ErrFileWrite    ErrorCode = "FILE_WRITE_FAILED"
	ErrFileCreate   ErrorCode = "FILE_CREATE_FAILED"

	// Network operation errors
	ErrNetworkRequest  ErrorCode = "NETWORK_REQUEST_FAILED"
	ErrNetworkResponse ErrorCode = "NETWORK_RESPONSE_FAILED"

	// Template and configuration errors
	ErrTemplateLoad  ErrorCode = "TEMPLATE_LOAD_FAILED"
	ErrTemplateParse ErrorCode = "TEMPLATE_PARSE_FAILED"
	ErrConfigInvalid ErrorCode = "CONFIG_INVALID"

	// Subscription errors
	ErrSubscriptionLoad  ErrorCode = "SUBSCRIPTION_LOAD_FAILED"
	ErrSubscriptionParse ErrorCode = "SUBSCRIPTION_PARSE_FAILED"

	// Regex errors
	ErrRegexCompile ErrorCode = "REGEX_COMPILE_FAILED"
	ErrRegexInvalid ErrorCode = "REGEX_INVALID"

	// Database errors
	ErrDatabaseConnect ErrorCode = "DATABASE_CONNECTION_FAILED"
	ErrDatabaseQuery   ErrorCode = "DATABASE_QUERY_FAILED"
	ErrRecordNotFound  ErrorCode = "RECORD_NOT_FOUND"

	// Validation errors
	ErrValidation   ErrorCode = "VALIDATION_FAILED"
	ErrInvalidInput ErrorCode = "INVALID_INPUT"
)

func GetErrorCode

func GetErrorCode(err error) (ErrorCode, bool)

GetErrorCode extracts the error code from an error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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