From 2c45879669d5c82a20a73fb95b8961b68daff267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AC=E6=98=8E?= <83812544+Ed1s0nZ@users.noreply.github.com> Date: Fri, 15 May 2026 11:48:58 +0800 Subject: [PATCH] Add files via upload --- internal/config/config.go | 11 +++++-- internal/config/server_https_bootstrap.go | 35 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 internal/config/server_https_bootstrap.go diff --git a/internal/config/config.go b/internal/config/config.go index 52245743..44890e2a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -444,8 +444,15 @@ type RobotLarkConfig struct { } type ServerConfig struct { - Host string `yaml:"host"` - Port int `yaml:"port"` + Host string `yaml:"host" json:"host"` + Port int `yaml:"port" json:"port"` + // TLSEnabled 为 true 时主 Web UI 使用 HTTPS;现代浏览器在同源下会协商 HTTP/2,缓解 HTTP/1.1 每源并发连接数限制。 + TLSEnabled bool `yaml:"tls_enabled,omitempty" json:"tls_enabled,omitempty"` + // TLSCertPath / TLSKeyPath 非空时从 PEM 文件加载证书(生产环境推荐)。 + TLSCertPath string `yaml:"tls_cert_path,omitempty" json:"tls_cert_path,omitempty"` + TLSKeyPath string `yaml:"tls_key_path,omitempty" json:"tls_key_path,omitempty"` + // TLSAutoSelfSign 为 true 且未配置有效证书路径时,启动时生成内存自签证书(仅本地/测试;浏览器会提示不受信任)。 + TLSAutoSelfSign bool `yaml:"tls_auto_self_sign,omitempty" json:"tls_auto_self_sign,omitempty"` } type LogConfig struct { diff --git a/internal/config/server_https_bootstrap.go b/internal/config/server_https_bootstrap.go new file mode 100644 index 00000000..4aaa8965 --- /dev/null +++ b/internal/config/server_https_bootstrap.go @@ -0,0 +1,35 @@ +package config + +import "strings" + +// MainWebUIUsesHTTPS 判断主 Web UI 是否以 HTTPS 监听(与 internal/app.prepareMainServerTLS 前置条件一致)。 +func MainWebUIUsesHTTPS(s *ServerConfig) bool { + if s == nil { + return false + } + if s.TLSEnabled { + return true + } + if s.TLSAutoSelfSign { + return true + } + cert := strings.TrimSpace(s.TLSCertPath) + key := strings.TrimSpace(s.TLSKeyPath) + return cert != "" && key != "" +} + +// ApplyDevHTTPSBootstrap 供 --https / 一键脚本使用:强制开启主站 TLS。 +// 若已配置 tls_cert_path 与 tls_key_path 则仅用 PEM,不开启自签;否则启用 tls_auto_self_sign(内存证书,仅本地测试)。 +func ApplyDevHTTPSBootstrap(cfg *Config) { + if cfg == nil { + return + } + cfg.Server.TLSEnabled = true + cert := strings.TrimSpace(cfg.Server.TLSCertPath) + key := strings.TrimSpace(cfg.Server.TLSKeyPath) + if cert != "" && key != "" { + cfg.Server.TLSAutoSelfSign = false + return + } + cfg.Server.TLSAutoSelfSign = true +}