Help us shape the future of Go! Take the 2026 Go Developer Survey

hostmodule

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 15 Imported by: 1

Documentation

Index

Constants

View Source
const EnvHTTPAllowedDomains = "SCRIPT_HTTP_ALLOWED_DOMAINS"

EnvHTTPAllowedDomains http 出站白名单环境变量名(逗号分隔域名)。

Variables

This section is empty.

Functions

func BuildHTTPClient

func BuildHTTPClient(opts HTTPOptions) *http.Client

BuildHTTPClient 构造带重定向限制的出站客户端。 重定向每次跳转都会重新校验白名单(CheckRedirect 内),防止白名单域名 302 到内网。

func CheckAllowedURL

func CheckAllowedURL(rawURL string, allowed []string, denyLoopback bool) error

CheckAllowedURL 校验 URL:仅 http(s)、host 必须精确命中白名单 (支持 "*.example.com" 通配一级子域;裸 "example.com" 不含子域)。

func HTTPRequest

func HTTPRequest(ctx context.Context, client *http.Client, rawURL, method string, headers map[string]string, body string, maxBody int64) (map[string]any, error)

HTTPRequest 执行受限的出站请求(核心实现,Lua/JS 适配层共用)。 options: method(GET默认)、headers(map)、body(string)。 返回: {status, body, headers(map, 全小写键)}。

Types

type HTTPOptions

type HTTPOptions struct {
	// AllowedDomains 域名白名单(精确匹配子域需显式列出;空 = 全部拒绝)。
	AllowedDomains []string
	// Timeout 单请求超时(默认 10s,上限 30s)。
	Timeout time.Duration
	// MaxResponseBody 响应体上限字节(默认/上限 1MB)。
	MaxResponseBody int64
	// DenyLoopback 环回地址硬禁开关(默认 true)。
	// 即使白名单显式列出 localhost/127.0.0.1 也拒绝,防 SSRF 基线;
	// 仅测试环境(httptest 监听 127.0.0.1)显式置 false 放行。
	DenyLoopback bool
}

HTTPOptions 约束 http 模块的出站行为(跨语言共享,Lua/JS 同一套护栏)。

func HTTPAllowlistFromEnv

func HTTPAllowlistFromEnv() HTTPOptions

HTTPAllowlistFromEnv 从环境变量读取域名白名单构造 http 护栏。 未设置 = 空 = 全部出站拒绝(fail-closed)。

func NormalizeHTTPOptions

func NormalizeHTTPOptions(o HTTPOptions) HTTPOptions

NormalizeHTTPOptions 填充默认值并收敛越界配置。

type ModuleDef

type ModuleDef struct {
	Name  string
	Funcs map[string]any
}

ModuleDef 是语言无关的模块定义:名称 + 一组 Go 函数。

func ModuleCrypto

func ModuleCrypto() ModuleDef

ModuleCrypto 构建语言无关的 crypto 模块。

func ModuleHTTP

func ModuleHTTP(opts HTTPOptions) ModuleDef

ModuleHTTP 构建语言无关的 http 模块(JS 等基于 map[string]any 桥接的语言使用)。 与 Lua 版(go-scripts/lua/host)共用本包 NormalizeHTTPOptions/BuildHTTPClient/CheckAllowedURL/HTTPRequest 护栏: 白名单 fail-closed、重定向逐跳复检、超时/体积上限。 返回的函数 (T, error) 形态在 goja 桥接下转为 JS 异常。

func ModuleLogger

func ModuleLogger(logger *bLogger.Helper) ModuleDef

ModuleLogger 构建语言无关的 logger 模块。

func ModuleUtil

func ModuleUtil(maxSleep ...time.Duration) ModuleDef

ModuleUtil 构建语言无关的 util 模块。 maxSleep 为 sleep 时长上限(防御脚本无限阻塞执行线程);未传时默认 5 秒。

Jump to

Keyboard shortcuts

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