2023-03-15 06:56:06 +00:00
|
|
|
//go:build linux || darwin
|
|
|
|
|
2022-10-25 04:55:00 +00:00
|
|
|
package libbox
|
|
|
|
|
|
|
|
import (
|
2023-04-02 02:35:03 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box"
|
2022-10-25 04:55:00 +00:00
|
|
|
"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
|
|
|
|
}
|
2023-04-02 02:35:03 +00:00
|
|
|
|
|
|
|
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(ctx, options, nil)
|
|
|
|
if err == nil {
|
|
|
|
instance.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|