| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 | 
							- 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
 
- }
 
 
  |