Add whitelist for PDS hosts and update repo PDS pointer on appropriate occasions

This commit is contained in:
Max Ignatenko 2024-02-22 12:05:44 +00:00
parent 600dac7694
commit 8f32c494f7
8 changed files with 108 additions and 43 deletions

View file

@ -3,6 +3,7 @@ package pds
import (
"context"
"fmt"
"path/filepath"
"time"
"gorm.io/gorm"
@ -12,6 +13,11 @@ import (
const Unknown models.ID = 0
var whitelist []string = []string{
"https://bsky.social",
"https://*.bsky.network",
}
type PDS struct {
ID models.ID `gorm:"primarykey"`
CreatedAt time.Time
@ -21,6 +27,7 @@ type PDS struct {
FirstCursorSinceReset int64
LastList time.Time
CrawlLimit int
Disabled bool
}
func AutoMigrate(db *gorm.DB) error {
@ -28,9 +35,21 @@ func AutoMigrate(db *gorm.DB) error {
}
func EnsureExists(ctx context.Context, db *gorm.DB, host string) (*PDS, error) {
if !IsWhitelisted(host) {
return nil, fmt.Errorf("host %q is not whitelisted", host)
}
remote := PDS{Host: host}
if err := db.Model(&remote).Where(&PDS{Host: host}).FirstOrCreate(&remote).Error; err != nil {
return nil, fmt.Errorf("failed to get PDS record from DB for %q: %w", remote.Host, err)
}
return &remote, nil
}
func IsWhitelisted(host string) bool {
for _, p := range whitelist {
if match, _ := filepath.Match(p, host); match {
return true
}
}
return false
}