mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-05 04:47:40 +03:00
Add serialize support for domain matcher
This commit is contained in:
parent
5a3d0edd1c
commit
6512789f97
1 changed files with 78 additions and 2 deletions
|
@ -1,8 +1,12 @@
|
||||||
package domain
|
package domain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
"sort"
|
"sort"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common/rw"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Matcher struct {
|
type Matcher struct {
|
||||||
|
@ -27,15 +31,87 @@ func NewMatcher(domains []string, domainSuffix []string) *Matcher {
|
||||||
domainList = append(domainList, reverseDomain(domain))
|
domainList = append(domainList, reverseDomain(domain))
|
||||||
}
|
}
|
||||||
sort.Strings(domainList)
|
sort.Strings(domainList)
|
||||||
return &Matcher{
|
return &Matcher{newSuccinctSet(domainList)}
|
||||||
newSuccinctSet(domainList),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadMatcher(reader io.Reader) (*Matcher, error) {
|
||||||
|
var version uint8
|
||||||
|
err := binary.Read(reader, binary.BigEndian, &version)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
leavesLength, err := rw.ReadUVariant(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
leaves := make([]uint64, leavesLength)
|
||||||
|
err = binary.Read(reader, binary.BigEndian, leaves)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
labelBitmapLength, err := rw.ReadUVariant(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
labelBitmap := make([]uint64, labelBitmapLength)
|
||||||
|
err = binary.Read(reader, binary.BigEndian, labelBitmap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
labelsLength, err := rw.ReadUVariant(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
labels := make([]byte, labelsLength)
|
||||||
|
_, err = io.ReadFull(reader, labels)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
set := &succinctSet{
|
||||||
|
leaves: leaves,
|
||||||
|
labelBitmap: labelBitmap,
|
||||||
|
labels: labels,
|
||||||
|
}
|
||||||
|
set.init()
|
||||||
|
return &Matcher{set}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Matcher) Match(domain string) bool {
|
func (m *Matcher) Match(domain string) bool {
|
||||||
return m.set.Has(reverseDomain(domain))
|
return m.set.Has(reverseDomain(domain))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Matcher) Write(writer io.Writer) error {
|
||||||
|
err := binary.Write(writer, binary.BigEndian, byte(1))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = rw.WriteUVariant(writer, uint64(len(m.set.leaves)))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = binary.Write(writer, binary.BigEndian, m.set.leaves)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = rw.WriteUVariant(writer, uint64(len(m.set.labelBitmap)))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = binary.Write(writer, binary.BigEndian, m.set.labelBitmap)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = rw.WriteUVariant(writer, uint64(len(m.set.labels)))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = writer.Write(m.set.labels)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func reverseDomain(domain string) string {
|
func reverseDomain(domain string) string {
|
||||||
l := len(domain)
|
l := len(domain)
|
||||||
b := make([]byte, l)
|
b := make([]byte, l)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue