* control: refactor: move filter adding code to a separate function
This commit is contained in:
parent
0884116de3
commit
b54f540f71
26
control.go
26
control.go
|
@ -559,15 +559,10 @@ func handleFilteringAddURL(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for duplicates
|
// Check for duplicates
|
||||||
config.RLock()
|
if filterExists(f.URL) {
|
||||||
for i := range config.Filters {
|
httpError(w, http.StatusBadRequest, "Filter URL already added -- %s", f.URL)
|
||||||
if config.Filters[i].URL == f.URL {
|
return
|
||||||
config.RUnlock()
|
|
||||||
httpError(w, http.StatusBadRequest, "Filter URL already added -- %s", f.URL)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
config.RUnlock()
|
|
||||||
|
|
||||||
// Set necessary properties
|
// Set necessary properties
|
||||||
f.ID = assignUniqueFilterID()
|
f.ID = assignUniqueFilterID()
|
||||||
|
@ -597,20 +592,11 @@ func handleFilteringAddURL(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// URL is deemed valid, append it to filters, update config, write new filter file and tell dns to reload it
|
// URL is deemed valid, append it to filters, update config, write new filter file and tell dns to reload it
|
||||||
// TODO: since we directly feed filters in-memory, revisit if writing configs is always necessary
|
// TODO: since we directly feed filters in-memory, revisit if writing configs is always necessary
|
||||||
config.Lock()
|
if !filterAdd(f) {
|
||||||
|
httpError(w, http.StatusBadRequest, "Filter URL already added -- %s", f.URL)
|
||||||
// Check for duplicates
|
return
|
||||||
for i := range config.Filters {
|
|
||||||
if config.Filters[i].URL == f.URL {
|
|
||||||
config.Unlock()
|
|
||||||
httpError(w, http.StatusBadRequest, "Filter URL already added -- %s", f.URL)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Filters = append(config.Filters, f)
|
|
||||||
config.Unlock()
|
|
||||||
|
|
||||||
err = writeAllConfigs()
|
err = writeAllConfigs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, http.StatusInternalServerError, "Couldn't write config file: %s", err)
|
httpError(w, http.StatusInternalServerError, "Couldn't write config file: %s", err)
|
||||||
|
|
32
filter.go
32
filter.go
|
@ -72,6 +72,38 @@ func filterEnable(url string, enable bool) bool {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return TRUE if a filter with this URL exists
|
||||||
|
func filterExists(url string) bool {
|
||||||
|
r := false
|
||||||
|
config.RLock()
|
||||||
|
for i := range config.Filters {
|
||||||
|
if config.Filters[i].URL == url {
|
||||||
|
r = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
config.RUnlock()
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a filter
|
||||||
|
// Return FALSE if a filter with this URL exists
|
||||||
|
func filterAdd(f filter) bool {
|
||||||
|
config.Lock()
|
||||||
|
|
||||||
|
// Check for duplicates
|
||||||
|
for i := range config.Filters {
|
||||||
|
if config.Filters[i].URL == f.URL {
|
||||||
|
config.Unlock()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
config.Filters = append(config.Filters, f)
|
||||||
|
config.Unlock()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// Load filters from the disk
|
// Load filters from the disk
|
||||||
// And if any filter has zero ID, assign a new one
|
// And if any filter has zero ID, assign a new one
|
||||||
func loadFilters() {
|
func loadFilters() {
|
||||||
|
|
Loading…
Reference in New Issue