plugins: Create plugins files only if webui is enabled. Fixes #4592. May fix #4600.

This commit is contained in:
negative0 2020-09-18 00:36:51 +05:30 committed by Nick Craig-Wood
parent 50a107a5f3
commit 687d2d495b
4 changed files with 66 additions and 18 deletions

View File

@ -387,9 +387,10 @@ func (s *Server) handleGet(w http.ResponseWriter, r *http.Request, path string)
s.serveRoot(w, r) s.serveRoot(w, r)
return return
case s.files != nil: case s.files != nil:
if s.opt.WebUI {
pluginsMatchResult := webgui.PluginsMatch.FindStringSubmatch(path) pluginsMatchResult := webgui.PluginsMatch.FindStringSubmatch(path)
if s.opt.WebUI && pluginsMatchResult != nil && len(pluginsMatchResult) > 2 { if pluginsMatchResult != nil && len(pluginsMatchResult) > 2 {
ok := webgui.ServePluginOK(w, r, pluginsMatchResult) ok := webgui.ServePluginOK(w, r, pluginsMatchResult)
if !ok { if !ok {
r.URL.Path = fmt.Sprintf("/%s/%s/app/build/%s", pluginsMatchResult[1], pluginsMatchResult[2], pluginsMatchResult[3]) r.URL.Path = fmt.Sprintf("/%s/%s/app/build/%s", pluginsMatchResult[1], pluginsMatchResult[2], pluginsMatchResult[3])
@ -397,9 +398,10 @@ func (s *Server) handleGet(w http.ResponseWriter, r *http.Request, path string)
return return
} }
return return
} else if s.opt.WebUI && webgui.ServePluginWithReferrerOK(w, r, path) { } else if webgui.ServePluginWithReferrerOK(w, r, path) {
return return
} }
}
// Serve the files // Serve the files
r.URL.Path = "/" + path r.URL.Path = "/" + path
s.files.ServeHTTP(w, r) s.files.ServeHTTP(w, r)

View File

