User rules -- hold them as a slice of strings, which is how dns forwarding server will expect them.

This commit is contained in:
Eugene Bujak 2018-11-28 16:05:24 +03:00
parent 5a548be16c
commit ea1353422f
3 changed files with 17 additions and 26 deletions

2
app.go
View file

@ -149,7 +149,7 @@ func main() {
log.Printf("Couldn't load filter %d contents due to %s", filter.ID, err) log.Printf("Couldn't load filter %d contents due to %s", filter.ID, err)
// clear LastUpdated so it gets fetched right away // clear LastUpdated so it gets fetched right away
} }
if len(filter.Contents) == 0 { if len(filter.Rules) == 0 {
filter.LastUpdated = time.Time{} filter.LastUpdated = time.Time{}
} }
} }

View file

@ -76,7 +76,7 @@ type filter struct {
LastUpdated time.Time `json:"lastUpdated,omitempty" yaml:"last_updated,omitempty"` 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 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"} 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 // Creates a helper object for working with the user rules
func userFilter() filter { func userFilter() filter {
// TODO: This should be calculated when UserRules are set return filter{
var contents []byte
for _, rule := range config.UserRules {
contents = append(contents, []byte(rule)...)
contents = append(contents, '\n')
}
userFilter := filter{
// User filter always has constant ID=0 // User filter always has constant ID=0
ID: userFilterID, ID: userFilterID,
Contents: contents, Rules: config.UserRules,
Enabled: true, Enabled: true,
} }
return userFilter
} }
// Loads configuration from the YAML file // Loads configuration from the YAML file

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -9,6 +8,7 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"regexp" "regexp"
"strconv" "strconv"
"strings" "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) // 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") lines := strings.Split(string(contents), "\n")
rulesCount := 0 rulesCount := 0
name := "" name := ""
@ -596,7 +596,7 @@ func parseFilterContents(contents []byte) (int, string) {
} }
} }
return rulesCount, name return rulesCount, name, lines
} }
// Checks for filters updates // Checks for filters updates
@ -645,21 +645,21 @@ func (filter *filter) update(force bool) (bool, error) {
} }
// Extract filter name and count number of rules // Extract filter name and count number of rules
rulesCount, filterName := parseFilterContents(body) rulesCount, filterName, rules := parseFilterContents(body)
if filterName != "" { if filterName != "" {
filter.Name = filterName filter.Name = filterName
} }
// Check if the filter has been really changed // 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) log.Printf("The filter %d text has not changed", filter.ID)
return false, nil return false, nil
} }
log.Printf("Filter %d has been updated: %d bytes, %d rules", filter.ID, len(body), rulesCount) log.Printf("Filter %d has been updated: %d bytes, %d rules", filter.ID, len(body), rulesCount)
filter.RulesCount = rulesCount filter.RulesCount = rulesCount
filter.Contents = body filter.Rules = rules
return true, nil return true, nil
} }
@ -668,8 +668,9 @@ func (filter *filter) update(force bool) (bool, error) {
func (filter *filter) save() error { func (filter *filter) save() error {
filterFilePath := filter.Path() filterFilePath := filter.Path()
log.Printf("Saving filter %d contents to: %s", filter.ID, filterFilePath) 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 // loads filter contents from the file in dataDir
@ -692,12 +693,11 @@ func (filter *filter) load() error {
return err return err
} }
log.Printf("Filter %d length is %d", filter.ID, len(filterFileContents)) log.Printf("File %s, id %d, length %d", filterFilePath, filter.ID, len(filterFileContents))
filter.Contents = filterFileContents rulesCount, _, rules := parseFilterContents(filterFileContents)
// Now extract the rules count
rulesCount, _ := parseFilterContents(filter.Contents)
filter.RulesCount = rulesCount filter.RulesCount = rulesCount
filter.Rules = rules
return nil return nil
} }