violet/domains/domains.go

133 lines
2.8 KiB
Go
Raw Normal View History

2023-04-21 03:21:46 +01:00
package domains
import (
2023-04-21 15:49:01 +01:00
"database/sql"
_ "embed"
"github.com/MrMelon54/rescheduler"
2023-04-21 03:21:46 +01:00
"github.com/MrMelon54/violet/utils"
2023-04-21 15:49:01 +01:00
"log"
2023-04-21 03:21:46 +01:00
"strings"
"sync"
)
//go:embed create-table-domains.sql
var createTableDomains string
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 {
2023-04-21 15:49:01 +01:00
db *sql.DB
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
2023-04-21 15:49:01 +01:00
func New(db *sql.DB) *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 22:18:39 +01:00
// init domains table
_, err := a.db.Exec(createTableDomains)
2023-04-22 22:18:39 +01:00
if err != nil {
log.Printf("[WARN] Failed to generate 'domains' table\n")
return nil
}
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.Query(`select domain from domains where active = 1`)
2023-04-21 15:49:01 +01:00
if err != nil {
return err
}
defer rows.Close()
// loop through rows and scan the allowed domain names
for rows.Next() {
var name string
err = rows.Scan(&name)
if err != nil {
return err
}
m[name] = struct{}{}
}
// check for errors
return rows.Err()
}
func (d *Domains) Put(domain string, active bool) {
d.s.Lock()
defer d.s.Unlock()
_, err := d.db.Exec("INSERT OR REPLACE INTO domains (domain, active) VALUES (?, ?)", domain, 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.Exec("INSERT OR REPLACE INTO domains (domain, active) VALUES (?, ?)", domain, false)
if err != nil {
log.Printf("[Violet] Database error: %s\n", err)
}
}