Skip to content

Commit

Permalink
new: add more checks in the input json
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Feb 10, 2024
1 parent 2bb627a commit 790fd73
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
32 changes: 19 additions & 13 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
_ "embed"
"encoding/json"
"fmt"
"path/filepath"

"os"

Expand All @@ -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, "", " ")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 790fd73

Please # to comment.