支持可配置反代HTTPS端口并统一管理与重置入口
This commit is contained in:
1 parent
b2b8951bcf
commit
54b19a19f8
23 files changed
+268
-50
No files matched your search
@@ -42,11 +42,12 @@ type AdminConfig struct {
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
HTTPSMode string `json:"https_mode"`
|
||||
TLSCert string `json:"tls_cert"`
|
||||
TLSKey string `json:"tls_key"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
HTTPSMode string `json:"https_mode"`
|
||||
ProxyHTTPSPort int `json:"proxy_https_port,omitempty"`
|
||||
TLSCert string `json:"tls_cert"`
|
||||
TLSKey string `json:"tls_key"`
|
||||
}
|
||||
|
||||
type WebConfig struct {
|
||||
@@ -287,6 +288,7 @@ func (c *Config) ApplyEnvironment() error {
|
||||
target *int
|
||||
}{
|
||||
{"SCERT_SERVER_PORT", &c.Server.Port},
|
||||
{"SCERT_SERVER_PROXY_HTTPS_PORT", &c.Server.ProxyHTTPSPort},
|
||||
{"SCERT_ACME_RENEWAL_CHECK_INTERVAL_DAYS", &c.ACME.RenewalCheckIntervalDays},
|
||||
{"SCERT_ACME_RENEWAL_NEAR_EXPIRY_DAYS", &c.ACME.RenewalNearExpiryDays},
|
||||
{"SCERT_ACME_RENEWAL_THRESHOLD_DAYS", &c.ACME.RenewalThresholdDays},
|
||||
@@ -459,6 +461,9 @@ func (c Config) Validate() error {
|
||||
if c.Server.HTTPSMode != "direct" && c.Server.HTTPSMode != "proxy" {
|
||||
return fmt.Errorf("HTTPS 模式无效:%s", c.Server.HTTPSMode)
|
||||
}
|
||||
if c.Server.ProxyHTTPSPort < 0 || c.Server.ProxyHTTPSPort > 65535 {
|
||||
return fmt.Errorf("代理 HTTPS 端口必须为 1 至 65535,或使用 0 表示默认 443")
|
||||
}
|
||||
if c.Database.Driver != "sqlite" && c.Database.Driver != "mysql" && c.Database.Driver != "postgres" {
|
||||
return fmt.Errorf("数据库驱动无效:%s", c.Database.Driver)
|
||||
}
|
||||
@@ -642,8 +647,8 @@ func databaseDSNContainsPassword(driver, dsn string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ResetURL 返回通过主 Web 服务访问密码重置入口的公开地址。
|
||||
func (c Config) ResetURL() string {
|
||||
// ManagementURL 使用公开入口端口,代理模式不暴露内部监听端口。
|
||||
func (c Config) ManagementURL() string {
|
||||
rawHost := strings.TrimSpace(c.Web.Hostname)
|
||||
if rawHost == "" {
|
||||
rawHost = strings.TrimSpace(c.Web.PublicIP)
|
||||
@@ -656,7 +661,26 @@ func (c Config) ResetURL() string {
|
||||
if ip := net.ParseIP(host); ip != nil && ip.To4() == nil {
|
||||
host = "[" + host + "]"
|
||||
}
|
||||
return (&url.URL{Scheme: "https", Host: host, Path: "/reset"}).String()
|
||||
port := c.Server.Port
|
||||
if c.Server.HTTPSMode == "proxy" {
|
||||
port = c.Server.ProxyHTTPSPort
|
||||
if port == 0 {
|
||||
port = 443
|
||||
}
|
||||
}
|
||||
if port != 443 {
|
||||
host = net.JoinHostPort(strings.Trim(host, "[]"), strconv.Itoa(port))
|
||||
}
|
||||
return (&url.URL{Scheme: "https", Host: host, Path: "/"}).String()
|
||||
}
|
||||
|
||||
// ResetURL 与管理入口共用域名和端口,不增加独立监听服务。
|
||||
func (c Config) ResetURL() string {
|
||||
address := c.ManagementURL()
|
||||
if address == "" {
|
||||
return ""
|
||||
}
|
||||
return address + "reset"
|
||||
}
|
||||
|
||||
func splitResetHost(rawHost string) (string, string) {
|
||||
|
||||
@@ -113,12 +113,12 @@ func TestValidateProtectsExternalDatabasePasswords(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetURLAlwaysUsesWebHostnameWithoutPort(t *testing.T) {
|
||||
func TestResetURLUsesDirectEntrypointPort(t *testing.T) {
|
||||
cfg := Default(t.TempDir())
|
||||
cfg.Web.Hostname = "scert.example.test"
|
||||
cfg.Server.Port = 8443
|
||||
if value := cfg.ResetURL(); value != "https://scert.example.test/reset" {
|
||||
t.Fatalf("密码重置地址不得暴露主服务端口:%s", value)
|
||||
if value := cfg.ResetURL(); value != "https://scert.example.test:8443/reset" {
|
||||
t.Fatalf("密码重置地址应使用直接监听端口:%s", value)
|
||||
}
|
||||
cfg.Server.Port = 443
|
||||
if value := cfg.ResetURL(); value != "https://scert.example.test/reset" {
|
||||
@@ -126,11 +126,11 @@ func TestResetURLAlwaysUsesWebHostnameWithoutPort(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetURLSupportsPublicIPv6WithoutPort(t *testing.T) {
|
||||
func TestResetURLSupportsPublicIPv6WithPort(t *testing.T) {
|
||||
cfg := Default(t.TempDir())
|
||||
cfg.Web.PublicIP = "2001:db8::10"
|
||||
cfg.Server.Port = 8443
|
||||
if value := cfg.ResetURL(); value != "https://[2001:db8::10]/reset" {
|
||||
if value := cfg.ResetURL(); value != "https://[2001:db8::10]:8443/reset" {
|
||||
t.Fatalf("IPv6 密码重置地址不正确:%s", value)
|
||||
}
|
||||
}
|
||||
@@ -156,6 +156,7 @@ func TestResetURLIgnoresExplicitPortAndURLInput(t *testing.T) {
|
||||
cfg := Default(t.TempDir())
|
||||
cfg.Web.Hostname = "https://scert.example.test:9443"
|
||||
cfg.Server.Port = 5200
|
||||
cfg.Server.HTTPSMode = "proxy"
|
||||
if value := cfg.ResetURL(); value != "https://scert.example.test/reset" {
|
||||
t.Fatalf("外部地址中的显式端口不得出现在重置地址中:%s", value)
|
||||
}
|
||||
@@ -165,6 +166,7 @@ func TestResetURLIgnoresPathFromWebHostname(t *testing.T) {
|
||||
cfg := Default(t.TempDir())
|
||||
cfg.Web.Hostname = "scert.example.test/manage"
|
||||
cfg.Server.Port = 8443
|
||||
cfg.Server.HTTPSMode = "proxy"
|
||||
if value := cfg.ResetURL(); value != "https://scert.example.test/reset" {
|
||||
t.Fatalf("管理页面域名附带路径时重置地址不正确:%s", value)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestProxyHTTPSPortValidationAndPersistence(t *testing.T) {
|
||||
for _, port := range []int{-1, 65536} {
|
||||
cfg := Default(t.TempDir())
|
||||
cfg.Server.ProxyHTTPSPort = port
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Fatalf("非法代理端口未被拒绝:%d", port)
|
||||
}
|
||||
}
|
||||
for _, port := range []int{0, 1, 443, 8004, 65535} {
|
||||
cfg := Default(t.TempDir())
|
||||
cfg.Server.HTTPSMode = "proxy"
|
||||
cfg.Server.ProxyHTTPSPort = port
|
||||
if err := cfg.Validate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
if err := cfg.Save(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := Load(path)
|
||||
if err != nil || loaded.Server.ProxyHTTPSPort != port {
|
||||
t.Fatalf("代理端口保存失败:%d,%v", port, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyHTTPSPortEnvironment(t *testing.T) {
|
||||
t.Setenv("SCERT_SERVER_PROXY_HTTPS_PORT", "8004")
|
||||
cfg := Default(t.TempDir())
|
||||
if err := cfg.ApplyEnvironment(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Server.ProxyHTTPSPort != 8004 {
|
||||
t.Fatal("环境变量未覆盖代理端口")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user