utils

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 21 Imported by: 0 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

View Source
var ErrUnsafeHost = errors.New("host resolves to a blocked (internal/loopback/link-local) address")

ErrUnsafeHost 表示目标主机解析到禁止访问的保留地址,已被 SSRF 防护拦截。

Functions

func BuildCookieHeader

func BuildCookieHeader(cookies []CookieItem) string

BuildCookieHeader 将 Cookie 列表拼接为 Cookie 请求头值

func CheckReferer added in v1.5.0

func CheckReferer(allowedReferers []string, ctx *gin.Context) bool

CheckReferer 提取请求中的 Referer 头,校验其 host 是否在白名单中。 若 Referer 为空、解析失败或不在白名单,返回 false。

func ExtractHost

func ExtractHost(rawURL string) (string, error)

ExtractHost 从完整 URL 中提取 host

func IsAllowed

func IsAllowed(allowedReferers []string, host string) bool

IsAllowed 检查 host 是否命中白名单(后缀匹配)

func IsUnsafeHost added in v1.7.0

func IsUnsafeHost(ctx context.Context, host string) bool

IsUnsafeHost 判断 host(域名或 IP 字面量,不带端口)是否禁止外连。 解析出的任一地址命中保留地址段即返回 true;解析失败或超时同样返回 true(fail-closed,宁可拒绝也不放过)。

func NewModuleLogger added in v1.4.0

func NewModuleLogger(moduleName string) *slog.Logger

NewModuleLogger 为指定模块创建一个带模块名的 slog.Logger。 所有通过该 logger 输出的日志会自动带上 "module=<name>" 属性。

用法示例:

var log = utils.NewModuleLogger("mymodule")
log.Info("started", "port", 8080)
// 输出: time=... level=INFO msg=started module=mymodule port=8080

Types

type CookieItem

type CookieItem struct {
	Name     string `json:"name"`
	Value    string `json:"value"`
	Domain   string `json:"domain,omitempty"`
	Path     string `json:"path,omitempty"`
	Expires  int64  `json:"expires,omitempty"`
	HTTPOnly bool   `json:"httpOnly"`
	Secure   bool   `json:"secure"`
	SameSite string `json:"sameSite,omitempty"`
}

CookieItem 单个 Cookie 条目

func CookieCloud

func CookieCloud(ctx context.Context, host, uuid, password, cryptoType string) ([]CookieItem, error)

CookieCloud 从远端 CookieCloud 服务获取并解密 Cookie host: 服务地址,如 "https://cookie.example.com" uuid: 用户标识 password: 解密密码 cryptoType: 加密算法,空字符串或 "legacy" 为旧算法,"aes-128-cbc-fixed" 为新算法

func FilterCookiesByDomain

func FilterCookiesByDomain(cookies []CookieItem, domain string) []CookieItem

FilterCookiesByDomain 筛选指定域名的 Cookie

func GetCookies

func GetCookies(ctx context.Context) ([]CookieItem, error)

GetCookies 一键获取 Cookie

type DecryptResult

type DecryptResult struct {
	CookieData map[string][]CookieItem `json:"cookie_data"`
}

DecryptResult cookie_decrypt 的返回值结构

Jump to

Keyboard shortcuts

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