/
Webserver.go
763 lines (697 loc) · 23.1 KB
/
Webserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
package webserver
/**
Handling of webserver and requests / uploads
*/
import (
"bytes"
"context"
"embed"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/NYTimes/gziphandler"
"github.com/forceu/gokapi/internal/configuration"
"github.com/forceu/gokapi/internal/configuration/database"
"github.com/forceu/gokapi/internal/encryption"
"github.com/forceu/gokapi/internal/helper"
"github.com/forceu/gokapi/internal/logging"
"github.com/forceu/gokapi/internal/models"
"github.com/forceu/gokapi/internal/storage"
"github.com/forceu/gokapi/internal/storage/processingstatus"
"github.com/forceu/gokapi/internal/webserver/api"
"github.com/forceu/gokapi/internal/webserver/authentication"
"github.com/forceu/gokapi/internal/webserver/authentication/oauth"
"github.com/forceu/gokapi/internal/webserver/authentication/sessionmanager"
"github.com/forceu/gokapi/internal/webserver/fileupload"
"github.com/forceu/gokapi/internal/webserver/ssl"
"github.com/r3labs/sse/v2"
"html/template"
"io"
"io/fs"
"log"
"net/http"
"os"
"sort"
"strings"
templatetext "text/template"
"time"
)
// TODO add 404 handler
// staticFolderEmbedded is the embedded version of the "static" folder
// This contains JS files, CSS, images etc
//
//go:embed web/static
var staticFolderEmbedded embed.FS
// templateFolderEmbedded is the embedded version of the "templates" folder
// This contains templates that Gokapi uses for creating the HTML output
//
//go:embed web/templates
var templateFolderEmbedded embed.FS
// wasmDownloadFile is the compiled binary of the wasm downloader
// Will be generated with go generate ./...
//
//go:embed web/main.wasm
var wasmDownloadFile embed.FS
// wasmE2EFile is the compiled binary of the wasm e2e encrypter
// Will be generated with go generate ./...
//
//go:embed web/e2e.wasm
var wasmE2EFile embed.FS
const timeOutWebserverRead = 12 * time.Hour
const timeOutWebserverWrite = 12 * time.Hour
// Variable containing all parsed templates
var templateFolder *template.Template
var imageExpiredPicture []byte
var srv http.Server
var sseServer *sse.Server
// Start the webserver on the port set in the config
func Start() {
initTemplates(templateFolderEmbedded)
webserverDir, _ := fs.Sub(staticFolderEmbedded, "web/static")
var err error
mux := http.NewServeMux()
sseServer = sse.New()
sseServer.CreateStream("changes")
processingstatus.Init(sseServer)
if helper.FolderExists("static") {
fmt.Println("Found folder 'static', using local folder instead of internal static folder")
mux.Handle("/", http.FileServer(http.Dir("static")))
} else {
mux.Handle("/", http.FileServer(http.FS(webserverDir)))
helper.Check(err)
}
loadExpiryImage()
mux.HandleFunc("/admin", requireLogin(showAdminMenu, false))
mux.HandleFunc("/api/", processApi)
mux.HandleFunc("/apiDelete", requireLogin(deleteApiKey, false))
mux.HandleFunc("/apiKeys", requireLogin(showApiAdmin, false))
mux.HandleFunc("/apiNew", requireLogin(newApiKey, false))
mux.HandleFunc("/d", showDownload)
mux.HandleFunc("/delete", requireLogin(deleteFile, false))
mux.HandleFunc("/downloadFile", downloadFile)
mux.HandleFunc("/e2eInfo", requireLogin(e2eInfo, true))
mux.HandleFunc("/e2eSetup", requireLogin(showE2ESetup, false))
mux.HandleFunc("/error", showError)
mux.HandleFunc("/error-auth", showErrorAuth)
mux.HandleFunc("/error-oauth", showErrorIntOAuth)
mux.HandleFunc("/forgotpw", forgotPassword)
mux.HandleFunc("/hotlink/", showHotlink)
mux.HandleFunc("/index", showIndex)
mux.HandleFunc("/login", showLogin)
mux.HandleFunc("/logs", requireLogin(showLogs, false))
mux.HandleFunc("/logout", doLogout)
mux.HandleFunc("/uploadChunk", requireLogin(uploadChunk, true))
mux.HandleFunc("/uploadComplete", requireLogin(uploadComplete, true))
mux.HandleFunc("/uploadStatus", requireLogin(sseServer.ServeHTTP, false))
mux.Handle("/main.wasm", gziphandler.GzipHandler(http.HandlerFunc(serveDownloadWasm)))
mux.Handle("/e2e.wasm", gziphandler.GzipHandler(http.HandlerFunc(serveE2EWasm)))
if configuration.Get().Authentication.Method == authentication.OAuth2 {
oauth.Init(configuration.Get().ServerUrl, configuration.Get().Authentication)
mux.HandleFunc("/oauth-login", oauth.HandlerLogin)
mux.HandleFunc("/oauth-callback", oauth.HandlerCallback)
}
fmt.Println("Binding webserver to " + configuration.Get().Port)
srv = http.Server{
Addr: configuration.Get().Port,
ReadTimeout: timeOutWebserverRead,
WriteTimeout: timeOutWebserverWrite,
Handler: mux,
}
infoMessage := "Webserver can be accessed at " + configuration.Get().ServerUrl + "admin\nPress CTRL+C to stop Gokapi"
if strings.Contains(configuration.Get().ServerUrl, "127.0.0.1") {
if configuration.Get().UseSsl {
infoMessage = strings.Replace(infoMessage, "http://", "https://", 1)
} else {
infoMessage = strings.Replace(infoMessage, "https://", "http://", 1)
}
}
if configuration.Get().UseSsl {
ssl.GenerateIfInvalidCert(configuration.Get().ServerUrl, false)
fmt.Println(infoMessage)
err = srv.ListenAndServeTLS(ssl.GetCertificateLocations())
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
} else {
fmt.Println(infoMessage)
err = srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}
}
func loadExpiryImage() {
svgTemplate, err := templatetext.ParseFS(templateFolderEmbedded, "web/templates/expired_file_svg.tmpl")
helper.Check(err)
var buf bytes.Buffer
view := UploadView{}
err = svgTemplate.Execute(&buf, view.convertGlobalConfig(ViewMain))
helper.Check(err)
imageExpiredPicture = buf.Bytes()
}
// Shutdown closes the webserver gracefully
func Shutdown() {
sseServer.Close()
err := srv.Shutdown(context.Background())
if err != nil {
log.Println(err)
}
}
// Initialises the templateFolder variable by scanning through all the templates.
// If a folder "templates" exists in the main directory, it is used.
// Otherwise, templateFolderEmbedded will be used.
func initTemplates(templateFolderEmbedded embed.FS) {
var err error
if helper.FolderExists("templates") {
fmt.Println("Found folder 'templates', using local folder instead of internal template folder")
templateFolder, err = template.ParseGlob("templates/*.tmpl")
helper.Check(err)
} else {
templateFolder, err = template.ParseFS(templateFolderEmbedded, "web/templates/*.tmpl")
helper.Check(err)
}
}
// Sends a redirect HTTP output to the client. Variable url is used to redirect to ./url
func redirect(w http.ResponseWriter, url string) {
_, _ = io.WriteString(w, "<html><head><meta http-equiv=\"Refresh\" content=\"0; URL=./"+url+"\"></head></html>")
}
// Handling of /main.wasm
func serveDownloadWasm(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "public, max-age=100800") // 2 days
w.Header().Set("content-type", "application/wasm")
file, err := wasmDownloadFile.ReadFile("web/main.wasm")
helper.Check(err)
_, _ = w.Write(file)
}
// Handling of /e2e.wasm
func serveE2EWasm(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "public, max-age=100800") // 2 days
w.Header().Set("content-type", "application/wasm")
file, err := wasmE2EFile.ReadFile("web/e2e.wasm")
helper.Check(err)
_, _ = w.Write(file)
}
// Handling of /logout
func doLogout(w http.ResponseWriter, r *http.Request) {
authentication.Logout(w, r)
}
// Handling of /index and redirecting to globalConfig.RedirectUrl
func showIndex(w http.ResponseWriter, r *http.Request) {
err := templateFolder.ExecuteTemplate(w, "index", genericView{RedirectUrl: configuration.Get().RedirectUrl, PublicName: configuration.Get().PublicName})
helper.CheckIgnoreTimeout(err)
}
// Handling of /error
func showError(w http.ResponseWriter, r *http.Request) {
const invalidFile = 0
const noCipherSupplied = 1
const wrongCipher = 2
errorReason := invalidFile
if r.URL.Query().Has("e2e") {
errorReason = noCipherSupplied
}
if r.URL.Query().Has("key") {
errorReason = wrongCipher
}
err := templateFolder.ExecuteTemplate(w, "error", genericView{ErrorId: errorReason, PublicName: configuration.Get().PublicName})
helper.CheckIgnoreTimeout(err)
}
// Handling of /error-auth
func showErrorAuth(w http.ResponseWriter, r *http.Request) {
err := templateFolder.ExecuteTemplate(w, "error_auth", genericView{PublicName: configuration.Get().PublicName})
helper.CheckIgnoreTimeout(err)
}
// Handling of /error-oauth
func showErrorIntOAuth(w http.ResponseWriter, r *http.Request) {
view := oauthErrorView{PublicName: configuration.Get().PublicName}
view.IsAuthDenied = r.URL.Query().Get("isDenied") == "true"
view.ErrorProvidedName = r.URL.Query().Get("error")
view.ErrorProvidedMessage = r.URL.Query().Get("error_description")
view.ErrorGenericMessage = r.URL.Query().Get("error_generic")
err := templateFolder.ExecuteTemplate(w, "error_int_oauth", view)
helper.CheckIgnoreTimeout(err)
}
// Handling of /forgotpw
func forgotPassword(w http.ResponseWriter, r *http.Request) {
err := templateFolder.ExecuteTemplate(w, "forgotpw", genericView{PublicName: configuration.Get().PublicName})
helper.CheckIgnoreTimeout(err)
}
// Handling of /api
// If user is authenticated, this menu lists all uploads and enables uploading new files
func showApiAdmin(w http.ResponseWriter, r *http.Request) {
err := templateFolder.ExecuteTemplate(w, "api", (&UploadView{}).convertGlobalConfig(ViewAPI))
helper.CheckIgnoreTimeout(err)
}
// Handling of /apiNew
func newApiKey(w http.ResponseWriter, r *http.Request) {
api.NewKey(true)
redirect(w, "apiKeys")
}
// Handling of /apiDelete
func deleteApiKey(w http.ResponseWriter, r *http.Request) {
keys, ok := r.URL.Query()["id"]
if ok {
api.DeleteKey(keys[0])
}
redirect(w, "apiKeys")
}
// Handling of /api/
func processApi(w http.ResponseWriter, r *http.Request) {
api.Process(w, r, configuration.Get().MaxMemory)
}
// Handling of /login
// Shows a login form. If not authenticated, client needs to wait for three seconds.
// If correct, a new session is created and the user is redirected to the admin menu
func showLogin(w http.ResponseWriter, r *http.Request) {
if authentication.IsAuthenticated(w, r) {
redirect(w, "admin")
return
}
if configuration.Get().Authentication.Method == authentication.OAuth2 {
// If user clicked logout, force consent
if r.URL.Query().Has("consent") {
redirect(w, "oauth-login?consent=true")
} else {
redirect(w, "oauth-login")
}
return
}
err := r.ParseForm()
if err != nil {
fmt.Println("Invalid form data sent to server for /login")
fmt.Println(err)
return
}
user := r.Form.Get("username")
pw := r.Form.Get("password")
failedLogin := false
if pw != "" && user != "" {
if authentication.IsCorrectUsernameAndPassword(user, pw) {
isOauth := configuration.Get().Authentication.Method == authentication.OAuth2
interval := configuration.Get().Authentication.OAuthRecheckInterval
sessionmanager.CreateSession(w, isOauth, interval)
redirect(w, "admin")
return
}
select {
case <-time.After(3 * time.Second):
}
failedLogin = true
}
err = templateFolder.ExecuteTemplate(w, "login", LoginView{
IsFailedLogin: failedLogin,
User: user,
IsAdminView: false,
PublicName: configuration.Get().PublicName,
})
helper.CheckIgnoreTimeout(err)
}
// LoginView contains variables for the login template
type LoginView struct {
IsFailedLogin bool
IsAdminView bool
IsDownloadView bool
User string
PublicName string
}
// Handling of /d
// Checks if a file exists for the submitted ID
// If it exists, a download form is shown or a password needs to be entered.
func showDownload(w http.ResponseWriter, r *http.Request) {
addNoCacheHeader(w)
keyId := queryUrl(w, r, "error")
file, ok := storage.GetFile(keyId)
if !ok {
redirect(w, "error")
return
}
view := DownloadView{
Name: file.Name,
Size: file.Size,
Id: file.Id,
IsDownloadView: true,
EndToEndEncryption: file.Encryption.IsEndToEndEncrypted,
PublicName: configuration.Get().PublicName,
IsFailedLogin: false,
UsesHttps: configuration.UsesHttps(),
}
if file.RequiresClientDecryption() {
view.ClientSideDecryption = true
if !file.Encryption.IsEndToEndEncrypted {
cipher, err := encryption.GetCipherFromFile(file.Encryption)
helper.Check(err)
view.Cipher = base64.StdEncoding.EncodeToString(cipher)
}
}
if file.PasswordHash != "" {
_ = r.ParseForm()
enteredPassword := r.Form.Get("password")
if configuration.HashPassword(enteredPassword, true) != file.PasswordHash && !isValidPwCookie(r, file) {
if enteredPassword != "" {
view.IsFailedLogin = true
select {
case <-time.After(1 * time.Second):
}
}
view.IsPasswordView = true
err := templateFolder.ExecuteTemplate(w, "download_password", view)
helper.CheckIgnoreTimeout(err)
return
}
if !isValidPwCookie(r, file) {
writeFilePwCookie(w, file)
// redirect so that there is no post data to be resent if user refreshes page
redirect(w, "d?id="+file.Id)
return
}
}
err := templateFolder.ExecuteTemplate(w, "download", view)
helper.CheckIgnoreTimeout(err)
}
// Handling of /hotlink/
// Hotlinks an image or returns a static error image if image has expired
func showHotlink(w http.ResponseWriter, r *http.Request) {
addNoCacheHeader(w)
hotlinkId := strings.Replace(r.URL.Path, "/hotlink/", "", 1)
file, ok := storage.GetFileByHotlink(hotlinkId)
if !ok {
w.Header().Set("Content-Type", "image/svg+xml")
_, _ = w.Write(imageExpiredPicture)
return
}
storage.ServeFile(file, w, r, false)
}
// Handling of /e2eInfo
// User needs to be admin. Receives or stores end2end encryption info
func e2eInfo(w http.ResponseWriter, r *http.Request) {
action, ok := r.URL.Query()["action"]
if !ok || len(action) < 1 {
responseError(w, errors.New("invalid action specified"))
return
}
switch action[0] {
case "get":
getE2eInfo(w)
case "store":
storeE2eInfo(w, r)
default:
responseError(w, errors.New("invalid action specified"))
}
}
func storeE2eInfo(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
responseError(w, err)
return
}
uploadedInfoBase64 := r.Form.Get("info")
if uploadedInfoBase64 == "" {
responseError(w, errors.New("empty info sent"))
return
}
uploadedInfo, err := base64.StdEncoding.DecodeString(uploadedInfoBase64)
if err != nil {
responseError(w, err)
return
}
var info models.E2EInfoEncrypted
err = json.Unmarshal(uploadedInfo, &info)
if err != nil {
responseError(w, err)
return
}
database.SaveEnd2EndInfo(info)
_, _ = w.Write([]byte("\"result\":\"OK\""))
}
func getE2eInfo(w http.ResponseWriter) {
info := database.GetEnd2EndInfo()
bytesE2e, err := json.Marshal(info)
helper.Check(err)
_, _ = w.Write(bytesE2e)
}
// Handling of /delete
// User needs to be admin. Deletes the requested file
func deleteFile(w http.ResponseWriter, r *http.Request) {
keyId := queryUrl(w, r, "admin")
if keyId == "" {
return
}
storage.DeleteFile(keyId, true)
redirect(w, "admin")
}
// Checks if a file is associated with the GET parameter from the current URL
// Stops for 500ms to limit brute forcing if invalid key and redirects to redirectUrl
func queryUrl(w http.ResponseWriter, r *http.Request, redirectUrl string) string {
keys, ok := r.URL.Query()["id"]
if !ok || len(keys[0]) < configuration.Get().LengthId {
select {
case <-time.After(500 * time.Millisecond):
}
redirect(w, redirectUrl)
return ""
}
return keys[0]
}
// Handling of /admin
// If user is authenticated, this menu lists all uploads and enables uploading new files
func showAdminMenu(w http.ResponseWriter, r *http.Request) {
if configuration.Get().Encryption.Level == encryption.EndToEndEncryption {
e2einfo := database.GetEnd2EndInfo()
if !e2einfo.HasBeenSetUp() {
redirect(w, "e2eSetup")
return
}
}
err := templateFolder.ExecuteTemplate(w, "admin", (&UploadView{}).convertGlobalConfig(ViewMain))
helper.CheckIgnoreTimeout(err)
}
// Handling of /logs
// If user is authenticated, this menu shows the stored logs
func showLogs(w http.ResponseWriter, r *http.Request) {
err := templateFolder.ExecuteTemplate(w, "logs", (&UploadView{}).convertGlobalConfig(ViewLogs))
helper.CheckIgnoreTimeout(err)
}
func showE2ESetup(w http.ResponseWriter, r *http.Request) {
if configuration.Get().Encryption.Level != encryption.EndToEndEncryption {
redirect(w, "admin")
return
}
e2einfo := database.GetEnd2EndInfo()
err := templateFolder.ExecuteTemplate(w, "e2esetup", e2ESetupView{HasBeenSetup: e2einfo.HasBeenSetUp(), PublicName: configuration.Get().PublicName})
helper.CheckIgnoreTimeout(err)
}
// DownloadView contains parameters for the download template
type DownloadView struct {
Name string
Size string
Id string
Cipher string
PublicName string
IsFailedLogin bool
IsAdminView bool
IsDownloadView bool
IsPasswordView bool
ClientSideDecryption bool
EndToEndEncryption bool
UsesHttps bool
}
type e2ESetupView struct {
IsAdminView bool
IsDownloadView bool
HasBeenSetup bool
PublicName string
}
// UploadView contains parameters for the admin menu template
type UploadView struct {
Items []models.FileApiOutput
ApiKeys []models.ApiKey
Url string
HotlinkUrl string
GenericHotlinkUrl string
DefaultPassword string
Logs string
PublicName string
IsAdminView bool
IsDownloadView bool
IsApiView bool
IsLogoutAvailable bool
DefaultUnlimitedDownload bool
DefaultUnlimitedTime bool
EndToEndEncryption bool
MaxFileSize int
DefaultDownloads int
DefaultExpiry int
ActiveView int
TimeNow int64
}
// ViewMain is the identifier for the main menu
const ViewMain = 0
// ViewLogs is the identifier for the log viewer menu
const ViewLogs = 1
// ViewAPI is the identifier for the API menu
const ViewAPI = 2
// Converts the globalConfig variable to an UploadView struct to pass the infos to
// the admin template
func (u *UploadView) convertGlobalConfig(view int) *UploadView {
var result []models.FileApiOutput
var resultApi []models.ApiKey
switch view {
case ViewMain:
for _, element := range database.GetAllMetadata() {
fileInfo, err := element.ToFileApiOutput()
helper.Check(err)
result = append(result, fileInfo)
}
sort.Slice(result[:], func(i, j int) bool {
if result[i].ExpireAt == result[j].ExpireAt {
return result[i].Id > result[j].Id
}
return result[i].ExpireAt > result[j].ExpireAt
})
case ViewAPI:
for _, element := range database.GetAllApiKeys() {
if element.LastUsed == 0 {
element.LastUsedString = "Never"
} else {
element.LastUsedString = time.Unix(element.LastUsed, 0).Format("2006-01-02 15:04:05")
}
resultApi = append(resultApi, element)
}
sort.Slice(resultApi[:], func(i, j int) bool {
if resultApi[i].LastUsed == resultApi[j].LastUsed {
return resultApi[i].Id < resultApi[j].Id
}
return resultApi[i].LastUsed > resultApi[j].LastUsed
})
case ViewLogs:
if helper.FileExists(logging.GetLogPath()) {
content, err := os.ReadFile(logging.GetLogPath())
helper.Check(err)
u.Logs = string(content)
} else {
u.Logs = "Warning: Log file not found!"
}
}
config := configuration.Get()
u.Url = config.ServerUrl + "d?id="
u.HotlinkUrl = config.ServerUrl + "hotlink/"
u.GenericHotlinkUrl = config.ServerUrl + "downloadFile?id="
u.Items = result
u.PublicName = config.PublicName
u.ApiKeys = resultApi
u.TimeNow = time.Now().Unix()
u.IsAdminView = true
u.ActiveView = view
u.MaxFileSize = config.MaxFileSizeMB
u.IsLogoutAvailable = authentication.IsLogoutAvailable()
defaultValues := database.GetUploadDefaults()
u.DefaultDownloads = defaultValues.Downloads
u.DefaultExpiry = defaultValues.TimeExpiry
u.DefaultPassword = defaultValues.Password
u.DefaultUnlimitedDownload = defaultValues.UnlimitedDownload
u.DefaultUnlimitedTime = defaultValues.UnlimitedTime
u.EndToEndEncryption = config.Encryption.Level == encryption.EndToEndEncryption
return u
}
// Handling of /uploadChunk
// If the user is authenticated, this parses the uploaded chunk and stores it
func uploadChunk(w http.ResponseWriter, r *http.Request) {
maxUpload := int64(configuration.Get().MaxFileSizeMB) * 1024 * 1024
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if r.ContentLength > maxUpload {
responseError(w, storage.ErrorFileTooLarge)
}
r.Body = http.MaxBytesReader(w, r.Body, maxUpload)
err := fileupload.ProcessNewChunk(w, r, false)
responseError(w, err)
}
// Handling of /uploadComplete
// If the user is authenticated, this parses the uploaded chunk and stores it
func uploadComplete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
err := fileupload.CompleteChunk(w, r, false)
responseError(w, err)
}
// Outputs an error in json format if err!=nil
func responseError(w http.ResponseWriter, err error) {
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = io.WriteString(w, "{\"Result\":\"error\",\"ErrorMessage\":\""+err.Error()+"\"}")
log.Println(err)
}
}
// Outputs the file to the user and reduces the download remaining count for the file
func downloadFile(w http.ResponseWriter, r *http.Request) {
addNoCacheHeader(w)
keyId := queryUrl(w, r, "error")
savedFile, ok := storage.GetFile(keyId)
if !ok {
redirect(w, "error")
return
}
if savedFile.PasswordHash != "" {
if !(isValidPwCookie(r, savedFile)) {
redirect(w, "d?id="+savedFile.Id)
return
}
}
storage.ServeFile(savedFile, w, r, true)
}
func requireLogin(next http.HandlerFunc, isUpload bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
addNoCacheHeader(w)
if authentication.IsAuthenticated(w, r) {
next.ServeHTTP(w, r)
return
}
if isUpload {
_, _ = io.WriteString(w, "{\"Result\":\"error\",\"ErrorMessage\":\"Not authenticated\"}")
return
}
redirect(w, "login")
}
}
// Write a cookie if the user has entered a correct password for a password-protected file
func writeFilePwCookie(w http.ResponseWriter, file models.File) {
http.SetCookie(w, &http.Cookie{
Name: "p" + file.Id,
Value: file.PasswordHash,
Expires: time.Now().Add(5 * time.Minute),
})
}
// Checks if a cookie contains the correct password hash for a password-protected file
// If incorrect, a 3-second delay is introduced unless the cookie was empty.
func isValidPwCookie(r *http.Request, file models.File) bool {
cookie, err := r.Cookie("p" + file.Id)
if err == nil {
if cookie.Value == file.PasswordHash {
return true
}
select {
case <-time.After(3 * time.Second):
}
}
return false
}
// Adds a header to disable external caching
func addNoCacheHeader(w http.ResponseWriter) {
w.Header().Set("cache-control", "no-store")
}
// A view containing parameters for a generic template
type genericView struct {
IsAdminView bool
IsDownloadView bool
PublicName string
RedirectUrl string
ErrorId int
}
// A view containing parameters for an oauth error
type oauthErrorView struct {
IsAdminView bool
IsDownloadView bool
PublicName string
IsAuthDenied bool
ErrorGenericMessage string
ErrorProvidedName string
ErrorProvidedMessage string
}