mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 21:15:35 +03:00
User rules -- hold them as a slice of strings, which is how dns forwarding server will expect them.
This commit is contained in:
parent
5a548be16c
commit
ea1353422f
3 changed files with 17 additions and 26 deletions
2
app.go
2
app.go
|
@ -149,7 +149,7 @@ func main() {
|
|||
log.Printf("Couldn't load filter %d contents due to %s", filter.ID, err)
|
||||
// clear LastUpdated so it gets fetched right away
|
||||
}
|
||||
if len(filter.Contents) == 0 {
|
||||
if len(filter.Rules) == 0 {
|
||||
filter.LastUpdated = time.Time{}
|
||||
}
|
||||
}
|
||||
|
|
15
config.go
15
config.go
|
@ -76,7 +76,7 @@ type filter struct {
|
|||
LastUpdated time.Time `json:"lastUpdated,omitempty" yaml:"last_updated,omitempty"`
|
||||
ID int64 `json:"id"` // auto-assigned when filter is added (see nextFilterID), json by default keeps ID uppercase but we need lowercase
|
||||
|
||||
Contents []byte `json:"-" yaml:"-"` // not in yaml or json
|
||||
Rules []string `json:"-" yaml:"-"` // not in yaml or json
|
||||
}
|
||||
|
||||
var defaultDNS = []string{"tls://1.1.1.1", "tls://1.0.0.1"}
|
||||
|
@ -112,21 +112,12 @@ var config = configuration{
|
|||
|
||||
// Creates a helper object for working with the user rules
|
||||
func userFilter() filter {
|
||||
// TODO: This should be calculated when UserRules are set
|
||||
var contents []byte
|
||||
for _, rule := range config.UserRules {
|
||||
contents = append(contents, []byte(rule)...)
|
||||
contents = append(contents, '\n')
|
||||
}
|
||||
|
||||
userFilter := filter{
|
||||
return filter{
|
||||
// User filter always has constant ID=0
|
||||
ID: userFilterID,
|
||||
Contents: contents,
|
||||
Rules: config.UserRules,
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
return userFilter
|
||||
}
|
||||
|
||||
// Loads configuration from the YAML file
|
||||
|
|
22
control.go
22
control.go
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
@ -9,6 +8,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -577,7 +577,7 @@ func refreshFiltersIfNeccessary(force bool) int {
|
|||
}
|
||||
|
||||
// A helper function that parses filter contents and returns a number of rules and a filter name (if there's any)
|
||||
func parseFilterContents(contents []byte) (int, string) {
|
||||
func parseFilterContents(contents []byte) (int, string, []string) {
|
||||
lines := strings.Split(string(contents), "\n")
|
||||
rulesCount := 0
|
||||
name := ""
|
||||
|
@ -596,7 +596,7 @@ func parseFilterContents(contents []byte) (int, string) {
|
|||
}
|
||||
}
|
||||
|
||||
return rulesCount, name
|
||||
return rulesCount, name, lines
|
||||
}
|
||||
|
||||
// Checks for filters updates
|
||||
|
@ -645,21 +645,21 @@ func (filter *filter) update(force bool) (bool, error) {
|
|||
}
|
||||
|
||||
// Extract filter name and count number of rules
|
||||
rulesCount, filterName := parseFilterContents(body)
|
||||
rulesCount, filterName, rules := parseFilterContents(body)
|
||||
|
||||
if filterName != "" {
|
||||
filter.Name = filterName
|
||||
}
|
||||
|
||||
// Check if the filter has been really changed
|
||||
if bytes.Equal(filter.Contents, body) {
|
||||
if reflect.DeepEqual(filter.Rules, rules) {
|
||||
log.Printf("The filter %d text has not changed", filter.ID)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
log.Printf("Filter %d has been updated: %d bytes, %d rules", filter.ID, len(body), rulesCount)
|
||||
filter.RulesCount = rulesCount
|
||||
filter.Contents = body
|
||||
filter.Rules = rules
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
@ -668,8 +668,9 @@ func (filter *filter) update(force bool) (bool, error) {
|
|||
func (filter *filter) save() error {
|
||||
filterFilePath := filter.Path()
|
||||
log.Printf("Saving filter %d contents to: %s", filter.ID, filterFilePath)
|
||||
body := []byte(strings.Join(filter.Rules, "\n"))
|
||||
|
||||
return safeWriteFile(filterFilePath, filter.Contents)
|
||||
return safeWriteFile(filterFilePath, body)
|
||||
}
|
||||
|
||||
// loads filter contents from the file in dataDir
|
||||
|
@ -692,12 +693,11 @@ func (filter *filter) load() error {
|
|||
return err
|
||||
}
|
||||
|
||||
log.Printf("Filter %d length is %d", filter.ID, len(filterFileContents))
|
||||
filter.Contents = filterFileContents
|
||||
log.Printf("File %s, id %d, length %d", filterFilePath, filter.ID, len(filterFileContents))
|
||||
rulesCount, _, rules := parseFilterContents(filterFileContents)
|
||||
|
||||
// Now extract the rules count
|
||||
rulesCount, _ := parseFilterContents(filter.Contents)
|
||||
filter.RulesCount = rulesCount
|
||||
filter.Rules = rules
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue