sing-box/experimental/libbox/profile_import.go

253 lines
6 KiB
Go
Raw Permalink Normal View History

2023-07-29 00:37:10 +00:00
package libbox
import (
2024-06-24 01:49:15 +00:00
"bufio"
2023-07-29 00:37:10 +00:00
"bytes"
2023-07-30 12:26:09 +00:00
"compress/gzip"
2023-07-29 00:37:10 +00:00
"encoding/binary"
E "github.com/sagernet/sing/common/exceptions"
2024-06-24 01:49:15 +00:00
"github.com/sagernet/sing/common/varbin"
2023-07-29 00:37:10 +00:00
)
func EncodeChunkedMessage(data []byte) []byte {
var buffer bytes.Buffer
binary.Write(&buffer, binary.BigEndian, uint16(len(data)))
buffer.Write(data)
return buffer.Bytes()
}
func DecodeLengthChunk(data []byte) int32 {
return int32(binary.BigEndian.Uint16(data))
}
const (
MessageTypeError = iota
MessageTypeProfileList
MessageTypeProfileContentRequest
MessageTypeProfileContent
)
type ErrorMessage struct {
Message string
}
func (e *ErrorMessage) Encode() []byte {
var buffer bytes.Buffer
buffer.WriteByte(MessageTypeError)
2024-06-24 01:49:15 +00:00
varbin.Write(&buffer, binary.BigEndian, e.Message)
2023-07-29 00:37:10 +00:00
return buffer.Bytes()
}
func DecodeErrorMessage(data []byte) (*ErrorMessage, error) {
reader := bytes.NewReader(data)
2024-06-24 01:49:15 +00:00
messageType, err := reader.ReadByte()
2023-07-29 00:37:10 +00:00
if err != nil {
return nil, err
}
if messageType != MessageTypeError {
return nil, E.New("invalid message")
}
var message ErrorMessage
2024-06-24 01:49:15 +00:00
message.Message, err = varbin.ReadValue[string](reader, binary.BigEndian)
2023-07-29 00:37:10 +00:00
if err != nil {
return nil, err
}
return &message, nil
}
const (
ProfileTypeLocal int32 = iota
ProfileTypeiCloud
ProfileTypeRemote
)
type ProfilePreview struct {
ProfileID int64
Name string
Type int32
}
type ProfilePreviewIterator interface {
Next() *ProfilePreview
HasNext() bool
}
type ProfileEncoder struct {
profiles []ProfilePreview
}
func (e *ProfileEncoder) Append(profile *ProfilePreview) {
e.profiles = append(e.profiles, *profile)
}
func (e *ProfileEncoder) Encode() []byte {
var buffer bytes.Buffer
buffer.WriteByte(MessageTypeProfileList)
binary.Write(&buffer, binary.BigEndian, uint16(len(e.profiles)))
for _, preview := range e.profiles {
binary.Write(&buffer, binary.BigEndian, preview.ProfileID)
2024-06-24 01:49:15 +00:00
varbin.Write(&buffer, binary.BigEndian, preview.Name)
2023-07-29 00:37:10 +00:00
binary.Write(&buffer, binary.BigEndian, preview.Type)
}
return buffer.Bytes()
}
type ProfileDecoder struct {
profiles []*ProfilePreview
}
func (d *ProfileDecoder) Decode(data []byte) error {
reader := bytes.NewReader(data)
messageType, err := reader.ReadByte()
if err != nil {
return err
}
if messageType != MessageTypeProfileList {
return E.New("invalid message")
}
var profileCount uint16
err = binary.Read(reader, binary.BigEndian, &profileCount)
if err != nil {
return err
}
for i := 0; i < int(profileCount); i++ {
var profile ProfilePreview
err = binary.Read(reader, binary.BigEndian, &profile.ProfileID)
if err != nil {
return err
}
2024-06-24 01:49:15 +00:00
profile.Name, err = varbin.ReadValue[string](reader, binary.BigEndian)
2023-07-29 00:37:10 +00:00
if err != nil {
return err
}
err = binary.Read(reader, binary.BigEndian, &profile.Type)
if err != nil {
return err
}
d.profiles = append(d.profiles, &profile)
}
return nil
}
func (d *ProfileDecoder) Iterator() ProfilePreviewIterator {
return newIterator(d.profiles)
}
type ProfileContentRequest struct {
ProfileID int64
}
func (r *ProfileContentRequest) Encode() []byte {
var buffer bytes.Buffer
buffer.WriteByte(MessageTypeProfileContentRequest)
binary.Write(&buffer, binary.BigEndian, r.ProfileID)
return buffer.Bytes()
}
func DecodeProfileContentRequest(data []byte) (*ProfileContentRequest, error) {
reader := bytes.NewReader(data)
2024-06-24 01:49:15 +00:00
messageType, err := reader.ReadByte()
2023-07-29 00:37:10 +00:00
if err != nil {
return nil, err
}
if messageType != MessageTypeProfileContentRequest {
return nil, E.New("invalid message")
}
var request ProfileContentRequest
err = binary.Read(reader, binary.BigEndian, &request.ProfileID)
if err != nil {
return nil, err
}
return &request, nil
}
type ProfileContent struct {
2023-09-21 02:51:47 +00:00
Name string
Type int32
Config string
RemotePath string
AutoUpdate bool
AutoUpdateInterval int32
LastUpdated int64
2023-07-29 00:37:10 +00:00
}
func (c *ProfileContent) Encode() []byte {
2023-07-30 12:26:09 +00:00
buffer := new(bytes.Buffer)
2023-07-29 00:37:10 +00:00
buffer.WriteByte(MessageTypeProfileContent)
2023-09-21 02:51:47 +00:00
buffer.WriteByte(1)
2024-06-24 01:49:15 +00:00
gWriter := gzip.NewWriter(buffer)
writer := bufio.NewWriter(gWriter)
varbin.Write(writer, binary.BigEndian, c.Name)
2023-07-30 12:26:09 +00:00
binary.Write(writer, binary.BigEndian, c.Type)
2024-06-24 01:49:15 +00:00
varbin.Write(writer, binary.BigEndian, c.Config)
2023-07-30 12:26:09 +00:00
if c.Type != ProfileTypeLocal {
2024-06-24 01:49:15 +00:00
varbin.Write(writer, binary.BigEndian, c.RemotePath)
2023-09-21 02:51:47 +00:00
}
if c.Type == ProfileTypeRemote {
2023-07-30 12:26:09 +00:00
binary.Write(writer, binary.BigEndian, c.AutoUpdate)
2023-09-21 02:51:47 +00:00
binary.Write(writer, binary.BigEndian, c.AutoUpdateInterval)
2023-07-30 12:26:09 +00:00
binary.Write(writer, binary.BigEndian, c.LastUpdated)
}
writer.Flush()
2024-06-24 01:49:15 +00:00
gWriter.Flush()
gWriter.Close()
2023-07-29 00:37:10 +00:00
return buffer.Bytes()
}
func DecodeProfileContent(data []byte) (*ProfileContent, error) {
2024-06-24 01:49:15 +00:00
reader := bytes.NewReader(data)
messageType, err := reader.ReadByte()
2023-07-29 00:37:10 +00:00
if err != nil {
return nil, err
}
if messageType != MessageTypeProfileContent {
return nil, E.New("invalid message")
}
2024-06-24 01:49:15 +00:00
version, err := reader.ReadByte()
2023-07-29 00:37:10 +00:00
if err != nil {
return nil, err
}
2024-06-24 01:49:15 +00:00
gReader, err := gzip.NewReader(reader)
2023-09-21 02:51:47 +00:00
if err != nil {
2023-07-30 12:26:09 +00:00
return nil, E.Cause(err, "unsupported profile")
2023-07-29 00:37:10 +00:00
}
2024-06-24 01:49:15 +00:00
bReader := varbin.StubReader(gReader)
2023-07-30 12:26:09 +00:00
var content ProfileContent
2024-06-24 01:49:15 +00:00
content.Name, err = varbin.ReadValue[string](bReader, binary.BigEndian)
2023-07-29 00:37:10 +00:00
if err != nil {
return nil, err
}
err = binary.Read(bReader, binary.BigEndian, &content.Type)
2023-07-29 00:37:10 +00:00
if err != nil {
return nil, err
}
2024-06-24 01:49:15 +00:00
content.Config, err = varbin.ReadValue[string](bReader, binary.BigEndian)
2023-07-29 00:37:10 +00:00
if err != nil {
return nil, err
}
2023-07-30 12:26:09 +00:00
if content.Type != ProfileTypeLocal {
2024-06-24 01:49:15 +00:00
content.RemotePath, err = varbin.ReadValue[string](bReader, binary.BigEndian)
2023-07-30 12:26:09 +00:00
if err != nil {
return nil, err
}
2023-09-21 02:51:47 +00:00
}
if content.Type == ProfileTypeRemote || (version == 0 && content.Type != ProfileTypeLocal) {
err = binary.Read(bReader, binary.BigEndian, &content.AutoUpdate)
2023-07-30 12:26:09 +00:00
if err != nil {
return nil, err
}
2023-09-21 02:51:47 +00:00
if version >= 1 {
err = binary.Read(bReader, binary.BigEndian, &content.AutoUpdateInterval)
2023-09-21 02:51:47 +00:00
if err != nil {
return nil, err
}
}
err = binary.Read(bReader, binary.BigEndian, &content.LastUpdated)
2023-07-30 12:26:09 +00:00
if err != nil {
return nil, err
}
2023-07-29 00:37:10 +00:00
}
return &content, nil
}