兼容执行插件

This commit is contained in:
zhangchenhao
2025-06-09 20:31:15 +08:00
parent 9a9a91bf37
commit 08172287ef
3 changed files with 283 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
package plugin
import (
"ALLinSSL/backend/internal/access"
"encoding/json"
"fmt"
"strconv"
)
type CertDeployPlugin struct {
Config map[string]any
Key string
Cert string
}
func Deploy(cfg map[string]any) error {
cert, ok := cfg["certificate"].(map[string]any)
if !ok {
return fmt.Errorf("证书不存在")
}
action, ok := cfg["action"].(string)
if !ok {
return fmt.Errorf("操作类型错误action")
}
var providerID string
switch v := cfg["provider_id"].(type) {
case float64:
providerID = strconv.Itoa(int(v))
case string:
providerID = v
default:
return fmt.Errorf("参数错误provider_id")
}
providerData, err := access.GetAccess(providerID)
if err != nil {
return err
}
providerConfigStr, ok := providerData["config"].(string)
if !ok {
return fmt.Errorf("api配置错误")
}
var providerConfig map[string]any
err = json.Unmarshal([]byte(providerConfigStr), &providerConfig)
if err != nil {
return fmt.Errorf("api配置解析错误%v", err)
}
pluginName, ok := providerConfig["name"].(string)
if !ok {
return fmt.Errorf("插件名称错误")
}
pluginConfig, ok := providerConfig["config"].(map[string]any)
if !ok {
return fmt.Errorf("插件配置错误")
}
pluginParams, ok := pluginConfig["params"].(string)
if !ok {
return fmt.Errorf("插件参数错误:")
}
var paramsMap map[string]any
err = json.Unmarshal([]byte(pluginParams), &paramsMap)
if err != nil {
return fmt.Errorf("插件参数解析错误:%v", err)
}
// 合并插件配置和参数
for k, v := range paramsMap {
pluginConfig[k] = v
}
// 设置证书
keyPem, ok := cert["key"].(string)
if !ok {
return fmt.Errorf("证书错误key")
}
certPem, ok := cert["cert"].(string)
if !ok {
return fmt.Errorf("证书错误cert")
}
params := map[string]any{
"config": pluginConfig,
"key": keyPem,
"cert": certPem,
}
rep, err := CallPlugin(pluginName, action, params)
if err != nil {
return fmt.Errorf("调用插件失败:%v", err)
}
fmt.Println(rep)
return err
}