···2233import (
44 "fmt"
55- "strings"
6576 "github.com/bluesky-social/indigo/cmd/relay/relay/models"
87)
981010-// Checks whether a host is allowed to be subscribed to
1111-//
1212-// Must be called with the slurper lock held
1313-func (r *Relay) canSlurpHost(hostname string) bool {
1414- // Check if we're over the limit for new hosts today
1515- if !r.HostPerDayLimiter.Allow() {
1616- return false
1717- }
1818-1919- // Check if the host is a trusted domain
2020- for _, d := range r.Config.TrustedDomains {
2121- // If the domain starts with a *., it's a wildcard
2222- if strings.HasPrefix(d, "*.") {
2323- // Cut off the * so we have .domain.com
2424- if strings.HasSuffix(hostname, strings.TrimPrefix(d, "*")) {
2525- return true
2626- }
2727- } else {
2828- if hostname == d {
2929- return true
3030- }
3131- }
3232- }
3333-3434- return true
3535-}
3636-379func (r *Relay) SubscribeToHost(hostname string, noSSL, adminForce bool) error {
38103911 // if we already have an active subscription, exit early
···50225123 if host.ID == 0 {
5224 newHost = true
5353- if !adminForce && !r.canSlurpHost(hostname) {
2525+2626+ // check if we're over the limit for new hosts today (bypass if admin mode)
2727+ if !adminForce && !r.HostPerDayLimiter.Allow() {
5428 // TODO: is this the correct error code?
5529 return ErrNewSubsDisabled
5630 }
57315858- // XXX: new host daily rate-limit
3232+ accountLimit := r.Config.DefaultRepoLimit
3333+ trusted := IsTrustedHostname(hostname, r.Config.TrustedDomains)
3434+ if trusted {
3535+ accountLimit = r.Config.TrustedRepoLimit
3636+ }
59376038 host = models.Host{
6139 Hostname: hostname,
6240 NoSSL: noSSL,
6341 Status: models.HostStatusActive,
6464- AccountLimit: r.Config.DefaultRepoLimit,
4242+ Trusted: trusted,
4343+ AccountLimit: accountLimit,
6544 }
66456767- if err := r.db.Create(&newHost).Error; err != nil {
4646+ if err := r.db.Create(&host).Error; err != nil {
6847 return err
6948 }
7049
+18
cmd/relay/relay/host.go
···6767 return r.db.Model(models.Host{}).Where("id = ?", hostID).Update("status", status).Error
6868}
69697070+func (r *Relay) UpdateHostAccountLimit(ctx context.Context, hostID uint64, accountLimit int64) error {
7171+7272+ host, err := r.GetHostByID(ctx, hostID)
7373+ if err != nil {
7474+ return err
7575+ }
7676+7777+ if err := r.db.Model(models.Host{}).Where("id = ?", hostID).Update("account_limit", accountLimit).Error; err != nil {
7878+ return err
7979+ }
8080+8181+ if r.Slurper.CheckIfSubscribed(host.Hostname) {
8282+ return r.Slurper.UpdateLimiters(host.Hostname, accountLimit, host.Trusted)
8383+ }
8484+8585+ return nil
8686+}
8787+7088// Persists all the host cursors in a single database transaction
7189//
7290// Note that in some situations this may have partial success.
+2-5
cmd/relay/relay/models/models.go
···3737 // maximum number of active accounts
3838 AccountLimit int64 `gorm:"column:account_limit"`
39394040- // TODO: ThrottleUntil time.Time
4141-4240 // indicates this is a highly trusted host (PDS), and different rate limits apply
4341 Trusted bool `gorm:"column:trusted;default:false"`
4442···79778078 // this is a reference to the ID field on Host; but it is not an explicit foreign key
8179 HostID uint64 `gorm:"column:host_id;not null"`
8282- Status AccountStatus `gorm:"column:status;default:active"`
8383- UpstreamStatus AccountStatus `gorm:"column:upstream_status;default:active"`
8484- ThrottleUntil time.Time `gorm:"column:throttle_util"`
8080+ Status AccountStatus `gorm:"column:status;not null;default:active"`
8181+ UpstreamStatus AccountStatus `gorm:"column:upstream_status;not null;default:active"`
8582}
86838784func (Account) TableName() string {