Get rid of unnecessary duplicate type coreDnsFilter.
This commit is contained in:
parent
47e2a1004d
commit
12a8011fb3
107
config.go
107
config.go
@ -50,40 +50,35 @@ type configuration struct {
|
|||||||
sync.RWMutex `yaml:"-"`
|
sync.RWMutex `yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type coreDnsFilter struct {
|
|
||||||
ID int64 `yaml:"-"`
|
|
||||||
Path string `yaml:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type coreDNSConfig struct {
|
type coreDNSConfig struct {
|
||||||
binaryFile string
|
binaryFile string
|
||||||
coreFile string
|
coreFile string
|
||||||
Filters []coreDnsFilter `yaml:"-"`
|
Filters []filter `yaml:"-"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
ProtectionEnabled bool `yaml:"protection_enabled"`
|
ProtectionEnabled bool `yaml:"protection_enabled"`
|
||||||
FilteringEnabled bool `yaml:"filtering_enabled"`
|
FilteringEnabled bool `yaml:"filtering_enabled"`
|
||||||
SafeBrowsingEnabled bool `yaml:"safebrowsing_enabled"`
|
SafeBrowsingEnabled bool `yaml:"safebrowsing_enabled"`
|
||||||
SafeSearchEnabled bool `yaml:"safesearch_enabled"`
|
SafeSearchEnabled bool `yaml:"safesearch_enabled"`
|
||||||
ParentalEnabled bool `yaml:"parental_enabled"`
|
ParentalEnabled bool `yaml:"parental_enabled"`
|
||||||
ParentalSensitivity int `yaml:"parental_sensitivity"`
|
ParentalSensitivity int `yaml:"parental_sensitivity"`
|
||||||
BlockedResponseTTL int `yaml:"blocked_response_ttl"`
|
BlockedResponseTTL int `yaml:"blocked_response_ttl"`
|
||||||
QueryLogEnabled bool `yaml:"querylog_enabled"`
|
QueryLogEnabled bool `yaml:"querylog_enabled"`
|
||||||
Ratelimit int `yaml:"ratelimit"`
|
Ratelimit int `yaml:"ratelimit"`
|
||||||
RefuseAny bool `yaml:"refuse_any"`
|
RefuseAny bool `yaml:"refuse_any"`
|
||||||
Pprof string `yaml:"-"`
|
Pprof string `yaml:"-"`
|
||||||
Cache string `yaml:"-"`
|
Cache string `yaml:"-"`
|
||||||
Prometheus string `yaml:"-"`
|
Prometheus string `yaml:"-"`
|
||||||
BootstrapDNS string `yaml:"bootstrap_dns"`
|
BootstrapDNS string `yaml:"bootstrap_dns"`
|
||||||
UpstreamDNS []string `yaml:"upstream_dns"`
|
UpstreamDNS []string `yaml:"upstream_dns"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type filter struct {
|
type filter struct {
|
||||||
ID int64 `json:"id" yaml:"id"` // auto-assigned when filter is added (see NextFilterId)
|
ID int64 `json:"id" yaml:"id"` // auto-assigned when filter is added (see NextFilterId)
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
Name string `json:"name" yaml:"name"`
|
Name string `json:"name" yaml:"name"`
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
RulesCount int `json:"rulesCount" yaml:"-"`
|
RulesCount int `json:"rulesCount" yaml:"-"`
|
||||||
contents []byte
|
Contents []byte `json:"-" yaml:"-"`
|
||||||
LastUpdated time.Time `json:"lastUpdated" yaml:"last_updated"`
|
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
|
// 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
|
// TODO: This should be calculated when UserRules are set
|
||||||
var contents []byte
|
var contents []byte
|
||||||
for _, rule := range config.UserRules {
|
for _, rule := range config.UserRules {
|
||||||
@ -131,7 +126,7 @@ func getUserFilter() filter {
|
|||||||
userFilter := filter{
|
userFilter := filter{
|
||||||
// User filter always has constant ID=0
|
// User filter always has constant ID=0
|
||||||
ID: UserFilterId,
|
ID: UserFilterId,
|
||||||
contents: contents,
|
Contents: contents,
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,7 +194,7 @@ func writeConfig() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
userFilter := getUserFilter()
|
userFilter := userFilter()
|
||||||
err = userFilter.save()
|
err = userFilter.save()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Couldn't save the user filter: %s", err)
|
log.Printf("Couldn't save the user filter: %s", err)
|
||||||
@ -243,27 +238,25 @@ func writeAllConfigs() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const coreDNSConfigTemplate = `.:{{.Port}} {
|
const coreDNSConfigTemplate = `.:{{.Port}} {
|
||||||
{{if .ProtectionEnabled}}dnsfilter {
|
{{if .ProtectionEnabled}}dnsfilter {
|
||||||
{{if .SafeBrowsingEnabled}}safebrowsing{{end}}
|
{{if .SafeBrowsingEnabled}}safebrowsing{{end}}
|
||||||
{{if .ParentalEnabled}}parental {{.ParentalSensitivity}}{{end}}
|
{{if .ParentalEnabled}}parental {{.ParentalSensitivity}}{{end}}
|
||||||
{{if .SafeSearchEnabled}}safesearch{{end}}
|
{{if .SafeSearchEnabled}}safesearch{{end}}
|
||||||
{{if .QueryLogEnabled}}querylog{{end}}
|
{{if .QueryLogEnabled}}querylog{{end}}
|
||||||
blocked_ttl {{.BlockedResponseTTL}}
|
blocked_ttl {{.BlockedResponseTTL}}
|
||||||
{{if .FilteringEnabled}}
|
{{if .FilteringEnabled}}{{range .Filters}}{{if and .Enabled .Contents}}
|
||||||
{{range .Filters}}
|
|
||||||
filter {{.ID}} "{{.Path}}"
|
filter {{.ID}} "{{.Path}}"
|
||||||
{{end}}
|
{{end}}{{end}}{{end}}
|
||||||
{{end}}
|
}{{end}}
|
||||||
}{{end}}
|
{{.Pprof}}
|
||||||
{{.Pprof}}
|
|
||||||
{{if .RefuseAny}}refuseany{{end}}
|
{{if .RefuseAny}}refuseany{{end}}
|
||||||
{{if gt .Ratelimit 0}}ratelimit {{.Ratelimit}}{{end}}
|
{{if gt .Ratelimit 0}}ratelimit {{.Ratelimit}}{{end}}
|
||||||
hosts {
|
hosts {
|
||||||
fallthrough
|
fallthrough
|
||||||
}
|
}
|
||||||
{{if .UpstreamDNS}}upstream {{range .UpstreamDNS}}{{.}} {{end}} { bootstrap {{.BootstrapDNS}} }{{end}}
|
{{if .UpstreamDNS}}upstream {{range .UpstreamDNS}}{{.}} {{end}} { bootstrap {{.BootstrapDNS}} }{{end}}
|
||||||
{{.Cache}}
|
{{.Cache}}
|
||||||
{{.Prometheus}}
|
{{.Prometheus}}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
@ -280,24 +273,16 @@ func generateCoreDNSConfigText() (string, error) {
|
|||||||
var configBytes bytes.Buffer
|
var configBytes bytes.Buffer
|
||||||
temporaryConfig := config.CoreDNS
|
temporaryConfig := config.CoreDNS
|
||||||
|
|
||||||
// fill the list of filters
|
// generate temporary filter list, needed to put userfilter in coredns config
|
||||||
filters := make([]coreDnsFilter, 0)
|
filters := []filter{}
|
||||||
|
|
||||||
// first of all, append the user filter
|
// first of all, append the user filter
|
||||||
userFilter := getUserFilter()
|
userFilter := userFilter()
|
||||||
|
|
||||||
if len(userFilter.contents) > 0 {
|
filters = append(filters, userFilter)
|
||||||
filters = append(filters, coreDnsFilter{ID: userFilter.ID, Path: userFilter.getFilterFilePath()})
|
|
||||||
}
|
|
||||||
|
|
||||||
// then go through other filters
|
// then go through other filters
|
||||||
for i := range config.Filters {
|
filters = append(filters, config.Filters...)
|
||||||
filter := &config.Filters[i]
|
|
||||||
|
|
||||||
if filter.Enabled && len(filter.contents) > 0 {
|
|
||||||
filters = append(filters, coreDnsFilter{ID: filter.ID, Path: filter.getFilterFilePath()})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
temporaryConfig.Filters = filters
|
temporaryConfig.Filters = filters
|
||||||
|
|
||||||
// run the template
|
// 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)
|
newFilters = append(newFilters, filter)
|
||||||
} else {
|
} else {
|
||||||
// Remove the filter file
|
// Remove the filter file
|
||||||
err := os.Remove(filter.getFilterFilePath())
|
err := os.Remove(filter.Path())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorText := fmt.Sprintf("Couldn't remove the filter file: %s", err)
|
errorText := fmt.Sprintf("Couldn't remove the filter file: %s", err)
|
||||||
http.Error(w, errorText, http.StatusInternalServerError)
|
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
|
// 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)
|
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.Contents = body
|
||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// saves filter contents to the file in config.ourDataDir
|
// saves filter contents to the file in config.ourDataDir
|
||||||
func (filter *filter) save() error {
|
func (filter *filter) save() error {
|
||||||
filterFilePath := filter.getFilterFilePath()
|
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)
|
||||||
|
|
||||||
err := writeFileSafe(filterFilePath, filter.contents)
|
err := writeFileSafe(filterFilePath, filter.Contents)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -679,7 +679,7 @@ func (filter *filter) load() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
filterFilePath := filter.getFilterFilePath()
|
filterFilePath := filter.Path()
|
||||||
log.Printf("Loading filter %d contents to: %s", filter.ID, filterFilePath)
|
log.Printf("Loading filter %d contents to: %s", filter.ID, filterFilePath)
|
||||||
|
|
||||||
if _, err := os.Stat(filterFilePath); os.IsNotExist(err) {
|
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))
|
log.Printf("Filter %d length is %d", filter.ID, len(filterFileContents))
|
||||||
filter.contents = filterFileContents
|
filter.Contents = filterFileContents
|
||||||
|
|
||||||
// Now extract the rules count
|
// Now extract the rules count
|
||||||
rulesCount, _ := parseFilterContents(filter.contents)
|
rulesCount, _ := parseFilterContents(filter.Contents)
|
||||||
filter.RulesCount = rulesCount
|
filter.RulesCount = rulesCount
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Path to the filter contents
|
// 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")
|
return filepath.Join(config.ourBinaryDir, config.ourDataDir, FiltersDir, strconv.FormatInt(filter.ID, 10)+".txt")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user