mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-10-26 11:27:18 +00:00
Updates \#6336. Updates \#7984. Squashed commit of the following: commit 58d6608217caf3d0bc695e7411d139300de03853 Merge: 7a8c0715dcee947f58Author: f.setrakov <f.setrakov@adguard.com> Date: Fri Sep 26 14:36:58 2025 +0300 Merge branch 'master' into AGDNS-3175-sup-service-groups commit 7a8c0715d74554bc2e9ac1e66292ef8f88be6f16 Merge:29607d440394b8c529Author: f.setrakov <f.setrakov@adguard.com> Date: Fri Sep 26 14:19:49 2025 +0300 Merge branch 'master' into AGDNS-3175-sup-service-groups commit29607d4404Author: f.setrakov <f.setrakov@adguard.com> Date: Tue Sep 23 20:17:00 2025 +0300 all: imp changelog, openapi commita11fefe34cAuthor: f.setrakov <f.setrakov@adguard.com> Date: Tue Sep 23 17:11:26 2025 +0300 all: imp docs and changelog, codegen, added group field commitf97a04da25Author: f.setrakov <f.setrakov@adguard.com> Date: Mon Sep 22 15:54:40 2025 +0300 openapi: imp new field description commit7f13e2a098Author: f.setrakov <f.setrakov@adguard.com> Date: Fri Sep 19 18:49:03 2025 +0300 openapi: changed versions commit094822ae68Author: f.setrakov <f.setrakov@adguard.com> Date: Fri Sep 19 17:43:04 2025 +0300 openapi: fix typo in docs commitc1f1a67c8aAuthor: f.setrakov <f.setrakov@adguard.com> Date: Fri Sep 19 17:36:53 2025 +0300 all: sup blocked service groups, upd docs
138 lines
3.8 KiB
Go
138 lines
3.8 KiB
Go
// blocked services fetches the most recent Hostlists Registry blocked service
|
|
// index and transforms the filters from it to AdGuard Home's data and code
|
|
// formats.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"slices"
|
|
"strings"
|
|
"text/template"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
|
)
|
|
|
|
func main() {
|
|
ctx := context.Background()
|
|
l := slogutil.New(nil)
|
|
|
|
urlStr := "https://adguardteam.github.io/HostlistsRegistry/assets/services.json"
|
|
if v, ok := os.LookupEnv("URL"); ok {
|
|
urlStr = v
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
hlSvcs := &hlServices{}
|
|
err = json.NewDecoder(resp.Body).Decode(hlSvcs)
|
|
errors.Check(err)
|
|
|
|
// Sort all services and rules to make the output more predictable.
|
|
slices.SortStableFunc(hlSvcs.BlockedServices, func(a, b *hlServicesService) (res int) {
|
|
return strings.Compare(a.ID, b.ID)
|
|
})
|
|
for _, s := range hlSvcs.BlockedServices {
|
|
slices.Sort(s.Rules)
|
|
}
|
|
|
|
// Use another set of delimiters to prevent them interfering with the Go
|
|
// code.
|
|
tmpl, err := template.New("main").Delims("<%", "%>").Funcs(template.FuncMap{
|
|
"isnotlast": func(idx, sliceLen int) (ok bool) {
|
|
return idx != sliceLen-1
|
|
},
|
|
}).Parse(tmplStr)
|
|
errors.Check(err)
|
|
|
|
f := errors.Must(os.OpenFile(
|
|
"./internal/filtering/servicelist.go",
|
|
os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
|
|
0o644,
|
|
))
|
|
defer slogutil.CloseAndLog(ctx, l, f, slog.LevelError)
|
|
|
|
errors.Check(tmpl.Execute(f, hlSvcs))
|
|
}
|
|
|
|
// tmplStr is the template for the Go source file with the services.
|
|
const tmplStr = `// Code generated by go run ./scripts/blocked-services/main.go; DO NOT EDIT.
|
|
|
|
package filtering
|
|
|
|
// blockedService represents a single blocked service.
|
|
type blockedService struct {
|
|
ID string ` + "`" + `json:"id"` + "`" + `
|
|
Name string ` + "`" + `json:"name"` + "`" + `
|
|
IconSVG []byte ` + "`" + `json:"icon_svg"` + "`" + `
|
|
Rules []string ` + "`" + `json:"rules"` + "`" + `
|
|
GroupID string ` + "`" + `json:"group_id"` + "`" + `
|
|
}
|
|
|
|
// serviceGroup represents single group of services.
|
|
type serviceGroup struct {
|
|
ID string ` + "`" + `json:"id"` + "`" + `
|
|
}
|
|
|
|
// blockedServices contains raw blocked service data.
|
|
var blockedServices = []blockedService{<% $l := len .BlockedServices %>
|
|
<%- range $i, $s := .BlockedServices %>{
|
|
ID: <% printf "%q" $s.ID %>,
|
|
Name: <% printf "%q" $s.Name %>,
|
|
IconSVG: []byte(<% printf "%q" $s.IconSVG %>),
|
|
Rules: []string{<% range $s.Rules %>
|
|
<% printf "%q" . %>,<% end %>
|
|
},
|
|
GroupID: <% printf "%q" $s.Group %>,
|
|
}<% if isnotlast $i $l %>, <% end %><% end %>}
|
|
|
|
// serviceGroups contains raw service group data.
|
|
var serviceGroups = []serviceGroup{<% $l := len .ServiceGroups %>
|
|
<%- range $i, $s := .ServiceGroups %>{
|
|
ID: <% printf "%q" $s.ID %>,
|
|
}<% if isnotlast $i $l %>, <% end %><% end %>}
|
|
`
|
|
|
|
// hlServices is the JSON structure for the Hostlists Registry blocked service
|
|
// index.
|
|
type hlServices struct {
|
|
BlockedServices []*hlServicesService `json:"blocked_services"`
|
|
ServiceGroups []*hlServicesGroup `json:"groups"`
|
|
}
|
|
|
|
// hlServicesService is the JSON structure for a service in the Hostlists
|
|
// Registry.
|
|
type hlServicesService struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
IconSVG string `json:"icon_svg"`
|
|
Rules []string `json:"rules"`
|
|
Group string `json:"group"`
|
|
}
|
|
|
|
// hlServicesGroup is the JSON structure for a service group in the Hostlists
|
|
// Registry.
|
|
type hlServicesGroup struct {
|
|
ID string `json:"id"`
|
|
}
|