sing-box/common/geosite/reader.go

138 lines
2.8 KiB
Go
Raw Normal View History

2022-07-04 11:39:58 +00:00
package geosite
import (
2024-06-24 01:49:15 +00:00
"bufio"
"encoding/binary"
2022-07-04 11:39:58 +00:00
"io"
2022-07-05 01:05:35 +00:00
"os"
2024-06-24 01:49:15 +00:00
"sync"
"sync/atomic"
2022-07-04 11:39:58 +00:00
E "github.com/sagernet/sing/common/exceptions"
2024-06-24 01:49:15 +00:00
"github.com/sagernet/sing/common/varbin"
2022-07-04 11:39:58 +00:00
)
type Reader struct {
2024-06-24 01:49:15 +00:00
access sync.Mutex
reader io.ReadSeeker
bufferedReader *bufio.Reader
metadataIndex int64
domainIndex map[string]int
domainLength map[string]int
2022-07-04 11:39:58 +00:00
}
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
}
2024-06-24 01:49:15 +00:00
reader, codes, err := NewReader(content)
if err != nil {
content.Close()
return nil, nil, err
}
return reader, codes, nil
}
func NewReader(readSeeker io.ReadSeeker) (*Reader, []string, error) {
2022-07-05 01:05:35 +00:00
reader := &Reader{
2024-06-24 01:49:15 +00:00
reader: readSeeker,
2022-07-05 01:05:35 +00:00
}
2024-06-24 01:49:15 +00:00
err := reader.readMetadata()
2022-07-05 01:05:35 +00:00
if err != nil {
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
}
2024-06-24 01:49:15 +00:00
type geositeMetadata struct {
Code string
Index uint64
Length uint64
}
2022-07-04 11:39:58 +00:00
func (r *Reader) readMetadata() error {
2024-06-24 01:49:15 +00:00
counter := &readCounter{Reader: r.reader}
reader := bufio.NewReader(counter)
version, err := reader.ReadByte()
2022-07-04 11:39:58 +00:00
if err != nil {
return err
}
if version != 0 {
return E.New("unknown version")
}
2024-06-24 01:49:15 +00:00
entryLength, err := binary.ReadUvarint(reader)
2022-07-04 11:39:58 +00:00
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
)
2024-06-24 01:49:15 +00:00
code, err = varbin.ReadValue[string](reader, binary.BigEndian)
2022-07-04 11:39:58 +00:00
if err != nil {
return err
}
keys[i] = code
2024-06-24 01:49:15 +00:00
codeIndex, err = binary.ReadUvarint(reader)
2022-07-04 11:39:58 +00:00
if err != nil {
return err
}
2024-06-24 01:49:15 +00:00
codeLength, err = binary.ReadUvarint(reader)
2022-07-04 11:39:58 +00:00
if err != nil {
return err
}
domainIndex[code] = int(codeIndex)
domainLength[code] = int(codeLength)
}
r.domainIndex = domainIndex
r.domainLength = domainLength
2024-06-24 01:49:15 +00:00
r.metadataIndex = counter.count - int64(reader.Buffered())
r.bufferedReader = reader
2022-07-04 11:39:58 +00:00
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!")
}
2024-06-24 01:49:15 +00:00
_, err := r.reader.Seek(r.metadataIndex+int64(index), io.SeekStart)
2022-07-07 15:36:32 +00:00
if err != nil {
return nil, err
}
2024-06-24 01:49:15 +00:00
r.bufferedReader.Reset(r.reader)
itemList := make([]Item, r.domainLength[code])
err = varbin.Read(r.bufferedReader, binary.BigEndian, &itemList)
if err != nil {
return nil, err
2022-07-04 11:39:58 +00:00
}
2024-06-24 01:49:15 +00:00
return itemList, nil
2022-07-04 11:39:58 +00:00
}
func (r *Reader) Upstream() any {
return r.reader
}
2024-06-24 01:49:15 +00:00
type readCounter struct {
io.Reader
count int64
}
func (r *readCounter) Read(p []byte) (n int, err error) {
n, err = r.Reader.Read(p)
if n > 0 {
atomic.AddInt64(&r.count, int64(n))
}
return
}