support domain property

This commit is contained in:
Darien Raymond 2018-12-01 21:06:45 +01:00
parent 0c9847e55a
commit bb11c5afc6
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

90
main.go
View File

@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
@ -16,6 +17,7 @@ import (
type Entry struct { type Entry struct {
Type string Type string
Value string Value string
Attrs []*router.Domain_Attribute
} }
type List struct { type List struct {
@ -37,23 +39,27 @@ func (l *ParsedList) toProto() (*router.GeoSite, error) {
switch entry.Type { switch entry.Type {
case "domain": case "domain":
site.Domain = append(site.Domain, &router.Domain{ site.Domain = append(site.Domain, &router.Domain{
Type: router.Domain_Domain, Type: router.Domain_Domain,
Value: entry.Value, Value: entry.Value,
Attribute: entry.Attrs,
}) })
case "regex": case "regex":
site.Domain = append(site.Domain, &router.Domain{ site.Domain = append(site.Domain, &router.Domain{
Type: router.Domain_Regex, Type: router.Domain_Regex,
Value: entry.Value, Value: entry.Value,
Attribute: entry.Attrs,
}) })
case "keyword": case "keyword":
site.Domain = append(site.Domain, &router.Domain{ site.Domain = append(site.Domain, &router.Domain{
Type: router.Domain_Plain, Type: router.Domain_Plain,
Value: entry.Value, Value: entry.Value,
Attribute: entry.Attrs,
}) })
case "full": case "full":
site.Domain = append(site.Domain, &router.Domain{ site.Domain = append(site.Domain, &router.Domain{
Type: router.Domain_Full, Type: router.Domain_Full,
Value: entry.Value, Value: entry.Value,
Attribute: entry.Attrs,
}) })
default: default:
return nil, errors.New("unknown domain type: " + entry.Type) return nil, errors.New("unknown domain type: " + entry.Type)
@ -70,21 +76,67 @@ func removeComment(line string) string {
return strings.TrimSpace(line[:idx]) return strings.TrimSpace(line[:idx])
} }
func parseEntry(line string) (Entry, error) { func parseDomain(domain string, entry *Entry) error {
kv := strings.Split(line, ":") kv := strings.Split(domain, ":")
if len(kv) == 1 { if len(kv) == 1 {
return Entry{ entry.Type = "domain"
Type: "domain", entry.Value = strings.ToLower(kv[0])
Value: kv[0], return nil
}, nil
} }
if len(kv) == 2 { if len(kv) == 2 {
return Entry{ entry.Type = strings.ToLower(kv[0])
Type: strings.ToLower(kv[0]), entry.Value = strings.ToLower(kv[1])
Value: strings.ToLower(kv[1]), return nil
}, nil
} }
return Entry{}, errors.New("Invalid format: " + line)
return errors.New("Invalid format: " + domain)
}
func parseAttribute(attr string) (router.Domain_Attribute, error) {
var attribute router.Domain_Attribute
if len(attr) == 0 || attr[0] != '@' {
return attribute, errors.New("invalid attribute: " + attr)
}
attr = attr[0:]
parts := strings.Split(attr, "=")
if len(parts) == 1 {
attribute.Key = strings.ToLower(parts[0])
attribute.TypedValue = &router.Domain_Attribute_BoolValue{BoolValue: true}
} else {
attribute.Key = strings.ToLower(parts[0])
intv, err := strconv.Atoi(parts[1])
if err != nil {
return attribute, errors.New("invalid attribute: " + attr + ": " + err.Error())
}
attribute.TypedValue = &router.Domain_Attribute_IntValue{IntValue: int64(intv)}
}
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, errors.New("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)
}
return entry, nil
} }
func DetectPath(path string) (string, error) { func DetectPath(path string) (string, error) {