/
repository.go
78 lines (72 loc) · 1.9 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package repository
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/imroc/req/v3"
"github.com/opq-osc/Yui/plugin/meta"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
"os"
"path/filepath"
"strings"
)
// 远程仓库 只允许加载签名的插件
const url = "https://api.github.com/repos/opq-osc/Yui-plugins/releases/latest"
//go:generate easyjson repository.go
type Plugin struct {
meta.PluginMeta
Permissions []string `json:"Permissions"`
DownloadUrl string `json:"DownloadUrl"`
}
func DownloadPlugin(ctx context.Context, plugin Plugin) error {
var r *req.Request
if proxy := viper.GetString("httpProxy"); proxy != "" {
r = req.C().SetProxyURL(proxy).R().SetContext(ctx)
} else {
r = req.R().SetContext(ctx)
}
resp, err := r.Get(plugin.DownloadUrl)
if err != nil {
return err
}
sha := sha256.New()
sha.Write(resp.Bytes())
if !strings.EqualFold(hex.EncodeToString(sha.Sum(nil)), plugin.Sha256) {
return fmt.Errorf("sha256比对失败")
}
return os.WriteFile(filepath.Join("plugins", plugin.PluginName+".opq"), resp.Bytes(), 0777)
}
func GetPluginList(ctx context.Context) ([]Plugin, error) {
var r *req.Request
if proxy := viper.GetString("httpProxy"); proxy != "" {
r = req.C().SetProxyURL(proxy).R().SetContext(ctx)
} else {
r = req.R().SetContext(ctx)
}
resp, err := r.Get(url)
if err != nil {
return nil, err
}
release := gjson.ParseBytes(resp.Bytes())
// 下载meta
assetsDownload := map[string]string{}
for _, v := range release.Get("assets").Array() {
assetsDownload[v.Get("name").String()] = v.Get("browser_download_url").String()
}
resp, err = r.Get(assetsDownload["meta.json"])
if err != nil {
return nil, err
}
var data = []Plugin{}
err = json.Unmarshal(resp.Bytes(), &data)
if err != nil {
return nil, err
}
for i, _ := range data {
data[i].DownloadUrl = assetsDownload[data[i].PluginName+".opq"]
}
return data, nil
}