diff --git a/pixelapi/misc.go b/pixelapi/misc.go index d702ee8..1ca41f4 100644 --- a/pixelapi/misc.go +++ b/pixelapi/misc.go @@ -1,13 +1,16 @@ package pixelapi +// Recaptcha stores the reCaptcha site key type Recaptcha struct { SiteKey string `json:"site_key"` } -func (p *PixelAPI) GetRecaptcha() (resp *Recaptcha, err error) { - err = p.jsonRequest("GET", p.apiEndpoint+"/misc/recpatcha", resp) +// GetRecaptcha gets the reCaptcha site key from the pixelapi server. If +// reCaptcha is disabled the key will be empty +func (p *PixelAPI) GetRecaptcha() (resp Recaptcha, err error) { + err = p.jsonRequest("GET", p.apiEndpoint+"/misc/recaptcha", &resp) if err != nil { - return nil, err + return resp, err } return resp, nil } diff --git a/pixelapi/pixelapi.go b/pixelapi/pixelapi.go index 5271cd8..4874b16 100644 --- a/pixelapi/pixelapi.go +++ b/pixelapi/pixelapi.go @@ -11,6 +11,8 @@ import ( "github.com/Fornaxian/log" ) +var client = &http.Client{} + // PixelAPI is the Pixeldrain API client type PixelAPI struct { apiEndpoint string @@ -59,7 +61,6 @@ func (p *PixelAPI) jsonRequest(method, url string, target interface{}) error { req.SetBasicAuth("", p.apiKey) } - client := &http.Client{} resp, err := client.Do(req) if err != nil { return Error{ @@ -106,8 +107,6 @@ func (p *PixelAPI) getRaw(url string) (io.ReadCloser, error) { req.SetBasicAuth("", p.apiKey) } - client := &http.Client{} - resp, err := client.Do(req) if err != nil { return nil, err @@ -130,7 +129,6 @@ func (p *PixelAPI) postForm(url string, vals url.Values, target interface{}) err req.SetBasicAuth("", p.apiKey) } - client := &http.Client{} resp, err := client.Do(req) if err != nil { return &Error{ diff --git a/res/static/res/script/compiled/home.js b/res/static/res/script/compiled/home.js index 2331843..7f8571f 100644 --- a/res/static/res/script/compiled/home.js +++ b/res/static/res/script/compiled/home.js @@ -204,6 +204,12 @@ var UploadWorker = /** @class */ (function () { }); }; UploadWorker.prototype.setHistoryCookie = function (id) { + // Make sure the user is not logged in, for privacy. This keeps the + // files uploaded while logged in and anonymously uploaded files + // separated + if (Cookie.read("pd_auth_key") !== null) { + return; + } var uc = Cookie.read("pduploads"); // First upload in this browser if (uc === null) { diff --git a/res/static/res/typescript/lib/uploader.ts b/res/static/res/typescript/lib/uploader.ts index 2ff4386..eb56d06 100644 --- a/res/static/res/typescript/lib/uploader.ts +++ b/res/static/res/typescript/lib/uploader.ts @@ -117,6 +117,13 @@ class UploadWorker { } private setHistoryCookie(id: string){ + // Make sure the user is not logged in, for privacy. This keeps the + // files uploaded while logged in and anonymously uploaded files + // separated + if (Cookie.read("pd_auth_key") !== null) { + return; + } + var uc = Cookie.read("pduploads") // First upload in this browser diff --git a/res/template/account/register.html b/res/template/account/register.html index 6843737..03e9084 100644 --- a/res/template/account/register.html +++ b/res/template/account/register.html @@ -43,13 +43,13 @@
- + {{if ne .Other "none"}} Turing test
(Click the white box) -
+
@@ -58,7 +58,7 @@ an evil robot that is trying to flood the website with fake accounts

