52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package supporter
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"miku/users"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func MapToStruct(toStructMap map[string]any) (users.VLESSUsers, error) {
|
|
var vlessUsers users.VLESSUsers
|
|
inbounds, ok := toStructMap["inbounds"].([]any)
|
|
if !ok || len(inbounds) == 0 {
|
|
return nil, errors.New("нет inbounds")
|
|
}
|
|
firstInbound, ok := inbounds[0].(map[string]any)
|
|
if !ok || len(firstInbound) == 0 {
|
|
return nil, errors.New("неизвестный формат inbound")
|
|
}
|
|
usedMap, ok := firstInbound["users"].([]any)
|
|
if !ok || len(usedMap) == 0 {
|
|
return nil, errors.New("неизвестный формат пользователей")
|
|
}
|
|
for _, curretMap := range usedMap {
|
|
userMap := curretMap.(map[string]any)
|
|
vlessUsers = append(vlessUsers, struct {
|
|
Name string "json:\"name\""
|
|
UUID uuid.UUID "json:\"uuid\""
|
|
}{
|
|
Name: userMap["name"].(string),
|
|
UUID: uuid.MustParse(userMap["uuid"].(string)),
|
|
})
|
|
}
|
|
return vlessUsers, nil
|
|
}
|
|
|
|
func StructToConfig(vlessUsers users.VLESSUsers, configMap map[string]any) (map[string]any, error) {
|
|
var toInterface []map[string]any
|
|
newConfigMap := configMap
|
|
structJson, err := json.Marshal(vlessUsers)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = json.Unmarshal(structJson, &toInterface)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
newConfigMap["inbounds"].([]any)[0].(map[string]any)["users"] = toInterface
|
|
return newConfigMap, nil
|
|
}
|