mirror of
https://github.com/v2fly/domain-list-community.git
synced 2025-12-31 23:07:30 +07:00
Feat: add support for partial include
This commit is contained in:
147
main.go
147
main.go
@@ -36,7 +36,10 @@ var (
|
||||
)
|
||||
|
||||
var (
|
||||
refMap = make(map[string]*List)
|
||||
refMap = make(map[string]*List)
|
||||
plMap = make(map[string]*ParsedList)
|
||||
finalMap = make(map[string]*List)
|
||||
cirIncMap = make(map[string]bool) // Used for circular inclusion detection
|
||||
)
|
||||
|
||||
type Entry struct {
|
||||
@@ -50,13 +53,19 @@ type List struct {
|
||||
Entry []Entry
|
||||
}
|
||||
|
||||
type ParsedList struct {
|
||||
Name string
|
||||
Inclusion map[string]bool
|
||||
Entry []Entry
|
||||
type Inclusion struct {
|
||||
Source string
|
||||
MustAttrs []*router.Domain_Attribute
|
||||
BannedAttrs []*router.Domain_Attribute
|
||||
}
|
||||
|
||||
func (entryList *ParsedList) toPlainText() error {
|
||||
type ParsedList struct {
|
||||
Name string
|
||||
Inclusions []Inclusion
|
||||
Entry []Entry
|
||||
}
|
||||
|
||||
func (entryList *List) toPlainText() error {
|
||||
var entryBytes []byte
|
||||
for _, entry := range entryList.Entry {
|
||||
var attrString string
|
||||
@@ -75,7 +84,7 @@ func (entryList *ParsedList) toPlainText() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *ParsedList) toProto() (*router.GeoSite, error) {
|
||||
func (l *List) toProto() (*router.GeoSite, error) {
|
||||
site := &router.GeoSite{
|
||||
CountryCode: l.Name,
|
||||
}
|
||||
@@ -113,7 +122,7 @@ func (l *ParsedList) toProto() (*router.GeoSite, error) {
|
||||
return site, nil
|
||||
}
|
||||
|
||||
func exportPlainTextList(exportFiles []string, entryList *ParsedList) {
|
||||
func exportPlainTextList(exportFiles []string, entryList *List) {
|
||||
for _, exportfilename := range exportFiles {
|
||||
if strings.EqualFold(entryList.Name, exportfilename) {
|
||||
if err := entryList.toPlainText(); err != nil {
|
||||
@@ -210,39 +219,67 @@ func Load(path string) (*List, error) {
|
||||
}
|
||||
|
||||
func ParseList(refList *List) (*ParsedList, error) {
|
||||
pl := &ParsedList{
|
||||
Name: refList.Name,
|
||||
Inclusion: make(map[string]bool),
|
||||
pl := &ParsedList{Name: refList.Name}
|
||||
for _, entry := range refList.Entry {
|
||||
if entry.Type == RuleTypeInclude {
|
||||
inc := Inclusion{Source: strings.ToUpper(entry.Value)}
|
||||
for _, attr := range entry.Attrs {
|
||||
if strings.HasPrefix(attr.Key, "-") {
|
||||
inc.BannedAttrs = append(inc.BannedAttrs, &router.Domain_Attribute{
|
||||
Key: attr.Key[1:], // Trim attribute prefix `-` character
|
||||
TypedValue: &router.Domain_Attribute_BoolValue{BoolValue: true},
|
||||
})
|
||||
} else {
|
||||
inc.MustAttrs = append(inc.MustAttrs, attr)
|
||||
}
|
||||
}
|
||||
pl.Inclusions = append(pl.Inclusions, inc)
|
||||
} else {
|
||||
pl.Entry = append(pl.Entry, entry)
|
||||
}
|
||||
}
|
||||
entryList := refList.Entry
|
||||
for {
|
||||
newEntryList := make([]Entry, 0, len(entryList))
|
||||
hasInclude := false
|
||||
for _, entry := range entryList {
|
||||
if entry.Type == RuleTypeInclude {
|
||||
refName := strings.ToUpper(entry.Value)
|
||||
if pl.Inclusion[refName] {
|
||||
continue
|
||||
}
|
||||
pl.Inclusion[refName] = true
|
||||
refList := refMap[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)
|
||||
return pl, nil
|
||||
}
|
||||
|
||||
func isMatchAttrFilters(entry Entry, incFilter Inclusion) bool {
|
||||
attrMap := make(map[string]bool)
|
||||
for _, attr := range entry.Attrs {
|
||||
attrMap[attr.Key] = true
|
||||
}
|
||||
for _, m := range incFilter.MustAttrs {
|
||||
if !attrMap[m.Key] { return false }
|
||||
}
|
||||
for _, b := range incFilter.BannedAttrs {
|
||||
if attrMap[b.Key] { return false }
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func ResolveList(pl *ParsedList) error {
|
||||
//TODO: deduplicate
|
||||
if _, pldone := finalMap[pl.Name]; pldone { return nil }
|
||||
|
||||
if cirIncMap[pl.Name] {
|
||||
return fmt.Errorf("circular inclusion in: %s", pl.Name)
|
||||
}
|
||||
cirIncMap[pl.Name] = true
|
||||
defer delete(cirIncMap, pl.Name)
|
||||
|
||||
finalList := &List{Name: pl.Name}
|
||||
finalList.Entry = append(finalList.Entry, pl.Entry...)
|
||||
|
||||
for _, inc := range pl.Inclusions {
|
||||
if err := ResolveList(plMap[inc.Source]); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, entry := range finalMap[inc.Source].Entry {
|
||||
if isMatchAttrFilters(entry, inc) {
|
||||
finalList.Entry = append(finalList.Entry, entry)
|
||||
}
|
||||
}
|
||||
entryList = newEntryList
|
||||
if !hasInclude {
|
||||
break
|
||||
}
|
||||
}
|
||||
pl.Entry = entryList
|
||||
|
||||
return pl, nil
|
||||
finalMap[pl.Name] = finalList
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -251,6 +288,7 @@ func main() {
|
||||
dir := *dataPath
|
||||
fmt.Println("Use domain lists in", dir)
|
||||
|
||||
// Generate refMap
|
||||
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -270,6 +308,24 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Generate plMap
|
||||
for refName, refList := range refMap {
|
||||
pl, err := ParseList(refList)
|
||||
if err != nil {
|
||||
fmt.Println("Failed to ParseList:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
plMap[refName] = pl
|
||||
}
|
||||
|
||||
// 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
|
||||
if _, err := os.Stat(*outputDir); os.IsNotExist(err) {
|
||||
if mkErr := os.MkdirAll(*outputDir, 0755); mkErr != nil {
|
||||
@@ -280,13 +336,8 @@ func main() {
|
||||
|
||||
protoList := new(router.GeoSiteList)
|
||||
var existList []string
|
||||
for _, refList := range refMap {
|
||||
pl, err := ParseList(refList)
|
||||
if err != nil {
|
||||
fmt.Println("Failed:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
site, err := pl.toProto()
|
||||
for _, siteEntries := range finalMap {
|
||||
site, err := siteEntries.toProto()
|
||||
if err != nil {
|
||||
fmt.Println("Failed:", err)
|
||||
os.Exit(1)
|
||||
@@ -296,7 +347,7 @@ func main() {
|
||||
// Flatten and export plaintext list
|
||||
if *exportLists != "" {
|
||||
if existList != nil {
|
||||
exportPlainTextList(existList, pl)
|
||||
exportPlainTextList(existList, siteEntries)
|
||||
} else {
|
||||
exportedListSlice := strings.Split(*exportLists, ",")
|
||||
for _, exportedListName := range exportedListSlice {
|
||||
@@ -309,7 +360,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
if existList != nil {
|
||||
exportPlainTextList(existList, pl)
|
||||
exportPlainTextList(existList, siteEntries)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -322,11 +373,11 @@ func main() {
|
||||
|
||||
protoBytes, err := proto.Marshal(protoList)
|
||||
if err != nil {
|
||||
fmt.Println("Failed:", err)
|
||||
fmt.Println("Failed to Marshal:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(*outputDir, *outputName), protoBytes, 0644); err != nil {
|
||||
fmt.Println("Failed:", err)
|
||||
fmt.Println("Failed to write output:", err)
|
||||
os.Exit(1)
|
||||
} else {
|
||||
fmt.Println(*outputName, "has been generated successfully.")
|
||||
|
||||
Reference in New Issue
Block a user