fix get viewtoken

This commit is contained in:
Wim Brand
2020-02-21 14:23:29 +01:00
parent 4de17609bb
commit 9cfe675d16
3 changed files with 21 additions and 7 deletions

View File

@@ -3,6 +3,10 @@ package webcontroller
import (
"net/http"
"fornaxian.com/pixeldrain-api/util"
"fornaxian.com/pixeldrain-web/pixelapi"
"github.com/Fornaxian/log"
"github.com/julienschmidt/httprouter"
)
@@ -11,12 +15,17 @@ func (wc *WebController) serveAdClick(w http.ResponseWriter, r *http.Request, p
// Redirect the user to the target page
http.Redirect(w, r, r.URL.Query().Get("target"), http.StatusTemporaryRedirect)
// Get a view token
td := wc.newTemplateData(w, r)
vt := viewTokenOrBust(td.PixelAPI)
api := pixelapi.New(wc.apiURLInternal)
// Get the view token without authentication
vt := viewTokenOrBust(api)
// Apply authentication and register the view
api.APIKey, _ = wc.getAPIKey(r)
api.RealIP = util.RemoteAddress(r)
// Log a view on the file
if err := td.PixelAPI.PostFileView(p.ByName("id"), vt); err != nil {
if err := api.PostFileView(p.ByName("id"), vt); err != nil {
log.Warn("Failed to log view")
}
}

View File

@@ -68,7 +68,7 @@ func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request,
templateData.Other = viewerData{
Type: "list",
CaptchaKey: wc.captchaKey(),
ViewToken: viewTokenOrBust(templateData.PixelAPI),
ViewToken: viewTokenOrBust(wc.systemPixelAPI),
APIResponse: pixelapi.List{
Success: true,
Title: "Multiple files",
@@ -81,7 +81,7 @@ func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request,
templateData.Other = viewerData{
Type: "file",
CaptchaKey: wc.captchaKey(),
ViewToken: viewTokenOrBust(templateData.PixelAPI),
ViewToken: viewTokenOrBust(wc.systemPixelAPI),
APIResponse: finfo[0].FileInfo,
}
}
@@ -143,7 +143,7 @@ func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request,
templateData.Other = viewerData{
Type: "list",
CaptchaKey: wc.captchaSiteKey,
ViewToken: viewTokenOrBust(templateData.PixelAPI),
ViewToken: viewTokenOrBust(wc.systemPixelAPI),
APIResponse: list,
}

View File

@@ -33,6 +33,10 @@ type WebController struct {
captchaSiteKey string
httpClient *http.Client
// This API client should only be used for system functions like getting
// view tokens. It has no authentication and no IP forwarding
systemPixelAPI *pixelapi.PixelAPI
}
// New initializes a new WebController by registering all the request handlers
@@ -54,6 +58,7 @@ func New(
apiURLExternal: apiURLExternal,
sessionCookieDomain: sessionCookieDomain,
httpClient: &http.Client{Timeout: time.Minute * 10},
systemPixelAPI: pixelapi.New(apiURLInternal),
}
wc.templates = NewTemplateManager(resourceDir, apiURLExternal, debugMode)
wc.templates.ParseTemplates(false)