Browse Source

platform: Add check config func

世界 2 years ago
parent
commit
c3d7401ead
1 changed files with 17 additions and 0 deletions
  1. 17 0
      experimental/libbox/config.go

+ 17 - 0
experimental/libbox/config.go

@@ -3,6 +3,9 @@
 package libbox
 package libbox
 
 
 import (
 import (
+	"context"
+
+	"github.com/sagernet/sing-box"
 	"github.com/sagernet/sing-box/option"
 	"github.com/sagernet/sing-box/option"
 	E "github.com/sagernet/sing/common/exceptions"
 	E "github.com/sagernet/sing/common/exceptions"
 )
 )
@@ -15,3 +18,17 @@ func parseConfig(configContent string) (option.Options, error) {
 	}
 	}
 	return options, nil
 	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(ctx, options, nil)
+	if err == nil {
+		instance.Close()
+	}
+	return err
+}