1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-23 14:44:42 -05:00

️ Improve

This commit is contained in:
Nite07 2024-08-05 01:53:16 +08:00
parent 5395417a31
commit 5afb06bdce
3 changed files with 93 additions and 40 deletions

View File

@ -1,21 +1,21 @@
package model
type VmessJson struct {
V string `json:"v"`
Ps string `json:"ps"`
Add string `json:"add"`
Port interface{} `json:"port"`
Id string `json:"id"`
Aid interface{} `json:"aid"`
Scy string `json:"scy"`
Net string `json:"net"`
Type string `json:"type"`
Host string `json:"host"`
Path string `json:"path"`
Tls string `json:"tls"`
Sni string `json:"sni"`
Alpn string `json:"alpn"`
Fp string `json:"fp"`
V string `json:"v"`
Ps string `json:"ps"`
Add string `json:"add"`
Port any `json:"port"`
Id string `json:"id"`
Aid any `json:"aid"`
Scy string `json:"scy"`
Net string `json:"net"`
Type string `json:"type"`
Host string `json:"host"`
Path string `json:"path"`
Tls string `json:"tls"`
Sni string `json:"sni"`
Alpn string `json:"alpn"`
Fp string `json:"fp"`
}
type VMessOutboundOptions struct {

View File

@ -1,25 +0,0 @@
package util
import "encoding/json"
func MergeAndMarshal(args ...interface{}) (string, error) {
merged := make(map[string]interface{})
for _, arg := range args {
jsonBytes, err := json.Marshal(arg)
if err != nil {
return "", err
}
var m map[string]interface{}
if err := json.Unmarshal(jsonBytes, &m); err != nil {
return "", err
}
for k, v := range m {
merged[k] = v
}
}
result, err := json.Marshal(merged)
if err != nil {
return "", err
}
return string(result), nil
}

78
util/merge.go Normal file
View File

@ -0,0 +1,78 @@
package util
import (
"encoding/json"
"fmt"
"reflect"
)
func MergeAndMarshal(s1, s2 any) (string, error) {
merged, err := mergeStructs(s1, s2)
if err != nil {
return "", err
}
jsonBytes, err := json.Marshal(merged)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
func mergeStructs(s1, s2 any) (any, error) {
v1 := reflect.ValueOf(s1)
v2 := reflect.ValueOf(s2)
if v1.Kind() == reflect.Pointer {
v1 = v1.Elem()
}
if v2.Kind() == reflect.Pointer {
v2 = v2.Elem()
}
if v1.Kind() != reflect.Struct || v2.Kind() != reflect.Struct {
return nil, fmt.Errorf("both arguments must be structs")
}
t1 := v1.Type()
t2 := v2.Type()
var fields []reflect.StructField
var fieldsSet = make(map[string]reflect.Type)
for i := 0; i < t1.NumField(); i++ {
field := t1.Field(i)
fields = append(fields, field)
fieldsSet[field.Name] = field.Type
}
for i := 0; i < t2.NumField(); i++ {
field := t2.Field(i)
if existingType, ok := fieldsSet[field.Name]; ok {
if existingType != field.Type {
return nil, fmt.Errorf("field %s has conflicting types: %s and %s", field.Name, existingType, field.Type)
}
} else {
fields = append(fields, field)
fieldsSet[field.Name] = field.Type
}
}
newType := reflect.StructOf(fields)
newValue := reflect.New(newType).Elem()
for i := 0; i < t1.NumField(); i++ {
valueField := newValue.FieldByName(t1.Field(i).Name)
if valueField.IsValid() {
valueField.Set(v1.Field(i))
}
}
for i := 0; i < t2.NumField(); i++ {
valueField := newValue.FieldByName(t2.Field(i).Name)
if valueField.IsValid() {
valueField.Set(v2.Field(i))
}
}
return newValue.Interface(), nil
}