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"
|
2024-05-13 19:33:33 +01:00
|
|
|
"github.com/1f349/violet/logger"
|
2023-07-22 01:11:47 +01:00
|
|
|
"github.com/1f349/violet/utils"
|
2024-04-20 16:17:32 +01:00
|
|
|
"github.com/mrmelon54/rescheduler"
|
2023-04-21 03:21:46 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2024-05-13 19:33:33 +01:00
|
|
|
var Logger = logger.Logger.WithPrefix("Violet Domains")
|
|
|
|
|
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 {
|
2024-05-13 19:33:33 +01:00
|
|
|
Logger.Info("Compile faile", "err", err)
|
2023-06-20 16:48:04 +01:00
|
|
|
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 {
|
2024-05-13 19:33:33 +01:00
|
|
|
Logger.Info("Updating domains from database")
|
2023-04-21 15:49:01 +01:00
|
|
|
|
|
|
|
// 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 {
|
2024-05-13 19:33:33 +01:00
|
|
|
logger.Logger.Infof("Database error: %s\n", err)
|
2023-06-20 16:48:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-05-13 19:33:33 +01:00
|
|
|
logger.Logger.Infof("Database error: %s\n", err)
|
2023-06-20 16:48:04 +01:00
|
|
|
}
|
|
|
|
}
|