mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-10 02:53:12 +00:00
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package libbox
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/sagernet/sing-box"
|
|
"github.com/sagernet/sing-box/option"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
)
|
|
|
|
func parseConfig(configContent string) (option.Options, error) {
|
|
var options option.Options
|
|
err := options.UnmarshalJSON([]byte(configContent))
|
|
if err != nil {
|
|
return option.Options{}, E.Cause(err, "decode config")
|
|
}
|
|
return options, nil
|
|
}
|
|
|
|
func CheckConfig(configContent string) error {
|
|
options, err := parseConfig(configContent)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
instance, err := box.New(box.Options{
|
|
Context: ctx,
|
|
Options: options,
|
|
})
|
|
if err == nil {
|
|
instance.Close()
|
|
}
|
|
return err
|
|
}
|
|
|
|
func FormatConfig(configContent string) (string, error) {
|
|
options, err := parseConfig(configContent)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
var buffer bytes.Buffer
|
|
json.NewEncoder(&buffer)
|
|
encoder := json.NewEncoder(&buffer)
|
|
encoder.SetIndent("", " ")
|
|
err = encoder.Encode(options)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return buffer.String(), nil
|
|
}
|