mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-30 12:31:31 +00:00
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package geosite
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"sort"
|
|
|
|
"github.com/sagernet/sing/common/varbin"
|
|
)
|
|
|
|
func Write(writer varbin.Writer, domains map[string][]Item) error {
|
|
keys := make([]string, 0, len(domains))
|
|
for code := range domains {
|
|
keys = append(keys, code)
|
|
}
|
|
sort.Strings(keys)
|
|
|
|
content := &bytes.Buffer{}
|
|
index := make(map[string]int)
|
|
for _, code := range keys {
|
|
index[code] = content.Len()
|
|
err := varbin.Write(content, binary.BigEndian, domains[code])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
err := writer.WriteByte(0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = varbin.WriteUvarint(writer, uint64(len(keys)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, code := range keys {
|
|
err = varbin.Write(writer, binary.BigEndian, code)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = varbin.WriteUvarint(writer, uint64(index[code]))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = varbin.WriteUvarint(writer, uint64(len(domains[code])))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
_, err = writer.Write(content.Bytes())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|