1 parent
6ad6f449ca
commit
362cf0d4cc
8 files changed
+99
-27
No files matched your search
@@ -77,7 +77,9 @@ func CurrentHTTPConfig() HTTPConfig {
|
||||
func ResolveHTTPPortDefaults(value HTTPConfig, setupPort int) HTTPConfig {
|
||||
result := cloneHTTPConfig(value)
|
||||
result.TLSMode = strings.ToLower(strings.TrimSpace(result.TLSMode))
|
||||
if result.Port == 0 {
|
||||
if override := environmentPort("SMAIL_HTTP_PORT_OVERRIDE"); override != 0 {
|
||||
result.Port = override
|
||||
} else if result.Port == 0 {
|
||||
result.Port = environmentPort("SMAIL_HTTP_PORT")
|
||||
if result.Port == 0 && validServicePort(setupPort) {
|
||||
result.Port = setupPort
|
||||
@@ -86,6 +88,9 @@ func ResolveHTTPPortDefaults(value HTTPConfig, setupPort int) HTTPConfig {
|
||||
result.Port = 80
|
||||
}
|
||||
}
|
||||
if override := strings.ToLower(strings.TrimSpace(os.Getenv("SMAIL_HTTP_TLS_MODE_OVERRIDE"))); override == HTTPSTLSModeDirect || override == HTTPSTLSModeProxy {
|
||||
result.TLSMode = override
|
||||
}
|
||||
if result.TLSMode == HTTPSTLSModeProxy {
|
||||
result.HTTPSPort = 0
|
||||
result.HTTPSEnabled = false
|
||||
|
||||
@@ -3,6 +3,8 @@ package config
|
||||
import "testing"
|
||||
|
||||
func TestResolveHTTPPortDefaultsPreservesConfiguredPorts(t *testing.T) {
|
||||
t.Setenv("SMAIL_HTTP_PORT_OVERRIDE", "")
|
||||
t.Setenv("SMAIL_HTTP_TLS_MODE_OVERRIDE", "")
|
||||
t.Setenv("SMAIL_HTTP_PORT", "19001")
|
||||
t.Setenv("SMAIL_HTTPS_PORT", "19002")
|
||||
|
||||
@@ -17,6 +19,8 @@ func TestResolveHTTPPortDefaultsPreservesConfiguredPorts(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResolveHTTPPortDefaultsUsesEnvironmentPorts(t *testing.T) {
|
||||
t.Setenv("SMAIL_HTTP_PORT_OVERRIDE", "")
|
||||
t.Setenv("SMAIL_HTTP_TLS_MODE_OVERRIDE", "")
|
||||
t.Setenv("SMAIL_HTTP_PORT", "9001")
|
||||
t.Setenv("SMAIL_HTTPS_PORT", "9002")
|
||||
|
||||
@@ -27,6 +31,8 @@ func TestResolveHTTPPortDefaultsUsesEnvironmentPorts(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResolveHTTPPortDefaultsUsesSetupAndDirectDefaults(t *testing.T) {
|
||||
t.Setenv("SMAIL_HTTP_PORT_OVERRIDE", "")
|
||||
t.Setenv("SMAIL_HTTP_TLS_MODE_OVERRIDE", "")
|
||||
t.Setenv("SMAIL_HTTP_PORT", "")
|
||||
t.Setenv("SMAIL_HTTPS_PORT", "")
|
||||
|
||||
@@ -37,6 +43,8 @@ func TestResolveHTTPPortDefaultsUsesSetupAndDirectDefaults(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResolveHTTPPortDefaultsDisablesInternalHTTPSInProxyMode(t *testing.T) {
|
||||
t.Setenv("SMAIL_HTTP_PORT_OVERRIDE", "")
|
||||
t.Setenv("SMAIL_HTTP_TLS_MODE_OVERRIDE", "")
|
||||
t.Setenv("SMAIL_HTTP_PORT", "9001")
|
||||
t.Setenv("SMAIL_HTTPS_PORT", "9002")
|
||||
|
||||
@@ -49,3 +57,20 @@ func TestResolveHTTPPortDefaultsDisablesInternalHTTPSInProxyMode(t *testing.T) {
|
||||
t.Fatalf("proxy HTTP config = %+v, want HTTP 9001 with internal HTTPS disabled", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveHTTPPortDefaultsUsesExplicitRuntimeOverride(t *testing.T) {
|
||||
t.Setenv("SMAIL_HTTP_PORT_OVERRIDE", "5201")
|
||||
t.Setenv("SMAIL_HTTP_TLS_MODE_OVERRIDE", HTTPSTLSModeProxy)
|
||||
t.Setenv("SMAIL_HTTP_PORT", "9001")
|
||||
t.Setenv("SMAIL_HTTPS_PORT", "9002")
|
||||
|
||||
got := ResolveHTTPPortDefaults(HTTPConfig{
|
||||
Port: 80,
|
||||
HTTPSPort: 443,
|
||||
HTTPSEnabled: true,
|
||||
TLSMode: HTTPSTLSModeDirect,
|
||||
}, 18080)
|
||||
if got.Port != 5201 || got.HTTPSPort != 0 || got.HTTPSEnabled || got.TLSMode != HTTPSTLSModeProxy {
|
||||
t.Fatalf("runtime override HTTP config = %+v, want proxy API on port 5201", got)
|
||||
}
|
||||
}
|
||||
@@ -31,9 +31,10 @@ func ResolveWebAuthnSecurity(root RootConfig) (SecurityConfig, error) {
|
||||
}
|
||||
if len(security.WebAuthnRPOrigins) == 0 {
|
||||
origin := "https://" + web
|
||||
httpConfig := ResolveHTTPPortDefaults(root.HTTP, 0)
|
||||
// 反代的外部端口不等于内部 HTTP 端口,非标准外部端口需显式配置。
|
||||
if root.HTTP.TLSMode != HTTPSTLSModeProxy {
|
||||
port := ResolveHTTPPortDefaults(root.HTTP, 0).HTTPSPort
|
||||
if httpConfig.TLSMode != HTTPSTLSModeProxy {
|
||||
port := httpConfig.HTTPSPort
|
||||
if !validServicePort(port) {
|
||||
return SecurityConfig{}, fmt.Errorf("安全密钥站点 HTTPS 端口无效")
|
||||
}
|
||||
|
||||
@@ -463,10 +463,7 @@ func dirInit() error {
|
||||
|
||||
// 等待 HTTP 服务启动,最长约 90 秒。
|
||||
func waitHTTPReady(serviceErrors <-chan error) error {
|
||||
port := 80
|
||||
if cfg := config.CurrentConfig(); cfg != nil && cfg.HttpPort > 0 {
|
||||
port = cfg.HttpPort
|
||||
}
|
||||
port := config.CurrentHTTPConfig().Port
|
||||
url := fmt.Sprintf("http://127.0.0.1:%d/api/ping", port)
|
||||
|
||||
client := &http.Client{Timeout: 2 * time.Second}
|
||||
|
||||
Reference in new issue
Block a user