- + {{end}} diff --git a/webcontroller/templates/manager.go b/webcontroller/templateManager.go similarity index 71% rename from webcontroller/templates/manager.go rename to webcontroller/templateManager.go index 9f5cac5..a5322a4 100644 --- a/webcontroller/templates/manager.go +++ b/webcontroller/templateManager.go @@ -1,9 +1,10 @@ -package templates +package webcontroller import ( "html/template" "os" "path/filepath" + "time" "github.com/Fornaxian/log" ) @@ -17,7 +18,7 @@ type TemplateManager struct { debugModeEnabled bool } -func New(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager { +func NewTemplateManager(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager { return &TemplateManager{ templateDir: templateDir, externalAPIEndpoint: externalAPIEndpoint, @@ -65,3 +66,23 @@ func (tm *TemplateManager) Get() *template.Template { } return tm.templates } + +func (tm *TemplateManager) funcMap() template.FuncMap { + return template.FuncMap{ + "bgPatternCount": tm.bgPatternCount, + "debugMode": tm.debugMode, + "apiUrl": tm.apiURL, + } +} + +func (tm *TemplateManager) bgPatternCount() uint8 { + return uint8(time.Now().UnixNano() % 17) +} + +func (tm *TemplateManager) debugMode() bool { + return tm.debugModeEnabled +} + +func (tm *TemplateManager) apiURL() string { + return tm.externalAPIEndpoint +} diff --git a/webcontroller/templates/funcs.go b/webcontroller/templates/funcs.go deleted file mode 100644 index 62a432b..0000000 --- a/webcontroller/templates/funcs.go +++ /dev/null @@ -1,26 +0,0 @@ -package templates - -import ( - "html/template" - "time" -) - -func (tm *TemplateManager) funcMap() template.FuncMap { - return template.FuncMap{ - "bgPatternCount": tm.bgPatternCount, - "debugMode": tm.debugMode, - "apiUrl": tm.apiURL, - } -} - -func (tm *TemplateManager) bgPatternCount() uint8 { - return uint8(time.Now().UnixNano() % 17) -} - -func (tm *TemplateManager) debugMode() bool { - return tm.debugModeEnabled -} - -func (tm *TemplateManager) apiURL() string { - return tm.externalAPIEndpoint -} diff --git a/webcontroller/userAccount.go b/webcontroller/userAccount.go index 17f6392..f83b4eb 100644 --- a/webcontroller/userAccount.go +++ b/webcontroller/userAccount.go @@ -8,6 +8,37 @@ import ( "github.com/julienschmidt/httprouter" ) +func (wc *WebController) serveRegister( + w http.ResponseWriter, + r *http.Request, + p httprouter.Params, +) { + var tpld = wc.newTemplateData(w, r) + + // This only runs on the first request + if wc.captchaSiteKey == "" { + var api = pixelapi.New(wc.conf.APIURLInternal, "") + capt, err := api.GetRecaptcha() + if err != nil { + log.Error("Error getting recaptcha key: %s", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + if capt.SiteKey == "" { + wc.captchaSiteKey = "none" + } else { + wc.captchaSiteKey = capt.SiteKey + } + } + + tpld.Other = wc.captchaSiteKey + + err := wc.templates.Get().ExecuteTemplate(w, "register", tpld) + if err != nil { + log.Error("Error executing template '%s': %s", "register", err) + } +} + func (wc *WebController) serveLogout( w http.ResponseWriter, r *http.Request, diff --git a/webcontroller/webcontroller.go b/webcontroller/webcontroller.go index ed75ef7..36d4f62 100644 --- a/webcontroller/webcontroller.go +++ b/webcontroller/webcontroller.go @@ -7,7 +7,6 @@ import ( "github.com/google/uuid" "fornaxian.com/pixeldrain-web/init/conf" - "fornaxian.com/pixeldrain-web/webcontroller/templates" "github.com/Fornaxian/log" "github.com/julienschmidt/httprouter" ) @@ -16,8 +15,11 @@ import ( // proper context when running type WebController struct { conf *conf.PixelWebConfig - templates *templates.TemplateManager + templates *TemplateManager staticResourceDir string + + // page-specific variables + captchaSiteKey string } // New initializes a new WebController by registering all the request handlers @@ -27,7 +29,7 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon conf: conf, staticResourceDir: conf.StaticResourceDir, } - wc.templates = templates.New( + wc.templates = NewTemplateManager( conf.TemplateDir, conf.APIURLExternal, conf.DebugMode, @@ -49,7 +51,7 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon r.GET(prefix+"/t" /* */, wc.serveTemplate("paste", false)) // User account pages - r.GET(prefix+"/register" /* */, wc.serveTemplate("register", false)) + r.GET(prefix+"/register" /* */, wc.serveRegister) r.GET(prefix+"/login" /* */, wc.serveTemplate("login", false)) r.GET(prefix+"/logout" /* */, wc.serveTemplate("logout", true)) r.POST(prefix+"/logout" /* */, wc.serveLogout)