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 (
|
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 {
|
type Entry struct {
|
||||||
@@ -50,13 +53,19 @@ type List struct {
|
|||||||
Entry []Entry
|
Entry []Entry
|
||||||
}
|
}
|
||||||
|
|
||||||
type ParsedList struct {
|
type Inclusion struct {
|
||||||
Name string
|
Source string
|
||||||
Inclusion map[string]bool
|
MustAttrs []*router.Domain_Attribute
|
||||||
Entry []Entry
|
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
|
var entryBytes []byte
|
||||||
for _, entry := range entryList.Entry {
|
for _, entry := range entryList.Entry {
|
||||||
var attrString string
|
var attrString string
|
||||||
@@ -75,7 +84,7 @@ func (entryList *ParsedList) toPlainText() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ParsedList) toProto() (*router.GeoSite, error) {
|
func (l *List) toProto() (*router.GeoSite, error) {
|
||||||
site := &router.GeoSite{
|
site := &router.GeoSite{
|
||||||
CountryCode: l.Name,
|
CountryCode: l.Name,
|
||||||
}
|
}
|
||||||
@@ -113,7 +122,7 @@ func (l *ParsedList) toProto() (*router.GeoSite, error) {
|
|||||||
return site, nil
|
return site, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func exportPlainTextList(exportFiles []string, entryList *ParsedList) {
|
func exportPlainTextList(exportFiles []string, entryList *List) {
|
||||||
for _, exportfilename := range exportFiles {
|
for _, exportfilename := range exportFiles {
|
||||||
if strings.EqualFold(entryList.Name, exportfilename) {
|
if strings.EqualFold(entryList.Name, exportfilename) {
|
||||||
if err := entryList.toPlainText(); err != nil {
|
if err := entryList.toPlainText(); err != nil {
|
||||||
@@ -210,39 +219,67 @@ func Load(path string) (*List, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ParseList(refList *List) (*ParsedList, error) {
|
func ParseList(refList *List) (*ParsedList, error) {
|
||||||
pl := &ParsedList{
|
pl := &ParsedList{Name: refList.Name}
|
||||||
Name: refList.Name,
|
for _, entry := range refList.Entry {
|
||||||
Inclusion: make(map[string]bool),
|
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
|
return pl, nil
|
||||||
for {
|
}
|
||||||
newEntryList := make([]Entry, 0, len(entryList))
|
|
||||||
hasInclude := false
|
func isMatchAttrFilters(entry Entry, incFilter Inclusion) bool {
|
||||||
for _, entry := range entryList {
|
attrMap := make(map[string]bool)
|
||||||
if entry.Type == RuleTypeInclude {
|
for _, attr := range entry.Attrs {
|
||||||
refName := strings.ToUpper(entry.Value)
|
attrMap[attr.Key] = true
|
||||||
if pl.Inclusion[refName] {
|
}
|
||||||
continue
|
for _, m := range incFilter.MustAttrs {
|
||||||
}
|
if !attrMap[m.Key] { return false }
|
||||||
pl.Inclusion[refName] = true
|
}
|
||||||
refList := refMap[refName]
|
for _, b := range incFilter.BannedAttrs {
|
||||||
if refList == nil {
|
if attrMap[b.Key] { return false }
|
||||||
return nil, fmt.Errorf("list not found: %s", entry.Value)
|
}
|
||||||
}
|
return true
|
||||||
newEntryList = append(newEntryList, refList.Entry...)
|
}
|
||||||
hasInclude = true
|
|
||||||
} else {
|
func ResolveList(pl *ParsedList) error {
|
||||||
newEntryList = append(newEntryList, entry)
|
//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
|
finalMap[pl.Name] = finalList
|
||||||
|
return nil
|
||||||
return pl, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -251,6 +288,7 @@ func main() {
|
|||||||
dir := *dataPath
|
dir := *dataPath
|
||||||
fmt.Println("Use domain lists in", dir)
|
fmt.Println("Use domain lists in", dir)
|
||||||
|
|
||||||
|
// Generate refMap
|
||||||
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
|
||||||
@@ -270,6 +308,24 @@ func main() {
|
|||||||
os.Exit(1)
|
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
|
// 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 {
|
||||||
@@ -280,13 +336,8 @@ func main() {
|
|||||||
|
|
||||||
protoList := new(router.GeoSiteList)
|
protoList := new(router.GeoSiteList)
|
||||||
var existList []string
|
var existList []string
|
||||||
for _, refList := range refMap {
|
for _, siteEntries := range finalMap {
|
||||||
pl, err := ParseList(refList)
|
site, err := siteEntries.toProto()
|
||||||
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)
|
||||||
@@ -296,7 +347,7 @@ func main() {
|
|||||||
// Flatten and export plaintext list
|
// Flatten and export plaintext list
|
||||||
if *exportLists != "" {
|
if *exportLists != "" {
|
||||||
if existList != nil {
|
if existList != nil {
|
||||||
exportPlainTextList(existList, pl)
|
exportPlainTextList(existList, siteEntries)
|
||||||
} else {
|
} else {
|
||||||
exportedListSlice := strings.Split(*exportLists, ",")
|
exportedListSlice := strings.Split(*exportLists, ",")
|
||||||
for _, exportedListName := range exportedListSlice {
|
for _, exportedListName := range exportedListSlice {
|
||||||
@@ -309,7 +360,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if existList != nil {
|
if existList != nil {
|
||||||
exportPlainTextList(existList, pl)
|
exportPlainTextList(existList, siteEntries)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -322,11 +373,11 @@ func main() {
|
|||||||
|
|
||||||
protoBytes, err := proto.Marshal(protoList)
|
protoBytes, err := proto.Marshal(protoList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed:", err)
|
fmt.Println("Failed to Marshal:", 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:", err)
|
fmt.Println("Failed to write output:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(*outputName, "has been generated successfully.")
|
fmt.Println(*outputName, "has been generated successfully.")
|
||||||
|
|||||||
Reference in New Issue
Block a user