Skip to content

Commit

Permalink
new: add convert to xray config
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Aug 6, 2024
1 parent f340989 commit 6086673
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func main() {
} else {
configs = read()
}
clash_conf, err := ray2sing.Ray2Singbox(configs)
clash_conf, err := ray2sing.Ray2Singbox(configs, true)
if err != nil {
log.Fatalf("Failed to parse config: %v", err)
}
Expand Down
15 changes: 8 additions & 7 deletions ray2sing/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var xrayConfigTypes = map[string]ParserFunc{
"direct://": DirectXray,
}

func processSingleConfig(config string) (outbound *T.Outbound, err error) {
func processSingleConfig(config string, useXrayWhenPossible bool) (outbound *T.Outbound, err error) {
defer func() {
if r := recover(); r != nil {
outbound = nil
Expand All @@ -60,13 +60,14 @@ func processSingleConfig(config string) (outbound *T.Outbound, err error) {
}()

var configSingbox *T.Outbound
if strings.Contains(config, "&core=xray") {
if strings.Contains(config, "&core=xray") || useXrayWhenPossible {
for k, v := range xrayConfigTypes {
if strings.HasPrefix(config, k) {
configSingbox, err = v(config)
}
}
} else {
}
if configSingbox == nil {
for k, v := range configTypes {
if strings.HasPrefix(config, k) {
configSingbox, err = v(config)
Expand All @@ -86,7 +87,7 @@ func processSingleConfig(config string) (outbound *T.Outbound, err error) {
json.MarshalIndent(configSingbox, "", " ")
return configSingbox, nil
}
func GenerateConfigLite(input string) (string, error) {
func GenerateConfigLite(input string, useXrayWhenPossible bool) (string, error) {

configArray := strings.Split(strings.ReplaceAll(input, "\r\n", "\n"), "\n")

Expand All @@ -101,7 +102,7 @@ func GenerateConfigLite(input string) (string, error) {
chains := strings.Split(config, "&&detour=")
for _, chain := range chains {
fmt.Printf("%s", chain)
configSingbox, err := processSingleConfig(chain)
configSingbox, err := processSingleConfig(chain, useXrayWhenPossible)

if err != nil {
fmt.Fprintf(os.Stderr, "Error in %s \n %v\n", config, err)
Expand Down Expand Up @@ -162,7 +163,7 @@ func GenerateConfigLite(input string) (string, error) {
return string(jsonOutbound), nil
}

func Ray2Singbox(configs string) (out string, err error) {
func Ray2Singbox(configs string, useXrayWhenPossible bool) (out string, err error) {
defer func() {
if r := recover(); r != nil {
out = ""
Expand All @@ -174,6 +175,6 @@ func Ray2Singbox(configs string) (out string, err error) {

configs, _ = decodeBase64IfNeeded(configs)

convertedData, err := GenerateConfigLite(configs)
convertedData, err := GenerateConfigLite(configs, useXrayWhenPossible)
return convertedData, err
}
2 changes: 1 addition & 1 deletion ray2sing/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func CheckUrlAndJson(url string, expectedJSON string, t *testing.T) {
configJson, err := Ray2Singbox(url)
configJson, err := Ray2Singbox(url, false)
if err != nil {
t.Fatalf("Error parsing URL: %v", err)
}
Expand Down

0 comments on commit 6086673

Please sign in to comment.