sing-box/common/geosite/reader.go

112 lines
2.2 KiB
Go
Raw Permalink Normal View History

2022-07-04 11:39:58 +00:00
package geosite
import (
"io"
2022-07-05 01:05:35 +00:00
"os"
2022-07-04 11:39:58 +00:00
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/rw"
)
type Reader struct {
reader io.ReadSeeker
domainIndex map[string]int
domainLength map[string]int
}
2022-07-05 05:23:47 +00:00
func Open(path string) (*Reader, []string, error) {
2022-07-05 01:05:35 +00:00
content, err := os.Open(path)
if err != nil {
2022-07-05 05:23:47 +00:00
return nil, nil, err
2022-07-05 01:05:35 +00:00
}
reader := &Reader{
reader: content,
}
err = reader.readMetadata()
if err != nil {
content.Close()
2022-07-05 05:23:47 +00:00
return nil, nil, err
2022-07-05 01:05:35 +00:00
}
2022-07-05 05:23:47 +00:00
codes := make([]string, 0, len(reader.domainIndex))
for code := range reader.domainIndex {
codes = append(codes, code)
}
return reader, codes, nil
2022-07-05 01:05:35 +00:00
}
2022-07-04 11:39:58 +00:00
func (r *Reader) readMetadata() error {
version, err := rw.ReadByte(r.reader)
if err != nil {
return err
}
if version != 0 {
return E.New("unknown version")
}
entryLength, err := rw.ReadUVariant(r.reader)
if err != nil {
return err
}
keys := make([]string, entryLength)
domainIndex := make(map[string]int)
domainLength := make(map[string]int)
for i := 0; i < int(entryLength); i++ {
var (
code string
codeIndex uint64
codeLength uint64
)
code, err = rw.ReadVString(r.reader)
if err != nil {
return err
}
keys[i] = code
codeIndex, err = rw.ReadUVariant(r.reader)
if err != nil {
return err
}
codeLength, err = rw.ReadUVariant(r.reader)
if err != nil {
return err
}
domainIndex[code] = int(codeIndex)
domainLength[code] = int(codeLength)
}
r.domainIndex = domainIndex
r.domainLength = domainLength
return nil
}
func (r *Reader) Read(code string) ([]Item, error) {
2022-07-07 15:36:32 +00:00
index, exists := r.domainIndex[code]
if !exists {
2022-07-04 11:39:58 +00:00
return nil, E.New("code ", code, " not exists!")
}
2022-07-07 15:36:32 +00:00
_, err := r.reader.Seek(int64(index), io.SeekCurrent)
if err != nil {
return nil, err
}
2022-07-04 11:39:58 +00:00
counter := &rw.ReadCounter{Reader: r.reader}
domain := make([]Item, r.domainLength[code])
for i := range domain {
var (
item Item
err error
)
item.Type, err = rw.ReadByte(counter)
if err != nil {
return nil, err
}
item.Value, err = rw.ReadVString(counter)
if err != nil {
return nil, err
}
domain[i] = item
}
2022-07-07 15:36:32 +00:00
_, err = r.reader.Seek(int64(-index)-counter.Count(), io.SeekCurrent)
2022-07-04 11:39:58 +00:00
return domain, err
}
func (r *Reader) Upstream() any {
return r.reader
}