2019-07-23 10:21:37 +01:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2019-07-23 10:16:36 +01:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2019-07-23 10:21:37 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/AdguardTeam/urlfilter"
|
|
|
|
)
|
|
|
|
|
|
|
|
var serviceRules map[string][]*urlfilter.NetworkRule // service name -> filtering rules
|
|
|
|
|
|
|
|
type svc struct {
|
|
|
|
name string
|
|
|
|
rules []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep in sync with:
|
|
|
|
// client/src/helpers/constants.js
|
|
|
|
// client/src/components/ui/Icons.js
|
|
|
|
var serviceRulesArray = []svc{
|
2019-10-31 17:20:13 +00:00
|
|
|
{"whatsapp", []string{"||whatsapp.net^", "||whatsapp.com^"}},
|
2019-11-20 18:12:04 +00:00
|
|
|
{"facebook", []string{"||facebook.com^", "||facebook.net^", "||fbcdn.net^", "||fb.me^", "||fb.com^", "||fbsbx.com^"}},
|
2019-07-23 10:21:37 +01:00
|
|
|
{"twitter", []string{"||twitter.com^", "||t.co^", "||twimg.com^"}},
|
2019-11-04 07:22:18 +00:00
|
|
|
{"youtube", []string{"||youtube.com^", "||ytimg.com^", "||youtu.be^", "||googlevideo.com^", "||youtubei.googleapis.com^"}},
|
2019-11-20 18:12:04 +00:00
|
|
|
{"messenger", []string{"||fb.com^", "||facebook.com^", "||messenger.com^"}},
|
2019-07-23 10:21:37 +01:00
|
|
|
{"twitch", []string{"||twitch.tv^", "||ttvnw.net^"}},
|
|
|
|
{"netflix", []string{"||nflxext.com^", "||netflix.com^"}},
|
2019-11-20 18:12:04 +00:00
|
|
|
{"instagram", []string{"||instagram.com^", "||cdninstagram.com^"}},
|
2019-11-20 17:42:38 +00:00
|
|
|
{"snapchat", []string{"||snapchat.com^", "||sc-cdn.net^"}},
|
2019-07-23 10:21:37 +01:00
|
|
|
{"discord", []string{"||discord.gg^", "||discordapp.net^", "||discordapp.com^"}},
|
|
|
|
{"ok", []string{"||ok.ru^"}},
|
|
|
|
{"skype", []string{"||skype.com^"}},
|
|
|
|
{"vk", []string{"||vk.com^"}},
|
2019-11-05 13:00:15 +00:00
|
|
|
{"origin", []string{"||origin.com^", "||signin.ea.com^", "||accounts.ea.com^"}},
|
2019-07-23 10:21:37 +01:00
|
|
|
{"steam", []string{"||steam.com^"}},
|
2019-10-04 14:38:04 +01:00
|
|
|
{"epic_games", []string{"||epicgames.com^"}},
|
2019-10-30 02:01:10 +00:00
|
|
|
{"reddit", []string{"||reddit.com^", "||redditstatic.com^", "||redditmedia.com^", "||redd.it^"}},
|
2019-07-23 10:21:37 +01:00
|
|
|
{"mail_ru", []string{"||mail.ru^"}},
|
2019-11-07 02:39:30 +00:00
|
|
|
{"cloudflare", []string{
|
|
|
|
"||cloudflare.com^",
|
|
|
|
"||cloudflare-dns.com^",
|
|
|
|
"||cloudflare.net^",
|
|
|
|
"||cloudflareinsights.com^",
|
|
|
|
"||cloudflarestream.com^",
|
|
|
|
"||cloudflareresolve.com^",
|
|
|
|
"||cloudflareclient.com^",
|
|
|
|
"||cloudflarebolt.com^",
|
|
|
|
"||cloudflarestatus.com^",
|
|
|
|
"||cloudflare.cn^",
|
|
|
|
"||one.one^",
|
|
|
|
"||warp.plus^",
|
|
|
|
}},
|
2019-11-05 09:36:06 +00:00
|
|
|
{"amazon", []string{
|
|
|
|
"||amazon.com^",
|
|
|
|
"||media-amazon.com^",
|
|
|
|
"||images-amazon.com^",
|
|
|
|
"||a2z.com^",
|
|
|
|
"||amazon.ae^",
|
|
|
|
"||amazon.ca^",
|
|
|
|
"||amazon.cn^",
|
|
|
|
"||amazon.de^",
|
|
|
|
"||amazon.es^",
|
|
|
|
"||amazon.fr^",
|
|
|
|
"||amazon.in^",
|
|
|
|
"||amazon.it^",
|
|
|
|
"||amazon.nl^",
|
|
|
|
"||amazon.com.au^",
|
|
|
|
"||amazon.com.br^",
|
|
|
|
"||amazon.co.jp^",
|
|
|
|
"||amazon.com.mx^",
|
|
|
|
"||amazon.co.uk^",
|
|
|
|
}},
|
2019-11-06 09:00:05 +00:00
|
|
|
{"ebay", []string{
|
|
|
|
"||ebay.com^",
|
|
|
|
"||ebayimg.com^",
|
|
|
|
"||ebaystatic.com^",
|
|
|
|
"||ebaycdn.net^",
|
|
|
|
"||ebayinc.com^",
|
|
|
|
"||ebay.at^",
|
|
|
|
"||ebay.be^",
|
|
|
|
"||ebay.ca^",
|
|
|
|
"||ebay.ch^",
|
|
|
|
"||ebay.cn^",
|
|
|
|
"||ebay.de^",
|
|
|
|
"||ebay.es^",
|
|
|
|
"||ebay.fr^",
|
|
|
|
"||ebay.ie^",
|
|
|
|
"||ebay.in^",
|
|
|
|
"||ebay.it^",
|
|
|
|
"||ebay.ph^",
|
|
|
|
"||ebay.pl^",
|
|
|
|
"||ebay.nl^",
|
|
|
|
"||ebay.com.au^",
|
|
|
|
"||ebay.com.cn^",
|
|
|
|
"||ebay.com.hk^",
|
|
|
|
"||ebay.com.my^",
|
|
|
|
"||ebay.com.sg^",
|
|
|
|
"||ebay.co.uk^",
|
|
|
|
}},
|
2019-09-17 22:26:58 +01:00
|
|
|
{"tiktok", []string{
|
|
|
|
"||tiktok.com^",
|
2019-10-31 17:20:13 +00:00
|
|
|
"||tiktokcdn.com^",
|
2019-09-17 22:26:58 +01:00
|
|
|
"||snssdk.com^",
|
|
|
|
"||amemv.com^",
|
|
|
|
"||toutiao.com^",
|
|
|
|
"||ixigua.com^",
|
|
|
|
"||pstatp.com^",
|
|
|
|
"||ixiguavideo.com^",
|
|
|
|
"||toutiaocloud.com^",
|
|
|
|
"||toutiaocloud.net^",
|
|
|
|
"||bdurl.com^",
|
|
|
|
"||bytecdn.cn^",
|
|
|
|
"||byteimg.com^",
|
|
|
|
"||ixigua.com^",
|
2019-11-21 21:51:52 +00:00
|
|
|
"||muscdn.com^",
|
|
|
|
"||bytedance.map.fastly.net^",
|
2019-09-17 22:26:58 +01:00
|
|
|
}},
|
2019-07-23 10:21:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert array to map
|
|
|
|
func initServices() {
|
|
|
|
serviceRules = make(map[string][]*urlfilter.NetworkRule)
|
|
|
|
for _, s := range serviceRulesArray {
|
|
|
|
rules := []*urlfilter.NetworkRule{}
|
|
|
|
for _, text := range s.rules {
|
|
|
|
rule, err := urlfilter.NewNetworkRule(text, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("urlfilter.NewNetworkRule: %s rule: %s", err, text)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
serviceRules[s.name] = rules
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyBlockedServices - set blocked services settings for this DNS request
|
|
|
|
func ApplyBlockedServices(setts *dnsfilter.RequestFilteringSettings, list []string) {
|
|
|
|
setts.ServicesRules = []dnsfilter.ServiceEntry{}
|
|
|
|
for _, name := range list {
|
|
|
|
rules, ok := serviceRules[name]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
log.Error("unknown service name: %s", name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s := dnsfilter.ServiceEntry{}
|
|
|
|
s.Name = name
|
|
|
|
s.Rules = rules
|
|
|
|
setts.ServicesRules = append(setts.ServicesRules, s)
|
|
|
|
}
|
|
|
|
}
|
2019-07-23 10:16:36 +01:00
|
|
|
|
|
|
|
func handleBlockedServicesList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
config.RLock()
|
|
|
|
list := config.DNS.BlockedServices
|
|
|
|
config.RUnlock()
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := json.NewEncoder(w).Encode(list)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusInternalServerError, "json.Encode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleBlockedServicesSet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
list := []string{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&list)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "json.Decode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Lock()
|
|
|
|
config.DNS.BlockedServices = list
|
|
|
|
config.Unlock()
|
|
|
|
|
|
|
|
log.Debug("Updated blocked services list: %d", len(list))
|
|
|
|
|
|
|
|
err = writeAllConfigsAndReloadDNS()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, http.StatusBadRequest, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
returnOK(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterBlockedServicesHandlers - register HTTP handlers
|
|
|
|
func RegisterBlockedServicesHandlers() {
|
2019-08-21 12:39:37 +01:00
|
|
|
httpRegister(http.MethodGet, "/control/blocked_services/list", handleBlockedServicesList)
|
|
|
|
httpRegister(http.MethodPost, "/control/blocked_services/set", handleBlockedServicesSet)
|
2019-07-23 10:16:36 +01:00
|
|
|
}
|