Skip to content

Commit

Permalink
Added log viewer, set JS versions through go generate
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceu committed Feb 13, 2023
1 parent a03b746 commit a313d26
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/forceu/gokapi

go 1.19
go 1.20

require (
git.mills.io/prologic/bitcask v1.0.2
Expand Down
8 changes: 6 additions & 2 deletions build/setVersionTemplate.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/sh
#Called by go generate
#Sets the version number in the template automatically
#Sets the version numbers in the template automatically
sed -i 's/{{define "version"}}.*{{end}}/{{define "version"}}'$1'{{end}}/g' ../../internal/webserver/web/templates/string_constants.tmpl
echo "Set version in web template"
echo "Updated version in web template"
sed -i 's/{{define "js_admin_version"}}.*{{end}}/{{define "js_admin_version"}}'$2'{{end}}/g' ../../internal/webserver/web/templates/string_constants.tmpl
sed -i 's/{{define "js_dropzone_version"}}.*{{end}}/{{define "js_dropzone_version"}}'$3'{{end}}/g' ../../internal/webserver/web/templates/string_constants.tmpl
sed -i 's/{{define "js_e2eversion"}}.*{{end}}/{{define "js_e2eversion"}}'$4'{{end}}/g' ../../internal/webserver/web/templates/string_constants.tmpl
echo "Updated JS version numbers"
13 changes: 8 additions & 5 deletions cmd/gokapi/Main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ import (
"time"
)

// Version is the current version in readable form.
// versionGokapi is the current version in readable form.
// The go generate call below needs to be modified as well
const Version = "1.6.2"
const versionGokapi = "1.6.2"

//go:generate sh "../../build/setVersionTemplate.sh" "1.6.1"
// The following call updates the version numbers
// Parameters:
// GokapiVersion, JsAdmin, JsDropzone, JsE2EAdmin
//go:generate sh "../../build/setVersionTemplate.sh" "1.6.1" "13" "3" "1"
//go:generate sh -c "cp \"$(go env GOROOT)/misc/wasm/wasm_exec.js\" ../../internal/webserver/web/static/js/ && echo Copied wasm_exec.js"
//go:generate sh -c "GOOS=js GOARCH=wasm go build -o ../../internal/webserver/web/main.wasm github.com/forceu/gokapi/cmd/wasmdownloader && echo Compiled Downloader WASM module"
//go:generate sh -c "GOOS=js GOARCH=wasm go build -o ../../internal/webserver/web/e2e.wasm github.com/forceu/gokapi/cmd/wasme2e && echo Compiled E2E WASM module"
Expand All @@ -42,7 +45,7 @@ func main() {
showVersion(passedFlags)
rand.Seed(time.Now().UnixNano())
fmt.Println(logo)
fmt.Println("Gokapi v" + Version + " starting")
fmt.Println("Gokapi v" + versionGokapi + " starting")
setup.RunIfFirstStart()
configuration.Load()
reconfigureServer(passedFlags)
Expand Down Expand Up @@ -70,7 +73,7 @@ func shutdown() {
// Checks for command line arguments that have to be parsed before loading the configuration
func showVersion(passedFlags flagparser.MainFlags) {
if passedFlags.ShowVersion {
fmt.Println("Gokapi v" + Version)
fmt.Println("Gokapi v" + versionGokapi)
fmt.Println()
fmt.Println("Builder: " + environment.Builder)
fmt.Println("Build Date: " + environment.BuildTime)
Expand Down
5 changes: 5 additions & 0 deletions internal/logging/Logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func AddString(text string) {
go writeToFile(text)
}

// GetLogPath returns the relative path to the log file
func GetLogPath() string {
return logPath
}

// AddDownload adds a line to the logfile when a download was requested. Non-Blocking
func AddDownload(file *models.File, r *http.Request) {
AddString(fmt.Sprintf("Download: Filename %s, IP %s, ID %s, Useragent %s", file.Name, getIpAddress(r), file.Id, r.UserAgent()))
Expand Down
38 changes: 31 additions & 7 deletions internal/webserver/Webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"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/webserver/api"
Expand Down Expand Up @@ -105,6 +106,7 @@ func Start() {
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))
Expand Down Expand Up @@ -237,7 +239,7 @@ func forgotPassword(w http.ResponseWriter, r *http.Request) {
// 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(false))
err := templateFolder.ExecuteTemplate(w, "api", (&UploadView{}).convertGlobalConfig(ViewAPI))
helper.Check(err)
}

Expand Down Expand Up @@ -460,7 +462,14 @@ func showAdminMenu(w http.ResponseWriter, r *http.Request) {
return
}
}
err := templateFolder.ExecuteTemplate(w, "admin", (&UploadView{}).convertGlobalConfig(true))
err := templateFolder.ExecuteTemplate(w, "admin", (&UploadView{}).convertGlobalConfig(ViewMain))
helper.Check(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.Check(err)
}

Expand Down Expand Up @@ -501,7 +510,6 @@ type UploadView struct {
GenericHotlinkUrl string
TimeNow int64
IsAdminView bool
IsMainView bool
IsApiView bool
MaxFileSize int
IsLogoutAvailable bool
Expand All @@ -511,14 +519,21 @@ type UploadView struct {
DefaultUnlimitedDownload bool
DefaultUnlimitedTime bool
EndToEndEncryption bool
ActiveView int
Logs string
}

const ViewMain = 0
const ViewLogs = 1
const ViewAPI = 2

// Converts the globalConfig variable to an UploadView struct to pass the infos to
// the admin template
func (u *UploadView) convertGlobalConfig(isMainView bool) *UploadView {
func (u *UploadView) convertGlobalConfig(view int) *UploadView {
var result []models.FileApiOutput
var resultApi []models.ApiKey
if isMainView {
switch view {
case ViewMain:
for _, element := range database.GetAllMetadata() {
fileInfo, err := element.ToFileApiOutput(storage.RequiresClientDecryption(element))
helper.Check(err)
Expand All @@ -530,7 +545,7 @@ func (u *UploadView) convertGlobalConfig(isMainView bool) *UploadView {
}
return result[i].ExpireAt > result[j].ExpireAt
})
} else {
case ViewAPI:
for _, element := range database.GetAllApiKeys() {
if element.LastUsed == 0 {
element.LastUsedString = "Never"
Expand All @@ -545,15 +560,24 @@ func (u *UploadView) convertGlobalConfig(isMainView bool) *UploadView {
}
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!"
}
}

u.Url = configuration.Get().ServerUrl + "d?id="
u.HotlinkUrl = configuration.Get().ServerUrl + "hotlink/"
u.GenericHotlinkUrl = configuration.Get().ServerUrl + "downloadFile?id="
u.Items = result
u.ApiKeys = resultApi
u.TimeNow = time.Now().Unix()
u.IsAdminView = true
u.IsMainView = isMainView
u.ActiveView = view
u.MaxFileSize = configuration.Get().MaxFileSizeMB
u.IsLogoutAvailable = authentication.IsLogoutAvailable()
defaultValues := database.GetUploadDefaults()
Expand Down
4 changes: 2 additions & 2 deletions internal/webserver/web/templates/html_admin.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
</div>
</div>

<script src="./js/admin.js?v=13"></script>
<script src="./js/admin.js?v={{ template "js_admin_version"}}"></script>
<script>
Dropzone.options.uploaddropzone["maxFilesize"] = {{ .MaxFileSize }};
Dropzone.options.uploaddropzone["timeout"] = 1200000;
Expand Down Expand Up @@ -127,7 +127,7 @@

{{ if .EndToEndEncryption }}
<script src="./js/wasm_exec.js"></script>
<script src="./js/end2end_admin.js?v=1"></script>
<script src="./js/end2end_admin.js?v={{ template "js_e2eversion"}}"></script>
<script>
checkIfE2EKeyIsSet();
</script>
Expand Down
2 changes: 1 addition & 1 deletion internal/webserver/web/templates/html_api.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</div>
</div>
</div>
<script src="./js/admin.js?v=4"></script>
<script src="./js/admin.js?v={{ template "js_admin_version"}}"></script>
<script>
document.querySelectorAll(".apiname").forEach(function(node) {
node.onclick = function() {
Expand Down
7 changes: 4 additions & 3 deletions internal/webserver/web/templates/html_header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<link href="./assets/dist/css/datatables.min.css" rel="stylesheet" />
<script src="./assets/dist/js/jquery.min.js"></script>
<script src="./assets/dist/js/bootstrap.bundle.min.js"></script>
<script src="./assets/dist/js/dropzone.min.js?v=3"></script>
<script src="./assets/dist/js/dropzone.min.js?v={{ template "js_dropzone_version"}}"></script>
<script src="./assets/dist/js/clipboard.min.js"></script>
<script src="./assets/dist/js/datatables.min.js"></script>
<style>
Expand Down Expand Up @@ -46,8 +46,9 @@
<div style="max-width: 80em; margin: 0 auto;" class="inner">
<h1>{{template "app_name"}}</h1>
<nav class="nav nav-masthead justify-content-center">
<a class="nav-link {{ if .IsMainView }}active{{ end }}" href="./admin">Upload</a>
<a class="nav-link {{ if not .IsMainView }}active{{ end }}" href="./apiKeys">API</a>
<a class="nav-link {{ if eq .ActiveView 0}}active{{ end }}" href="./admin">Upload</a>
<a class="nav-link {{ if eq .ActiveView 1 }}active{{ end }}" href="./logs">Logs</a>
<a class="nav-link {{ if eq .ActiveView 2 }}active{{ end }}" href="./apiKeys">API</a>
{{ if .IsLogoutAvailable }}<a class="nav-link" href="./logout">Logout</a>{{ end }}
</nav>
</div>
Expand Down
25 changes: 25 additions & 0 deletions internal/webserver/web/templates/html_logs.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{ define "logs" }}
{{ template "header" . }}
<div class="row">
<div class="col">
<div id="container" class="card" style="width: 80%">
<div class="card-body">
<h3 class="card-title">Log File</h3>
<br>

<textarea class="form-control" id="logviewer" rows="20" readonly>
{{ .Logs }}</textarea>
</div>
</div>
</div>
</div>
<script>
let textarea = document.getElementById('logviewer');
textarea.scrollTop = textarea.scrollHeight;

window.setTimeout( function() {
window.location.reload();
}, 30000);
</script>
{{ template "footer" }}
{{ end }}
7 changes: 7 additions & 0 deletions internal/webserver/web/templates/string_constants.tmpl
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
// Change these for rebranding
{{define "app_name"}}Gokapi{{end}}
{{define "version"}}1.6.1{{end}}

// Specifies the version of JS files, so that the browser doesn't
// use a cached version, if the file has been updated
{{define "js_admin_version"}}13{{end}}
{{define "js_dropzone_version"}}3{{end}}
{{define "js_e2eversion"}}1{{end}}

0 comments on commit a313d26

Please sign in to comment.