config

package
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Servers Servers // 服务器集
	Sites   Sites   // 站点集
	// contains filtered or unexported fields
}

Config 配置

func (*Config) ParseFile

func (T *Config) ParseFile(file string) error

ParseFile 解析服务器配置文件,一个 JSON 格式的文件。

参:
  file string     文件
返:
  error           错误,如果文件无法打开,或无法解析的情况

func (*Config) ParseFiles

func (T *Config) ParseFiles(file string) error

ParseFiles 先加载主配置,再自动加载同目录下所有 .site.json / .listen.json 子配置。

func (*Config) ParseReader

func (T *Config) ParseReader(r io.Reader) error

ParseReader 解析服务器配置数据,一个 JSON 格式的数据。

参:
  r   io.Reader       读接口
返:
  error               错误,如果无法解析的情况

type Conn

type Conn struct {
	// 引用公共配置后,该以结构中的CC和CS如果也有设置,将会使用优先使用。
	PublicName      string // 引用公共配置的名字
	Deadline        int64  // 设置读写超时(毫秒单位)
	WriteDeadline   int64  // 设置写入超时(毫秒单位)
	ReadDeadline    int64  // 设置读取超时(毫秒单位)
	KeepAlive       bool   // 即使没有任何通信,一个客户端可能希望保持连接到服务器的状态。
	KeepAlivePeriod int64  // 保持连接超时(毫秒单位)
	Linger          int    // 连接关闭后,等待发送或待确认的数据(秒单位)。如果 sec > 0,经过sec秒后,所有剩余的未发送数据都可能会被丢弃。则与sec < 0 一样在后台发送数据。
	NoDelay         bool   // 设置操作系统是否延迟发送数据包,建议设置为true(无延迟)
	ReadBuffer      int    // 在缓冲区读取数据大小
	WriteBuffer     int    // 写入数据到缓冲区大小
}

Conn 连接相关参数

type Listen

type Listen struct {
	Status bool   // 状态,是否启动此服务器
	CC     Conn   // 连接设置
	CS     Server // 服务器设置
}

Listen 监听配置

type Server

type Server struct {
	// 引用公共配置后,该结构中的 CC 和 CS 如果也有设置,将会优先使用。
	PublicName                   string     // 引用公共配置的名字
	ReadTimeout                  int64      // 设置读取超时(毫秒单位)
	WriteTimeout                 int64      // 设置写入超时(毫秒单位)
	ReadHeaderTimeout            int64      // 读取标头超时(毫秒单位)
	IdleTimeout                  int64      // 保持连接空闲超时,如果为0,使用 ReadTimeout,(毫秒单位)
	MaxHeaderBytes               int        // 如果0,最大请求头的大小,http.DefaultMaxHeaderBytes
	KeepAlivesEnabled            bool       // 支持客户端Keep-Alive
	ShutdownConn                 bool       // 服务器关闭监听,不会即时关闭正在下载的连接。空闲后再关闭。(默认即时关闭)
	DisableGeneralOptionsHandler bool       // 如果为真,将“OPTIONS *”请求传递给处理程序,否则响应 200 OK 和 Content-Length: 0。
	TLS                          *ServerTLS // TLS
}

Server 服务器参数

type ServerPublic

type ServerPublic struct {
	CC map[string]Conn   // 连接设置
	CS map[string]Server // 服务器设置
}

ServerPublic 公共服务器配置

func (*ServerPublic) ConfigConn

func (T *ServerPublic) ConfigConn(origin *Conn, handle func(name string, dsc, src reflect.Value) bool) bool

ConfigConn 将公共连接配置合并到 origin(origin 非零字段优先)。 返回 true 表示成功合并。并发安全(读锁保护 map 查找)。

func (*ServerPublic) ConfigServer

func (T *ServerPublic) ConfigServer(origin *Server, handle func(name string, dsc, src reflect.Value) bool) bool

ConfigServer 优化版本(关键改动)

