mirror of
https://github.com/langgenius/dify-plugin-daemon.git
synced 2026-07-22 09:45:27 -04:00
888ad788bc
* refactor: introduce local plugin control panel and cleanup environment setup process * fix: args * refactor: new local runtime * temp: stash work for refactor on RemotePluginServer * refactor: unify local runtime lifetime and sperate init environment process * chore: add missing files * stash * refactor: local plugin lifetime control * refactor: complete installation process of control panel * refactor: adapt service layer to new controlpanel * refactor: pluginManager.Install * fix: add routine wrap to InstallServerless, avoid blocking main thread * feat: reinstall serverless runtime * chore: add comments to Reinstall and update confusing naming * refactor: unify install plugin service * refactor: add labels to debugging runtime * refactor: add getters to plugin manager * refactor: split install service to decode/install_task/install service * ??? * refactor: adapt controllers * refactor: session write * refactor: session runtime * Refine install task orchestration (#501) * refactor: installing task * refactor cluster management, decouple lifetime management and cluster * fix cli test command * fix: cleanup TODO comments and implement GracefulStop for instance * feat: add logger to control panel * fix: multiple nil references * refactor: better lifetime control * refactor: better cycle interval * fix(LocalPluginRuntime): prevent returning err when it's not error * fix: avoid adding empty PipExtraArgs * fix: missing errors in Environment init * fix: add truncateMessage to avoid db explosion * cleanup: better lifecycle management * fix: init status at the beginning of installation * optimize: GracefulStop for pluginInstance * refactor: tests * refactor: centralize routine labels (#504) * cleanup: RoutineKey * fix: init routine pool * fix: correctly handle cluster register error * fix: memory leak * fix: add \n to instance write * fix(installer.go): set success to true after succeed for defer func * refactor * fix: missing cwd in testutils * fix: scaleup default runtime nums to 1 when testing * fix: localruntime appconfig in testing module * Update internal/core/local_runtime/load_balancing.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * fix: more efficiency implement in installer_local.go * fix: returns after failing in onDebuggingRuntimeDisconnected * fix: returns after failing in onDebuggingRuntimeDisconnected * fix: splits tests * refactor: naming * refactor: manifest.VersionX * fix: adapt SetDefault to tests * fix: enforce use constants in DBType * fix: generate * fix: linter * cleanup tests * refactor: change package to * cleanup: useless codes * Update internal/cluster/plugin.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * cleanup * refactor: decouple connection_key management from debugging_time * refactor: confused naming * feat: recycle resources to adapt to https://github.com/langgenius/dify-plugin-daemon/pull/500 * refactor: confusing redirecting * fix: support get serverless runtime * fix: race condition in Launching * fix: avoid ManifestValidate in first step of debugging handshake * fix: adding ReleaseAllLocks to finalizers * wtf: what a beautiful code * refactor: rename Stream.Async to Stream.Process * fix: kill process if daed instance was detected * fix: correctly handle failures * fix: consistence of difference interfaces * fix: add stacktrace to panic * fix: only trigger once event * fix: ensure plugin runtime was shutdown * feat: cleanup install tasks * fix: add scale logs --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
340 lines
7.6 KiB
Go
340 lines
7.6 KiB
Go
package decoder
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/plugin_packager/consts"
|
|
"github.com/langgenius/dify-plugin-daemon/pkg/utils/parser"
|
|
)
|
|
|
|
type ZipPluginDecoder struct {
|
|
PluginDecoder
|
|
PluginDecoderHelper
|
|
|
|
reader *zip.Reader
|
|
err error
|
|
|
|
sig string
|
|
createTime int64
|
|
verification *Verification
|
|
|
|
thirdPartySignatureVerificationConfig *ThirdPartySignatureVerificationConfig
|
|
}
|
|
|
|
type ThirdPartySignatureVerificationConfig struct {
|
|
Enabled bool
|
|
PublicKeyPaths []string
|
|
}
|
|
|
|
func newZipPluginDecoder(
|
|
binary []byte,
|
|
thirdPartySignatureVerificationConfig *ThirdPartySignatureVerificationConfig,
|
|
) (*ZipPluginDecoder, error) {
|
|
reader, err := zip.NewReader(bytes.NewReader(binary), int64(len(binary)))
|
|
if err != nil {
|
|
return nil, errors.New(strings.ReplaceAll(err.Error(), "zip", "difypkg"))
|
|
}
|
|
|
|
decoder := &ZipPluginDecoder{
|
|
reader: reader,
|
|
err: err,
|
|
thirdPartySignatureVerificationConfig: thirdPartySignatureVerificationConfig,
|
|
}
|
|
|
|
err = decoder.Open()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if _, err := decoder.Manifest(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return decoder, nil
|
|
}
|
|
|
|
// NewZipPluginDecoder is a helper function to create ZipPluginDecoder
|
|
func NewZipPluginDecoder(binary []byte) (*ZipPluginDecoder, error) {
|
|
return newZipPluginDecoder(binary, nil)
|
|
}
|
|
|
|
// NewZipPluginDecoderWithThirdPartySignatureVerificationConfig is a helper function
|
|
// to create a ZipPluginDecoder with a third party signature verification
|
|
func NewZipPluginDecoderWithThirdPartySignatureVerificationConfig(
|
|
binary []byte,
|
|
thirdPartySignatureVerificationConfig *ThirdPartySignatureVerificationConfig,
|
|
) (*ZipPluginDecoder, error) {
|
|
return newZipPluginDecoder(binary, thirdPartySignatureVerificationConfig)
|
|
}
|
|
|
|
// NewZipPluginDecoderWithSizeLimit is a helper function to create a ZipPluginDecoder with a size limit
|
|
// It checks the total uncompressed size of the plugin package and returns an error if it exceeds the max size
|
|
func NewZipPluginDecoderWithSizeLimit(binary []byte, maxSize int64) (*ZipPluginDecoder, error) {
|
|
reader, err := zip.NewReader(bytes.NewReader(binary), int64(len(binary)))
|
|
if err != nil {
|
|
return nil, errors.New(strings.ReplaceAll(err.Error(), "zip", "difypkg"))
|
|
}
|
|
|
|
totalSize := int64(0)
|
|
for _, file := range reader.File {
|
|
totalSize += int64(file.UncompressedSize64)
|
|
if totalSize > maxSize {
|
|
return nil, errors.New(
|
|
"plugin package size is too large, please ensure the uncompressed size is less than " +
|
|
strconv.FormatInt(maxSize, 10) + " bytes",
|
|
)
|
|
}
|
|
}
|
|
|
|
return newZipPluginDecoder(binary, nil)
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) Stat(filename string) (fs.FileInfo, error) {
|
|
f, err := z.reader.Open(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
return f.Stat()
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) Open() error {
|
|
if z.reader == nil {
|
|
return z.err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) Walk(fn func(filename string, dir string) error) error {
|
|
if z.reader == nil {
|
|
return z.err
|
|
}
|
|
|
|
for _, file := range z.reader.File {
|
|
// split the path into directory and filename
|
|
dir, filename := path.Split(file.Name)
|
|
if err := fn(filename, dir); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) ReadFile(filename string) ([]byte, error) {
|
|
if z.reader == nil {
|
|
return nil, z.err
|
|
}
|
|
|
|
file, err := z.reader.Open(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
data := new(bytes.Buffer)
|
|
_, err = data.ReadFrom(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return data.Bytes(), nil
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) ReadDir(dirname string) ([]string, error) {
|
|
if z.reader == nil {
|
|
return nil, z.err
|
|
}
|
|
|
|
files := make([]string, 0)
|
|
dirNameWithSlash := strings.TrimSuffix(dirname, "/") + "/"
|
|
|
|
for _, file := range z.reader.File {
|
|
if strings.HasPrefix(file.Name, dirNameWithSlash) {
|
|
files = append(files, file.Name)
|
|
}
|
|
}
|
|
|
|
return files, nil
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) FileReader(filename string) (io.ReadCloser, error) {
|
|
return z.reader.Open(filename)
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) decode() error {
|
|
if z.reader == nil {
|
|
return z.err
|
|
}
|
|
|
|
signatureData, err := parser.UnmarshalJson[struct {
|
|
Signature string `json:"signature"`
|
|
Time int64 `json:"time"`
|
|
}](z.reader.Comment)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pluginSig := signatureData.Signature
|
|
pluginTime := signatureData.Time
|
|
|
|
var verification *Verification
|
|
|
|
// try to read the verification file
|
|
verificationData, err := z.ReadFile(consts.VERIFICATION_FILE)
|
|
if err != nil {
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
return err
|
|
}
|
|
|
|
// if the verification file is not found, set the verification to nil
|
|
verification = nil
|
|
} else {
|
|
// unmarshal the verification data
|
|
verificationData, err := parser.UnmarshalJsonBytes[Verification](verificationData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
verification = &verificationData
|
|
}
|
|
|
|
z.sig = pluginSig
|
|
z.createTime = pluginTime
|
|
z.verification = verification
|
|
|
|
return nil
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) Signature() (string, error) {
|
|
if z.sig != "" {
|
|
return z.sig, nil
|
|
}
|
|
|
|
if z.reader == nil {
|
|
return "", z.err
|
|
}
|
|
|
|
err := z.decode()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return z.sig, nil
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) CreateTime() (int64, error) {
|
|
if z.createTime != 0 {
|
|
return z.createTime, nil
|
|
}
|
|
|
|
if z.reader == nil {
|
|
return 0, z.err
|
|
}
|
|
|
|
err := z.decode()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return z.createTime, nil
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) Verification() (*Verification, error) {
|
|
if !z.Verified() {
|
|
return nil, errors.New("plugin is not verified")
|
|
}
|
|
|
|
if z.verification != nil {
|
|
return z.verification, nil
|
|
}
|
|
|
|
err := z.decode()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// if the plugin is verified but the verification is nil
|
|
// it's historical reason that all plugins are signed by langgenius
|
|
return nil, nil
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) Manifest() (plugin_entities.PluginDeclaration, error) {
|
|
return z.PluginDecoderHelper.Manifest(z)
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) Assets() (map[string][]byte, error) {
|
|
// FIXES: https://github.com/langgenius/dify-plugin-daemon/issues/166
|
|
// zip file is os-independent, `/` is the separator
|
|
return z.PluginDecoderHelper.Assets(z, "/")
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) Checksum() (string, error) {
|
|
return z.PluginDecoderHelper.Checksum(z)
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) UniqueIdentity() (plugin_entities.PluginUniqueIdentifier, error) {
|
|
return z.PluginDecoderHelper.UniqueIdentity(z)
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) ExtractTo(dst string) error {
|
|
// copy to working directory
|
|
if err := z.Walk(func(filename, dir string) error {
|
|
workingPath := path.Join(dst, dir)
|
|
// check if directory exists
|
|
if err := os.MkdirAll(workingPath, 0755); err != nil {
|
|
return err
|
|
}
|
|
|
|
bytes, err := z.ReadFile(filepath.Join(dir, filename))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
filename = filepath.Join(workingPath, filename)
|
|
|
|
// copy file
|
|
if err := os.WriteFile(filename, bytes, 0644); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}); err != nil {
|
|
// if error, delete the working directory
|
|
os.RemoveAll(dst)
|
|
return errors.Join(fmt.Errorf("copy plugin to working directory error: %v", err), err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) CheckAssetsValid() error {
|
|
return z.PluginDecoderHelper.CheckAssetsValid(z)
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) Verified() bool {
|
|
return z.PluginDecoderHelper.verified(z)
|
|
}
|
|
|
|
func (z *ZipPluginDecoder) AvailableI18nReadme() (map[string]string, error) {
|
|
return z.PluginDecoderHelper.AvailableI18nReadme(z, "/")
|
|
}
|