diff --git a/config/parser.go b/config/parser.go index b584564..3bee77d 100644 --- a/config/parser.go +++ b/config/parser.go @@ -5,6 +5,7 @@ import ( _ "embed" "encoding/json" "fmt" + "path/filepath" "os" @@ -29,23 +30,28 @@ func ParseConfig(path string, debug bool) ([]byte, error) { var jsonObj map[string]interface{} fmt.Printf("Convert using json\n") + var tmpJsonResult any jsonDecoder := json.NewDecoder(SJ.NewCommentFilter(bytes.NewReader(content))) - if err := jsonDecoder.Decode(&jsonObj); err == nil { - if jsonObj["outbounds"] == nil { - if jsonArray, ok := jsonObj.([]map[string]interface{}); ok { - jsonObj = map[string]interface{}{"outbounds": jsonArray} - if jsonArray[0]["type"] == nil { - return nil, fmt.Errorf("[SingboxParser] no outbounds found") - } - } else if jsonObj["type"] == nil { - return nil, fmt.Errorf("[SingboxParser] no outbounds found") - } else { + if err := jsonDecoder.Decode(&tmpJsonResult); err == nil { + if tmpJsonObj, ok := tmpJsonResult.(map[string]interface{}); ok { + if tmpJsonObj["outbounds"] == nil { jsonObj = map[string]interface{}{"outbounds": []interface{}{jsonObj}} + } else { + jsonObj["outbounds"] = tmpJsonObj["outbounds"] } + } else if jsonArray, ok := tmpJsonResult.([]map[string]interface{}); ok { + jsonObj = map[string]interface{}{"outbounds": jsonArray} + } else { + return nil, fmt.Errorf("[SingboxParser] Incorrect Json Format") } - - jsonObj = map[string]interface{}{ - "outbounds": jsonObj["outbounds"], + if outbounds, ok := jsonObj["outbounds"].([]map[string]interface{}); ok { + for _, base := range outbounds { + if base["type"] == nil { + return nil, fmt.Errorf("[SingboxParser] No Type found!") + } + } + } else { + return nil, fmt.Errorf("[SingboxParser] Incorrect Outbounds Format") } newContent, _ := json.MarshalIndent(jsonObj, "", " ") diff --git a/go.mod b/go.mod index 794bbbc..2245c9e 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( require ( berty.tech/go-libtor v1.0.385 // indirect github.com/ajg/form v1.5.1 // indirect + github.com/akavel/rsrc v0.10.2 // indirect github.com/andybalholm/brotli v1.0.6 // indirect github.com/caddyserver/certmagic v0.20.0 // indirect github.com/cloudflare/circl v1.3.7 // indirect diff --git a/go.sum b/go.sum index f53651c..fc101af 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ berty.tech/go-libtor v1.0.385 h1:RWK94C3hZj6Z2GdvePpHJLnWYobFr3bY/OdUJ5aoEXw= berty.tech/go-libtor v1.0.385/go.mod h1:9swOOQVb+kmvuAlsgWUK/4c52pm69AdbJsxLzk+fJEw= github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU= github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= +github.com/akavel/rsrc v0.10.2 h1:Zxm8V5eI1hW4gGaYsJQUhxpjkENuG91ki8B4zCrvEsw= +github.com/akavel/rsrc v0.10.2/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c= github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/caddyserver/certmagic v0.20.0 h1:bTw7LcEZAh9ucYCRXyCpIrSAGplplI0vGYJ4BpCQ/Fc=