2020-07-27 20:31:49 +02:00
|
|
|
package webgui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2020-08-22 21:52:38 +02:00
|
|
|
"os"
|
2020-07-27 20:31:49 +02:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-08-20 20:01:38 +02:00
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
2020-07-27 20:31:49 +02:00
|
|
|
)
|
|
|
|
|
2020-08-20 20:01:38 +02:00
|
|
|
// PackageJSON is the structure of package.json of a plugin
|
2020-07-27 20:31:49 +02: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 20:01:38 +02:00
|
|
|
// RcloneConfig represents the rclone specific config
|
2020-07-27 20:31:49 +02:00
|
|
|
type RcloneConfig struct {
|
2020-08-20 20:01:38 +02: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 20:31:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-08-20 20:01:38 +02: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-07-27 20:31:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cachePath = filepath.Join(config.CacheDir, "webgui")
|
|
|
|
PluginsPath = filepath.Join(cachePath, "plugins")
|
|
|
|
pluginsConfigPath = filepath.Join(PluginsPath, "config")
|
|
|
|
|
2020-08-20 20:01:38 +02:00
|
|
|
loadedPlugins = newPlugins(availablePluginsJSONPath)
|
2020-07-27 20:31:49 +02:00
|
|
|
err := loadedPlugins.readFromFile()
|
|
|
|
if err != nil {
|
2020-08-20 20:01:38 +02:00
|
|
|
fs.Errorf(nil, "error reading available plugins: %v", err)
|
2020-07-27 20:31:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 20:01:38 +02:00
|
|
|
// Plugins represents the structure how plugins are saved onto disk
|
2020-07-27 20:31:49 +02: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugins) readFromFile() (err error) {
|
|
|
|
//p.mutex.Lock()
|
|
|
|
//defer p.mutex.Unlock()
|
|
|
|
err = CreatePathIfNotExist(pluginsConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 20:01:38 +02:00
|
|
|
availablePluginsJSON := filepath.Join(pluginsConfigPath, p.fileName)
|
2020-08-22 21:52:38 +02:00
|
|
|
_, err = os.Stat(availablePluginsJSON)
|
|
|
|
if err == nil {
|
|
|
|
data, err := ioutil.ReadFile(availablePluginsJSON)
|
|
|
|
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 20:31:49 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 20:01:38 +02:00
|
|
|
func (p *Plugins) addPlugin(pluginName string, packageJSONPath string) (err error) {
|
2020-07-27 20:31:49 +02:00
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
2020-08-20 20:01:38 +02:00
|
|
|
data, err := ioutil.ReadFile(packageJSONPath)
|
2020-07-27 20:31:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 20:01:38 +02:00
|
|
|
var pkgJSON = PackageJSON{}
|
|
|
|
err = json.Unmarshal(data, &pkgJSON)
|
2020-07-27 20:31:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 20:01:38 +02:00
|
|
|
p.LoadedPlugins[pluginName] = pkgJSON
|
2020-07-27 20:31:49 +02:00
|
|
|
|
|
|
|
err = p.writeToFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugins) addTestPlugin(pluginName string, testURL string, handlesType []string) (err error) {
|
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
err = p.readFromFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-20 20:01:38 +02:00
|
|
|
var pkgJSON = PackageJSON{
|
2020-07-27 20:31:49 +02:00
|
|
|
Name: pluginName,
|
|
|
|
TestURL: testURL,
|
|
|
|
Rclone: RcloneConfig{
|
|
|
|
HandlesType: handlesType,
|
2020-08-20 20:01:38 +02:00
|
|
|
Test: true,
|
2020-07-27 20:31:49 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-20 20:01:38 +02:00
|
|
|
p.LoadedPlugins[pluginName] = pkgJSON
|
2020-07-27 20:31:49 +02:00
|
|
|
|
|
|
|
err = p.writeToFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugins) writeToFile() (err error) {
|
|
|
|
//p.mutex.Lock()
|
|
|
|
//defer p.mutex.Unlock()
|
2020-08-20 20:01:38 +02:00
|
|
|
availablePluginsJSON := filepath.Join(pluginsConfigPath, p.fileName)
|
2020-07-27 20:31:49 +02:00
|
|
|
|
|
|
|
file, err := json.MarshalIndent(p, "", " ")
|
2020-08-29 19:59:05 +02:00
|
|
|
if err != nil {
|
|
|
|
fs.Logf(nil, "%s", err)
|
|
|
|
}
|
2020-08-20 20:01:38 +02:00
|
|
|
err = ioutil.WriteFile(availablePluginsJSON, file, 0755)
|
2020-07-27 20:31:49 +02: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 20:01:38 +02:00
|
|
|
return fmt.Errorf("plugin %s not loaded", name)
|
2020-07-27 20:31:49 +02:00
|
|
|
}
|
|
|
|
delete(p.LoadedPlugins, name)
|
|
|
|
|
|
|
|
err = p.writeToFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 20:01:38 +02:00
|
|
|
// GetPluginByName returns the plugin object for the key (author/plugin-name)
|
2020-07-27 20:31:49 +02: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 20:01:38 +02:00
|
|
|
return nil, fmt.Errorf("plugin %s not loaded", name)
|
2020-07-27 20:31:49 +02:00
|
|
|
}
|
|
|
|
return &po, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAuthorRepoBranchGithub gives author, repoName and branch from a github.com url
|
|
|
|
// 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
|
|
|
|
// github.com/rclone/rclone-webui-react
|
|
|
|
//
|
|
|
|
func getAuthorRepoBranchGithub(url string) (author string, repoName string, branch string, err error) {
|
2020-08-20 20:01:38 +02:00
|
|
|
repoURL := url
|
|
|
|
repoURL = strings.Replace(repoURL, "https://", "", 1)
|
|
|
|
repoURL = strings.Replace(repoURL, "http://", "", 1)
|
2020-07-27 20:31:49 +02:00
|
|
|
|
2020-08-20 20:01:38 +02:00
|
|
|
urlSplits := strings.Split(repoURL, "/")
|
2020-07-27 20:31:49 +02:00
|
|
|
|
|
|
|
if len(urlSplits) < 3 || len(urlSplits) > 5 || urlSplits[0] != "github.com" {
|
2020-08-20 20:01:38 +02:00
|
|
|
return "", "", "", fmt.Errorf("invalid github url: %s", url)
|
2020-07-27 20:31:49 +02: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 20:01:38 +02:00
|
|
|
// getDirectorForProxy is a helper function for reverse proxy of test plugins
|
2020-07-27 20:31:49 +02: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 20:01:38 +02:00
|
|
|
// ServePluginOK checks the plugin url and uses reverse proxy to allow redirection for content not being served by rclone
|
2020-07-27 20:31:49 +02:00
|
|
|
func ServePluginOK(w http.ResponseWriter, r *http.Request, pluginsMatchResult []string) (ok bool) {
|
2020-08-20 20:01:38 +02:00
|
|
|
testPlugin, err := loadedPlugins.GetPluginByName(fmt.Sprintf("%s/%s", pluginsMatchResult[1], pluginsMatchResult[2]))
|
2020-07-27 20:31:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-08-20 20:01:38 +02:00
|
|
|
if !testPlugin.Rclone.Test {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-27 20:31:49 +02: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 20:01:38 +02:00
|
|
|
|
2020-08-23 19:20:11 +02:00
|
|
|
var referrerPathReg = regexp.MustCompile("^(https?):\\/\\/(.+):([0-9]+)?\\/(.*)\\/?\\?(.*)$")
|
2020-08-20 20:01:38 +02: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) {
|
|
|
|
referrer := r.Referer()
|
|
|
|
referrerPathMatch := referrerPathReg.FindStringSubmatch(referrer)
|
|
|
|
|
2020-08-29 20:15:09 +02:00
|
|
|
if referrerPathMatch != nil && len(referrerPathMatch) > 3 {
|
2020-08-20 20:01:38 +02:00
|
|
|
referrerPluginMatch := PluginsMatch.FindStringSubmatch(referrerPathMatch[4])
|
2020-08-29 20:15:09 +02:00
|
|
|
if referrerPluginMatch != nil && len(referrerPluginMatch) > 2 {
|
|
|
|
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 20:01:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|