Add updates module and fix issues
This commit is contained in:
113
ui/serve.go
113
ui/serve.go
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user