1
0

node_mtls.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package service
  2. import (
  3. "crypto/x509"
  4. "encoding/pem"
  5. "strings"
  6. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  7. )
  8. // NodeMtlsCaCert returns the PEM of this panel's node-auth CA certificate (the
  9. // public half) to copy into a node's mTLS trust setting, minting the CA and the
  10. // master client cert on first call so the panel is ready to present a client
  11. // certificate to mtls nodes.
  12. func (s *NodeService) NodeMtlsCaCert() (string, error) {
  13. settings := SettingService{}
  14. ca, err := settings.EnsureNodeMtlsCA()
  15. if err != nil {
  16. return "", err
  17. }
  18. if _, err := settings.EnsureMasterClientCert(); err != nil {
  19. return "", err
  20. }
  21. return string(ca.CertPEM), nil
  22. }
  23. // SetNodeMtlsTrustCA stores the CA certificate this panel trusts for incoming
  24. // node-API client certificates. An empty value clears it (mTLS off). A
  25. // non-empty value must be a PEM certificate (fail closed). Takes effect on the
  26. // next panel restart, when the listener's ClientCAs is rebuilt.
  27. func (s *NodeService) SetNodeMtlsTrustCA(caPem string) error {
  28. caPem = strings.TrimSpace(caPem)
  29. if caPem != "" {
  30. block, _ := pem.Decode([]byte(caPem))
  31. if block == nil || block.Type != "CERTIFICATE" {
  32. return common.NewError("trust CA must be a PEM-encoded certificate")
  33. }
  34. if _, err := x509.ParseCertificate(block.Bytes); err != nil {
  35. return common.NewError("invalid trust CA certificate: " + err.Error())
  36. }
  37. }
  38. return (&SettingService{}).setString(settingNodeMtlsClientCA, caPem)
  39. }