2019-08-21 04:39:37 -07:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-09-04 04:12:00 -07:00
|
|
|
"net/url"
|
2019-08-21 04:39:37 -07:00
|
|
|
"os"
|
|
|
|
"strings"
|
2019-09-04 04:12:00 -07:00
|
|
|
"time"
|
2019-08-21 04:39:37 -07:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
// IsValidURL - return TRUE if URL is valid
|
|
|
|
func IsValidURL(rawurl string) bool {
|
|
|
|
url, err := url.ParseRequestURI(rawurl)
|
2019-08-21 04:39:37 -07:00
|
|
|
if err != nil {
|
2019-09-04 04:12:00 -07:00
|
|
|
return false //Couldn't even parse the rawurl
|
2019-08-21 04:39:37 -07:00
|
|
|
}
|
2019-09-04 04:12:00 -07:00
|
|
|
if len(url.Scheme) == 0 {
|
|
|
|
return false //No Scheme found
|
2019-08-21 04:39:37 -07:00
|
|
|
}
|
2019-09-04 04:12:00 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type filterAddJSON struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
URL string `json:"url"`
|
2019-08-21 04:39:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleFilteringAddURL(w http.ResponseWriter, r *http.Request) {
|
2019-09-04 04:12:00 -07:00
|
|
|
fj := filterAddJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&fj)
|
2019-08-21 04:39:37 -07:00
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "Failed to parse request body json: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
if !IsValidURL(fj.URL) {
|
|
|
|
http.Error(w, "Invalid URL", http.StatusBadRequest)
|
2019-08-21 04:39:37 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for duplicates
|
2019-09-04 04:12:00 -07:00
|
|
|
if filterExists(fj.URL) {
|
|
|
|
httpError(w, http.StatusBadRequest, "Filter URL already added -- %s", fj.URL)
|
2019-08-21 04:39:37 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set necessary properties
|
2019-09-04 04:12:00 -07:00
|
|
|
f := filter{
|
|
|
|
Enabled: true,
|
|
|
|
URL: fj.URL,
|
|
|
|
Name: fj.Name,
|
|
|
|
}
|
2019-08-21 04:39:37 -07:00
|
|
|
f.ID = assignUniqueFilterID()
|
|
|
|
|
|
|
|
// Download the filter contents
|
|
|
|
ok, err := f.update()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "Couldn't fetch filter from url %s: %s", f.URL, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if f.RulesCount == 0 {
|
|
|
|
httpError(w, http.StatusBadRequest, "Filter at the url %s has no rules (maybe it points to blank page?)", f.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
httpError(w, http.StatusBadRequest, "Filter at the url %s is invalid (maybe it points to blank page?)", f.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the filter contents
|
|
|
|
err = f.save()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "Failed to save filter %d due to %s", f.ID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if !filterAdd(f) {
|
|
|
|
httpError(w, http.StatusBadRequest, "Filter URL already added -- %s", f.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-09 09:51:26 -07:00
|
|
|
onConfigModified()
|
|
|
|
enableFilters(true)
|
2019-08-21 04:39:37 -07:00
|
|
|
|
|
|
|
_, err = fmt.Fprintf(w, "OK %d rules\n", f.RulesCount)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "Couldn't write body: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleFilteringRemoveURL(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
type request struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
req := request{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "Failed to parse request body json: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
if !IsValidURL(req.URL) {
|
2019-08-21 04:39:37 -07:00
|
|
|
http.Error(w, "URL parameter is not valid request URL", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// go through each element and delete if url matches
|
|
|
|
config.Lock()
|
2019-10-09 09:51:26 -07:00
|
|
|
newFilters := []filter{}
|
2019-08-21 04:39:37 -07:00
|
|
|
for _, filter := range config.Filters {
|
|
|
|
if filter.URL != req.URL {
|
|
|
|
newFilters = append(newFilters, filter)
|
|
|
|
} else {
|
2019-10-09 09:51:26 -07:00
|
|
|
err := os.Rename(filter.Path(), filter.Path()+".old")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("os.Rename: %s: %s", filter.Path(), err)
|
2019-08-21 04:39:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update the configuration after removing filter files
|
|
|
|
config.Filters = newFilters
|
|
|
|
config.Unlock()
|
2019-10-09 09:51:26 -07:00
|
|
|
|
|
|
|
onConfigModified()
|
|
|
|
enableFilters(true)
|
|
|
|
|
|
|
|
// Note: the old files "filter.txt.old" aren't deleted - it's not really necessary,
|
|
|
|
// but will require the additional code to run after enableFilters() is finished: i.e. complicated
|
2019-08-21 04:39:37 -07:00
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
type filterURLJSON struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
}
|
2019-08-21 04:39:37 -07:00
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
func handleFilteringSetURL(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fj := filterURLJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&fj)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "json decode: %s", err)
|
2019-08-21 04:39:37 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
if !IsValidURL(fj.URL) {
|
|
|
|
http.Error(w, "invalid URL", http.StatusBadRequest)
|
2019-08-21 04:39:37 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
found := filterEnable(fj.URL, fj.Enabled)
|
2019-08-21 04:39:37 -07:00
|
|
|
if !found {
|
2019-09-04 04:12:00 -07:00
|
|
|
http.Error(w, "URL doesn't exist", http.StatusBadRequest)
|
2019-08-21 04:39:37 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-09 09:51:26 -07:00
|
|
|
onConfigModified()
|
|
|
|
enableFilters(true)
|
2019-08-21 04:39:37 -07:00
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
func handleFilteringSetRules(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
2019-08-21 04:39:37 -07:00
|
|
|
if err != nil {
|
2019-09-04 04:12:00 -07:00
|
|
|
httpError(w, http.StatusBadRequest, "Failed to read request body: %s", err)
|
2019-08-21 04:39:37 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
config.UserRules = strings.Split(string(body), "\n")
|
2019-10-30 01:52:58 -07:00
|
|
|
onConfigModified()
|
|
|
|
userFilter := userFilter()
|
|
|
|
err = userFilter.save()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Couldn't save the user filter: %s", err)
|
|
|
|
}
|
2019-10-09 09:51:26 -07:00
|
|
|
enableFilters(true)
|
2019-09-04 04:12:00 -07:00
|
|
|
}
|
2019-08-21 04:39:37 -07:00
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
func handleFilteringRefresh(w http.ResponseWriter, r *http.Request) {
|
2019-10-10 07:12:32 -07:00
|
|
|
config.controlLock.Unlock()
|
|
|
|
nUpdated, err := refreshFilters()
|
|
|
|
config.controlLock.Lock()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "OK %d filters updated\n", nUpdated)
|
2019-09-04 04:12:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type filterJSON struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
RulesCount uint32 `json:"rules_count"`
|
|
|
|
LastUpdated string `json:"last_updated"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type filteringConfig struct {
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
Interval uint32 `json:"interval"` // in hours
|
|
|
|
Filters []filterJSON `json:"filters"`
|
|
|
|
UserRules []string `json:"user_rules"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get filtering configuration
|
2019-10-14 05:55:58 -07:00
|
|
|
func handleFilteringStatus(w http.ResponseWriter, r *http.Request) {
|
2019-09-04 04:12:00 -07:00
|
|
|
resp := filteringConfig{}
|
|
|
|
config.RLock()
|
|
|
|
resp.Enabled = config.DNS.FilteringEnabled
|
|
|
|
resp.Interval = config.DNS.FiltersUpdateIntervalHours
|
|
|
|
for _, f := range config.Filters {
|
|
|
|
fj := filterJSON{
|
|
|
|
ID: f.ID,
|
|
|
|
Enabled: f.Enabled,
|
|
|
|
URL: f.URL,
|
|
|
|
Name: f.Name,
|
|
|
|
RulesCount: uint32(f.RulesCount),
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:52:58 -07:00
|
|
|
if !f.LastUpdated.IsZero() {
|
2019-09-04 04:12:00 -07:00
|
|
|
fj.LastUpdated = f.LastUpdated.Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Filters = append(resp.Filters, fj)
|
2019-08-21 04:39:37 -07:00
|
|
|
}
|
2019-09-04 04:12:00 -07:00
|
|
|
resp.UserRules = config.UserRules
|
|
|
|
config.RUnlock()
|
2019-08-21 04:39:37 -07:00
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
jsonVal, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "json encode: %s", err)
|
2019-08-21 04:39:37 -07:00
|
|
|
return
|
|
|
|
}
|
2019-09-04 04:12:00 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, err = w.Write(jsonVal)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "http write: %s", err)
|
|
|
|
}
|
2019-08-21 04:39:37 -07:00
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
// Set filtering configuration
|
|
|
|
func handleFilteringConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
req := filteringConfig{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
2019-08-21 04:39:37 -07:00
|
|
|
if err != nil {
|
2019-09-04 04:12:00 -07:00
|
|
|
httpError(w, http.StatusBadRequest, "json decode: %s", err)
|
2019-08-21 04:39:37 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
if !checkFiltersUpdateIntervalHours(req.Interval) {
|
|
|
|
httpError(w, http.StatusBadRequest, "Unsupported interval")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config.DNS.FilteringEnabled = req.Enabled
|
|
|
|
config.DNS.FiltersUpdateIntervalHours = req.Interval
|
2019-10-09 09:51:26 -07:00
|
|
|
onConfigModified()
|
|
|
|
enableFilters(true)
|
2019-08-21 04:39:37 -07:00
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
// RegisterFilteringHandlers - register handlers
|
|
|
|
func RegisterFilteringHandlers() {
|
2019-10-14 05:55:58 -07:00
|
|
|
httpRegister(http.MethodGet, "/control/filtering/status", handleFilteringStatus)
|
|
|
|
httpRegister(http.MethodPost, "/control/filtering/config", handleFilteringConfig)
|
2019-09-04 04:12:00 -07:00
|
|
|
httpRegister(http.MethodPost, "/control/filtering/add_url", handleFilteringAddURL)
|
|
|
|
httpRegister(http.MethodPost, "/control/filtering/remove_url", handleFilteringRemoveURL)
|
|
|
|
httpRegister(http.MethodPost, "/control/filtering/set_url", handleFilteringSetURL)
|
|
|
|
httpRegister(http.MethodPost, "/control/filtering/refresh", handleFilteringRefresh)
|
|
|
|
httpRegister(http.MethodPost, "/control/filtering/set_rules", handleFilteringSetRules)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkFiltersUpdateIntervalHours(i uint32) bool {
|
|
|
|
return i == 0 || i == 1 || i == 12 || i == 1*24 || i == 3*24 || i == 7*24
|
2019-08-21 04:39:37 -07:00
|
|
|
}
|