type ServerTLS

type ServerTLS struct {
	RootCAs                     []ServerTLSFile // 服务端证书文件
	NextProtos                  []string        // http版本
	CipherSuites                []uint16        // 密码套件(仅影响 TLS 1.0–1.2;TLS 1.3 由运行时固定安全套件处理,不可配置)
	SessionTicketsDisabled      bool            // 设置为 true 可禁用会话票证 (恢复) 支持。
	SetSessionTicketKeys        [][32]byte      // 会话恢复票证
	DynamicRecordSizingDisabled bool            // 禁用TLS动态记录自适应大小
	MinVersion                  uint16          // 最小SSL/TLS版本。如果为零,则SSLv3的被取为最小(现代 Go 实际最低为 TLS 1.0,推荐 TLS 1.2+)。
	MaxVersion                  uint16          // 最大SSL/TLS版本。如果为零,则该包所支持的最高版本被使用。
	ClientCAs                   []string        // 客户端拥有的“权威组织”证书的列表。(Server/Client端使用)
}

ServerTLS TLS 相关配置

func (*ServerTLS) CipherSuitesAuto

func (T *ServerTLS) CipherSuitesAuto()

CipherSuitesAuto 根据 MinVersion/MaxVersion 自动填充安全密码套件。 仅填充 TLS 1.0–1.2 套件;TLS 1.3 套件由 crypto/tls 自动管理且不可配置。 注意:若多个 Server 共享同一 *ServerTLS 指针,首次并发调用存在竞态窗口, 建议在配置加载阶段单线程调用。方法本身对已填充的 CipherSuites 直接返回。

type ServerTLSFile

type ServerTLSFile struct {
	CertFile, KeyFile string // 证书,key 文件地址
}

ServerTLSFile 证书文件对

type Servers

type Servers struct {
	Public ServerPublic
	Listen map[string]Listen
}

Servers 服务器集合

type Site

type Site struct {
	Status   bool   // 状态,是否启动此站点
	Name     string // 站点别名
	Identity string // 站点维一码,可以说是池名

	Host      []string                // 域名绑定
	Forward   map[string]SiteForwards // 转发
	Plugin    SitePlugins             // 插件
	Directory SiteDirectory           // 目录

	IndexFile []string // 默认页
	Dynamic   SiteDynamic

	Header    SiteHeader        // HTTP头
	Log       SiteLog           // 日志
	ErrorPage map[string]string // 错误页

	Session SiteSession // 会话
}

Site 配置-站点

type SiteDirectory

type SiteDirectory struct {
	Root    string   // 主目录
	Virtual []string // 虚目录
}

SiteDirectory 配置-目录

func (*SiteDirectory) RootDir

func (T *SiteDirectory) RootDir(upath string) string

RootDir 根目录

r *http.Request	    		请求
string			    		根目录路径

type SiteDynamic

type SiteDynamic struct {
	// 引用公共配置后,该结构中的字段如果也有设置,将会优先使用。
	PublicName string // 引用公共配置的名字

	Ext                []string // 动态文件后缀
	Cache              bool     // 动态文件缓存解析,非缓存执行
	CacheParseTimeout  int64    // 动态文件缓存解析超时,(秒为单位)
	CacheStaticFileDir string   // 缓存静态文件目录,仅适于 markdown 转 HTML
	CacheStaticTimeout int64    // 缓存静态超时,(秒为单位)
}

SiteDynamic 动态相关配置

type SiteForward

type SiteForward struct {
	Status       bool     // 启用或禁止
	Path         []string // 多种路径匹配
	ExcludePath  []string // 排除多种路径匹配
	RePath       string   // 重写路径
	RedirectCode int      // 重定向状态码,默认不转向
	End          bool     // 不进行二次匹配/转发
	// contains filtered or unexported fields
}

SiteForward 配置-转发-配置

func (*SiteForward) Compile

func (T *SiteForward) Compile() (fw *vweb.ForwardRewriter, err error)

