new: add chain support & fix warp bug

This commit is contained in:
Hiddify
2024-01-30 19:13:14 +01:00
parent 571e910026
commit 8a56261e95
5 changed files with 42 additions and 11 deletions

View File

@@ -166,6 +166,15 @@ func patchOutbound(base option.Outbound, configOpt ConfigOptions) (*option.Outbo
}
func patchWarp(base *option.Outbound) error {
if base.Type == C.TypeWireGuard {
host := base.WireGuardOptions.Server
if host == "default" || host == "random" || host == "auto" {
base.WireGuardOptions.Server = getRandomIP()
}
if base.WireGuardOptions.ServerPort == 0 {
base.WireGuardOptions.ServerPort = generateRandomPort()
}
}
if base.Type == C.TypeCustom {
if warp, ok := base.CustomOptions["warp"].(map[string]interface{}); ok {
key, _ := warp["key"].(string)
@@ -186,6 +195,7 @@ func patchWarp(base *option.Outbound) error {
}
}
return nil
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/hiddify/ray2sing/ray2sing"
"github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing-box/option"
SJ "github.com/sagernet/sing/common/json"
"github.com/xmdhs/clash2singbox/convert"
"github.com/xmdhs/clash2singbox/model/clash"
@@ -38,12 +39,13 @@ func ParseConfig(path string, debug bool) ([]byte, error) {
}
newContent, _ := json.MarshalIndent(jsonObj, "", " ")
return validateResult(newContent, "SingboxParser")
return patchConfig([]byte(newContent), "SingboxParser")
}
fmt.Printf("Convert using v2ray\n")
v2rayStr, err := ray2sing.Ray2Singbox(string(content))
if err == nil {
return validateResult([]byte(v2rayStr), "V2rayParser")
return patchConfig([]byte(v2rayStr), "V2rayParser")
}
fmt.Printf("Convert using clash\n")
clashObj := clash.Clash{}
@@ -60,13 +62,35 @@ func ParseConfig(path string, debug bool) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("[ClashParser] patching clash config error: %w", err)
}
return validateResult(output, "ClashParser")
return patchConfig(output, "ClashParser")
}
return nil, fmt.Errorf("unable to determine config format")
}
func patchConfig(content []byte, name string) ([]byte, error) {
options := option.Options{}
err := json.Unmarshal(content, &options)
if err != nil {
return nil, fmt.Errorf("[SingboxParser] unmarshal error: %w", err)
}
for i, base := range options.Outbounds {
err := patchWarp(&base)
if err != nil {
return nil, fmt.Errorf("[Warp] patch warp error: %w", err)
}
options.Outbounds[i] = base
}
content, _ = json.MarshalIndent(options, "", " ")
fmt.Printf("%s\n", content)
return validateResult(content, name)
}
func validateResult(content []byte, name string) ([]byte, error) {
err := libbox.CheckConfig(string(content))
if err != nil {
return nil, fmt.Errorf("[%s] invalid sing-box config: %w", name, err)

View File

@@ -162,11 +162,8 @@ func generateWarp(license string, host string, port uint16, fakePackets string)
if host == "auto" && fakePackets == "" {
fakePackets = "5-10"
}
if host == "default" || host == "random" || host == "auto" {
host = getRandomIP()
}
if port == 0 {
port = generateRandomPort()
if _, err := os.Stat("./wgcf-identity.json"); err == nil {
os.Remove("./wgcf-identity.json")
}
if !warp.CheckProfileExists(license) {