mirror of
https://git.vectorsigma.ru/public/AdGuardHome.git
synced 2026-08-03 20:59:27 +00:00
Squashed commit of the following: commite841e3a0b4Author: Ildar Kamalov <ik@adguard.com> Date: Tue Aug 26 15:49:58 2025 +0300 fix todo commitcf251e209bAuthor: Ildar Kamalov <ik@adguard.com> Date: Tue Aug 26 15:48:31 2025 +0300 fix lint issue commitc47e8216ecMerge:f754d7cd90e3692f9aAuthor: Ildar Kamalov <ik@adguard.com> Date: Tue Aug 26 15:38:36 2025 +0300 Merge branch 'ADG-10291' into ADG-10295 commitf754d7cd97Merge:d56ea7d8afebcc6814Author: Ildar Kamalov <ik@adguard.com> Date: Fri Aug 22 18:26:15 2025 +0300 Merge branch 'ADG-10291' into ADG-10295 commitd56ea7d8a6Author: Ildar Kamalov <ik@adguard.com> Date: Thu Aug 21 16:34:47 2025 +0300 fix form validation commita5fee78963Author: Ildar Kamalov <ik@adguard.com> Date: Wed Aug 20 17:59:53 2025 +0300 fix form commita2da460426Merge:cff3f4588ebfa5230aAuthor: Ildar Kamalov <ik@adguard.com> Date: Fri Aug 15 17:30:28 2025 +0300 Merge branch 'ADG-10291' into ADG-10295 commitcff3f45883Author: Ildar Kamalov <ik@adguard.com> Date: Wed Aug 13 19:28:46 2025 +0300 ADG-10295 add encryption settings page commit3f6d759b7fAuthor: Ildar Kamalov <ik@adguard.com> Date: Tue Aug 12 16:40:31 2025 +0300 fix placeholder commit507631ab71Author: Ildar Kamalov <ik@adguard.com> Date: Tue Aug 12 16:15:57 2025 +0300 use batch commite495a5585cAuthor: Ildar Kamalov <ik@adguard.com> Date: Tue Aug 12 16:13:13 2025 +0300 review fix commit2ba671b408Author: Ildar Kamalov <ik@adguard.com> Date: Tue Aug 12 10:45:00 2025 +0300 fix commit6f696238a0Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 18:38:28 2025 +0300 fix language change commit8206f3f885Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 14:54:58 2025 +0300 fix custom radio commit0920a51c4aAuthor: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 14:30:26 2025 +0300 fix commitc7ca1e8eafAuthor: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 14:25:16 2025 +0300 fix ids commit7ecd6426d9Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 13:56:29 2025 +0300 fix constant import commit246f1277b8Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 13:46:11 2025 +0300 fix version commit0224f567e6Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 13:34:29 2025 +0300 fix version commit75c9caffbdAuthor: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 13:14:01 2025 +0300 lint fix commitd0cf6ad5abAuthor: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 12:40:49 2025 +0300 lint fix commit87eb68e519Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 12:06:35 2025 +0300 use npm commit7df7d60ec9Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 11:23:00 2025 +0300 rm unused commitc285ea978aAuthor: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 10:42:39 2025 +0300 fix commitd00f1e12b3Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 11 09:43:35 2025 +0300 fix ... and 2 more commits
175 lines
4.6 KiB
Go
175 lines
4.6 KiB
Go
// vetted-filters fetches the most recent Hostlists Registry filtering rule list
|
|
// index and transforms the filters from it to AdGuard Home's format.
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
|
"github.com/google/renameio/v2/maybe"
|
|
)
|
|
|
|
func main() {
|
|
ctx := context.Background()
|
|
l := slogutil.New(nil)
|
|
|
|
urlStr := "https://adguardteam.github.io/HostlistsRegistry/assets/filters.json"
|
|
if s := os.Getenv("URL"); s != "" {
|
|
urlStr = s
|
|
}
|
|
|
|
// Validate the URL.
|
|
_, err := url.Parse(urlStr)
|
|
errors.Check(err)
|
|
|
|
c := &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
}
|
|
|
|
resp := errors.Must(c.Get(urlStr))
|
|
defer slogutil.CloseAndLog(ctx, l, resp.Body, slog.LevelError)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
panic(fmt.Errorf("expected code %d, got %d", http.StatusOK, resp.StatusCode))
|
|
}
|
|
|
|
hlFlt := &hlFilters{}
|
|
err = json.NewDecoder(resp.Body).Decode(hlFlt)
|
|
errors.Check(err)
|
|
|
|
aghFlt := &aghFilters{
|
|
Categories: map[string]*aghFiltersCategory{
|
|
"general": {
|
|
Name: "filter_category_general",
|
|
Description: "filter_category_general_desc",
|
|
},
|
|
"other": {
|
|
Name: "filter_category_other",
|
|
Description: "filter_category_other_desc",
|
|
},
|
|
"regional": {
|
|
Name: "filter_category_regional",
|
|
Description: "filter_category_regional_desc",
|
|
},
|
|
"security": {
|
|
Name: "filter_category_security",
|
|
Description: "filter_category_security_desc",
|
|
},
|
|
},
|
|
Filters: map[string]*aghFiltersFilter{},
|
|
}
|
|
|
|
for i, f := range hlFlt.Filters {
|
|
key := f.FilterKey
|
|
cat := f.category()
|
|
if cat == "" {
|
|
l.WarnContext(ctx, "no fitting category for filter", "key", key, "idx", i)
|
|
}
|
|
|
|
aghFlt.Filters[key] = &aghFiltersFilter{
|
|
Name: f.Name,
|
|
CategoryID: cat,
|
|
Homepage: f.Homepage,
|
|
// NOTE: The source URL in filters.json is not guaranteed to contain
|
|
// the URL of the filtering rule list. So, use our mirror for the
|
|
// vetted blocklists, which are mostly guaranteed to be valid and
|
|
// available lists.
|
|
Source: f.DownloadURL,
|
|
}
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
|
_, _ = buf.WriteString(jsHeader)
|
|
|
|
enc := json.NewEncoder(buf)
|
|
enc.SetIndent("", " ")
|
|
|
|
errors.Check(enc.Encode(aghFlt))
|
|
|
|
// TODO(ik): Update output path to "client_v2/src/helpers/filters/filters.ts" for new frontend migration
|
|
err = maybe.WriteFile("client/src/helpers/filters/filters.ts", buf.Bytes(), 0o644)
|
|
errors.Check(err)
|
|
}
|
|
|
|
// jsHeader is the header for the generated JavaScript file. It informs the
|
|
// reader that the file is generated and disables some style-related eslint
|
|
// checks.
|
|
const jsHeader = `// Code generated by go run ./scripts/vetted-filters/main.go; DO NOT EDIT.
|
|
|
|
/* eslint quote-props: 'off', quotes: 'off', comma-dangle: 'off', semi: 'off' */
|
|
|
|
export default `
|
|
|
|
// hlFilters is the JSON structure for the Hostlists Registry rule list index.
|
|
type hlFilters struct {
|
|
Filters []*hlFiltersFilter `json:"filters"`
|
|
}
|
|
|
|
// hlFiltersFilter is the JSON structure for a filter in the Hostlists Registry.
|
|
type hlFiltersFilter struct {
|
|
DownloadURL string `json:"downloadUrl"`
|
|
FilterKey string `json:"filterKey"`
|
|
Homepage string `json:"homepage"`
|
|
Name string `json:"name"`
|
|
Tags []int `json:"tags"`
|
|
}
|
|
|
|
// Known tag IDs. Keep in sync with tags/metadata.json in the source repo.
|
|
const (
|
|
tagIDGeneral = 1
|
|
tagIDSecurity = 2
|
|
tagIDRegional = 3
|
|
tagIDOther = 4
|
|
)
|
|
|
|
// category returns the AdGuard Home category for this filter. If there is no
|
|
// fitting category, cat is empty.
|
|
func (f *hlFiltersFilter) category() (cat string) {
|
|
for _, t := range f.Tags {
|
|
switch t {
|
|
case tagIDGeneral:
|
|
return "general"
|
|
case tagIDSecurity:
|
|
return "security"
|
|
case tagIDRegional:
|
|
return "regional"
|
|
case tagIDOther:
|
|
return "other"
|
|
}
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
// aghFilters is the JSON structure for AdGuard Home's list of vetted filtering
|
|
// rule list in file client/src/helpers/filters/filters.ts.
|
|
type aghFilters struct {
|
|
Categories map[string]*aghFiltersCategory `json:"categories"`
|
|
Filters map[string]*aghFiltersFilter `json:"filters"`
|
|
}
|
|
|
|
// aghFiltersCategory is the JSON structure for a category in the vetted
|
|
// filtering rule list file.
|
|
type aghFiltersCategory struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
// aghFiltersFilter is the JSON structure for a filter in the vetted filtering
|
|
// rule list file.
|
|
type aghFiltersFilter struct {
|
|
Name string `json:"name"`
|
|
CategoryID string `json:"categoryId"`
|
|
Homepage string `json:"homepage"`
|
|
Source string `json:"source"`
|
|
}
|