outbound

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 15 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

Overview

Package outbound 是只出站的通知适配器(Bark、Server 酱、ntfy、钉钉、飞书、企业微信群机器人)共用的发送工具。

发送走自己的 http.Client:请求绑定调用方的 ctx(超时就取消,不会超时之后才送达、再被重试送一遍); 不走环境代理、不跟随跳转;连接前检查目标地址(链路本地、组播与保留地址一律不连,本机与内网只在允许时连); 响应最多读 64 KiB;HTTP 错误只报状态码,响应里的错误信息由各适配器挑字段、用 Clean 脱敏后再报。

Index

Constants

View Source
const MaxBody = 64 << 10

MaxBody 是响应最多读多少字节。

Variables

View Source
var ErrTooLarge = errors.New("响应超过 64 KiB")

ErrTooLarge 表示响应超过 MaxBody。

Functions

func CheckHost

func CheckHost(host string, allowPrivate bool) error

CheckHost 在保存配置时检查地址里的主机:是 IP 或 localhost 时按 CheckIP 的规则检查; 其余主机名不查 DNS(不做 I/O),连接时再按解析出的 IP 检查。

func CheckIP

func CheckIP(ip netip.Addr, allowPrivate bool) error

CheckIP 检查一个要连的 IP(规则见 alwaysBlocked 与 privateNets)。

func Clean

func Clean(msg string, secrets ...string) string

Clean 整理服务端返回的错误信息再放进错误:把 secrets(配置里的密钥、token)换成 ***、 空白压成一个空格、最多 120 个字符。

func Config

func Config(conf string, dst any, typ string) error

Config 解析 ConfigJSON 到 dst。

func HTTPURL

func HTTPURL(raw, what string) (string, error)

HTTPURL 校验并规整一个 http(s) 地址(去掉末尾的 /);不能带用户名密码、? 或 #(后面还要拼路径)。

func PrivateHint

func PrivateHint(err error) string

PrivateHint 在 err 是「本机或内网地址」时给出打开「允许内网地址」的提示,否则为空串(给自建服务器的通道用)。

func ServerURL

func ServerURL(raw, what string, allowPrivate bool) (string, error)

ServerURL 是自建服务器地址:HTTPURL 的规则,再加上主机是 IP 或 localhost 时按 CheckHost 检查。

func Text

func Text(n notify.Notification) string

Text 是标题和正文拼成的纯文本(标题为空时只有正文),链接附在最后。

Types

type AddrError

type AddrError struct {
	Addr string
	// Private 为 true 时是本机或内网地址(允许内网时能连);为 false 时是链路本地、组播或保留地址,一律不连。
	Private bool
}

AddrError 表示连接前检查目标地址没通过。

func (*AddrError) Error

func (e *AddrError) Error() string

type Client

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

Client 发通知请求。用 NewClient 或 Default 取得。

func Default

func Default() *Client

Default 是正式发送用的 Client。

func NewClient

func NewClient(o Options) *Client

NewClient 建一个 Client。

func Use

func Use(c *Client) *Client

Use 返回 c;c 为空时返回 Default()。

func (*Client) PostJSON

func (c *Client) PostJSON(ctx context.Context, r Request) (Response, error)

PostJSON 把 r.Payload 以 JSON POST 到 r.URL。状态码不是 2xx 时返回 *HTTPError,响应体照样返回 (给适配器挑错误字段)。错误里不带请求地址:Webhook 地址、SendKey 往往就在地址里。

type HTTPError

type HTTPError struct{ Status int }

HTTPError 表示 HTTP 状态码不是 2xx。只带状态码:响应体可能回显请求里的密钥。

func (*HTTPError) Error

func (e *HTTPError) Error() string

type Options

type Options struct {
	// Divert 非空时(host:port),主机名不是 IP 也不是 localhost 的连接都改连到这里,且不校验证书。
	// 只给测试与 qa 构建用:让发往各家官方地址的请求落到本地的收件端。
	Divert string
}

Options 是 Client 的选项。

type Request

type Request struct {
	URL     string
	Payload any
	Header  map[string]string
	// AllowPrivate 允许连本机与内网地址(自建在内网的 Bark、ntfy 由用户打开)。
	AllowPrivate bool
}

Request 是一次发送。

type Response

type Response struct {
	Status int
	Body   []byte
}

Response 是 HTTP 状态码与响应体。

Jump to

Keyboard shortcuts

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