ssl

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 50 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 ssl 实现证书管理模块:ACME 自动签发与续期、上传证书、读取文件证书, 并可把证书写到目录供其他服务使用,同时为 Web 服务模块提供按 SNI 选择证书的能力。

Index

Constants

View Source
const (
	SourceACME   = "acme"
	SourceUpload = "upload"
	SourceFile   = "file"
)

证书来源

View Source
const (
	ChallengeDNS  = "dns"
	ChallengeHTTP = "http"
)

验证方式

View Source
const (
	NetworkIPv4 = "tcp4"
	NetworkIPv6 = "tcp6"
)

签发使用的网络(ACMEConfig.Network),留空为自动

View Source
const (
	// DeploySSH 经 Web 终端中的 SSH 连接把证书上传到远程主机,再执行命令
	DeploySSH = "ssh"
	// DeployDocker 重启本机 Docker 容器,让挂载了映射目录的服务加载新证书
	DeployDocker = "docker"
	// DeployLocal 把证书复制到本机目录,再在本机执行命令
	DeployLocal = "local"
)

部署目标类型

View Source
const ID = "ssl"

ID 是模块标识。

View Source
const ProfileShortLived = "shortlived"

ProfileShortLived 是 Let's Encrypt 的短期证书配置(约 6 天有效期),也是其签发 IP 地址证书的唯一配置。

Variables

This section is empty.

Functions

This section is empty.

Types

type ACMEConfig

type ACMEConfig struct {
	Domains []string `json:"domains"`
	Email   string   `json:"email"`
	// CA 为预置名称或 custom
	CA           string `json:"ca"`
	DirectoryURL string `json:"directoryUrl"`
	// EAB 用于 ZeroSSL、Google 等需要外部账户绑定的 CA
	EABKid  string `json:"eabKid"`
	EABHMAC string `json:"eabHmac"`
	KeyType string `json:"keyType"`
	// Profile 为 ACME 证书配置(profile),留空使用 CA 默认配置;shortlived 为短期证书,IP 地址证书需要它
	Profile string `json:"profile"`
	// Challenge 为验证方式:dns(默认,支持通配符)或 http(HTTP-01,无需 DNS 服务商,要求 80 端口可从公网访问)
	Challenge string `json:"challenge"`
	// HTTPPort 为 HTTP-01 临时监听端口,默认 80(80 端口已被 Web 服务占用时由 Web 服务应答)
	HTTPPort int `json:"httpPort"`
	// Provider 与 Credentials 为 DNS 服务商,用于写入 _acme-challenge TXT 记录
	Provider    string                  `json:"provider"`
	Credentials dnsprovider.Credentials `json:"credentials"`
	// Zone 显式指定主域名,留空自动推断
	Zone string `json:"zone"`
	// Resolvers 为检查 TXT 生效所用的递归 DNS,留空使用系统配置
	Resolvers []string `json:"resolvers"`
	// PropagationTimeout 为等待 TXT 生效的最长时间(秒)
	PropagationTimeout int `json:"propagationTimeout"`
	// SkipPropagationCheck 跳过本地生效检查,等待固定时间后直接通知 CA 验证
	SkipPropagationCheck bool `json:"skipPropagationCheck"`
	// DisableAuthoritativeCheck 只通过递归 DNS 检查 TXT 是否生效,不查询权威服务器
	DisableAuthoritativeCheck bool `json:"disableAuthoritativeCheck"`
	// DisableCNAME 不跟随 _acme-challenge 的 CNAME,验证记录直接写在 _acme-challenge.<域名>。
	// 默认跟随(用于把验证委派到其他域名),但来自泛解析的 CNAME 会被自动识别并不跟随
	DisableCNAME bool `json:"disableCname,omitempty"`
	// Network 限定签发时的出站网络:留空自动选择,tcp4 只用 IPv4,tcp6 只用 IPv6。
	// 作用于 ACME 接口、DNS 服务商接口与 TXT 生效检查,适用于有 IPv6 地址但 IPv6 不通的主机
	Network string `json:"network,omitempty"`
}

ACMEConfig 是 ACME 自动签发参数,验证方式为 DNS-01 或 HTTP-01。

type Account

