Create CNAME

This commit is contained in:
2025-11-28 12:31:45 +00:00
parent 22552aec99
commit 9c1c1eb9c6
17 changed files with 939 additions and 156 deletions
+17
View File
@@ -292,6 +292,23 @@ type BackupInfo struct {
Size int64
}
// GetBackupContent returns the content of a backup file.
func (m *HostsManager) GetBackupContent(name string) (string, error) {
backupPath := filepath.Join(m.backupDir, name)
// Validate backup name to prevent path traversal
if filepath.Base(name) != name || !strings.HasPrefix(name, "hosts.") || !strings.HasSuffix(name, ".bak") {
return "", fmt.Errorf("invalid backup name")
}
content, err := os.ReadFile(backupPath)
if err != nil {
return "", fmt.Errorf("failed to read backup: %w", err)
}
return string(content), nil
}
// RestoreBackup restores a backup by name.
func (m *HostsManager) RestoreBackup(name string) error {
backupPath := filepath.Join(m.backupDir, name)
+22
View File
@@ -258,6 +258,9 @@ func (s *Server) handleRequest(req *protocol.Request, creds *PeerCredentials) *p
case protocol.RequestBackups:
return s.handleBackups()
case protocol.RequestBackupContent:
return s.handleBackupContent(req)
case protocol.RequestAdd:
resp := s.handleAdd(req)
if s.auditLogger != nil {
@@ -514,6 +517,25 @@ func (s *Server) handleBackups() *protocol.Response {
return resp
}
func (s *Server) handleBackupContent(req *protocol.Request) *protocol.Response {
var payload protocol.BackupContentPayload
if err := req.ParsePayload(&payload); err != nil {
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "invalid payload")
}
if payload.BackupName == "" {
return protocol.NewErrorResponse(protocol.ErrCodeInvalidRequest, "backup name is required")
}
content, err := s.hosts.GetBackupContent(payload.BackupName)
if err != nil {
return protocol.NewErrorResponse(protocol.ErrCodeNotFound, fmt.Sprintf("failed to get backup content: %v", err))
}
resp, _ := protocol.NewOKResponse(protocol.BackupContentData{Content: content})
return resp
}
func (s *Server) handleAdd(req *protocol.Request) *protocol.Response {
var payload protocol.AddPayload
if err := req.ParsePayload(&payload); err != nil {