2022-08-28 11:21:57 +00:00
|
|
|
// Package webgui provides plugin functionality to the Web GUI.
|
2020-07-27 18:31:49 +00:00
|
|
|
package webgui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-11-04 10:12:57 +00:00
|
|
|
"errors"
|
2020-07-27 18:31:49 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2020-08-22 19:52:38 +00:00
|
|
|
"os"
|
2020-07-27 18:31:49 +00:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-08-20 18:01:38 +00:00
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
2020-09-17 19:06:51 +00:00
|
|
|
"github.com/rclone/rclone/fs/rc/rcflags"
|
2020-07-27 18:31:49 +00:00
|
|
|
)
|
|
|
|
|
2020-08-20 18:01:38 +00:00
|
|
|
// PackageJSON is the structure of package.json of a plugin
|
2020-07-27 18:31:49 +00:00
|
|
|
type PackageJSON struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Author string `json:"author"`
|
|
|
|
Copyright string `json:"copyright"`
|
|
|
|
License string `json:"license"`
|
|
|
|
Private bool `json:"private"`
|
|
|
|
Homepage string `json:"homepage"`
|
|
|
|
TestURL string `json:"testUrl"`
|
|
|
|
Repository struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
} `json:"repository"`
|
|
|
|
Bugs struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
} `json:"bugs"`
|
|
|
|
Rclone RcloneConfig `json:"rclone"`
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:01:38 +00:00
|
|
|
// RcloneConfig represents the rclone specific config
|
2020-07-27 18:31:49 +00:00
|
|
|
type RcloneConfig struct {
|
2020-08-20 18:01:38 +00:00
|
|
|
HandlesType []string `json:"handlesType"`
|
|
|
|
PluginType string `json:"pluginType"`
|
|
|
|
RedirectReferrer bool `json:"redirectReferrer"`
|
|
|
|
Test bool `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PackageJSON) isTesting() bool {
|
|
|
|
return r.Rclone.Test
|
2020-07-27 18:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-08-20 18:01:38 +00:00
|
|
|
//loadedTestPlugins *Plugins
|
|
|
|
cachePath string
|
|
|
|
|
|
|
|
loadedPlugins *Plugins
|
|
|
|
pluginsProxy = &httputil.ReverseProxy{}
|
|
|
|
// PluginsMatch is used for matching author and plugin name in the url path
|
|
|
|
PluginsMatch = regexp.MustCompile(`^plugins\/([^\/]*)\/([^\/\?]+)[\/]?(.*)$`)
|
|
|
|
// PluginsPath is the base path where webgui plugins are stored
|
|
|
|
PluginsPath string
|
|
|
|
pluginsConfigPath string
|
|
|
|
availablePluginsJSONPath = "availablePlugins.json"
|
2020-09-17 19:06:51 +00:00
|
|
|
initSuccess = false
|
|
|
|
initMutex = &sync.Mutex{}
|
2020-07-27 18:31:49 +00:00
|
|
|
)
|
|
|
|
|
2020-08-20 18:01:38 +00:00
|
|
|
// Plugins represents the structure how plugins are saved onto disk
|
2020-07-27 18:31:49 +00:00
|
|
|
type Plugins struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
LoadedPlugins map[string]PackageJSON `json:"loadedPlugins"`
|
|
|
|
fileName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPlugins(fileName string) *Plugins {
|
|
|
|
p := Plugins{LoadedPlugins: map[string]PackageJSON{}}
|
|
|
|
p.fileName = fileName
|
|
|
|
p.mutex = sync.Mutex{}
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
2020-09-17 19:06:51 +00:00
|
|
|
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 {
|
2021-09-10 13:35:53 +00:00
|
|
|
cachePath = filepath.Join(config.GetCacheDir(), "webgui")
|
2021-01-25 17:41:52 +00:00
|
|
|
PluginsPath = filepath.Join(cachePath, "plugins")
|
|
|
|
pluginsConfigPath = filepath.Join(PluginsPath, "config")
|
2020-09-17 19:06:51 +00:00
|
|
|
loadedPlugins = newPlugins(availablePluginsJSONPath)
|
|
|
|
err := loadedPlugins.readFromFile()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "error reading available plugins: %v", err)
|
|
|
|
}
|
|
|
|
initSuccess = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-27 18:31:49 +00:00
|
|
|
func (p *Plugins) readFromFile() (err error) {
|
|
|
|
err = CreatePathIfNotExist(pluginsConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 18:01:38 +00:00
|
|
|
availablePluginsJSON := filepath.Join(pluginsConfigPath, p.fileName)
|
2020-08-22 19:52:38 +00:00
|
|
|
_, err = os.Stat(availablePluginsJSON)
|
|
|
|
if err == nil {
|
2022-08-20 14:38:02 +00:00
|
|
|
data, err := os.ReadFile(availablePluginsJSON)
|
2020-08-22 19:52:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(data, &p)
|
|
|
|
if err != nil {
|
|
|
|
fs.Logf(nil, "%s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
// path does not exist
|
|
|
|
err = p.writeToFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-27 18:31:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:01:38 +00:00
|
|
|
func (p *Plugins) addPlugin(pluginName string, packageJSONPath string) (err error) {
|
2020-07-27 18:31:49 +00:00
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
2022-08-20 14:38:02 +00:00
|
|
|
data, err := os.ReadFile(packageJSONPath)
|
2020-07-27 18:31:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 18:01:38 +00:00
|
|
|
var pkgJSON = PackageJSON{}
|
|
|
|
err = json.Unmarshal(data, &pkgJSON)
|
2020-07-27 18:31:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 18:01:38 +00:00
|
|
|
p.LoadedPlugins[pluginName] = pkgJSON
|
2020-07-27 18:31:49 +00:00
|
|
|
|
|
|
|
err = p.writeToFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugins) writeToFile() (err error) {
|
2020-08-20 18:01:38 +00:00
|
|
|
availablePluginsJSON := filepath.Join(pluginsConfigPath, p.fileName)
|
2020-07-27 18:31:49 +00:00
|
|
|
|
|
|
|
file, err := json.MarshalIndent(p, "", " ")
|
2020-08-29 17:59:05 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Logf(nil, "%s", err)
|
|
|
|
}
|
2022-08-20 14:38:02 +00:00
|
|
|
err = os.WriteFile(availablePluginsJSON, file, 0755)
|
2020-07-27 18:31:49 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Logf(nil, "%s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugins) removePlugin(name string) (err error) {
|
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
err = p.readFromFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := p.LoadedPlugins[name]
|
|
|
|
if !ok {
|
2020-08-20 18:01:38 +00:00
|
|
|
return fmt.Errorf("plugin %s not loaded", name)
|
2020-07-27 18:31:49 +00:00
|
|
|
}
|
|
|
|
delete(p.LoadedPlugins, name)
|
|
|
|
|
|
|
|
err = p.writeToFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:01:38 +00:00
|
|
|
// GetPluginByName returns the plugin object for the key (author/plugin-name)
|
2020-07-27 18:31:49 +00:00
|
|
|
func (p *Plugins) GetPluginByName(name string) (out *PackageJSON, err error) {
|
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
po, ok := p.LoadedPlugins[name]
|
|
|
|
if !ok {
|
2020-08-20 18:01:38 +00:00
|
|
|
return nil, fmt.Errorf("plugin %s not loaded", name)
|
2020-07-27 18:31:49 +00:00
|
|
|
}
|
|
|
|
return &po, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-14 02:56:32 +00:00
|
|
|
// getAuthorRepoBranchGitHub gives author, repoName and branch from a github.com url
|
2022-08-05 15:35:41 +00:00
|
|
|
//
|
2020-07-27 18:31:49 +00:00
|
|
|
// url examples:
|
|
|
|
// https://github.com/rclone/rclone-webui-react/
|
|
|
|
// http://github.com/rclone/rclone-webui-react
|
|
|
|
// https://github.com/rclone/rclone-webui-react/tree/caman-js
|
2022-08-05 15:35:41 +00:00
|
|
|
// github.com/rclone/rclone-webui-react
|
2022-08-14 02:56:32 +00:00
|
|
|
func getAuthorRepoBranchGitHub(url string) (author string, repoName string, branch string, err error) {
|
2020-08-20 18:01:38 +00:00
|
|
|
repoURL := url
|
|
|
|
repoURL = strings.Replace(repoURL, "https://", "", 1)
|
|
|
|
repoURL = strings.Replace(repoURL, "http://", "", 1)
|
2020-07-27 18:31:49 +00:00
|
|
|
|
2020-08-20 18:01:38 +00:00
|
|
|
urlSplits := strings.Split(repoURL, "/")
|
2020-07-27 18:31:49 +00:00
|
|
|
|
|
|
|
if len(urlSplits) < 3 || len(urlSplits) > 5 || urlSplits[0] != "github.com" {
|
2020-08-20 18:01:38 +00:00
|
|
|
return "", "", "", fmt.Errorf("invalid github url: %s", url)
|
2020-07-27 18:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get branch name
|
|
|
|
if len(urlSplits) == 5 && urlSplits[3] == "tree" {
|
|
|
|
return urlSplits[1], urlSplits[2], urlSplits[4], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return urlSplits[1], urlSplits[2], "master", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterPlugins(plugins *Plugins, compare func(packageJSON *PackageJSON) bool) map[string]PackageJSON {
|
|
|
|
output := map[string]PackageJSON{}
|
|
|
|
|
|
|
|
for key, val := range plugins.LoadedPlugins {
|
|
|
|
if compare(&val) {
|
|
|
|
output[key] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:01:38 +00:00
|
|
|
// getDirectorForProxy is a helper function for reverse proxy of test plugins
|
2020-07-27 18:31:49 +00:00
|
|
|
func getDirectorForProxy(origin *url.URL) func(req *http.Request) {
|
|
|
|
return func(req *http.Request) {
|
|
|
|
req.Header.Add("X-Forwarded-Host", req.Host)
|
|
|
|
req.Header.Add("X-Origin-Host", origin.Host)
|
|
|
|
req.URL.Scheme = "http"
|
|
|
|
req.URL.Host = origin.Host
|
|
|
|
req.URL.Path = origin.Path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 18:01:38 +00:00
|
|
|
// ServePluginOK checks the plugin url and uses reverse proxy to allow redirection for content not being served by rclone
|
2020-07-27 18:31:49 +00:00
|
|
|
func ServePluginOK(w http.ResponseWriter, r *http.Request, pluginsMatchResult []string) (ok bool) {
|
2020-08-20 18:01:38 +00:00
|
|
|
testPlugin, err := loadedPlugins.GetPluginByName(fmt.Sprintf("%s/%s", pluginsMatchResult[1], pluginsMatchResult[2]))
|
2020-07-27 18:31:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-08-20 18:01:38 +00:00
|
|
|
if !testPlugin.Rclone.Test {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-27 18:31:49 +00:00
|
|
|
origin, _ := url.Parse(fmt.Sprintf("%s/%s", testPlugin.TestURL, pluginsMatchResult[3]))
|
|
|
|
|
|
|
|
director := getDirectorForProxy(origin)
|
|
|
|
|
|
|
|
pluginsProxy.Director = director
|
|
|
|
pluginsProxy.ServeHTTP(w, r)
|
|
|
|
return true
|
|
|
|
}
|
2020-08-20 18:01:38 +00:00
|
|
|
|
2022-06-08 20:25:17 +00:00
|
|
|
var referrerPathReg = regexp.MustCompile(`^(https?):\/\/(.+):([0-9]+)?\/(.*)\/?\?(.*)$`)
|
2020-08-20 18:01:38 +00:00
|
|
|
|
|
|
|
// ServePluginWithReferrerOK check if redirectReferrer is set for the referred a plugin, if yes,
|
|
|
|
// 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
|
|
|
|
func ServePluginWithReferrerOK(w http.ResponseWriter, r *http.Request, path string) (ok bool) {
|
2020-09-17 19:06:51 +00:00
|
|
|
err := initPluginsOrError()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-08-20 18:01:38 +00:00
|
|
|
referrer := r.Referer()
|
|
|
|
referrerPathMatch := referrerPathReg.FindStringSubmatch(referrer)
|
|
|
|
|
2022-06-08 20:25:17 +00:00
|
|
|
if len(referrerPathMatch) > 3 {
|
2020-08-20 18:01:38 +00:00
|
|
|
referrerPluginMatch := PluginsMatch.FindStringSubmatch(referrerPathMatch[4])
|
2022-06-08 20:25:17 +00:00
|
|
|
if len(referrerPluginMatch) > 2 {
|
2020-08-29 18:15:09 +00:00
|
|
|
pluginKey := fmt.Sprintf("%s/%s", referrerPluginMatch[1], referrerPluginMatch[2])
|
|
|
|
currentPlugin, err := loadedPlugins.GetPluginByName(pluginKey)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if currentPlugin.Rclone.RedirectReferrer {
|
|
|
|
path = fmt.Sprintf("/plugins/%s/%s/%s", referrerPluginMatch[1], referrerPluginMatch[2], path)
|
|
|
|
|
|
|
|
http.Redirect(w, r, path, http.StatusMovedPermanently)
|
|
|
|
return true
|
|
|
|
}
|
2020-08-20 18:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|