2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-08-21 04:07:25 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime/debug"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
"github.com/99designs/gqlgen/handler"
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
|
|
|
"github.com/gobuffalo/packr/v2"
|
2019-02-11 22:44:13 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2019-02-09 12:30:49 +00:00
|
|
|
"github.com/rs/cors"
|
2020-03-22 21:07:15 +00:00
|
|
|
"github.com/stashapp/stash/pkg/database"
|
2019-02-14 23:42:52 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
2019-11-17 21:42:24 +00:00
|
|
|
"github.com/stashapp/stash/pkg/manager"
|
2019-03-23 14:56:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/manager/config"
|
2019-04-11 17:55:58 +00:00
|
|
|
"github.com/stashapp/stash/pkg/manager/paths"
|
2019-02-14 23:42:52 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2019-11-17 21:41:08 +00:00
|
|
|
var version string = ""
|
2019-08-21 04:07:25 +00:00
|
|
|
var buildstamp string = ""
|
|
|
|
var githash string = ""
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
var uiBox *packr.Box
|
2019-03-23 14:56:59 +00:00
|
|
|
|
2019-03-15 22:46:47 +00:00
|
|
|
//var legacyUiBox *packr.Box
|
2019-02-11 10:49:39 +00:00
|
|
|
var setupUIBox *packr.Box
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2019-07-28 09:36:52 +00:00
|
|
|
func authenticateHandler() func(http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// only do this if credentials have been configured
|
|
|
|
if !config.HasCredentials() {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
authUser, authPW, ok := r.BasicAuth()
|
|
|
|
|
|
|
|
if !ok || !config.ValidateCredentials(authUser, authPW) {
|
|
|
|
unauthorized(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func unauthorized(w http.ResponseWriter) {
|
|
|
|
w.Header().Add("WWW-Authenticate", `Basic realm=\"Stash\"`)
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
func Start() {
|
2019-03-15 22:46:47 +00:00
|
|
|
uiBox = packr.New("UI Box", "../../ui/v2/build")
|
|
|
|
//legacyUiBox = packr.New("UI Box", "../../ui/v1/dist/stash-frontend")
|
2019-02-14 23:42:52 +00:00
|
|
|
setupUIBox = packr.New("Setup UI Box", "../../ui/setup")
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2019-08-24 07:17:25 +00:00
|
|
|
initialiseImages()
|
2019-11-17 21:41:08 +00:00
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
|
2019-07-28 09:36:52 +00:00
|
|
|
r.Use(authenticateHandler())
|
2019-02-09 12:30:49 +00:00
|
|
|
r.Use(middleware.Recoverer)
|
2019-10-25 00:13:44 +00:00
|
|
|
|
|
|
|
if config.GetLogAccess() {
|
|
|
|
r.Use(middleware.Logger)
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
r.Use(middleware.DefaultCompress)
|
|
|
|
r.Use(middleware.StripSlashes)
|
|
|
|
r.Use(cors.AllowAll().Handler)
|
|
|
|
r.Use(BaseURLMiddleware)
|
2019-02-11 10:49:39 +00:00
|
|
|
r.Use(ConfigCheckMiddleware)
|
2020-03-22 21:07:15 +00:00
|
|
|
r.Use(DatabaseCheckMiddleware)
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
recoverFunc := handler.RecoverFunc(func(ctx context.Context, err interface{}) error {
|
|
|
|
logger.Error(err)
|
|
|
|
debug.PrintStack()
|
|
|
|
|
|
|
|
message := fmt.Sprintf("Internal system error. Error <%v>", err)
|
|
|
|
return errors.New(message)
|
|
|
|
})
|
|
|
|
requestMiddleware := handler.RequestMiddleware(func(ctx context.Context, next func(ctx context.Context) []byte) []byte {
|
|
|
|
//api.GetRequestContext(ctx).Variables[]
|
|
|
|
return next(ctx)
|
|
|
|
})
|
2019-02-11 22:44:13 +00:00
|
|
|
websocketUpgrader := handler.WebsocketUpgrader(websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
})
|
|
|
|
gqlHandler := handler.GraphQL(models.NewExecutableSchema(models.Config{Resolvers: &Resolver{}}), recoverFunc, requestMiddleware, websocketUpgrader)
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
r.Handle("/graphql", gqlHandler)
|
|
|
|
r.Handle("/playground", handler.Playground("GraphQL playground", "/graphql"))
|
|
|
|
|
|
|
|
r.Mount("/gallery", galleryRoutes{}.Routes())
|
|
|
|
r.Mount("/performer", performerRoutes{}.Routes())
|
|
|
|
r.Mount("/scene", sceneRoutes{}.Routes())
|
|
|
|
r.Mount("/studio", studioRoutes{}.Routes())
|
2020-03-10 03:28:15 +00:00
|
|
|
r.Mount("/movie", movieRoutes{}.Routes())
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2019-08-22 22:24:14 +00:00
|
|
|
r.HandleFunc("/css", func(w http.ResponseWriter, r *http.Request) {
|
2020-03-08 01:55:42 +00:00
|
|
|
w.Header().Set("Content-Type", "text/css")
|
2019-08-22 22:24:14 +00:00
|
|
|
if !config.GetCSSEnabled() {
|
|
|
|
return
|
|
|
|
}
|
2019-11-17 21:41:08 +00:00
|
|
|
|
2019-08-22 22:24:14 +00:00
|
|
|
// search for custom.css in current directory, then $HOME/.stash
|
|
|
|
fn := config.GetCSSPath()
|
|
|
|
exists, _ := utils.FileExists(fn)
|
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeFile(w, r, fn)
|
|
|
|
})
|
|
|
|
|
2020-03-22 21:07:15 +00:00
|
|
|
// Serve the migration UI
|
|
|
|
r.Get("/migrate", getMigrateHandler)
|
|
|
|
r.Post("/migrate", doMigrateHandler)
|
|
|
|
|
2019-02-11 10:49:39 +00:00
|
|
|
// Serve the setup UI
|
|
|
|
r.HandleFunc("/setup*", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ext := path.Ext(r.URL.Path)
|
2019-02-14 22:53:32 +00:00
|
|
|
if ext == ".html" || ext == "" {
|
2019-12-28 17:53:16 +00:00
|
|
|
data, _ := setupUIBox.Find("index.html")
|
2019-02-11 10:49:39 +00:00
|
|
|
_, _ = w.Write(data)
|
|
|
|
} else {
|
|
|
|
r.URL.Path = strings.Replace(r.URL.Path, "/setup", "", 1)
|
|
|
|
http.FileServer(setupUIBox).ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
r.Post("/init", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("error: %s", err), 500)
|
|
|
|
}
|
|
|
|
stash := filepath.Clean(r.Form.Get("stash"))
|
2019-03-23 14:56:59 +00:00
|
|
|
generated := filepath.Clean(r.Form.Get("generated"))
|
2019-02-11 10:49:39 +00:00
|
|
|
metadata := filepath.Clean(r.Form.Get("metadata"))
|
|
|
|
cache := filepath.Clean(r.Form.Get("cache"))
|
|
|
|
//downloads := filepath.Clean(r.Form.Get("downloads")) // TODO
|
|
|
|
downloads := filepath.Join(metadata, "downloads")
|
|
|
|
|
2019-03-23 14:56:59 +00:00
|
|
|
exists, _ := utils.DirExists(stash)
|
|
|
|
if !exists || stash == "." {
|
2019-02-11 10:49:39 +00:00
|
|
|
http.Error(w, fmt.Sprintf("the stash path either doesn't exist, or is not a directory <%s>. Go back and try again.", stash), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-23 14:56:59 +00:00
|
|
|
exists, _ = utils.DirExists(generated)
|
|
|
|
if !exists || generated == "." {
|
|
|
|
http.Error(w, fmt.Sprintf("the generated path either doesn't exist, or is not a directory <%s>. Go back and try again.", generated), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, _ = utils.DirExists(metadata)
|
|
|
|
if !exists || metadata == "." {
|
2019-02-11 10:49:39 +00:00
|
|
|
http.Error(w, fmt.Sprintf("the metadata path either doesn't exist, or is not a directory <%s> Go back and try again.", metadata), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-23 14:56:59 +00:00
|
|
|
exists, _ = utils.DirExists(cache)
|
|
|
|
if !exists || cache == "." {
|
2019-02-11 10:49:39 +00:00
|
|
|
http.Error(w, fmt.Sprintf("the cache path either doesn't exist, or is not a directory <%s> Go back and try again.", cache), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 02:27:10 +00:00
|
|
|
_ = os.Mkdir(downloads, 0755)
|
2019-02-11 10:49:39 +00:00
|
|
|
|
2019-03-23 14:56:59 +00:00
|
|
|
config.Set(config.Stash, stash)
|
|
|
|
config.Set(config.Generated, generated)
|
|
|
|
config.Set(config.Metadata, metadata)
|
|
|
|
config.Set(config.Cache, cache)
|
|
|
|
config.Set(config.Downloads, downloads)
|
|
|
|
if err := config.Write(); err != nil {
|
2019-02-11 10:49:39 +00:00
|
|
|
http.Error(w, fmt.Sprintf("there was an error saving the config file: %s", err), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-17 21:42:24 +00:00
|
|
|
manager.GetInstance().RefreshConfig()
|
|
|
|
|
2019-02-11 10:49:39 +00:00
|
|
|
http.Redirect(w, r, "/", 301)
|
|
|
|
})
|
|
|
|
|
2019-04-11 17:55:58 +00:00
|
|
|
// Serve the web app
|
2019-02-09 12:30:49 +00:00
|
|
|
r.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ext := path.Ext(r.URL.Path)
|
2019-02-14 22:53:32 +00:00
|
|
|
if ext == ".html" || ext == "" {
|
2019-12-28 17:53:16 +00:00
|
|
|
data, _ := uiBox.Find("index.html")
|
2019-02-09 12:30:49 +00:00
|
|
|
_, _ = w.Write(data)
|
|
|
|
} else {
|
2020-01-31 22:20:14 +00:00
|
|
|
isStatic, _ := path.Match("/static/*/*", r.URL.Path)
|
|
|
|
if isStatic {
|
|
|
|
w.Header().Add("Cache-Control", "max-age=604800000")
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
http.FileServer(uiBox).ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-12-31 17:22:34 +00:00
|
|
|
displayHost := config.GetHost()
|
|
|
|
if displayHost == "0.0.0.0" {
|
|
|
|
displayHost = "localhost"
|
|
|
|
}
|
|
|
|
displayAddress := displayHost + ":" + strconv.Itoa(config.GetPort())
|
|
|
|
|
2019-04-11 17:55:58 +00:00
|
|
|
address := config.GetHost() + ":" + strconv.Itoa(config.GetPort())
|
|
|
|
if tlsConfig := makeTLSConfig(); tlsConfig != nil {
|
|
|
|
httpsServer := &http.Server{
|
|
|
|
Addr: address,
|
|
|
|
Handler: r,
|
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2019-04-11 17:55:58 +00:00
|
|
|
go func() {
|
2019-08-21 04:07:25 +00:00
|
|
|
printVersion()
|
2020-01-07 22:21:23 +00:00
|
|
|
printLatestVersion()
|
2019-12-31 22:42:06 +00:00
|
|
|
logger.Infof("stash is listening on " + address)
|
|
|
|
logger.Infof("stash is running at https://" + displayAddress + "/")
|
2019-04-11 17:55:58 +00:00
|
|
|
logger.Fatal(httpsServer.ListenAndServeTLS("", ""))
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: address,
|
|
|
|
Handler: r,
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2019-04-11 17:55:58 +00:00
|
|
|
go func() {
|
2019-08-21 04:07:25 +00:00
|
|
|
printVersion()
|
2020-01-07 22:21:23 +00:00
|
|
|
printLatestVersion()
|
2019-12-31 22:42:06 +00:00
|
|
|
logger.Infof("stash is listening on " + address)
|
|
|
|
logger.Infof("stash is running at http://" + displayAddress + "/")
|
2019-04-11 17:55:58 +00:00
|
|
|
logger.Fatal(server.ListenAndServe())
|
|
|
|
}()
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 04:07:25 +00:00
|
|
|
func printVersion() {
|
2019-11-17 21:41:08 +00:00
|
|
|
versionString := githash
|
|
|
|
if version != "" {
|
|
|
|
versionString = version + " (" + versionString + ")"
|
|
|
|
}
|
|
|
|
fmt.Printf("stash version: %s - %s\n", versionString, buildstamp)
|
2019-08-21 04:07:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-17 21:41:08 +00:00
|
|
|
func GetVersion() (string, string, string) {
|
|
|
|
return version, githash, buildstamp
|
2019-08-21 04:47:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
func makeTLSConfig() *tls.Config {
|
2019-04-11 17:55:58 +00:00
|
|
|
cert, err := ioutil.ReadFile(paths.GetSSLCert())
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := ioutil.ReadFile(paths.GetSSLKey())
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
certs := make([]tls.Certificate, 1)
|
|
|
|
certs[0], err = tls.X509KeyPair(cert, key)
|
|
|
|
if err != nil {
|
2019-04-11 17:55:58 +00:00
|
|
|
return nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
Certificates: certs,
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type contextKey struct {
|
|
|
|
name string
|
|
|
|
}
|
2019-02-14 22:53:32 +00:00
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
var (
|
|
|
|
BaseURLCtxKey = &contextKey{"BaseURL"}
|
|
|
|
)
|
2019-02-14 22:53:32 +00:00
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
func BaseURLMiddleware(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var scheme string
|
2019-10-11 18:01:18 +00:00
|
|
|
if strings.Compare("https", r.URL.Scheme) == 0 || r.Proto == "HTTP/2.0" || r.Header.Get("X-Forwarded-Proto") == "https" {
|
2019-02-09 12:30:49 +00:00
|
|
|
scheme = "https"
|
|
|
|
} else {
|
|
|
|
scheme = "http"
|
|
|
|
}
|
|
|
|
baseURL := scheme + "://" + r.Host
|
|
|
|
|
2020-02-17 03:01:02 +00:00
|
|
|
externalHost := config.GetExternalHost()
|
|
|
|
if externalHost != "" {
|
|
|
|
baseURL = externalHost
|
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
r = r.WithContext(context.WithValue(ctx, BaseURLCtxKey, baseURL))
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
2019-02-11 10:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ConfigCheckMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ext := path.Ext(r.URL.Path)
|
|
|
|
shouldRedirect := ext == "" && r.Method == "GET" && r.URL.Path != "/init"
|
2019-03-23 14:56:59 +00:00
|
|
|
if !config.IsValid() && shouldRedirect {
|
2019-02-11 10:49:39 +00:00
|
|
|
if !strings.HasPrefix(r.URL.Path, "/setup") {
|
|
|
|
http.Redirect(w, r, "/setup", 301)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2019-02-14 22:53:32 +00:00
|
|
|
}
|
2020-03-22 21:07:15 +00:00
|
|
|
|
|
|
|
func DatabaseCheckMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ext := path.Ext(r.URL.Path)
|
|
|
|
shouldRedirect := ext == "" && r.Method == "GET"
|
|
|
|
if shouldRedirect && database.NeedsMigration() {
|
|
|
|
if !strings.HasPrefix(r.URL.Path, "/migrate") {
|
|
|
|
http.Redirect(w, r, "/migrate", 301)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|