mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-14 18:51:34 +03:00
Get rid of unnecessary duplicate type coreDnsFilter.
This commit is contained in:
parent
47e2a1004d
commit
12a8011fb3
39
config.go
39
config.go
@ -50,15 +50,10 @@ type configuration struct {
|
||||
sync.RWMutex `yaml:"-"`
|
||||
}
|
||||
|
||||
type coreDnsFilter struct {
|
||||
ID int64 `yaml:"-"`
|
||||
Path string `yaml:"-"`
|
||||
}
|
||||
|
||||
type coreDNSConfig struct {
|
||||
binaryFile string
|
||||
coreFile string
|
||||
Filters []coreDnsFilter `yaml:"-"`
|
||||
Filters []filter `yaml:"-"`
|
||||
Port int `yaml:"port"`
|
||||
ProtectionEnabled bool `yaml:"protection_enabled"`
|
||||
FilteringEnabled bool `yaml:"filtering_enabled"`
|
||||
@ -83,7 +78,7 @@ type filter struct {
|
||||
Name string `json:"name" yaml:"name"`
|
||||
Enabled bool `json:"enabled"`
|
||||
RulesCount int `json:"rulesCount" yaml:"-"`
|
||||
contents []byte
|
||||
Contents []byte `json:"-" yaml:"-"`
|
||||
LastUpdated time.Time `json:"lastUpdated" yaml:"last_updated"`
|
||||
}
|
||||
|
||||
@ -120,7 +115,7 @@ var config = configuration{
|
||||
}
|
||||
|
||||
// Creates a helper object for working with the user rules
|
||||
func getUserFilter() filter {
|
||||
func userFilter() filter {
|
||||
// TODO: This should be calculated when UserRules are set
|
||||
var contents []byte
|
||||
for _, rule := range config.UserRules {
|
||||
@ -131,7 +126,7 @@ func getUserFilter() filter {
|
||||
userFilter := filter{
|
||||
// User filter always has constant ID=0
|
||||
ID: UserFilterId,
|
||||
contents: contents,
|
||||
Contents: contents,
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
@ -199,7 +194,7 @@ func writeConfig() error {
|
||||
return err
|
||||
}
|
||||
|
||||
userFilter := getUserFilter()
|
||||
userFilter := userFilter()
|
||||
err = userFilter.save()
|
||||
if err != nil {
|
||||
log.Printf("Couldn't save the user filter: %s", err)
|
||||
@ -249,11 +244,9 @@ const coreDNSConfigTemplate = `.:{{.Port}} {
|
||||
{{if .SafeSearchEnabled}}safesearch{{end}}
|
||||
{{if .QueryLogEnabled}}querylog{{end}}
|
||||
blocked_ttl {{.BlockedResponseTTL}}
|
||||
{{if .FilteringEnabled}}
|
||||
{{range .Filters}}
|
||||
{{if .FilteringEnabled}}{{range .Filters}}{{if and .Enabled .Contents}}
|
||||
filter {{.ID}} "{{.Path}}"
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}{{end}}{{end}}
|
||||
}{{end}}
|
||||
{{.Pprof}}
|
||||
{{if .RefuseAny}}refuseany{{end}}
|
||||
@ -280,24 +273,16 @@ func generateCoreDNSConfigText() (string, error) {
|
||||
var configBytes bytes.Buffer
|
||||
temporaryConfig := config.CoreDNS
|
||||
|
||||
// fill the list of filters
|
||||
filters := make([]coreDnsFilter, 0)
|
||||
// generate temporary filter list, needed to put userfilter in coredns config
|
||||
filters := []filter{}
|
||||
|
||||
// first of all, append the user filter
|
||||
userFilter := getUserFilter()
|
||||
userFilter := userFilter()
|
||||
|
||||
if len(userFilter.contents) > 0 {
|
||||
filters = append(filters, coreDnsFilter{ID: userFilter.ID, Path: userFilter.getFilterFilePath()})
|
||||
}
|
||||
filters = append(filters, userFilter)
|
||||
|
||||
// then go through other filters
|
||||
for i := range config.Filters {
|
||||
filter := &config.Filters[i]
|
||||
|
||||
if filter.Enabled && len(filter.contents) > 0 {
|
||||
filters = append(filters, coreDnsFilter{ID: filter.ID, Path: filter.getFilterFilePath()})
|
||||
}
|
||||
}
|
||||
filters = append(filters, config.Filters...)
|
||||
temporaryConfig.Filters = filters
|
||||
|
||||
// run the template
|
||||
|
18
control.go
18
control.go
@ -424,7 +424,7 @@ func handleFilteringRemoveURL(w http.ResponseWriter, r *http.Request) {
|
||||
newFilters = append(newFilters, filter)
|
||||
} else {
|
||||
// Remove the filter file
|
||||
err := os.Remove(filter.getFilterFilePath())
|
||||
err := os.Remove(filter.Path())
|
||||
if err != nil {
|
||||
errorText := fmt.Sprintf("Couldn't remove the filter file: %s", err)
|
||||
http.Error(w, errorText, http.StatusInternalServerError)
|
||||
@ -647,24 +647,24 @@ func (filter *filter) update(force bool) (bool, error) {
|
||||
}
|
||||
|
||||
// Check if the filter has been really changed
|
||||
if bytes.Equal(filter.contents, body) {
|
||||
if bytes.Equal(filter.Contents, body) {
|
||||
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.Contents = body
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// saves filter contents to the file in config.ourDataDir
|
||||
func (filter *filter) save() error {
|
||||
filterFilePath := filter.getFilterFilePath()
|
||||
filterFilePath := filter.Path()
|
||||
log.Printf("Saving filter %d contents to: %s", filter.ID, filterFilePath)
|
||||
|
||||
err := writeFileSafe(filterFilePath, filter.contents)
|
||||
err := writeFileSafe(filterFilePath, filter.Contents)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -679,7 +679,7 @@ func (filter *filter) load() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
filterFilePath := filter.getFilterFilePath()
|
||||
filterFilePath := filter.Path()
|
||||
log.Printf("Loading filter %d contents to: %s", filter.ID, filterFilePath)
|
||||
|
||||
if _, err := os.Stat(filterFilePath); os.IsNotExist(err) {
|
||||
@ -693,17 +693,17 @@ func (filter *filter) load() error {
|
||||
}
|
||||
|
||||
log.Printf("Filter %d length is %d", filter.ID, len(filterFileContents))
|
||||
filter.contents = filterFileContents
|
||||
filter.Contents = filterFileContents
|
||||
|
||||
// Now extract the rules count
|
||||
rulesCount, _ := parseFilterContents(filter.contents)
|
||||
rulesCount, _ := parseFilterContents(filter.Contents)
|
||||
filter.RulesCount = rulesCount
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Path to the filter contents
|
||||
func (filter *filter) getFilterFilePath() string {
|
||||
func (filter *filter) Path() string {
|
||||
return filepath.Join(config.ourBinaryDir, config.ourDataDir, FiltersDir, strconv.FormatInt(filter.ID, 10)+".txt")
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user