type Account struct {
	Email        string `json:"email"`
	DirectoryURL string `json:"directoryUrl"`
	KeyPEM       string `json:"keyPem"`
	// Registration 为 lego registration.Resource 的 JSON
	Registration string `json:"registration"`
}

Account 是缓存的 ACME 账户,按 CA 地址与邮箱区分。

type Cert

type Cert struct {
	Key    string `json:"key"`
	Remark string `json:"remark"`
	// Group 为列表中的分组名称,只影响管理界面的展示,留空为未分组
	Group  string `json:"group"`
	Enable bool   `json:"enable"`
	Source string `json:"source"`

	ACME ACMEConfig `json:"acme"`
	// CertFile / KeyFile 为文件来源的路径,定期重新读取
	CertFile string `json:"certFile"`
	KeyFile  string `json:"keyFile"`

	// 证书内容(PEM)。CertPEM 为包含中间证书的完整链
	CertPEM   string `json:"certPem"`
	KeyPEM    string `json:"keyPem"`
	IssuerPEM string `json:"issuerPem"`

	Mapping Mapping `json:"mapping"`
	// Deploys 为证书内容变化后自动执行的部署目标(SSH 主机、本机目录、Docker 容器)
	Deploys []Deploy `json:"deploys"`
	// UpdatedAt 为证书内容最近一次变化时间
	UpdatedAt time.Time `json:"updatedAt,omitzero"`
	// IssuedProfile 为当前证书签发时使用的 ACME 证书配置,与 ACME.Profile 不同时重新签发
	IssuedProfile string `json:"issuedProfile,omitempty"`
}

Cert 是一张证书。

func (*Cert) Validate

func (c *Cert) Validate(settings Settings) error

Validate 校验配置。上传来源要求证书与私钥匹配。

type CertInfo

type CertInfo struct {
	Domains     []string  `json:"domains"`
	Issuer      string    `json:"issuer"`
	NotBefore   time.Time `json:"notBefore"`
	NotAfter    time.Time `json:"notAfter"`
	Fingerprint string    `json:"fingerprint"`
}

CertInfo 是从证书内容解析出的信息。

type CertState

type CertState struct {
	Obtaining   bool      `json:"obtaining"`
	LastError   string    `json:"lastError,omitempty"`
	LastAttempt time.Time `json:"lastAttempt,omitzero"`
	NextRetry   time.Time `json:"nextRetry,omitzero"`
	MappingNote string    `json:"mappingNote,omitempty"`
	// Deploys 为各部署目标的运行状态,与 Cert.Deploys 按下标对应
	Deploys []DeployState `json:"deploys,omitempty"`
	// contains filtered or unexported fields
}

CertState 是证书的运行状态(不持久化)。

type CertView

type CertView struct {
	Cert
	Info  *CertInfo `json:"info,omitempty"`
	State CertState `json:"state"`
}

CertView 是 API 输出的证书,私钥与凭据脱敏。

type Deploy added in v0.5.5

type Deploy struct {
	Enable bool   `json:"enable"`
	Type   string `json:"type"`
	Remark string `json:"remark"`
	// Connection 为 Web 终端中 SSH 连接的 key,复用其地址、认证、代理与已记录的主机密钥
	Connection string `json:"connection"`
	// Dir 为证书目录(绝对路径):SSH 部署为远程主机上的目录,本机部署为本机目录。
	// 默认文件名与证书映射相同:<备注>.crt/.key/.pem/_issuerCertificate.crt
	Dir string `json:"dir"`
	// CertName / KeyName 为自定义的证书(完整链)与私钥文件名,需同时填写;填写后只写这两个文件
	CertName string `json:"certName,omitempty"`
	KeyName  string `json:"keyName,omitempty"`
	// Command 为写入证书后执行的命令(SSH 部署在远程主机执行,本机部署在本机执行),可使用 $certName、$certDir、$certFile、$keyFile
	Command string `json:"command"`
	// Containers 为要重启的本机容器名称或 ID
	Containers []string `json:"containers"`
	// DeployedHash 为最近一次成功部署时证书与目标设置的摘要,与当前不同则需要重新部署
	DeployedHash string    `json:"deployedHash,omitempty"`
	DeployedAt   time.Time `json:"deployedAt,omitzero"`
}

Deploy 是证书内容变化后自动执行的部署目标。

