Add updates module and fix issues

This commit is contained in:
Daniel
2019-01-24 15:23:02 +01:00
parent bde81d815d
commit 20af9efecc
22 changed files with 953 additions and 96 deletions

66
ui/launch.go Normal file
View File

@@ -0,0 +1,66 @@
package ui
import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"runtime"
"github.com/Safing/portbase/modules"
"github.com/Safing/portmaster/updates"
)
var (
launchUI bool
)
func init() {
flag.BoolVar(&launchUI, "ui", false, "launch user interface and exit")
}
func launchUIByFlag() error {
if !launchUI {
return nil
}
err := updates.ReloadLatest()
if err != nil {
return err
}
osAndPlatform := fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)
switch osAndPlatform {
case "linux_amd64":
file, err := updates.GetPlatformFile("app/portmaster-ui")
if err != nil {
return fmt.Errorf("ui currently not available: %s - you may need to first start portmaster and wait for it to fetch the update index", err)
}
// check permission
info, err := os.Stat(file.Path())
if info.Mode() != 0755 {
fmt.Printf("%v\n", info.Mode())
err := os.Chmod(file.Path(), 0755)
if err != nil {
return fmt.Errorf("failed to set exec permissions on %s: %s", file.Path(), err)
}
}
// exec
cmd := exec.Command(file.Path())
err = cmd.Start()
if err != nil {
return fmt.Errorf("failed to start ui: %s", err)
}
// gracefully exit portmaster
return modules.ErrCleanExit
default:
return errors.New("this os/platform is no UI support yet")
}
}

View File

@@ -5,13 +5,14 @@ import (
)
func init() {
modules.Register("ui", prep, start, stop, "database", "api")
modules.Register("ui", prep, nil, nil, "updates", "api")
}
func prep() error {
return nil
}
err := launchUIByFlag()
if err != nil {
return err
}
func stop() error {
return nil
return registerRoutes()
}

View File

@@ -6,16 +6,16 @@ import (
"mime"
"net/http"
"net/url"
"path"
"path/filepath"
"strings"
"sync"
resources "github.com/cookieo9/resources-go"
"github.com/gorilla/mux"
"github.com/Safing/portbase/api"
"github.com/Safing/portbase/database"
"github.com/Safing/portbase/log"
"github.com/Safing/portmaster/updates"
)
var (
@@ -25,66 +25,80 @@ var (
assetsLock sync.RWMutex
)
func start() error {
basePath := path.Join(database.GetDatabaseRoot(), "updates", "files", "apps")
serveUIRouter := mux.NewRouter()
serveUIRouter.HandleFunc("/assets/{resPath:[a-zA-Z0-9/\\._-]+}", ServeAssets(basePath))
serveUIRouter.HandleFunc("/app/{appName:[a-z]+}/", ServeApps(basePath))
serveUIRouter.HandleFunc("/app/{appName:[a-z]+}/{resPath:[a-zA-Z0-9/\\._-]+}", ServeApps(basePath))
serveUIRouter.HandleFunc("/", RedirectToControl)
api.RegisterAdditionalRoute("/assets/", serveUIRouter)
api.RegisterAdditionalRoute("/app/", serveUIRouter)
api.RegisterAdditionalRoute("/", serveUIRouter)
func registerRoutes() error {
api.RegisterHandleFunc("/assets/{resPath:[a-zA-Z0-9/\\._-]+}", ServeBundle("assets")).Methods("GET", "HEAD")
api.RegisterHandleFunc("/ui/modules/{moduleName:[a-z]+}", redirAddSlash).Methods("GET", "HEAD")
api.RegisterHandleFunc("/ui/modules/{moduleName:[a-z]+}/", ServeBundle("")).Methods("GET", "HEAD")
api.RegisterHandleFunc("/ui/modules/{moduleName:[a-z]+}/{resPath:[a-zA-Z0-9/\\._-]+}", ServeBundle("")).Methods("GET", "HEAD")
api.RegisterHandleFunc("/", RedirectToBase)
return nil
}
// ServeApps serves app files.
func ServeApps(basePath string) func(w http.ResponseWriter, r *http.Request) {
// ServeBundle serves bundles.
func ServeBundle(defaultModuleName string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// log.Tracef("ui: request for %s", r.RequestURI)
vars := mux.Vars(r)
appName, ok := vars["appName"]
moduleName, ok := vars["moduleName"]
if !ok {
http.Error(w, "missing app name", http.StatusBadRequest)
return
moduleName = defaultModuleName
if moduleName == "" {
http.Error(w, "missing module name", http.StatusBadRequest)
return
}
}
resPath, ok := vars["resPath"]
if !ok {
http.Error(w, "missing resource path", http.StatusBadRequest)
return
if !ok || strings.HasSuffix(resPath, "/") {
resPath = "index.html"
}
appsLock.RLock()
bundle, ok := apps[appName]
bundle, ok := apps[moduleName]
appsLock.RUnlock()
if ok {
ServeFileFromBundle(w, r, bundle, resPath)
ServeFileFromBundle(w, r, moduleName, bundle, resPath)
return
}
newBundle, err := resources.OpenZip(path.Join(basePath, fmt.Sprintf("%s.zip", appName)))
// get file from update system
zipFile, err := updates.GetFile(fmt.Sprintf("ui/modules/%s.zip", moduleName))
if err != nil {
if err == updates.ErrNotFound {
log.Tracef("ui: requested module %s does not exist", moduleName)
http.Error(w, err.Error(), http.StatusNotFound)
} else {
log.Tracef("ui: error loading module %s: %s", moduleName, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
// open bundle
newBundle, err := resources.OpenZip(zipFile.Path())
if err != nil {
log.Tracef("ui: error prepping module %s: %s", moduleName, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
bundle = &resources.BundleSequence{newBundle}
appsLock.Lock()
apps[appName] = bundle
apps[moduleName] = bundle
appsLock.Unlock()
ServeFileFromBundle(w, r, bundle, resPath)
ServeFileFromBundle(w, r, moduleName, bundle, resPath)
}
}
// ServeFileFromBundle serves a file from the given bundle.
func ServeFileFromBundle(w http.ResponseWriter, r *http.Request, bundle *resources.BundleSequence, path string) {
func ServeFileFromBundle(w http.ResponseWriter, r *http.Request, bundleName string, bundle *resources.BundleSequence, path string) {
readCloser, err := bundle.Open(path)
if err != nil {
log.Tracef("ui: error opening module %s: %s", bundleName, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -110,45 +124,16 @@ func ServeFileFromBundle(w http.ResponseWriter, r *http.Request, bundle *resourc
return
}
// ServeAssets serves global UI assets.
func ServeAssets(basePath string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
resPath, ok := vars["resPath"]
if !ok {
http.Error(w, "missing resource path", http.StatusBadRequest)
return
}
assetsLock.RLock()
bundle := assets
assetsLock.RUnlock()
if bundle != nil {
ServeFileFromBundle(w, r, bundle, resPath)
}
newBundle, err := resources.OpenZip(path.Join(basePath, "assets.zip"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
bundle = &resources.BundleSequence{newBundle}
assetsLock.Lock()
assets = bundle
assetsLock.Unlock()
ServeFileFromBundle(w, r, bundle, resPath)
}
}
// RedirectToControl redirects the requests to the control app
func RedirectToControl(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse("/app/control")
// RedirectToBase redirects the requests to the control app
func RedirectToBase(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse("/ui/modules/base/")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, r.URL.ResolveReference(u).String(), http.StatusPermanentRedirect)
}
func redirAddSlash(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.RequestURI+"/", http.StatusPermanentRedirect)
}