Support configurable TLS probes in the API and dashboard

This commit is contained in:
ayurishchev committed 2026-11-20 21:15:22 +03:00
1 parent 0bde5627f0
commit c7fdd2c430
38 files changed
+495 -203

No files matched your search

+48 -12
View File
@@ -6,12 +6,15 @@ import (
"net/http"
"strconv"
"strings"
"cloudipvalidator/internal/inbound"
)
type settingsPageData struct {
PageData
Settings orchestratorSettingsDTO
Inbound inboundChecksDTO
Settings orchestratorSettingsDTO
Inbound inboundChecksDTO
InboundError bannerData
// AutoCycle is the automatic-check-cycle panel's status/parameters.
AutoCycle autoCycleDTO
// Subnets is the list the analytics page groups addresses by.
@@ -43,6 +46,10 @@ func (s *Server) handleSettingsPage(w http.ResponseWriter, r *http.Request) {
// banner — same pattern as renderIPsTable: always reflect true current
// state regardless of whether the mutation itself succeeded.
func (s *Server) renderSettingsForm(w http.ResponseWriter, r *http.Request, actionErr error) {
s.renderSettings(w, r, actionErr, nil)
}
func (s *Server) renderSettings(w http.ResponseWriter, r *http.Request, actionErr, inboundFormErr error) {
settings, getErr := s.CA.GetOrchestratorSettings(r.Context())
if actionErr == nil {
actionErr = getErr
@@ -59,7 +66,9 @@ func (s *Server) renderSettingsForm(w http.ResponseWriter, r *http.Request, acti
if actionErr == nil {
actionErr = subnetsErr
}
s.renderFragment(w, "settings_form", settingsPageData{Settings: settings, Inbound: inbound, AutoCycle: autoCycle, Subnets: subnets}, actionErr)
s.renderFragment(w, "settings_form", settingsPageData{
Settings: settings, Inbound: inbound, InboundError: bannerFor(inboundFormErr), AutoCycle: autoCycle, Subnets: subnets,
}, actionErr)
}
func (s *Server) handleSettingsPut(w http.ResponseWriter, r *http.Request) {
@@ -86,14 +95,9 @@ func (s *Server) handleSettingsPut(w http.ResponseWriter, r *http.Request) {
s.renderSettingsForm(w, r, err)
}
// handleInboundChecksPut parses the comma-separated ports field of the
// second form on /settings and saves it via PUT
// /api/v1/admin/config/inbound-checks. A separate form/handler from
// fip_settle_seconds above — the two settings are unrelated and shouldn't
// share one submit.
func (s *Server) handleInboundChecksPut(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
s.renderSettingsForm(w, r, fmt.Errorf("invalid form: %w", err))
s.renderSettings(w, r, nil, fmt.Errorf("invalid form: %w", err))
return
}
raw := strings.TrimSpace(r.PostFormValue("ports"))
@@ -106,15 +110,47 @@ func (s *Server) handleInboundChecksPut(w http.ResponseWriter, r *http.Request)
}
p, err := strconv.Atoi(part)
if err != nil {
s.renderSettingsForm(w, r, &apiErr{Status: http.StatusBadRequest, Message: "порты должны быть целыми числами через запятую"})
s.renderSettings(w, r, nil, &apiErr{Status: http.StatusBadRequest, Message: "порты должны быть целыми числами через запятую"})
return
}
ports = append(ports, p)
}
}
tlsChecks, err := parseInboundTLSForm(r)
if err != nil {
s.renderSettings(w, r, nil, &apiErr{Status: http.StatusBadRequest, Message: err.Error()})
return
}
icmp := r.PostFormValue("icmp") == "on"
_, err := s.CA.PutInboundChecks(r.Context(), ports, icmp)
s.renderSettingsForm(w, r, err)
_, err = s.CA.PutInboundChecks(r.Context(), ports, icmp, tlsChecks)
s.renderSettings(w, r, nil, err)
}
func parseInboundTLSForm(r *http.Request) (*[]inbound.TLSCheck, error) {
var checks []inbound.TLSCheck
switch r.PostFormValue("tls_mode") {
case "":
return nil, nil
case "auto":
return &checks, nil
case "disabled":
checks = []inbound.TLSCheck{}
case "custom":
ports, versions := r.PostForm["tls_port"], r.PostForm["tls_version"]
if len(ports) == 0 || len(ports) != len(versions) {
return nil, fmt.Errorf("добавьте хотя бы одну TLS-проверку с портом и версией")
}
for i, rawPort := range ports {
port, err := strconv.Atoi(rawPort)
if err != nil {
return nil, fmt.Errorf("порт в строке TLS %d должен быть целым числом", i+1)
}
checks = append(checks, inbound.TLSCheck{Port: port, Version: versions[i]})
}
default:
return nil, fmt.Errorf("выберите режим TLS")
}
return &checks, nil
}
// handleSubnetsPut saves the subnet list from the textarea of /settings: one