Fix parser
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/hiddify/ray2sing/ray2sing"
|
"github.com/hiddify/ray2sing/ray2sing"
|
||||||
"github.com/sagernet/sing-box/experimental/libbox"
|
"github.com/sagernet/sing-box/experimental/libbox"
|
||||||
|
"github.com/sagernet/sing-box/option"
|
||||||
"github.com/xmdhs/clash2singbox/convert"
|
"github.com/xmdhs/clash2singbox/convert"
|
||||||
"github.com/xmdhs/clash2singbox/model/clash"
|
"github.com/xmdhs/clash2singbox/model/clash"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -15,58 +16,73 @@ import (
|
|||||||
//go:embed config.json.template
|
//go:embed config.json.template
|
||||||
var configByte []byte
|
var configByte []byte
|
||||||
|
|
||||||
|
var configParsers = []func([]byte, bool) ([]byte, error){
|
||||||
|
parseSingboxConfig,
|
||||||
|
parseV2rayConfig,
|
||||||
|
parseClashConfig,
|
||||||
|
}
|
||||||
|
|
||||||
func ParseConfig(path string, tempPath string, debug bool) error {
|
func ParseConfig(path string, tempPath string, debug bool) error {
|
||||||
content, err := os.ReadFile(tempPath)
|
content, err := os.ReadFile(tempPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
config, err := parseV2rayFormat(content)
|
|
||||||
if err != nil {
|
|
||||||
config = content
|
|
||||||
config, err = parseClash(content)
|
|
||||||
if err != nil {
|
|
||||||
config = content
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = libbox.CheckConfig(string(config))
|
var parseError error
|
||||||
if err != nil {
|
for index, parser := range configParsers {
|
||||||
return err
|
config, err := parser(content, debug)
|
||||||
|
if err == nil {
|
||||||
|
fmt.Printf("[ConfigParser] success with parser #%d, checking...\n", index)
|
||||||
|
err = libbox.CheckConfig(string(config))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = os.WriteFile(path, config, 0777)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
parseError = err
|
||||||
}
|
}
|
||||||
err = os.WriteFile(path, config, 0777)
|
return parseError
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseV2rayFormat(content []byte) ([]byte, error) {
|
func parseV2rayConfig(content []byte, debug bool) ([]byte, error) {
|
||||||
singconf, err := ray2sing.Ray2Singbox(string(content))
|
config, err := ray2sing.Ray2Singbox(string(content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error: %v\n", err)
|
fmt.Printf("[V2rayParser] error: %s\n", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return []byte(singconf), nil
|
return []byte(config), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseClash(content []byte) ([]byte, error) {
|
func parseClashConfig(content []byte, debug bool) ([]byte, error) {
|
||||||
clashConfig := clash.Clash{}
|
clashConfig := clash.Clash{}
|
||||||
err := yaml.Unmarshal(content, &clashConfig)
|
err := yaml.Unmarshal(content, &clashConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("unmarshal error %s", err)
|
fmt.Printf("[ClashParser] unmarshal error: %s\n", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sbConfig, err := convert.Clash2sing(clashConfig)
|
sbConfig, err := convert.Clash2sing(clashConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("convert error %s", err)
|
fmt.Printf("[ClashParser] convert error: %s\n", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
output := configByte
|
output := configByte
|
||||||
output, err = convert.Patch(output, sbConfig, "", "", nil)
|
output, err = convert.Patch(output, sbConfig, "", "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("patch error %s", err)
|
fmt.Printf("[ClashParser] patch error: %s\n", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return output, nil
|
return output, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseSingboxConfig(content []byte, debug bool) ([]byte, error) {
|
||||||
|
var options option.Options
|
||||||
|
err := options.UnmarshalJSON(content)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("[SingboxParser] unmarshal error: %s\n", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return content, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user