violet/domains/domains.go

120 lines
2.4 KiB
Go
Raw Normal View History

2023-04-21 03:21:46 +01:00
package domains
import (
"context"
_ "embed"
"github.com/1f349/violet/database"
2023-07-22 01:11:47 +01:00
"github.com/1f349/violet/utils"
"github.com/MrMelon54/rescheduler"
2023-04-21 15:49:01 +01:00
"log"
2023-04-21 03:21:46 +01:00
"strings"
"sync"
)
2023-04-24 01:35:23 +01:00
// Domains is the domain list and management system.
2023-04-21 03:21:46 +01:00
type Domains struct {
db *database.Queries
2023-04-21 15:49:01 +01:00
s *sync.RWMutex
m map[string]struct{}
r *rescheduler.Rescheduler
2023-04-21 03:21:46 +01:00
}
2023-04-22 18:11:21 +01:00
// New creates a new domain list
func New(db *database.Queries) *Domains {
2023-04-22 18:11:21 +01:00
a := &Domains{
2023-04-21 15:49:01 +01:00
db: db,
s: &sync.RWMutex{},
m: make(map[string]struct{}),
2023-04-21 03:21:46 +01:00
}
a.r = rescheduler.NewRescheduler(a.threadCompile)
2023-04-22 18:11:21 +01:00
return a
2023-04-21 03:21:46 +01:00
}
2023-04-22 18:11:21 +01:00
// IsValid returns true if a domain is valid.
2023-04-21 03:21:46 +01:00
func (d *Domains) IsValid(host string) bool {
domain, _, _ := utils.SplitDomainPort(host, 0)
2023-04-21 15:49:01 +01:00
// read lock for safety
2023-04-21 03:21:46 +01:00
d.s.RLock()
defer d.s.RUnlock()
2023-04-21 15:49:01 +01:00
// check root domains `www.example.com`, `example.com`, `com`
2023-06-03 19:33:06 +01:00
for len(domain) > 0 {
if _, ok := d.m[domain]; ok {
2023-04-21 03:21:46 +01:00
return true
}
2023-06-03 19:33:06 +01:00
n := strings.IndexByte(domain, '.')
if n == -1 {
break
}
domain = domain[n+1:]
2023-04-21 03:21:46 +01:00
}
return false
}
2023-04-21 15:49:01 +01:00
2023-04-22 18:11:21 +01:00
// Compile downloads the list of domains from the database and loads them into
// memory for faster lookups.
//
// This method makes use of the rescheduler instead of just ignoring multiple
// calls.
2023-04-21 15:49:01 +01:00
func (d *Domains) Compile() {
d.r.Run()
}
2023-04-24 01:35:23 +01:00
func (d *Domains) threadCompile() {
// new map
domainMap := make(map[string]struct{})
2023-04-24 01:35:23 +01:00
// compile map and check errors
err := d.internalCompile(domainMap)
if err != nil {
log.Printf("[Domains] Compile failed: %s\n", err)
return
}
// lock while replacing the map
d.s.Lock()
d.m = domainMap
d.s.Unlock()
2023-04-21 15:49:01 +01:00
}
2023-04-22 18:11:21 +01:00
// internalCompile is a hidden internal method for querying the database during
// the Compile() method.
2023-04-21 15:49:01 +01:00
func (d *Domains) internalCompile(m map[string]struct{}) error {
log.Println("[Domains] Updating domains from database")
// sql or something?
rows, err := d.db.GetActiveDomains(context.Background())
2023-04-21 15:49:01 +01:00
if err != nil {
return err
}
for _, i := range rows {
m[i] = struct{}{}
2023-04-21 15:49:01 +01:00
}
// check for errors
return nil
2023-04-21 15:49:01 +01:00
}
func (d *Domains) Put(domain string, active bool) {
d.s.Lock()
defer d.s.Unlock()
err := d.db.AddDomain(context.Background(), database.AddDomainParams{
Domain: domain,
Active: active,
})
if err != nil {
log.Printf("[Violet] Database error: %s\n", err)
}
}
func (d *Domains) Delete(domain string) {
d.s.Lock()
defer d.s.Unlock()
err := d.db.DeleteDomain(context.Background(), domain)
if err != nil {
log.Printf("[Violet] Database error: %s\n", err)
}
}