Skip to content

Commit

Permalink
Change config parser
Browse files Browse the repository at this point in the history
  • Loading branch information
0xConsumer committed Jan 17, 2024
1 parent 841d999 commit d63eb58
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 78 deletions.
97 changes: 28 additions & 69 deletions config/parser.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package config


import (
_ "embed"
"encoding/json"
"fmt"
"os"
"github.com/titanous/json5"

"github.com/hiddify/ray2sing/ray2sing"
"github.com/sagernet/sing-box/experimental/libbox"
Expand All @@ -17,89 +16,49 @@ import (
//go:embed config.json.template
var configByte []byte

var configParsers = []func([]byte, bool) ([]byte, error){
parseSingboxConfig,
parseV2rayConfig,
parseClashConfig,
}

func ParseConfig(path string, debug bool) ([]byte, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}

var parseError error
for index, parser := range configParsers {
config, err := parser(content, debug)
if err == nil {
fmt.Printf("[ConfigParser] success with parser #%d, checking...\n", index)
err_internal_check:=isCorrectSingboxConfig(config,debug)
if err_internal_check!=nil{
return config, err_internal_check
}
err = libbox.CheckConfig(string(config))
return config, err
var jsonObj map[string]interface{}
if err := json.Unmarshal(content, &jsonObj); err == nil {
if jsonObj["outbounds"] == nil {
return nil, fmt.Errorf("[SingboxParser] no outbounds found")
}
parseError = err
return validateResult(content, "SingboxParser")
}
return nil, parseError
}

func parseV2rayConfig(content []byte, debug bool) ([]byte, error) {
config, err := ray2sing.Ray2Singbox(string(content))
if err != nil {
fmt.Printf("[V2rayParser] error: %s\n", err)
return nil, err
v2rayStr, err := ray2sing.Ray2Singbox(string(content))
if err == nil {
return validateResult([]byte(v2rayStr), "V2rayParser")
}
return []byte(config), nil
}

func parseClashConfig(content []byte, debug bool) ([]byte, error) {
clashConfig := clash.Clash{}
err := yaml.Unmarshal(content, &clashConfig)
if err != nil {
fmt.Printf("[ClashParser] unmarshal error: %s\n", err)
return nil, err
}
if len(clashConfig.Proxies)==0{
return nil,fmt.Errorf("No Outbound Available! %s", string(content))
}


sbConfig, err := convert.Clash2sing(clashConfig)
if err != nil {
fmt.Printf("[ClashParser] convert error: %s\n", err)
return nil, err
clashObj := clash.Clash{}
if err := yaml.Unmarshal(content, &clashObj); err == nil && clashObj.Proxies != nil {
if len(clashObj.Proxies) == 0 {
return nil, fmt.Errorf("[ClashParser] no outbounds found")
}
converted, err := convert.Clash2sing(clashObj)
if err != nil {
return nil, fmt.Errorf("[ClashParser] converting clash to sing-box error: %w", err)
}
output := configByte
output, err = convert.Patch(output, converted, "", "", nil)
if err != nil {
return nil, fmt.Errorf("[ClashParser] patching clash config error: %w", err)
}
return validateResult(output, "ClashParser")
}

output := configByte
output, err = convert.Patch(output, sbConfig, "", "", nil)
if err != nil {
fmt.Printf("[ClashParser] patch error: %s\n", err)
return nil, err
}
return output, nil
return nil, fmt.Errorf("unable to determine config format")
}

func parseSingboxConfig(content []byte, debug bool) ([]byte, error) {
var dummy map[string]interface{}
err := json5.Unmarshal(content, &dummy)
func validateResult(content []byte, name string) ([]byte, error) {
err := libbox.CheckConfig(string(content))
if err != nil {
return nil, err
return nil, fmt.Errorf("[%s] invalid sing-box config: %w", name, err)
}
return content, nil
}
func isCorrectSingboxConfig(content []byte, debug bool) error {
var dummy map[string]interface{}
err := json5.Unmarshal(content, &dummy)
if err != nil {
return err
}

if dummy["outbounds"]==nil {
return fmt.Errorf("No Outbound Available! %s", string(content))
}

return nil
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/sagernet/sing-box v1.7.8
github.com/sagernet/sing-dns v0.1.12
github.com/spf13/cobra v1.8.0
github.com/titanous/json5 v1.0.0
github.com/xmdhs/clash2singbox v0.0.2
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/q
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/libdns/alidns v1.0.3 h1:LFHuGnbseq5+HCeGa1aW8awyX/4M2psB9962fdD2+yQ=
github.com/libdns/alidns v1.0.3/go.mod h1:e18uAG6GanfRhcJj6/tps2rCMzQJaYVcGKT+ELjdjGE=
github.com/libdns/cloudflare v0.1.0 h1:93WkJaGaiXCe353LHEP36kAWCUw0YjFqwhkBkU2/iic=
Expand Down Expand Up @@ -114,8 +112,6 @@ github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/qtls-go1-20 v0.4.1 h1:D33340mCNDAIKBqXuAvexTNMUByrYmFYVfKfDN5nfFs=
github.com/quic-go/qtls-go1-20 v0.4.1/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k=
github.com/robertkrimen/otto v0.2.1 h1:FVP0PJ0AHIjC+N4pKCG9yCDz6LHNPCwi/GKID5pGGF0=
github.com/robertkrimen/otto v0.2.1/go.mod h1:UPwtJ1Xu7JrLcZjNWN8orJaM5n5YEtqL//farB5FlRY=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a h1:+NkI2670SQpQWvkkD2QgdTuzQG263YZ+2emfpeyGqW0=
github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a/go.mod h1:63s7jpZqcDAIpj8oI/1v4Izok+npJOHACFCU6+huCkM=
Expand Down Expand Up @@ -180,8 +176,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/titanous/json5 v1.0.0 h1:hJf8Su1d9NuI/ffpxgxQfxh/UiBFZX7bMPid0rIL/7s=
github.com/titanous/json5 v1.0.0/go.mod h1:7JH1M8/LHKc6cyP5o5g3CSaRj+mBrIimTxzpvmckH8c=
github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 h1:tHNk7XK9GkmKUR6Gh8gVBKXc2MVSZ4G/NnWLtzw4gNA=
github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923/go.mod h1:eLL9Nub3yfAho7qB0MzZizFhTU2QkLeoVsWdHtDW264=
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg=
Expand Down Expand Up @@ -288,8 +282,6 @@ google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down

0 comments on commit d63eb58

Please sign in to comment.