Skip to content

Commit

Permalink
Merge pull request #7 from r7wx/develop
Browse files Browse the repository at this point in the history
configurable blocklists update time
  • Loading branch information
r7wx authored May 11, 2023
2 parents 2da04df + 04a7726 commit 22e1e2a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ hosts:
ip: 127.0.0.1

# luna-dns supports blocklists both from local files or remote URI
# each blocklist will be updated every 12 hours.
# Blocklists must contain only one domain name per line.
# Every blocked record resolves to 0.0.0.0
# ex.
Expand All @@ -97,4 +96,7 @@ blocklists:
- http://test.test/test.txt
- file://folder/test.txt
- file:///root/test.txt

# blocklists update time (in minute)
blocklists_update: 720 # 12 hours (default)
```
5 changes: 5 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ hosts:
ip: 127.0.0.1
- host: "*.test.com"
ip: 127.0.0.1
#blocklists:
# - http://test.test/test.txt
# - file://folder/test.txt
# - file:///root/test.txt
#blocklists_update: 720
17 changes: 10 additions & 7 deletions internal/blocklists/blocklists.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ import (
"github.com/r7wx/luna-dns/internal/tree"
)

const (
updateTime = 12 * time.Hour
)

// Blocklists - Blocklists strutct
type Blocklists struct {
hosts *tree.Tree
blocklists []string
updateTime int64
}

// NewBlocklists - Create a new Blocklists
func NewBlocklists(blocklists []string) *Blocklists {
func NewBlocklists(blocklists []string, updateTime int64) *Blocklists {
if updateTime == 0 {
updateTime = 720
}

return &Blocklists{
hosts: tree.NewTree(),
blocklists: blocklists,
updateTime: updateTime,
}
}

Expand All @@ -44,8 +46,9 @@ func (b *Blocklists) Routine() {
b.processRemote(blocklist, newHosts)
}
b.hosts = newHosts
log.Printf("Blocklists updated, next update in %s\n", updateTime)
log.Printf("Blocklists updated, next update in %d minutes\n",
b.updateTime)

time.Sleep(updateTime)
time.Sleep(time.Duration(b.updateTime * int64(time.Minute)))
}
}
2 changes: 1 addition & 1 deletion internal/blocklists/blocklists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestNewBlocklists(t *testing.T) {
blocklists := []string{"blocklist1", "blocklist2"}
b := NewBlocklists(blocklists)
b := NewBlocklists(blocklists, 1)
if b.hosts == nil {
t.Errorf("Expected hosts to be initialized")
}
Expand Down
15 changes: 8 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ type DNS struct {

// Config - Main configuration struct
type Config struct {
Addr string `yaml:"addr"`
Network string `yaml:"network"`
LogFile string `yaml:"log_file"`
DNS []DNS `yaml:"dns"`
Hosts []Host `yaml:"hosts"`
Blocklists []string `yaml:"blocklists"`
CacheTTL int64 `yaml:"cache_ttl"`
Addr string `yaml:"addr"`
Network string `yaml:"network"`
LogFile string `yaml:"log_file"`
DNS []DNS `yaml:"dns"`
Hosts []Host `yaml:"hosts"`
Blocklists []string `yaml:"blocklists"`
BlocklistUpdate int64 `yaml:"blocklists_update"`
CacheTTL int64 `yaml:"cache_ttl"`
}

// Load - Load configuration from file
Expand Down
8 changes: 4 additions & 4 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func NewEngine(config *config.Config) (*Engine, error) {
}

return &Engine{
Hosts: Hosts,
Blocklists: blocklists.NewBlocklists(config.Blocklists),
Hosts: Hosts,
Blocklists: blocklists.NewBlocklists(config.Blocklists,
config.BlocklistUpdate),
cache: cache.NewCache(time.Duration(config.CacheTTL) *
time.Second),
addr: config.Addr,
Expand All @@ -51,8 +52,7 @@ func (e *Engine) Start() error {
go e.Blocklists.Routine()
go e.cache.Routine()

log.Printf("Listening on %s (%s)\n", e.addr,
e.network)
log.Printf("Listening on %s (%s)\n", e.addr, e.network)

dns.HandleFunc(".", e.handler)
server := &dns.Server{Addr: e.addr, Net: e.network}
Expand Down

0 comments on commit 22e1e2a

Please sign in to comment.