type SiteForwards

type SiteForwards struct {
	// 引用公共配置后,该结构中的字段如果也有设置,将会优先使用。
	PublicName string // 引用公共配置的名字

	List []SiteForward
}

SiteForwards 转发集合

type SiteHeader struct {
	// 引用公共配置后,该结构中的字段如果也有设置,将会优先使用。
	PublicName      string                    // 引用公共配置的名字
	Static, Dynamic map[string]SiteHeaderType // 静态 / 动态 Header,key 形如 ".html"
	MIME            map[string]string         // MIME 类型
}

SiteHeader 配置-标头

type SiteHeaderType

type SiteHeaderType struct {
	Header      map[string][]string // Header
	PageExpired int64               // 页面过期(秒单位)
}

SiteHeaderType 配置-标头-类型

type SiteLog

type SiteLog struct {
	Level     SiteLogLevel // 级别
	Directory string       // 目录
}

SiteLog 配置-日志,这个功能后面待加。

type SiteLogLevel

type SiteLogLevel int

SiteLogLevel 配置-日志-级别

const (
	SiteLogLevelDisable SiteLogLevel = iota // 禁用日志记录,默认不开启
)

type SitePlugin

type SitePlugin struct {
	// 引用公共配置后,该结构中的字段如果也有设置,将会优先使用。
	PublicName string // 引用公共配置的名字
	Status     bool   // 状态,是否启用

	// 公共
	Addr          string // 地址
	LocalAddr     string // 本地拨号 IP
	Timeout       int64  // 拨号超时(毫秒单位)
	KeepAlive     int64  // 保持连接超时(毫秒单位)
	FallbackDelay int64  // 后退延时,等待双协议栈延时,(毫秒单位,默认 300ms)
	IdeConn       int    // 空闲连接数(历史字段名,保持导出兼容)

	// RPC
	Path    string // 路径
	MaxConn int    // 最大连接数

	// HTTP
	ProxyURL               string         // 代理 URL(可含用户密码或 socks5)
	Host                   string         // Host
	Scheme                 string         // 协议
	TLS                    *SitePluginTLS // TLS
	TLSHandshakeTimeout    int64          // 握手超时(毫秒单位)
	DisableKeepAlives      bool           // 禁止长连接
	DisableCompression     bool           // 禁止压缩
	MaxIdleConnsPerHost    int            // 每个主机最大空闲连接
	MaxConnsPerHost        int            // 每个主机最大连接
	IdleConnTimeout        int64          // 空闲连接超时(毫秒单位)
	ResponseHeaderTimeout  int64          // 请求 Header 超时(毫秒单位)
	ExpectContinueTimeout  int64          // 发送 Expect: 100-continue 的 PUT 超时
	ProxyConnectHeader     http.Header    // CONNECT 代理请求额外标头
	MaxResponseHeaderBytes int64          // 最大响应标头限制(字节)
	ReadBufferSize         int            // 读取缓冲大小
	WriteBufferSize        int            // 写入缓冲大小
	ForceAttemptHTTP2      bool           // 支持 HTTP/2
}

SitePlugin 配置-插件

func (*SitePlugin) ConfigPluginHTTPClient

func (T *SitePlugin) ConfigPluginHTTPClient(c *vweb.PluginHTTPClient) error

func (*SitePlugin) ConfigPluginRPCClient

func (T *SitePlugin) ConfigPluginRPCClient(c *vweb.PluginRPCClient) error

type SitePluginTLS

type SitePluginTLS struct {
	ServerName         string        // 服务器名称
	InsecureSkipVerify bool          // 跳过证书验证
	NextProtos         []string      // ALPN 协议,如:http/1.1
	CipherSuites       []uint16      // 密码套件列表
	ClientSessionCache int           // TLS 会话恢复 ClientSessionState 缓存大小(Client 端)
	CurvePreferences   []tls.CurveID // ECDHE 握手曲线偏好(Client 端)
	RootCAs            []string      // 根证书文件路径列表
}