@ -15,6 +15,8 @@ import (
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config" "github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/fs/rc/rcflags"
"github.com/rclone/rclone/lib/errors"
) )
// PackageJSON is the structure of package.json of a plugin // PackageJSON is the structure of package.json of a plugin
@ -62,6 +64,8 @@ var (
PluginsPath string PluginsPath string
pluginsConfigPath string pluginsConfigPath string
availablePluginsJSONPath = "availablePlugins.json" availablePluginsJSONPath = "availablePlugins.json"
initSuccess = false
initMutex = &sync.Mutex{}
) )
func init() { func init() {
@ -69,11 +73,6 @@ func init() {
PluginsPath = filepath.Join(cachePath, "plugins") PluginsPath = filepath.Join(cachePath, "plugins")
pluginsConfigPath = filepath.Join(PluginsPath, "config") pluginsConfigPath = filepath.Join(PluginsPath, "config")
loadedPlugins = newPlugins(availablePluginsJSONPath)
err := loadedPlugins.readFromFile()
if err != nil {
fs.Errorf(nil, "error reading available plugins: %v", err)
}
} }
// Plugins represents the structure how plugins are saved onto disk // Plugins represents the structure how plugins are saved onto disk
@ -90,9 +89,25 @@ func newPlugins(fileName string) *Plugins {
return &p return &p
} }
func initPluginsOrError() error {
if !rcflags.Opt.WebUI {
return errors.New("WebUI needs to be enabled for plugins to work")
}
initMutex.Lock()
defer initMutex.Unlock()
if !initSuccess {
loadedPlugins = newPlugins(availablePluginsJSONPath)
err := loadedPlugins.readFromFile()
if err != nil {
fs.Errorf(nil, "error reading available plugins: %v", err)
}
initSuccess = true
}
return nil
}
func (p *Plugins) readFromFile() (err error) { func (p *Plugins) readFromFile() (err error) {
//p.mutex.Lock()
//defer p.mutex.Unlock()
err = CreatePathIfNotExist(pluginsConfigPath) err = CreatePathIfNotExist(pluginsConfigPath)
if err != nil { if err != nil {
return err return err
@ -169,8 +184,6 @@ func (p *Plugins) addTestPlugin(pluginName string, testURL string, handlesType [
} }
func (p *Plugins) writeToFile() (err error) { func (p *Plugins) writeToFile() (err error) {
//p.mutex.Lock()
//defer p.mutex.Unlock()
availablePluginsJSON := filepath.Join(pluginsConfigPath, p.fileName) availablePluginsJSON := filepath.Join(pluginsConfigPath, p.fileName)
file, err := json.MarshalIndent(p, "", " ") file, err := json.MarshalIndent(p, "", " ")
@ -290,6 +303,10 @@ var referrerPathReg = regexp.MustCompile("^(https?):\\/\\/(.+):([0-9]+)?\\/(.*)\
// sends a redirect to actual url. This function is useful for plugins to refer to absolute paths when // sends a redirect to actual url. This function is useful for plugins to refer to absolute paths when
// the referrer in http.Request is set // the referrer in http.Request is set
func ServePluginWithReferrerOK(w http.ResponseWriter, r *http.Request, path string) (ok bool) { func ServePluginWithReferrerOK(w http.ResponseWriter, r *http.Request, path string) (ok bool) {
err := initPluginsOrError()
if err != nil {
return false
}
referrer := r.Referer() referrer := r.Referer()
referrerPathMatch := referrerPathReg.FindStringSubmatch(referrer) referrerPathMatch := referrerPathReg.FindStringSubmatch(referrer)

View File

@ -30,6 +30,10 @@ Eg
} }
func rcListTestPlugins(_ context.Context, _ rc.Params) (out rc.Params, err error) { func rcListTestPlugins(_ context.Context, _ rc.Params) (out rc.Params, err error) {
err = initPluginsOrError()
if err != nil {
return nil, err
}
return rc.Params{ return rc.Params{
"loadedTestPlugins": filterPlugins(loadedPlugins, func(json *PackageJSON) bool { return json.isTesting() }), "loadedTestPlugins": filterPlugins(loadedPlugins, func(json *PackageJSON) bool { return json.isTesting() }),
}, nil }, nil
@ -54,6 +58,10 @@ Eg
}) })
} }
func rcRemoveTestPlugin(_ context.Context, in rc.Params) (out rc.Params, err error) { func rcRemoveTestPlugin(_ context.Context, in rc.Params) (out rc.Params, err error) {
err = initPluginsOrError()
if err != nil {
return nil, err
}
name, err := in.GetString("name") name, err := in.GetString("name")
if err != nil { if err != nil {
return nil, err return nil, err
@ -85,6 +93,10 @@ Eg
} }
func rcAddPlugin(_ context.Context, in rc.Params) (out rc.Params, err error) { func rcAddPlugin(_ context.Context, in rc.Params) (out rc.Params, err error) {
err = initPluginsOrError()
if err != nil {
return nil, err
}
pluginURL, err := in.GetString("url") pluginURL, err := in.GetString("url")
if err != nil { if err != nil {
return nil, err return nil, err
@ -192,6 +204,10 @@ Eg
} }
func rcGetPlugins(_ context.Context, _ rc.Params) (out rc.Params, err error) { func rcGetPlugins(_ context.Context, _ rc.Params) (out rc.Params, err error) {
err = initPluginsOrError()
if err != nil {
return nil, err
}
err = loadedPlugins.readFromFile() err = loadedPlugins.readFromFile()
if err != nil { if err != nil {
return nil, err return nil, err
@ -222,6 +238,10 @@ Eg
} }
func rcRemovePlugin(_ context.Context, in rc.Params) (out rc.Params, err error) { func rcRemovePlugin(_ context.Context, in rc.Params) (out rc.Params, err error) {
err = initPluginsOrError()
if err != nil {
return nil, err
}
name, err := in.GetString("name") name, err := in.GetString("name")
if err != nil { if err != nil {
return nil, err return nil, err
@ -260,6 +280,10 @@ Eg
} }
func rcGetPluginsForType(_ context.Context, in rc.Params) (out rc.Params, err error) { func rcGetPluginsForType(_ context.Context, in rc.Params) (out rc.Params, err error) {
err = initPluginsOrError()
if err != nil {
return nil, err
}
handlesType, err := in.GetString("type") handlesType, err := in.GetString("type")
if err != nil { if err != nil {
handlesType = "" handlesType = ""

View File

@ -9,6 +9,7 @@ import (
"testing" "testing"
"github.com/rclone/rclone/fs/rc" "github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/fs/rc/rcflags"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -18,6 +19,10 @@ const testPluginAuthor = "rclone"
const testPluginKey = testPluginAuthor + "/" + testPluginName const testPluginKey = testPluginAuthor + "/" + testPluginName
const testPluginURL = "https://github.com/" + testPluginAuthor + "/" + testPluginName + "/" const testPluginURL = "https://github.com/" + testPluginAuthor + "/" + testPluginName + "/"
func init() {
rcflags.Opt.WebUI = true
}
func setCacheDir(t *testing.T) string { func setCacheDir(t *testing.T) string {
cacheDir, err := ioutil.TempDir("", "rclone-cache-dir") cacheDir, err := ioutil.TempDir("", "rclone-cache-dir")
assert.Nil(t, err) assert.Nil(t, err)