plugin_publish.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package core
  2. import (
  3. "errors"
  4. "fmt"
  5. "regexp"
  6. "strings"
  7. "time"
  8. "github.com/beego/beego/v2/adapter/httplib"
  9. "github.com/cdle/sillyplus/core/common"
  10. "github.com/cdle/sillyplus/core/storage"
  11. "github.com/cdle/sillyplus/utils"
  12. "github.com/goccy/go-json"
  13. )
  14. // var plugin_path = "/etc/sillyplus/public/"
  15. // var plugin_download_file = plugin_path + "download"
  16. func CheckPluginAddress(address string) error {
  17. if !strings.HasSuffix(address, "list.json") {
  18. address += "/api/plugins/list.json"
  19. }
  20. data, _ := httplib.Get(address).Bytes()
  21. rr := RequestPluginResult{}
  22. err := json.Unmarshal(data, &rr)
  23. if IsCdle {
  24. console.Error(err, address, string(data))
  25. }
  26. if !rr.Success {
  27. return errors.New("无效的地址")
  28. }
  29. return nil
  30. }
  31. func initPluginPublish() {
  32. storage.Watch(sillyGirl, "plugin_sublink", func(old, address, key string) *storage.Final {
  33. if strings.HasPrefix(address, "link://") {
  34. return nil
  35. }
  36. if err := CheckPluginAddress(address); err != nil {
  37. return &storage.Final{
  38. Error: err,
  39. }
  40. }
  41. str, err := EncryptByAes(utils.JsonMarshal(common.PluginPublisher{
  42. Address: address,
  43. // MachineID: GetMachineID(),
  44. }))
  45. sublink := fmt.Sprintf("link://%s", str)
  46. return &storage.Final{
  47. Now: sublink,
  48. Message: sublink,
  49. Error: err,
  50. }
  51. })
  52. // os.MkdirAll(plugin_download_file, 0666)
  53. // os.WriteFile(plugin_path+"list.json", utils.JsonMarshal(GetPublicResponse()), 0666)
  54. // for _, f := range Functions {
  55. // if f.UUID != "" && f.Public {
  56. // os.WriteFile(fmt.Sprintf("%s/%s.js", plugin_download_file, f.UUID), []byte(publicScript(plugins.GetString(f.UUID))), 0666)
  57. // }
  58. // }
  59. }
  60. func publicScript(str string) string {
  61. su := &ScriptUtils{
  62. script: str,
  63. }
  64. if version := su.GetValue("version"); regexp.MustCompile(`v\d+\.\d+\.\d`).FindString(version) != version {
  65. su.SetValue("version", "v1.0.0")
  66. }
  67. if su.GetValue("author") == "" {
  68. su.SetValue("author", "佚名")
  69. }
  70. if su.GetValue("description") == "" {
  71. su.SetValue("description", "🐒这个人很懒什么都没有留下")
  72. }
  73. if su.GetValue("public") == "true" {
  74. su.SetValue("public", "false")
  75. }
  76. if su.GetValue("title") == "" {
  77. su.SetValue("title", "无名脚本")
  78. }
  79. if su.GetValue("message") != "" {
  80. su.DeleteValue("message")
  81. }
  82. create_at := su.GetValue("create_at")
  83. if _, err := time.Parse("2006-01-02 15:04:05", create_at); create_at == "" || err != nil {
  84. su.SetValue("create_at", time.Now().Format("2006-01-02 15:04:05"))
  85. }
  86. if su.GetValue("encrypt") == "true" {
  87. su.script = EncryptPlugin(su.script)
  88. }
  89. su.script = halfEct(su.script)
  90. return su.script
  91. }