diff --git a/adapter/outbound/hysteria.go b/adapter/outbound/hysteria.go index be285245..932e8feb 100644 --- a/adapter/outbound/hysteria.go +++ b/adapter/outbound/hysteria.go @@ -99,7 +99,6 @@ type HysteriaOption struct { Down string `proxy:"down"` DownSpeed int `proxy:"down-speed,omitempty"` // compatible with Stash Auth string `proxy:"auth,omitempty"` - OldAuthString string `proxy:"auth_str,omitempty"` AuthString string `proxy:"auth-str,omitempty"` Obfs string `proxy:"obfs,omitempty"` SNI string `proxy:"sni,omitempty"` diff --git a/adapter/parser.go b/adapter/parser.go index c0ed8792..3ca0c3ee 100644 --- a/adapter/parser.go +++ b/adapter/parser.go @@ -9,14 +9,10 @@ import ( C "github.com/Dreamacro/clash/constant" ) -func ParseProxy(mapping map[string]any) (C.Proxy, error) { - newMapping := make(map[string]any) - for key := range mapping { - newMapping[strings.ReplaceAll(key, "_", "-")] = mapping[key] - } - mapping = newMapping +var keyReplacer = strings.NewReplacer("_", "-") - decoder := structure.NewDecoder(structure.Option{TagName: "proxy", WeaklyTypedInput: true}) +func ParseProxy(mapping map[string]any) (C.Proxy, error) { + decoder := structure.NewDecoder(structure.Option{TagName: "proxy", WeaklyTypedInput: true, KeyReplacer: keyReplacer}) proxyType, existType := mapping["type"].(string) if !existType { return nil, fmt.Errorf("missing type") diff --git a/common/structure/structure.go b/common/structure/structure.go index e74c349a..2b5e024f 100644 --- a/common/structure/structure.go +++ b/common/structure/structure.go @@ -13,6 +13,7 @@ import ( type Option struct { TagName string WeaklyTypedInput bool + KeyReplacer *strings.Replacer } // Decoder is the core of structure @@ -48,7 +49,24 @@ func (d *Decoder) Decode(src map[string]any, dst any) error { key, omitKey, found := strings.Cut(tag, ",") omitempty := found && omitKey == "omitempty" + if d.option.KeyReplacer != nil { + key = d.option.KeyReplacer.Replace(key) + } + value, ok := src[key] + if !ok { + for _strKey := range src { + strKey := _strKey + if d.option.KeyReplacer != nil { + strKey = d.option.KeyReplacer.Replace(strKey) + } + if strings.EqualFold(key, strKey) { + value = src[_strKey] + ok = true + break + } + } + } if !ok || value == nil { if omitempty { continue @@ -346,6 +364,9 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e tagValue = strings.SplitN(tagValue, ",", 2)[0] if tagValue != "" { fieldName = tagValue + if d.option.KeyReplacer != nil { + fieldName = d.option.KeyReplacer.Replace(fieldName) + } } rawMapKey := reflect.ValueOf(fieldName) @@ -359,6 +380,9 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e // Not a string key continue } + if d.option.KeyReplacer != nil { + mK = d.option.KeyReplacer.Replace(mK) + } if strings.EqualFold(mK, fieldName) { rawMapKey = dataValKey