2023-04-21 03:21:46 +01:00
|
|
|
package domains
|
|
|
|
|
|
|
|
import (
|
2024-03-08 16:05:39 +00:00
|
|
|
"context"
|
2023-04-24 15:36:21 +01:00
|
|
|
_ "embed"
|
2024-03-08 16:05:39 +00:00
|
|
|
"github.com/1f349/violet/database"
|
2023-07-22 01:11:47 +01:00
|
|
|
"github.com/1f349/violet/utils"
|
2023-06-20 16:48:04 +01:00
|
|
|
"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 {
|
2024-03-08 16:05:39 +00:00
|
|
|
db *database.Queries
|
2023-04-21 15:49:01 +01:00
|
|
|
s *sync.RWMutex
|
|
|
|
m map[string]struct{}
|
2023-06-20 16:48:04 +01:00
|
|
|
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
|
2024-03-08 16:05:39 +00:00
|
|
|
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
|
|
|
}
|
2023-06-20 16:48:04 +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 {
|
2023-04-24 15:36:21 +01:00
|
|
|
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.
|
|
|
|
//
|
2023-06-20 16:48:04 +01:00
|
|
|
// 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() {
|
2023-06-20 16:48:04 +01:00
|
|
|
d.r.Run()
|
|
|
|
}
|
2023-04-24 01:35:23 +01:00
|
|
|
|
2023-06-20 16:48:04 +01:00
|
|
|
func (d *Domains) threadCompile() {
|
|
|
|
// new map
|
|
|
|
domainMap := make(map[string]struct{})
|
2023-04-24 01:35:23 +01:00
|
|
|
|
2023-06-20 16:48:04 +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?
|
2024-03-08 16:05:39 +00:00
|
|
|
rows, err := d.db.GetActiveDomains(context.Background())
|
2023-04-21 15:49:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-08 16:05:39 +00:00
|
|
|
|
|
|
|
for _, i := range rows {
|
|
|
|
m[i] = struct{}{}
|
2023-04-21 15:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for errors
|
2024-03-08 16:05:39 +00:00
|
|
|
return nil
|
2023-04-21 15:49:01 +01:00
|
|
|
}
|
2023-06-20 16:48:04 +01:00
|
|
|
|
|
|
|
func (d *Domains) Put(domain string, active bool) {
|
|
|
|
d.s.Lock()
|
|
|
|
defer d.s.Unlock()
|
2024-03-08 16:05:39 +00:00
|
|
|
err := d.db.AddDomain(context.Background(), database.AddDomainParams{
|
|
|
|
Domain: domain,
|
|
|
|
Active: active,
|
|
|
|
})
|
2023-06-20 16:48:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[Violet] Database error: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Domains) Delete(domain string) {
|
|
|
|
d.s.Lock()
|
|
|
|
defer d.s.Unlock()
|
2024-03-08 16:05:39 +00:00
|
|
|
err := d.db.DeleteDomain(context.Background(), domain)
|
2023-06-20 16:48:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[Violet] Database error: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|