SitePluginTLS 配置-插件-TLS

type SitePlugins

type SitePlugins struct {
	RPC  map[string]SitePlugin
	HTTP map[string]SitePlugin
}

SitePlugins 配置-插件

func (*SitePlugins) ConfigSitePluginHTTP

func (T *SitePlugins) ConfigSitePluginHTTP(origin *SitePlugin, handle func(name string, dsc, src reflect.Value) bool) bool

ConfigSitePluginHTTP 从公共 HTTP 配置模板中合并配置到 origin 中

func (*SitePlugins) ConfigSitePluginRPC

func (T *SitePlugins) ConfigSitePluginRPC(origin *SitePlugin, handle func(name string, dsc, src reflect.Value) bool) bool

ConfigSitePluginRPC 从公共 RPC 配置模板中合并配置到 origin 中

type SitePublic

type SitePublic struct {
	Header  map[string]SiteHeader
	Session map[string]SiteSession
	Plugin  SitePlugins
	Forward map[string]SiteForwards
	Dynamic map[string]SiteDynamic
}

SitePublic 公共站点配置

func (*SitePublic) ConfigSiteDynamic

func (T *SitePublic) ConfigSiteDynamic(origin *SiteDynamic, handle func(name string, dsc, src reflect.Value) bool) bool

ConfigSiteDynamic 将公共 Dynamic 配置合并到 origin。 规则:以公共配置为底,origin 中非零字段覆盖公共配置,结果写回 origin。 返回 true 表示成功合并;PublicName 为空、未找到或拷贝失败时返回 false。

func (*SitePublic) ConfigSiteForward

func (T *SitePublic) ConfigSiteForward(origin *SiteForwards, handle func(name string, dsc, src reflect.Value) bool) bool

ConfigSiteForward 将公共 Forward 列表追加到 origin.List。 与其它 Config* 不同:采用追加语义(公共规则在前或后由调用方 List 初始内容决定), 且不覆盖 origin 已有项。合并时会复制元素并清空运行时字段 forwardWriter,避免共享可变状态。 handle 保留签名以兼容既有调用方;转发列表为切片追加,不走字段级深拷贝过滤。

func (*SitePublic) ConfigSiteHeader

func (T *SitePublic) ConfigSiteHeader(origin *SiteHeader, handle func(name string, dsc, src reflect.Value) bool) bool

ConfigSiteHeader 将公共 Header 配置合并到 origin(公共为底,origin 非零字段覆盖)。

func (*SitePublic) ConfigSitePlugin added in v3.1.0

func (T *SitePublic) ConfigSitePlugin(origin *SitePlugin, kind string, handle func(name string, dsc, src reflect.Value) bool) bool

ConfigSitePlugin 按 kind("rpc" / "http",大小写不敏感)与 PublicName 合并公共插件配置到 origin。 补全原结构体存在 Plugin 字段却无对应合并入口的功能盲区。 未找到或参数非法时返回 false。

func (*SitePublic) ConfigSiteSession

func (T *SitePublic) ConfigSiteSession(origin *SiteSession, handle func(name string, dsc, src reflect.Value) bool) bool

ConfigSiteSession 将公共 Session 配置合并到 origin(公共为底,origin 非零字段覆盖)。

type SiteSession

type SiteSession struct {
	// 引用公共配置后,该结构中的字段如果也有设置,将会优先使用。
	PublicName string // 引用公共配置的名字
	Name       string // 会话名称
	Expired    int64  // 过期时间(秒单位,默认 20 分钟)
	Size       int    // 会话 ID 长度(默认 40)
	Salt       string // 加盐(伪随机数补充熵,可为空)
}

SiteSession 配置-会话

type Sites

type Sites struct {
	Public SitePublic
	Site   []Site // 站点
}

Sites 站点集合

Jump to

Keyboard shortcuts

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