支持可配置反代HTTPS端口并统一管理与重置入口
This commit is contained in:
1 parent
b2b8951bcf
commit
54b19a19f8
23 files changed
+268
-50
No files matched your search
@@ -5,11 +5,8 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -66,6 +63,7 @@ type CompleteRequest struct {
|
||||
AdminEmail string `json:"admin_email"`
|
||||
AdminPassword string `json:"admin_password"`
|
||||
ServerPort int `json:"server_port"`
|
||||
ProxyHTTPSPort int `json:"proxy_https_port"`
|
||||
WebHostname string `json:"web_hostname"`
|
||||
WebPublicIP string `json:"web_public_ip"`
|
||||
WebEnabled bool `json:"web_enabled"`
|
||||
@@ -264,7 +262,7 @@ func (completer *Completer) Complete(ctx context.Context, request CompleteReques
|
||||
PropagationPollingIntervalSecs: request.PropagationPollingIntervalSecs,
|
||||
CertificateRoot: candidate.Paths.Certificate,
|
||||
WebHostname: request.WebHostname, WebPublicIP: request.WebPublicIP,
|
||||
WebEnabled: request.WebEnabled, HTTPSMode: request.HTTPSMode,
|
||||
WebEnabled: request.WebEnabled, HTTPSMode: request.HTTPSMode, ProxyHTTPSPort: request.ProxyHTTPSPort,
|
||||
}
|
||||
applyNotificationInput(&input, notification)
|
||||
if err := setup.Apply(&candidate, input); err != nil {
|
||||
@@ -503,17 +501,7 @@ func (completer *Completer) cleanupCredential(cleanup func() error, message stri
|
||||
}
|
||||
|
||||
func managementURL(candidate config.Config) string {
|
||||
host := strings.TrimSpace(candidate.Web.Hostname)
|
||||
if host == "" {
|
||||
host = strings.TrimSpace(candidate.Web.PublicIP)
|
||||
}
|
||||
if ip := net.ParseIP(strings.Trim(host, "[]")); ip != nil && ip.To4() == nil {
|
||||
host = "[" + ip.String() + "]"
|
||||
}
|
||||
if candidate.Server.HTTPSMode != "proxy" && candidate.Server.Port != 443 {
|
||||
host = net.JoinHostPort(strings.Trim(host, "[]"), strconv.Itoa(candidate.Server.Port))
|
||||
}
|
||||
return (&url.URL{Scheme: "https", Host: host, Path: "/"}).String()
|
||||
return candidate.ManagementURL()
|
||||
}
|
||||
|
||||
func completeError(kind CompleteErrorKind, message string, err error) error {
|
||||
|
||||
@@ -176,22 +176,29 @@ func TestManagementURLHandlesDirectAndProxyEntrypoints(t *testing.T) {
|
||||
for _, item := range []struct {
|
||||
hostname, ip, mode string
|
||||
port int
|
||||
proxyPort int
|
||||
want string
|
||||
}{
|
||||
{"admin.example.test", "", "direct", 443, "https://admin.example.test/"},
|
||||
{"admin.example.test", "", "direct", 8443, "https://admin.example.test:8443/"},
|
||||
{"admin.example.test", "", "proxy", 8080, "https://admin.example.test/"},
|
||||
{"", "203.0.113.70", "direct", 8443, "https://203.0.113.70:8443/"},
|
||||
{"", "2001:db8::70", "direct", 443, "https://[2001:db8::70]/"},
|
||||
{"", "2001:db8::70", "direct", 8443, "https://[2001:db8::70]:8443/"},
|
||||
{"", "2001:db8::70", "proxy", 8080, "https://[2001:db8::70]/"},
|
||||
{"admin.example.test", "", "direct", 443, 8004, "https://admin.example.test/"},
|
||||
{"admin.example.test", "", "direct", 8443, 8004, "https://admin.example.test:8443/"},
|
||||
{"admin.example.test", "", "proxy", 8080, 0, "https://admin.example.test/"},
|
||||
{"admin.example.test", "", "proxy", 9004, 8004, "https://admin.example.test:8004/"},
|
||||
{"other.example.test", "", "proxy", 9004, 443, "https://other.example.test/"},
|
||||
{"", "203.0.113.70", "direct", 8443, 0, "https://203.0.113.70:8443/"},
|
||||
{"", "2001:db8::70", "direct", 443, 0, "https://[2001:db8::70]/"},
|
||||
{"", "2001:db8::70", "direct", 8443, 0, "https://[2001:db8::70]:8443/"},
|
||||
{"", "2001:db8::70", "proxy", 8080, 0, "https://[2001:db8::70]/"},
|
||||
{"", "2001:db8::70", "proxy", 9004, 8004, "https://[2001:db8::70]:8004/"},
|
||||
} {
|
||||
candidate := config.Config{
|
||||
Web: config.WebConfig{Hostname: item.hostname, PublicIP: item.ip},
|
||||
Server: config.ServerConfig{Port: item.port, HTTPSMode: item.mode},
|
||||
Server: config.ServerConfig{Port: item.port, HTTPSMode: item.mode, ProxyHTTPSPort: item.proxyPort},
|
||||
}
|
||||
if actual := managementURL(candidate); actual != item.want {
|
||||
t.Fatalf("管理入口不正确:实际=%s,预期=%s", actual, item.want)
|
||||
}
|
||||
if actual := candidate.ResetURL(); actual != item.want+"reset" {
|
||||
t.Fatalf("重置入口与管理入口不一致:%s", actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
func TestCompleterPreservesExternalProxyPort(t *testing.T) {
|
||||
fixture := newCompletionFixture(t, "admin.example.test")
|
||||
request := fixture.request("admin.example.test")
|
||||
request.HTTPSMode = "proxy"
|
||||
request.ServerPort = 9004
|
||||
request.ProxyHTTPSPort = 8004
|
||||
result, err := fixture.completer(nil).Complete(context.Background(), request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.ManagementURL != "https://admin.example.test:8004/" {
|
||||
t.Fatalf("初始化结果未保留代理端口:%s", result.ManagementURL)
|
||||
}
|
||||
cfg, err := config.Load(fixture.configPath)
|
||||
if err != nil || cfg.Server.ProxyHTTPSPort != 8004 || cfg.Server.Port != 9004 {
|
||||
t.Fatalf("保存代理端口失败:%v", err)
|
||||
}
|
||||
}
|
||||
@@ -187,7 +187,7 @@ func bootstrapHandler(router *Router, cfg config.Config) (http.Handler, func())
|
||||
writeBootstrapJSON(writer, http.StatusOK, map[string]any{
|
||||
"mode": "bootstrap", "initialized": false, "server_port": cfg.Server.Port,
|
||||
"web_enabled": true, "web_hostname": cfg.Web.Hostname, "web_public_ip": cfg.Web.PublicIP,
|
||||
"https_mode": cfg.Server.HTTPSMode, "certificate_root": cfg.Paths.Certificate,
|
||||
"https_mode": cfg.Server.HTTPSMode, "proxy_https_port": cfg.Server.ProxyHTTPSPort, "certificate_root": cfg.Paths.Certificate,
|
||||
"dns_provider": dnsprovider.Aliyun, "acme_environment": cfg.ACME.Environment,
|
||||
"propagation_timeout_secs": cfg.ACME.PropagationTimeoutSecs,
|
||||
"propagation_polling_interval_secs": cfg.ACME.PropagationPollingIntervalSecs,
|
||||
|
||||
Reference in new issue
Block a user