violet/domains/domains.go

110 lines
2.3 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"
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"
)
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{}
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
}
2023-04-22 22:18:39 +01:00
// init domains table
_, err := a.db.Exec(`create table if not exists domains (id integer primary key autoincrement, domain varchar)`)
if err != nil {
log.Printf("[WARN] Failed to generate 'domains' table\n")
return nil
}
// run compile to get the initial data
2023-04-22 18:11:21 +01:00
a.Compile()
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-21 15:49:01 +01:00
// remove the port
2023-04-21 03:21:46 +01:00
domain, ok := utils.GetDomainWithoutPort(host)
if !ok {
return false
}
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-04-22 18:11:21 +01:00
// TODO: could be faster using indexes and cropping the string?
2023-04-21 03:21:46 +01:00
n := strings.Split(domain, ".")
for i := 0; i < len(n); i++ {
if _, ok := d.m[strings.Join(n[i:], ".")]; ok {
return true
}
}
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 is asynchronous and uses locks for safety.
2023-04-21 15:49:01 +01:00
func (d *Domains) Compile() {
// async compile magic
go func() {
2023-04-24 01:35:23 +01:00
// new map
2023-04-21 15:49:01 +01:00
domainMap := make(map[string]struct{})
2023-04-24 01:35:23 +01:00
// compile map and check errors
2023-04-21 15:49:01 +01:00
err := d.internalCompile(domainMap)
if err != nil {
log.Printf("[Domains] Compile failed: %s\n", err)
return
}
2023-04-24 01:35:23 +01:00
2023-04-21 15:49:01 +01:00
// lock while replacing the map
d.s.Lock()
d.m = domainMap
d.s.Unlock()
}()
}
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 name from domains where enabled = true")
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()
}