2020-02-18 17:17:35 +00:00
|
|
|
package dnsfilter
|
2019-07-23 09:21:37 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-23 09:16:36 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2019-07-23 09:21:37 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2019-11-27 12:11:46 +00:00
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
2019-07-23 09:21:37 +00:00
|
|
|
)
|
|
|
|
|
2019-11-27 12:11:46 +00:00
|
|
|
var serviceRules map[string][]*rules.NetworkRule // service name -> filtering rules
|
2019-07-23 09:21:37 +00:00
|
|
|
|
|
|
|
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^"}},
|
2020-03-26 20:26:05 +00:00
|
|
|
{"facebook", []string{
|
|
|
|
"||facebook.com^",
|
|
|
|
"||facebook.net^",
|
|
|
|
"||fbcdn.net^",
|
2020-04-20 22:52:45 +00:00
|
|
|
"||accountkit.com^",
|
2020-03-26 20:26:05 +00:00
|
|
|
"||fb.me^",
|
|
|
|
"||fb.com^",
|
|
|
|
"||fbsbx.com^",
|
|
|
|
"||messenger.com^",
|
|
|
|
"||facebookcorewwwi.onion^",
|
|
|
|
"||fbcdn.com^",
|
|
|
|
}},
|
2020-04-20 22:52:45 +00:00
|
|
|
{"twitter", []string{"||twitter.com^", "||twttr.com^", "||t.co^", "||twimg.com^"}},
|
2020-03-26 20:26:05 +00:00
|
|
|
{"youtube", []string{
|
|
|
|
"||youtube.com^",
|
|
|
|
"||ytimg.com^",
|
|
|
|
"||youtu.be^",
|
|
|
|
"||googlevideo.com^",
|
|
|
|
"||youtubei.googleapis.com^",
|
|
|
|
"||youtube-nocookie.com^",
|
|
|
|
}},
|
2020-04-20 22:52:45 +00:00
|
|
|
{"twitch", []string{"||twitch.tv^", "||ttvnw.net^", "||jtvnw.net^", "||twitchcdn.net^"}},
|
|
|
|
{"netflix", []string{"||nflxext.com^", "||netflix.com^", "||nflximg.net^", "||nflxvideo.net^"}},
|
2019-11-20 18:12:04 +00:00
|
|
|
{"instagram", []string{"||instagram.com^", "||cdninstagram.com^"}},
|
2020-04-20 22:52:45 +00:00
|
|
|
{"snapchat", []string{
|
|
|
|
"||snapchat.com^",
|
|
|
|
"||sc-cdn.net^",
|
|
|
|
"||snap-dev.net^",
|
|
|
|
"||snapkit.co",
|
|
|
|
"||snapads.com^",
|
|
|
|
"||impala-media-production.s3.amazonaws.com^",
|
|
|
|
}},
|
2020-05-05 16:13:29 +00:00
|
|
|
{"discord", []string{"||discord.gg^", "||discordapp.net^", "||discordapp.com^", "||discord.com^", "||discord.media^"}},
|
2019-07-23 09:21:37 +00:00
|
|
|
{"ok", []string{"||ok.ru^"}},
|
2020-04-20 22:52:45 +00:00
|
|
|
{"skype", []string{"||skype.com^", "||skypeassets.com^"}},
|
|
|
|
{"vk", []string{"||vk.com^", "||userapi.com^", "||vk-cdn.net^", "||vkuservideo.net^"}},
|
2019-11-05 13:00:15 +00:00
|
|
|
{"origin", []string{"||origin.com^", "||signin.ea.com^", "||accounts.ea.com^"}},
|
2020-04-20 22:52:45 +00:00
|
|
|
{"steam", []string{
|
|
|
|
"||steam.com^",
|
|
|
|
"||steampowered.com^",
|
|
|
|
"||steamcommunity.com^",
|
|
|
|
"||steamstatic.com^",
|
|
|
|
"||steamstore-a.akamaihd.net^",
|
|
|
|
"||steamcdn-a.akamaihd.net^",
|
|
|
|
}},
|
|
|
|
{"epic_games", []string{"||epicgames.com^", "||easyanticheat.net^", "||easy.ac^", "||eac-cdn.com^"}},
|
2019-10-30 02:01:10 +00:00
|
|
|
{"reddit", []string{"||reddit.com^", "||redditstatic.com^", "||redditmedia.com^", "||redd.it^"}},
|
2019-07-23 09:21:37 +00: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^",
|
2020-03-26 20:26:05 +00:00
|
|
|
"||1.1.1.1^",
|
|
|
|
"||dns4torpnlfs2ifuz2s2yf3fc7rdmsbhm6rw75euj35pac6ap25zgqad.onion^",
|
2019-11-07 02:39:30 +00:00
|
|
|
}},
|
2019-11-05 09:36:06 +00:00
|
|
|
{"amazon", []string{
|
|
|
|
"||amazon.com^",
|
|
|
|
"||media-amazon.com^",
|
2020-04-20 22:52:45 +00:00
|
|
|
"||primevideo.com^",
|
|
|
|
"||amazontrust.com^",
|
2019-11-05 09:36:06 +00:00
|
|
|
"||images-amazon.com^",
|
2020-04-20 22:52:45 +00:00
|
|
|
"||ssl-images-amazon.com^",
|
|
|
|
"||amazonpay.com^",
|
|
|
|
"||amazonpay.in^",
|
|
|
|
"||amazon-adsystem.com^",
|
2019-11-05 09:36:06 +00:00
|
|
|
"||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^",
|
2020-04-20 22:52:45 +00:00
|
|
|
"||createspace.com^",
|
2019-11-05 09:36:06 +00:00
|
|
|
}},
|
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 21:26:58 +00:00
|
|
|
{"tiktok", []string{
|
|
|
|
"||tiktok.com^",
|
2019-10-31 17:20:13 +00:00
|
|
|
"||tiktokcdn.com^",
|
2020-04-20 22:52:45 +00:00
|
|
|
"||musical.ly^",
|
2019-09-17 21:26:58 +00: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^",
|
2020-03-26 20:26:05 +00:00
|
|
|
"||douyin.com^",
|
2020-05-25 08:01:17 +00:00
|
|
|
"||tiktokv.com^",
|
2019-09-17 21:26:58 +00:00
|
|
|
}},
|
2019-07-23 09:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert array to map
|
2020-02-18 17:17:35 +00:00
|
|
|
func initBlockedServices() {
|
2019-11-27 12:11:46 +00:00
|
|
|
serviceRules = make(map[string][]*rules.NetworkRule)
|
2019-07-23 09:21:37 +00:00
|
|
|
for _, s := range serviceRulesArray {
|
2019-11-27 12:11:46 +00:00
|
|
|
netRules := []*rules.NetworkRule{}
|
2019-07-23 09:21:37 +00:00
|
|
|
for _, text := range s.rules {
|
2019-11-27 12:11:46 +00:00
|
|
|
rule, err := rules.NewNetworkRule(text, 0)
|
2019-07-23 09:21:37 +00:00
|
|
|
if err != nil {
|
2019-11-27 12:11:46 +00:00
|
|
|
log.Error("rules.NewNetworkRule: %s rule: %s", err, text)
|
2019-07-23 09:21:37 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-27 12:11:46 +00:00
|
|
|
netRules = append(netRules, rule)
|
2019-07-23 09:21:37 +00:00
|
|
|
}
|
2019-11-27 12:11:46 +00:00
|
|
|
serviceRules[s.name] = netRules
|
2019-07-23 09:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 17:17:35 +00:00
|
|
|
// BlockedSvcKnown - return TRUE if a blocked service name is known
|
|
|
|
func BlockedSvcKnown(s string) bool {
|
2020-03-02 15:51:48 +00:00
|
|
|
_, ok := serviceRules[s]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-07-23 09:21:37 +00:00
|
|
|
// ApplyBlockedServices - set blocked services settings for this DNS request
|
2020-02-18 17:17:35 +00:00
|
|
|
func (d *Dnsfilter) ApplyBlockedServices(setts *RequestFilteringSettings, list []string, global bool) {
|
|
|
|
setts.ServicesRules = []ServiceEntry{}
|
|
|
|
if global {
|
|
|
|
d.confLock.RLock()
|
|
|
|
defer d.confLock.RUnlock()
|
|
|
|
list = d.Config.BlockedServices
|
|
|
|
}
|
2019-07-23 09:21:37 +00:00
|
|
|
for _, name := range list {
|
|
|
|
rules, ok := serviceRules[name]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
log.Error("unknown service name: %s", name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-18 17:17:35 +00:00
|
|
|
s := ServiceEntry{}
|
2019-07-23 09:21:37 +00:00
|
|
|
s.Name = name
|
|
|
|
s.Rules = rules
|
|
|
|
setts.ServicesRules = append(setts.ServicesRules, s)
|
|
|
|
}
|
|
|
|
}
|
2019-07-23 09:16:36 +00:00
|
|
|
|
2020-02-18 17:17:35 +00:00
|
|
|
func (d *Dnsfilter) handleBlockedServicesList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d.confLock.RLock()
|
|
|
|
list := d.Config.BlockedServices
|
|
|
|
d.confLock.RUnlock()
|
2019-07-23 09:16:36 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := json.NewEncoder(w).Encode(list)
|
|
|
|
if err != nil {
|
2020-02-18 17:17:35 +00:00
|
|
|
httpError(r, w, http.StatusInternalServerError, "json.Encode: %s", err)
|
2019-07-23 09:16:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 17:17:35 +00:00
|
|
|
func (d *Dnsfilter) handleBlockedServicesSet(w http.ResponseWriter, r *http.Request) {
|
2019-07-23 09:16:36 +00:00
|
|
|
list := []string{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&list)
|
|
|
|
if err != nil {
|
2020-02-18 17:17:35 +00:00
|
|
|
httpError(r, w, http.StatusBadRequest, "json.Decode: %s", err)
|
2019-07-23 09:16:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-18 17:17:35 +00:00
|
|
|
d.confLock.Lock()
|
|
|
|
d.Config.BlockedServices = list
|
|
|
|
d.confLock.Unlock()
|
2019-07-23 09:16:36 +00:00
|
|
|
|
|
|
|
log.Debug("Updated blocked services list: %d", len(list))
|
|
|
|
|
2020-02-18 17:17:35 +00:00
|
|
|
d.ConfigModified()
|
2019-07-23 09:16:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 17:17:35 +00:00
|
|
|
// registerBlockedServicesHandlers - register HTTP handlers
|
|
|
|
func (d *Dnsfilter) registerBlockedServicesHandlers() {
|
|
|
|
d.Config.HTTPRegister("GET", "/control/blocked_services/list", d.handleBlockedServicesList)
|
|
|
|
d.Config.HTTPRegister("POST", "/control/blocked_services/set", d.handleBlockedServicesSet)
|
2019-07-23 09:16:36 +00:00
|
|
|
}
|