AdGuardDNS/internal/ecscache/ecsblocklist_generate.go

73 lines
1.8 KiB
Go
Raw Normal View History

2024-01-04 19:22:32 +03:00
//go:build generate
package main
import (
"bytes"
2024-11-08 16:26:22 +03:00
"context"
2024-01-04 19:22:32 +03:00
"io"
2024-11-08 16:26:22 +03:00
"log/slog"
2024-01-04 19:22:32 +03:00
"net/http"
"os"
"slices"
"text/template"
"time"
"github.com/AdguardTeam/AdGuardDNS/internal/agdhttp"
2024-11-08 16:26:22 +03:00
"github.com/AdguardTeam/golibs/errors"
2024-01-04 19:22:32 +03:00
"github.com/AdguardTeam/golibs/httphdr"
2024-11-08 16:26:22 +03:00
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/osutil"
2024-01-04 19:22:32 +03:00
)
func main() {
2024-11-08 16:26:22 +03:00
ctx := context.Background()
logger := slogutil.New(nil)
defer slogutil.RecoverAndExit(ctx, logger, osutil.ExitCodeFailure)
2024-01-04 19:22:32 +03:00
c := &http.Client{
Timeout: 10 * time.Second,
}
2024-11-08 16:26:22 +03:00
req := errors.Must(http.NewRequest(http.MethodGet, fakeECSBlocklistURL, nil))
2024-01-04 19:22:32 +03:00
req.Header.Add(httphdr.UserAgent, agdhttp.UserAgent())
2024-11-08 16:26:22 +03:00
resp := errors.Must(c.Do(req))
defer slogutil.CloseAndLog(ctx, logger, resp.Body, slog.LevelError)
2024-01-04 19:22:32 +03:00
2024-11-08 16:26:22 +03:00
out := errors.Must(os.OpenFile("./ecsblocklist.go", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o664))
defer slogutil.CloseAndLog(ctx, logger, out, slog.LevelError)
2024-01-04 19:22:32 +03:00
2024-11-08 16:26:22 +03:00
contents := errors.Must(io.ReadAll(resp.Body))
2024-01-04 19:22:32 +03:00
lines := bytes.Split(contents, []byte("\n"))
lines = lines[:len(lines)-1]
slices.SortStableFunc(lines, bytes.Compare)
2024-11-08 16:26:22 +03:00
tmpl := template.Must(template.New("main").Parse(tmplStr))
2024-01-04 19:22:32 +03:00
2024-11-08 16:26:22 +03:00
err := tmpl.Execute(out, lines)
errors.Check(err)
2024-01-04 19:22:32 +03:00
}
// fakeECSBlocklistURL is the default URL from where to get ECS fake domains.
const fakeECSBlocklistURL = `https://filters.adtidy.org/dns/fake-ecs-blacklist`
// tmplStr is the template of the generated Go code.
const tmplStr = `// Code generated by go run ./ecsblocklist_generate.go; DO NOT EDIT.
package ecscache
2024-06-07 14:27:46 +03:00
import "github.com/AdguardTeam/golibs/container"
2024-01-04 19:22:32 +03:00
// FakeECSFQDNs contains all domains that indicate ECS support, but in fact
// don't have one.
2024-06-07 14:27:46 +03:00
var FakeECSFQDNs = container.NewMapSet(
2024-01-04 19:22:32 +03:00
{{- range $_, $h := . }}
2024-06-07 14:27:46 +03:00
{{ printf "%q" ( printf "%s." $h ) }},
2024-01-04 19:22:32 +03:00
{{- end }}
2024-06-07 14:27:46 +03:00
)
2024-01-04 19:22:32 +03:00
`