Compare commits

..

1 Commits

Author SHA1 Message Date
深鸣
c05ce9952d xiaomi-ads: add stats.music.xiaomi.com (#3159) 2026-01-10 11:15:23 +08:00
2 changed files with 233 additions and 195 deletions

View File

@@ -14,6 +14,7 @@ logupdate.avlyun.sec.miui.com @ads
misc.in.duokanbox.com @ads misc.in.duokanbox.com @ads
sentry.d.mi.com @ads sentry.d.mi.com @ads
sentry.d.xiaomi.net @ads sentry.d.xiaomi.net @ads
stats.music.xiaomi.com @ads
tjqonline.cn @ads tjqonline.cn @ads
tracker.ai.xiaomi.com @ads tracker.ai.xiaomi.com @ads
tracking.miui.com @ads tracking.miui.com @ads

427
main.go
View File

@@ -29,29 +29,10 @@ const (
RuleTypeInclude string = "include" RuleTypeInclude string = "include"
) )
var (
TypeChecker = regexp.MustCompile(`^(domain|full|keyword|regexp|include)$`)
ValueChecker = regexp.MustCompile(`^[a-z0-9!\.-]+$`)
AttrChecker = regexp.MustCompile(`^[a-z0-9!-]+$`)
)
var (
refMap = make(map[string]*List)
plMap = make(map[string]*ParsedList)
finalMap = make(map[string][]Entry)
cirIncMap = make(map[string]bool) // Used for circular inclusion detection
)
type Entry struct { type Entry struct {
Type string Type string
Value string Value string
Attrs []string Attrs []*router.Domain_Attribute
}
type Inclusion struct {
Source string
MustAttrs []string
BannedAttrs []string
} }
type List struct { type List struct {
@@ -60,108 +41,150 @@ type List struct {
} }
type ParsedList struct { type ParsedList struct {
Name string Name string
Inclusions []Inclusion Inclusion map[string]bool
Entry []Entry Entry []Entry
} }
func makeProtoList(listName string, entries []Entry) (*router.GeoSite, error) { func (l *ParsedList) toPlainText(listName string) error {
site := &router.GeoSite{ var entryBytes []byte
CountryCode: listName, for _, entry := range l.Entry {
} var attrString string
for _, entry := range entries { if entry.Attrs != nil {
pdomain := &router.Domain{Value: entry.Value} for _, attr := range entry.Attrs {
for _, attr := range entry.Attrs { attrString += "@" + attr.GetKey() + ","
pdomain.Attribute = append(pdomain.Attribute, &router.Domain_Attribute{ }
Key: attr, attrString = strings.TrimRight(":"+attrString, ",")
TypedValue: &router.Domain_Attribute_BoolValue{BoolValue: true},
})
} }
// Entry output format is: type:domain.tld:@attr1,@attr2
entryBytes = append(entryBytes, []byte(entry.Type+":"+entry.Value+attrString+"\n")...)
}
if err := os.WriteFile(filepath.Join(*outputDir, listName+".txt"), entryBytes, 0644); err != nil {
return err
}
return nil
}
func (l *ParsedList) toProto() (*router.GeoSite, error) {
site := &router.GeoSite{
CountryCode: l.Name,
}
for _, entry := range l.Entry {
switch entry.Type { switch entry.Type {
case RuleTypeDomain: case RuleTypeDomain:
pdomain.Type = router.Domain_RootDomain site.Domain = append(site.Domain, &router.Domain{
Type: router.Domain_RootDomain,
Value: entry.Value,
Attribute: entry.Attrs,
})
case RuleTypeRegexp: case RuleTypeRegexp:
pdomain.Type = router.Domain_Regex // check regexp validity to avoid runtime error
_, err := regexp.Compile(entry.Value)
if err != nil {
return nil, fmt.Errorf("invalid regexp in list %s: %s", l.Name, entry.Value)
}
site.Domain = append(site.Domain, &router.Domain{
Type: router.Domain_Regex,
Value: entry.Value,
Attribute: entry.Attrs,
})
case RuleTypeKeyword: case RuleTypeKeyword:
pdomain.Type = router.Domain_Plain site.Domain = append(site.Domain, &router.Domain{
Type: router.Domain_Plain,
Value: entry.Value,
Attribute: entry.Attrs,
})
case RuleTypeFullDomain: case RuleTypeFullDomain:
pdomain.Type = router.Domain_Full site.Domain = append(site.Domain, &router.Domain{
Type: router.Domain_Full,
Value: entry.Value,
Attribute: entry.Attrs,
})
default:
return nil, fmt.Errorf("unknown domain type: %s", entry.Type)
} }
site.Domain = append(site.Domain, pdomain)
} }
return site, nil return site, nil
} }
func writePlainList(exportedName string) error { func exportPlainTextList(list []string, refName string, pl *ParsedList) {
targetList, exist := finalMap[strings.ToUpper(exportedName)] for _, listName := range list {
if !exist || len(targetList) == 0 { if strings.EqualFold(refName, listName) {
return fmt.Errorf("'%s' list does not exist or is empty.", exportedName) if err := pl.toPlainText(strings.ToLower(refName)); err != nil {
} fmt.Println("Failed:", err)
file, err := os.Create(filepath.Join(*outputDir, strings.ToLower(exportedName) + ".txt")) continue
if err != nil { }
return err fmt.Printf("'%s' has been generated successfully.\n", listName)
}
defer file.Close()
w := bufio.NewWriter(file)
for _, entry := range targetList {
// Entry output format is: type:domain.tld:@attr1,@attr2
var attrString string
if entry.Attrs != nil {
attrString = ":@" + strings.Join(entry.Attrs, ",@")
} }
fmt.Fprintln(w, entry.Type + ":" + entry.Value + attrString)
} }
return w.Flush()
} }
func parseEntry(line string) (Entry, error) { func removeComment(line string) string {
var entry Entry idx := strings.Index(line, "#")
parts := strings.Fields(line) if idx == -1 {
return line
}
return strings.TrimSpace(line[:idx])
}
// Parse type and value func parseDomain(domain string, entry *Entry) error {
rawTypeVal := parts[0] kv := strings.Split(domain, ":")
kv := strings.Split(rawTypeVal, ":")
if len(kv) == 1 { if len(kv) == 1 {
entry.Type = RuleTypeDomain // Default type entry.Type = RuleTypeDomain
entry.Value = strings.ToLower(rawTypeVal) entry.Value = strings.ToLower(kv[0])
} else if len(kv) == 2 { return nil
}
if len(kv) == 2 {
entry.Type = strings.ToLower(kv[0]) entry.Type = strings.ToLower(kv[0])
if entry.Type == RuleTypeRegexp {
if strings.EqualFold(entry.Type, RuleTypeRegexp) {
entry.Value = kv[1] entry.Value = kv[1]
} else { } else {
entry.Value = strings.ToLower(kv[1]) entry.Value = strings.ToLower(kv[1])
} }
} else {
return entry, fmt.Errorf("invalid format: %s", line) return nil
}
// Check type and value
if !TypeChecker.MatchString(entry.Type) {
return entry, fmt.Errorf("invalid type: %s", entry.Type)
}
if entry.Type == RuleTypeRegexp {
if _, err := regexp.Compile(entry.Value); err != nil {
return entry, fmt.Errorf("invalid regexp: %s", entry.Value)
}
} else if !ValueChecker.MatchString(entry.Value) {
return entry, fmt.Errorf("invalid value: %s", entry.Value)
} }
// Parse/Check attributes return fmt.Errorf("invalid format: %s", domain)
for _, part := range parts[1:] { }
if !strings.HasPrefix(part, "@") {
return entry, fmt.Errorf("invalid attribute: %s", part) func parseAttribute(attr string) (*router.Domain_Attribute, error) {
} var attribute router.Domain_Attribute
attr := strings.ToLower(part[1:]) // Trim attribute prefix `@` character if len(attr) == 0 || attr[0] != '@' {
if !AttrChecker.MatchString(attr) { return &attribute, fmt.Errorf("invalid attribute: %s", attr)
return entry, fmt.Errorf("invalid attribute key: %s", attr) }
attribute.Key = strings.ToLower(attr[1:]) // Trim attribute prefix `@` character
attribute.TypedValue = &router.Domain_Attribute_BoolValue{BoolValue: true}
return &attribute, nil
}
func parseEntry(line string) (Entry, error) {
line = strings.TrimSpace(line)
parts := strings.Split(line, " ")
var entry Entry
if len(parts) == 0 {
return entry, fmt.Errorf("empty entry")
}
if err := parseDomain(parts[0], &entry); err != nil {
return entry, err
}
for i := 1; i < len(parts); i++ {
attr, err := parseAttribute(parts[i])
if err != nil {
return entry, err
} }
entry.Attrs = append(entry.Attrs, attr) entry.Attrs = append(entry.Attrs, attr)
} }
// Sort attributes
sort.Slice(entry.Attrs, func(i, j int) bool {
return entry.Attrs[i] < entry.Attrs[j]
})
return entry, nil return entry, nil
} }
@@ -178,13 +201,9 @@ func Load(path string) (*List, error) {
} }
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := strings.TrimSpace(scanner.Text())
// Remove comments line = removeComment(line)
if idx := strings.Index(line, "#"); idx != -1 { if len(line) == 0 {
line = line[:idx]
}
line = strings.TrimSpace(line)
if line == "" {
continue continue
} }
entry, err := parseEntry(line) entry, err := parseEntry(line)
@@ -197,80 +216,99 @@ func Load(path string) (*List, error) {
return list, nil return list, nil
} }
func ParseList(refList *List) error { func isMatchAttr(Attrs []*router.Domain_Attribute, includeKey string) bool {
//TODO: one Entry -> multiple ParsedLists isMatch := false
pl := &ParsedList{Name: refList.Name} mustMatch := true
for _, entry := range refList.Entry { matchName := includeKey
if entry.Type == RuleTypeInclude { if strings.HasPrefix(includeKey, "!") {
inc := Inclusion{Source: strings.ToUpper(entry.Value)} isMatch = true
for _, attr := range entry.Attrs { mustMatch = false
if strings.HasPrefix(attr, "-") { matchName = strings.TrimLeft(includeKey, "!")
inc.BannedAttrs = append(inc.BannedAttrs, attr[1:]) // Trim attribute prefix `-` character }
} else {
inc.MustAttrs = append(inc.MustAttrs, attr) for _, Attr := range Attrs {
} attrName := Attr.Key
if mustMatch {
if matchName == attrName {
isMatch = true
break
} }
pl.Inclusions = append(pl.Inclusions, inc)
} else { } else {
pl.Entry = append(pl.Entry, entry) if matchName == attrName {
isMatch = false
break
}
} }
} }
plMap[refList.Name] = pl return isMatch
return nil
} }
func ResolveList(pl *ParsedList) error { func createIncludeAttrEntrys(list *List, matchAttr *router.Domain_Attribute) []Entry {
if _, pldone := finalMap[pl.Name]; pldone { return nil } newEntryList := make([]Entry, 0, len(list.Entry))
matchName := matchAttr.Key
if cirIncMap[pl.Name] { for _, entry := range list.Entry {
return fmt.Errorf("circular inclusion in: %s", pl.Name) matched := isMatchAttr(entry.Attrs, matchName)
} if matched {
cirIncMap[pl.Name] = true newEntryList = append(newEntryList, entry)
defer delete(cirIncMap, pl.Name)
entry2String := func(e Entry) string { // Attributes already sorted
return e.Type + ":" + e.Value + "@" + strings.Join(e.Attrs, "@")
}
isMatchAttrFilters := func(entry Entry, incFilter Inclusion) bool {
if len(incFilter.MustAttrs) == 0 && len(incFilter.BannedAttrs) == 0 { return true }
attrMap := make(map[string]bool)
for _, attr := range entry.Attrs {
attrMap[attr] = true
}
for _, m := range incFilter.MustAttrs {
if !attrMap[m] { return false }
}
for _, b := range incFilter.BannedAttrs {
if attrMap[b] { return false }
}
return true
}
bscDupMap := make(map[string]bool) // Used for basic duplicates detection
var finalList []Entry
for _, dentry := range pl.Entry {
if dstring := entry2String(dentry); !bscDupMap[dstring] {
bscDupMap[dstring] = true
finalList = append(finalList, dentry)
} }
} }
return newEntryList
}
for _, inc := range pl.Inclusions { func ParseList(list *List, ref map[string]*List) (*ParsedList, error) {
if err := ResolveList(plMap[inc.Source]); err != nil { pl := &ParsedList{
return err Name: list.Name,
} Inclusion: make(map[string]bool),
for _, ientry := range finalMap[inc.Source] { }
if isMatchAttrFilters(ientry, inc) { entryList := list.Entry
if istring := entry2String(ientry); !bscDupMap[istring] { for {
bscDupMap[istring] = true newEntryList := make([]Entry, 0, len(entryList))
finalList = append(finalList, ientry) hasInclude := false
for _, entry := range entryList {
if entry.Type == RuleTypeInclude {
refName := strings.ToUpper(entry.Value)
if entry.Attrs != nil {
for _, attr := range entry.Attrs {
InclusionName := strings.ToUpper(refName + "@" + attr.Key)
if pl.Inclusion[InclusionName] {
continue
}
pl.Inclusion[InclusionName] = true
refList := ref[refName]
if refList == nil {
return nil, fmt.Errorf("list not found: %s", entry.Value)
}
attrEntrys := createIncludeAttrEntrys(refList, attr)
if len(attrEntrys) != 0 {
newEntryList = append(newEntryList, attrEntrys...)
}
}
} else {
InclusionName := refName
if pl.Inclusion[InclusionName] {
continue
}
pl.Inclusion[InclusionName] = true
refList := ref[refName]
if refList == nil {
return nil, fmt.Errorf("list not found: %s", entry.Value)
}
newEntryList = append(newEntryList, refList.Entry...)
} }
hasInclude = true
} else {
newEntryList = append(newEntryList, entry)
} }
} }
entryList = newEntryList
if !hasInclude {
break
}
} }
finalMap[pl.Name] = finalList pl.Entry = entryList
return nil
return pl, nil
} }
func main() { func main() {
@@ -279,7 +317,7 @@ func main() {
dir := *dataPath dir := *dataPath
fmt.Println("Use domain lists in", dir) fmt.Println("Use domain lists in", dir)
// Generate refMap ref := make(map[string]*List)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@@ -291,7 +329,7 @@ func main() {
if err != nil { if err != nil {
return err return err
} }
refMap[list.Name] = list ref[list.Name] = list
return nil return nil
}) })
if err != nil { if err != nil {
@@ -299,22 +337,6 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// Generate plMap
for _, refList := range refMap {
if err := ParseList(refList); err != nil {
fmt.Println("Failed to ParseList:", err)
os.Exit(1)
}
}
// Generate finalMap
for _, pl := range plMap {
if err := ResolveList(pl); err != nil {
fmt.Println("Failed to ResolveList:", err)
os.Exit(1)
}
}
// Create output directory if not exist // Create output directory if not exist
if _, err := os.Stat(*outputDir); os.IsNotExist(err) { if _, err := os.Stat(*outputDir); os.IsNotExist(err) {
if mkErr := os.MkdirAll(*outputDir, 0755); mkErr != nil { if mkErr := os.MkdirAll(*outputDir, 0755); mkErr != nil {
@@ -323,28 +345,43 @@ func main() {
} }
} }
// Export plaintext list
if *exportLists != "" {
exportedListSlice := strings.Split(*exportLists, ",")
for _, exportedList := range exportedListSlice {
if err := writePlainList(exportedList); err != nil {
fmt.Println("Failed to write list:", err)
continue
}
fmt.Printf("list: '%s' has been generated successfully.\n", exportedList)
}
}
// Generate dat file
protoList := new(router.GeoSiteList) protoList := new(router.GeoSiteList)
for siteName, siteEntries := range finalMap { var existList []string
site, err := makeProtoList(siteName, siteEntries) for refName, list := range ref {
pl, err := ParseList(list, ref)
if err != nil {
fmt.Println("Failed:", err)
os.Exit(1)
}
site, err := pl.toProto()
if err != nil { if err != nil {
fmt.Println("Failed:", err) fmt.Println("Failed:", err)
os.Exit(1) os.Exit(1)
} }
protoList.Entry = append(protoList.Entry, site) protoList.Entry = append(protoList.Entry, site)
// Flatten and export plaintext list
if *exportLists != "" {
if existList != nil {
exportPlainTextList(existList, refName, pl)
} else {
exportedListSlice := strings.Split(*exportLists, ",")
for _, exportedListName := range exportedListSlice {
fileName := filepath.Join(dir, exportedListName)
_, err := os.Stat(fileName)
if err == nil || os.IsExist(err) {
existList = append(existList, exportedListName)
} else {
fmt.Printf("'%s' list does not exist in '%s' directory.\n", exportedListName, dir)
}
}
if existList != nil {
exportPlainTextList(existList, refName, pl)
}
}
}
} }
// Sort protoList so the marshaled list is reproducible // Sort protoList so the marshaled list is reproducible
sort.SliceStable(protoList.Entry, func(i, j int) bool { sort.SliceStable(protoList.Entry, func(i, j int) bool {
return protoList.Entry[i].CountryCode < protoList.Entry[j].CountryCode return protoList.Entry[i].CountryCode < protoList.Entry[j].CountryCode
@@ -352,11 +389,11 @@ func main() {
protoBytes, err := proto.Marshal(protoList) protoBytes, err := proto.Marshal(protoList)
if err != nil { if err != nil {
fmt.Println("Failed to marshal:", err) fmt.Println("Failed:", err)
os.Exit(1) os.Exit(1)
} }
if err := os.WriteFile(filepath.Join(*outputDir, *outputName), protoBytes, 0644); err != nil { if err := os.WriteFile(filepath.Join(*outputDir, *outputName), protoBytes, 0644); err != nil {
fmt.Println("Failed to write output:", err) fmt.Println("Failed:", err)
os.Exit(1) os.Exit(1)
} else { } else {
fmt.Println(*outputName, "has been generated successfully.") fmt.Println(*outputName, "has been generated successfully.")