+ GET /filtering/check_host: Check if host name is filtered

This commit is contained in:
Simon Zolin 2019-11-06 15:13:31 +03:00
parent dcc575402b
commit 0d7c01d50f
3 changed files with 131 additions and 8 deletions

View File

@ -56,6 +56,7 @@ Contents:
* API: Get filtering parameters * API: Get filtering parameters
* API: Set filtering parameters * API: Set filtering parameters
* API: Set URL parameters * API: Set URL parameters
* API: Domain Check
* Log-in page * Log-in page
* API: Log in * API: Log in
* API: Log out * API: Log out
@ -1355,6 +1356,30 @@ Response:
200 OK 200 OK
### API: Domain Check
Check if host name is filtered.
Request:
GET /control/filtering/check_host?name=hostname
Response:
200 OK
{
"reason":"FilteredBlackList",
"filter_id":1,
"rule":"||doubleclick.net^",
"service_name": "...", // set if reason=FilteredBlockedService
// if reason=ReasonRewrite:
"cname": "...",
"ip_addrs": ["1.2.3.4", ...],
}
## Log-in page ## Log-in page
After user completes the steps of installation wizard, he must log in into dashboard using his name and password. After user successfully logs in, he gets the Cookie which allows the server to authenticate him next time without password. After the Cookie is expired, user needs to perform log-in operation again. After user completes the steps of installation wizard, he must log in into dashboard using his name and password. After user successfully logs in, he gets the Cookie which allows the server to authenticate him next time without password. After the Cookie is expired, user needs to perform log-in operation again.

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -11,6 +12,7 @@ import (
"time" "time"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
) )
// IsValidURL - return TRUE if URL is valid // IsValidURL - return TRUE if URL is valid
@ -290,15 +292,58 @@ func handleFilteringConfig(w http.ResponseWriter, r *http.Request) {
enableFilters(true) enableFilters(true)
} }
type checkHostResp struct {
Reason string `json:"reason"`
FilterID int64 `json:"filter_id"`
Rule string `json:"rule"`
// for FilteredBlockedService:
SvcName string `json:"service_name"`
// for ReasonRewrite:
CanonName string `json:"cname"` // CNAME value
IPList []net.IP `json:"ip_addrs"` // list of IP addresses
}
func handleCheckHost(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
host := q.Get("name")
setts := Context.dnsFilter.GetConfig()
setts.FilteringEnabled = true
ApplyBlockedServices(&setts, config.DNS.BlockedServices)
result, err := Context.dnsFilter.CheckHost(host, dns.TypeA, &setts)
if err != nil {
httpError(w, http.StatusInternalServerError, "couldn't apply filtering: %s: %s", host, err)
return
}
resp := checkHostResp{}
resp.Reason = result.Reason.String()
resp.FilterID = result.FilterID
resp.Rule = result.Rule
resp.SvcName = result.ServiceName
resp.CanonName = result.CanonName
resp.IPList = result.IPList
js, err := json.Marshal(resp)
if err != nil {
httpError(w, http.StatusInternalServerError, "json encode: %s", err)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(js)
}
// RegisterFilteringHandlers - register handlers // RegisterFilteringHandlers - register handlers
func RegisterFilteringHandlers() { func RegisterFilteringHandlers() {
httpRegister(http.MethodGet, "/control/filtering/status", handleFilteringStatus) httpRegister("GET", "/control/filtering/status", handleFilteringStatus)
httpRegister(http.MethodPost, "/control/filtering/config", handleFilteringConfig) httpRegister("POST", "/control/filtering/config", handleFilteringConfig)
httpRegister(http.MethodPost, "/control/filtering/add_url", handleFilteringAddURL) httpRegister("POST", "/control/filtering/add_url", handleFilteringAddURL)
httpRegister(http.MethodPost, "/control/filtering/remove_url", handleFilteringRemoveURL) httpRegister("POST", "/control/filtering/remove_url", handleFilteringRemoveURL)
httpRegister(http.MethodPost, "/control/filtering/set_url", handleFilteringSetURL) httpRegister("POST", "/control/filtering/set_url", handleFilteringSetURL)
httpRegister(http.MethodPost, "/control/filtering/refresh", handleFilteringRefresh) httpRegister("POST", "/control/filtering/refresh", handleFilteringRefresh)
httpRegister(http.MethodPost, "/control/filtering/set_rules", handleFilteringSetRules) httpRegister("POST", "/control/filtering/set_rules", handleFilteringSetRules)
httpRegister("GET", "/control/filtering/check_host", handleCheckHost)
} }
func checkFiltersUpdateIntervalHours(i uint32) bool { func checkFiltersUpdateIntervalHours(i uint32) bool {

View File

@ -2,7 +2,7 @@ swagger: '2.0'
info: info:
title: 'AdGuard Home' title: 'AdGuard Home'
description: 'AdGuard Home REST API. Admin web interface is built on top of this REST API.' description: 'AdGuard Home REST API. Admin web interface is built on top of this REST API.'
version: '0.99.3' version: '0.101'
schemes: schemes:
- http - http
basePath: /control basePath: /control
@ -594,6 +594,22 @@ paths:
200: 200:
description: OK description: OK
/filtering/check_host:
get:
tags:
- filtering
operationId: filteringCheckHost
summary: 'Check if host name is filtered'
parameters:
- name: name
in: query
type: string
responses:
200:
description: OK
schema:
$ref: "#/definitions/FilterCheckHostResponse"
# -------------------------------------------------- # --------------------------------------------------
# Safebrowsing methods # Safebrowsing methods
# -------------------------------------------------- # --------------------------------------------------
@ -1178,6 +1194,42 @@ definitions:
enabled: enabled:
type: "boolean" type: "boolean"
FilterCheckHostResponse:
type: "object"
description: "Check Host Result"
properties:
reason:
type: "string"
description: "DNS filter status"
enum:
- "NotFilteredNotFound"
- "NotFilteredWhiteList"
- "NotFilteredError"
- "FilteredBlackList"
- "FilteredSafeBrowsing"
- "FilteredParental"
- "FilteredInvalid"
- "FilteredSafeSearch"
- "FilteredBlockedService"
- "ReasonRewrite"
filter_id:
type: "integer"
rule:
type: "string"
example: "||example.org^"
description: "Filtering rule applied to the request (if any)"
service_name:
type: "string"
description: "Set if reason=FilteredBlockedService"
cname:
type: "string"
description: "Set if reason=ReasonRewrite"
ip_addrs:
type: "array"
items:
type: "string"
description: "Set if reason=ReasonRewrite"
GetVersionRequest: GetVersionRequest:
type: "object" type: "object"
description: "/version.json request data" description: "/version.json request data"
@ -1471,6 +1523,7 @@ definitions:
- "FilteredInvalid" - "FilteredInvalid"
- "FilteredSafeSearch" - "FilteredSafeSearch"
- "FilteredBlockedService" - "FilteredBlockedService"
- "ReasonRewrite"
service_name: service_name:
type: "string" type: "string"
description: "Set if reason=FilteredBlockedService" description: "Set if reason=FilteredBlockedService"