type DeployState added in v0.5.5

type DeployState struct {
	Running   bool      `json:"running"`
	LastError string    `json:"lastError,omitempty"`
	NextRetry time.Time `json:"nextRetry,omitzero"`
}

DeployState 是部署目标的运行状态(不持久化),与 Cert.Deploys 按下标对应。

type Deployers added in v0.5.5

type Deployers struct {
	// Push 经 SSH 连接上传文件并执行命令,返回命令输出
	Push func(ctx context.Context, conn, dir string, files []RemoteFile, command string) (string, error)
	// Restart 重启本机容器
	Restart func(ctx context.Context, names []string) error
	// SSHTargets 列出可用的 SSH 连接
	SSHTargets func() []SSHTarget
	// Containers 列出本机容器名称
	Containers func(ctx context.Context) ([]string, error)
}

Deployers 由主程序注入,连接 Web 终端与 Docker 模块,避免证书模块直接依赖它们。

type Mapping

type Mapping struct {
	Enable bool   `json:"enable"`
	Dir    string `json:"dir"`
	// Script 在证书文件变化后执行,例如重载 nginx
	Script string `json:"script"`
}

Mapping 把证书写到目录供其他服务使用,文件名为 <备注>.crt/.key/.pem/_issuerCertificate.crt。

type Module

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

Module 是证书模块。

func New

func New() *Module

New 创建模块实例。

func (*Module) GetCertificate

func (m *Module) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error)

GetCertificate 按 SNI 选择证书,供 Web 服务模块的 tls.Config 使用。

func (*Module) HasCertificate

func (m *Module) HasCertificate(name string) bool

HasCertificate 判断是否有覆盖该域名的证书。

func (*Module) ID

func (m *Module) ID() string

ID 实现 module.Module。

func (*Module) ImportLucky

func (m *Module) ImportLucky(doc luckyimport.Document, opts luckyimport.Options) (*luckyimport.Report, error)

ImportLucky 实现 luckyimport.Importer。

func (*Module) Init

func (m *Module) Init(env module.Env) error

Init 实现 module.Module。

func (*Module) Metrics added in v0.5.0

func (m *Module) Metrics() []module.Sample

Metrics 实现 module.Metricer:证书到期时间与剩余天数。

func (*Module) Routes

func (m *Module) Routes(r module.Router)

Routes 实现 module.Module。

func (*Module) SelfCheck added in v0.5.0

func (m *Module) SelfCheck(_ context.Context, _ module.CheckEnv) []module.CheckResult

SelfCheck 实现 module.SelfChecker:检查启用的证书是否已过期或即将过期。

func (*Module) SetDeployers added in v0.5.5

func (m *Module) SetDeployers(d Deployers)

SetDeployers 注入部署能力,需在 Start 之前调用。

func (*Module) Start

func (m *Module) Start(ctx context.Context) error

Start 实现 module.Module。

func (*Module) Stop

func (m *Module) Stop() error

Stop 实现 module.Module。

func (*Module) Summary

func (m *Module) Summary() []module.SummaryItem

Summary 实现 module.Summarizer:证书数量与最近到期时间。

func (*Module) Title

func (m *Module) Title() string

Title 实现 module.Module。

type RemoteFile added in v0.5.5

type RemoteFile struct {
	Name string
	Data []byte
	Mode os.FileMode
}

RemoteFile 是部署时上传的文件。

type SSHTarget added in v0.5.5

type SSHTarget struct {
	Key     string `json:"key"`
	Name    string `json:"name"`
	Host    string `json:"host"`
	Trusted bool   `json:"trusted"`
}

SSHTarget 是可选的 SSH 连接。

type Settings

type Settings struct {
	// Enable 为自动续签开关,关闭后不再自动签发(已有证书仍可使用)
	Enable bool `json:"enable"`
	// RenewBeforeDays 为到期前多少天续签
	RenewBeforeDays int `json:"renewBeforeDays"`
	// ShortLivedRenewHours 为短期证书(有效期不足 10 天)到期前多少小时续签
	ShortLivedRenewHours int    `json:"shortLivedRenewHours"`
	DefaultEmail         string `json:"defaultEmail"`
}

Settings 是模块级配置。

Jump to

Keyboard shortcuts

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