* dns: rename dnsfilter.Filter.Rule -> dnsfilter.Filter.Data
This commit is contained in:
parent
9644f79a03
commit
bd68bf2e25
8
dns.go
8
dns.go
|
@ -34,13 +34,13 @@ func generateServerConfig() dnsforward.ServerConfig {
|
||||||
filters := []dnsfilter.Filter{}
|
filters := []dnsfilter.Filter{}
|
||||||
userFilter := userFilter()
|
userFilter := userFilter()
|
||||||
filters = append(filters, dnsfilter.Filter{
|
filters = append(filters, dnsfilter.Filter{
|
||||||
ID: userFilter.ID,
|
ID: userFilter.ID,
|
||||||
Rules: userFilter.Rules,
|
Data: userFilter.Data,
|
||||||
})
|
})
|
||||||
for _, filter := range config.Filters {
|
for _, filter := range config.Filters {
|
||||||
filters = append(filters, dnsfilter.Filter{
|
filters = append(filters, dnsfilter.Filter{
|
||||||
ID: filter.ID,
|
ID: filter.ID,
|
||||||
Rules: filter.Rules,
|
Data: filter.Data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -122,8 +122,8 @@ type Dnsfilter struct {
|
||||||
|
|
||||||
// Filter represents a filter list
|
// Filter represents a filter list
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
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
|
||||||
Rules []string `json:"-" yaml:"-"` // not in yaml or json
|
Data []byte `json:"-" yaml:"-"` // List of rules divided by '\n'
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:generate stringer -type=Reason
|
//go:generate stringer -type=Reason
|
||||||
|
|
26
filter.go
26
filter.go
|
@ -35,13 +35,12 @@ type filter struct {
|
||||||
|
|
||||||
// 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 {
|
||||||
return filter{
|
f := filter{
|
||||||
// User filter always has constant ID=0
|
// User filter always has constant ID=0
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
Filter: dnsfilter.Filter{
|
|
||||||
Rules: config.UserRules,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
f.Filter.Data = []byte(strings.Join(config.UserRules, "\n"))
|
||||||
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable or disable a filter
|
// Enable or disable a filter
|
||||||
|
@ -242,7 +241,7 @@ func refreshFiltersIfNecessary(force bool) int {
|
||||||
log.Info("Updated filter #%d. Rules: %d -> %d",
|
log.Info("Updated filter #%d. Rules: %d -> %d",
|
||||||
f.ID, f.RulesCount, uf.RulesCount)
|
f.ID, f.RulesCount, uf.RulesCount)
|
||||||
f.Name = uf.Name
|
f.Name = uf.Name
|
||||||
f.Rules = uf.Rules
|
f.Data = uf.Data
|
||||||
f.RulesCount = uf.RulesCount
|
f.RulesCount = uf.RulesCount
|
||||||
f.checksum = uf.checksum
|
f.checksum = uf.checksum
|
||||||
updateCount++
|
updateCount++
|
||||||
|
@ -261,7 +260,7 @@ func refreshFiltersIfNecessary(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, []string) {
|
func parseFilterContents(contents []byte) (int, string) {
|
||||||
lines := strings.Split(string(contents), "\n")
|
lines := strings.Split(string(contents), "\n")
|
||||||
rulesCount := 0
|
rulesCount := 0
|
||||||
name := ""
|
name := ""
|
||||||
|
@ -286,7 +285,7 @@ func parseFilterContents(contents []byte) (int, string, []string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return rulesCount, name, lines
|
return rulesCount, name
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform upgrade on a filter
|
// Perform upgrade on a filter
|
||||||
|
@ -327,13 +326,13 @@ func (filter *filter) update() (bool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract filter name and count number of rules
|
// Extract filter name and count number of rules
|
||||||
rulesCount, filterName, rules := parseFilterContents(body)
|
rulesCount, filterName := parseFilterContents(body)
|
||||||
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)
|
||||||
if filterName != "" {
|
if filterName != "" {
|
||||||
filter.Name = filterName
|
filter.Name = filterName
|
||||||
}
|
}
|
||||||
filter.RulesCount = rulesCount
|
filter.RulesCount = rulesCount
|
||||||
filter.Rules = rules
|
filter.Data = body
|
||||||
filter.checksum = checksum
|
filter.checksum = checksum
|
||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
|
@ -343,9 +342,8 @@ func (filter *filter) update() (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"))
|
|
||||||
|
|
||||||
err := file.SafeWrite(filterFilePath, body)
|
err := file.SafeWrite(filterFilePath, filter.Data)
|
||||||
|
|
||||||
// update LastUpdated field after saving the file
|
// update LastUpdated field after saving the file
|
||||||
filter.LastUpdated = filter.LastTimeUpdated()
|
filter.LastUpdated = filter.LastTimeUpdated()
|
||||||
|
@ -368,10 +366,10 @@ func (filter *filter) load() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Tracef("File %s, id %d, length %d", filterFilePath, filter.ID, len(filterFileContents))
|
log.Tracef("File %s, id %d, length %d", filterFilePath, filter.ID, len(filterFileContents))
|
||||||
rulesCount, _, rules := parseFilterContents(filterFileContents)
|
rulesCount, _ := parseFilterContents(filterFileContents)
|
||||||
|
|
||||||
filter.RulesCount = rulesCount
|
filter.RulesCount = rulesCount
|
||||||
filter.Rules = rules
|
filter.Data = filterFileContents
|
||||||
filter.checksum = crc32.ChecksumIEEE(filterFileContents)
|
filter.checksum = crc32.ChecksumIEEE(filterFileContents)
|
||||||
filter.LastUpdated = filter.LastTimeUpdated()
|
filter.LastUpdated = filter.LastTimeUpdated()
|
||||||
|
|
||||||
|
@ -380,7 +378,7 @@ func (filter *filter) load() error {
|
||||||
|
|
||||||
// Clear filter rules
|
// Clear filter rules
|
||||||
func (filter *filter) unload() {
|
func (filter *filter) unload() {
|
||||||
filter.Rules = []string{}
|
filter.Data = nil
|
||||||
filter.RulesCount = 0
|
filter.RulesCount = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue