mirror of
https://github.com/v2fly/domain-list-community.git
synced 2025-12-31 23:07:30 +07:00
391 lines
9.5 KiB
Go
391 lines
9.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
|
|
router "github.com/v2fly/v2ray-core/v5/app/router/routercommon"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
var (
|
|
dataPath = flag.String("datapath", "./data", "Path to your custom 'data' directory")
|
|
outputName = flag.String("outputname", "dlc.dat", "Name of the generated dat file")
|
|
outputDir = flag.String("outputdir", "./", "Directory to place all generated files")
|
|
exportLists = flag.String("exportlists", "", "Lists to be flattened and exported in plaintext format, separated by ',' comma")
|
|
)
|
|
|
|
const (
|
|
RuleTypeDomain string = "domain"
|
|
RuleTypeFullDomain string = "full"
|
|
RuleTypeKeyword string = "keyword"
|
|
RuleTypeRegexp string = "regexp"
|
|
RuleTypeInclude string = "include"
|
|
)
|
|
|
|
var (
|
|
TypeChecker = regexp.MustCompile(`^(domain|full|keyword|regexp|include)$`)
|
|
ValueChecker = regexp.MustCompile(`^[a-z0-9!\.-]+$`)
|
|
AttrChecker = regexp.MustCompile(`^[a-z0-9!-]+$`)
|
|
)
|
|
|
|
type Entry struct {
|
|
Type string
|
|
Value string
|
|
Attrs []*router.Domain_Attribute
|
|
}
|
|
|
|
type List struct {
|
|
Name string
|
|
Entry []Entry
|
|
}
|
|
|
|
type ParsedList struct {
|
|
Name string
|
|
Inclusion map[string]bool
|
|
Entry []Entry
|
|
}
|
|
|
|
func (entryList *ParsedList) toPlainText() error {
|
|
var entryBytes []byte
|
|
for _, entry := range entryList.Entry {
|
|
var attrString string
|
|
if entry.Attrs != nil {
|
|
for _, attr := range entry.Attrs {
|
|
attrString += "@" + attr.GetKey() + ","
|
|
}
|
|
attrString = strings.TrimRight(":"+attrString, ",")
|
|
}
|
|
// 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, strings.ToLower(entryList.Name)+".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 {
|
|
case RuleTypeDomain:
|
|
site.Domain = append(site.Domain, &router.Domain{
|
|
Type: router.Domain_RootDomain,
|
|
Value: entry.Value,
|
|
Attribute: entry.Attrs,
|
|
})
|
|
|
|
case RuleTypeRegexp:
|
|
site.Domain = append(site.Domain, &router.Domain{
|
|
Type: router.Domain_Regex,
|
|
Value: entry.Value,
|
|
Attribute: entry.Attrs,
|
|
})
|
|
|
|
case RuleTypeKeyword:
|
|
site.Domain = append(site.Domain, &router.Domain{
|
|
Type: router.Domain_Plain,
|
|
Value: entry.Value,
|
|
Attribute: entry.Attrs,
|
|
})
|
|
|
|
case RuleTypeFullDomain:
|
|
site.Domain = append(site.Domain, &router.Domain{
|
|
Type: router.Domain_Full,
|
|
Value: entry.Value,
|
|
Attribute: entry.Attrs,
|
|
})
|
|
}
|
|
}
|
|
return site, nil
|
|
}
|
|
|
|
func exportPlainTextList(exportFiles []string, entryList *ParsedList) {
|
|
for _, exportfilename := range exportFiles {
|
|
if strings.EqualFold(entryList.Name, exportfilename) {
|
|
if err := entryList.toPlainText(); err != nil {
|
|
fmt.Println("Failed to exportPlainTextList:", err)
|
|
continue
|
|
}
|
|
fmt.Printf("'%s' has been generated successfully.\n", exportfilename)
|
|
}
|
|
}
|
|
}
|
|
|
|
func parseEntry(line string) (Entry, error) {
|
|
var entry Entry
|
|
parts := strings.Fields(line)
|
|
|
|
// Parse/Check type and value
|
|
rawTypeVal := parts[0]
|
|
kv := strings.Split(rawTypeVal, ":")
|
|
if len(kv) == 1 {
|
|
entry.Type = RuleTypeDomain // Default type
|
|
entry.Value = strings.ToLower(rawTypeVal)
|
|
} else if len(kv) == 2 {
|
|
entry.Type = strings.ToLower(kv[0])
|
|
if entry.Type == RuleTypeRegexp {
|
|
entry.Value = kv[1]
|
|
} else {
|
|
entry.Value = strings.ToLower(kv[1])
|
|
}
|
|
} else {
|
|
return entry, fmt.Errorf("invalid format: %s", line)
|
|
}
|
|
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
|
|
for _, part := range parts[1:] {
|
|
if !strings.HasPrefix(part, "@") {
|
|
return entry, fmt.Errorf("invalid attribute: %s", part)
|
|
}
|
|
attrKey := strings.ToLower(part[1:]) // Trim attribute prefix `@` character
|
|
if !AttrChecker.MatchString(attrKey) {
|
|
return entry, fmt.Errorf("invalid attribute key: %s", attrKey)
|
|
}
|
|
entry.Attrs = append(entry.Attrs, &router.Domain_Attribute{
|
|
Key: attrKey,
|
|
TypedValue: &router.Domain_Attribute_BoolValue{BoolValue: true},
|
|
})
|
|
}
|
|
// Sort attributes
|
|
sort.Slice(entry.Attrs, func(i, j int) bool {
|
|
return entry.Attrs[i].Key < entry.Attrs[j].Key
|
|
})
|
|
|
|
return entry, nil
|
|
}
|
|
|
|
func Load(path string) (*List, error) {
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
list := &List{
|
|
Name: strings.ToUpper(filepath.Base(path)),
|
|
}
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
// Remove comments
|
|
if idx := strings.Index(line, "#"); idx != -1 {
|
|
line = line[:idx]
|
|
}
|
|
line = strings.TrimSpace(line)
|
|
if line == "" {
|
|
continue
|
|
}
|
|
entry, err := parseEntry(line)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list.Entry = append(list.Entry, entry)
|
|
}
|
|
|
|
return list, nil
|
|
}
|
|
|
|
func isMatchAttr(Attrs []*router.Domain_Attribute, includeKey string) bool {
|
|
isMatch := false
|
|
mustMatch := true
|
|
matchName := includeKey
|
|
if strings.HasPrefix(includeKey, "!") {
|
|
isMatch = true
|
|
mustMatch = false
|
|
matchName = strings.TrimLeft(includeKey, "!")
|
|
}
|
|
|
|
for _, Attr := range Attrs {
|
|
attrName := Attr.Key
|
|
if mustMatch {
|
|
if matchName == attrName {
|
|
isMatch = true
|
|
break
|
|
}
|
|
} else {
|
|
if matchName == attrName {
|
|
isMatch = false
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return isMatch
|
|
}
|
|
|
|
func createIncludeAttrEntrys(list *List, matchAttr *router.Domain_Attribute) []Entry {
|
|
newEntryList := make([]Entry, 0, len(list.Entry))
|
|
matchName := matchAttr.Key
|
|
for _, entry := range list.Entry {
|
|
matched := isMatchAttr(entry.Attrs, matchName)
|
|
if matched {
|
|
newEntryList = append(newEntryList, entry)
|
|
}
|
|
}
|
|
return newEntryList
|
|
}
|
|
|
|
func ParseList(list *List, ref map[string]*List) (*ParsedList, error) {
|
|
pl := &ParsedList{
|
|
Name: list.Name,
|
|
Inclusion: make(map[string]bool),
|
|
}
|
|
entryList := list.Entry
|
|
for {
|
|
newEntryList := make([]Entry, 0, len(entryList))
|
|
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
|
|
}
|
|
}
|
|
pl.Entry = entryList
|
|
|
|
return pl, nil
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
dir := *dataPath
|
|
fmt.Println("Use domain lists in", dir)
|
|
|
|
ref := make(map[string]*List)
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
list, err := Load(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ref[list.Name] = list
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
fmt.Println("Failed:", 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 {
|
|
fmt.Println("Failed:", mkErr)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
protoList := new(router.GeoSiteList)
|
|
var existList []string
|
|
for _, 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 {
|
|
fmt.Println("Failed:", err)
|
|
os.Exit(1)
|
|
}
|
|
protoList.Entry = append(protoList.Entry, site)
|
|
|
|
// Flatten and export plaintext list
|
|
if *exportLists != "" {
|
|
if existList != nil {
|
|
exportPlainTextList(existList, 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, pl)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sort protoList so the marshaled list is reproducible
|
|
sort.SliceStable(protoList.Entry, func(i, j int) bool {
|
|
return protoList.Entry[i].CountryCode < protoList.Entry[j].CountryCode
|
|
})
|
|
|
|
protoBytes, err := proto.Marshal(protoList)
|
|
if err != nil {
|
|
fmt.Println("Failed:", err)
|
|
os.Exit(1)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(*outputDir, *outputName), protoBytes, 0644); err != nil {
|
|
fmt.Println("Failed:", err)
|
|
os.Exit(1)
|
|
} else {
|
|
fmt.Println(*outputName, "has been generated successfully.")
|
|
}
|
|
}
|