Added logging #18, set Dockerfile to Go 1.16.5, minor changes

This commit is contained in:
Marc Ole Bulling
2021-06-16 19:44:27 +02:00
parent 5068b742aa
commit 6cffccb3be
11 changed files with 151 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
FROM golang:1.16.4 AS build_base
FROM golang:1.16.5 AS build_base
## Usage:
## docker build . -t gokapi

View File

@@ -1,4 +1,4 @@
FROM golang:1.16.4
FROM golang:1.16.5
## To compile:
## cd Gokapi/build/

View File

@@ -2,4 +2,4 @@
#Called by go generate
#Sets the version number 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 "Version in web template set"

View File

@@ -9,6 +9,7 @@ import (
"Gokapi/internal/configuration/cloudconfig"
"Gokapi/internal/environment"
"Gokapi/internal/helper"
"Gokapi/internal/logging"
"Gokapi/internal/storage"
"Gokapi/internal/storage/cloudstorage/aws"
"Gokapi/internal/webserver"
@@ -44,6 +45,7 @@ func main() {
fmt.Println("Saving new files to local storage")
}
go storage.CleanUp(true)
logging.AddString("Gokapi started")
webserver.Start()
}

View File

@@ -7,6 +7,7 @@ Loading and saving of the persistent configuration
import (
"Gokapi/internal/environment"
"Gokapi/internal/helper"
log "Gokapi/internal/logging"
"Gokapi/internal/models"
"crypto/sha1"
"encoding/hex"
@@ -78,6 +79,7 @@ func Load() {
updateConfig()
serverSettings.MaxMemory = Environment.MaxMemory
helper.CreateDir(serverSettings.DataDir)
log.Init(Environment.ConfigDir)
}
// Lock locks configuration to prevent race conditions (blocking)

View File

@@ -0,0 +1,71 @@
package logging
import (
"Gokapi/internal/helper"
"Gokapi/internal/models"
"fmt"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
)
var logPath = "config/log.txt"
var mutex sync.Mutex
// Init sets the path where to write the log file to
func Init(filePath string) {
logPath = filePath + "/log.txt"
}
// AddString adds a line to the logfile including the current date. Non-Blocking
func AddString(text string) {
go writeToFile(text)
}
// 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()))
}
func writeToFile(text string) {
mutex.Lock()
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
helper.Check(err)
defer file.Close()
defer mutex.Unlock()
_, err = file.WriteString(time.Now().UTC().Format(time.RFC1123) + " " + text + "\n")
helper.Check(err)
}
func getIpAddress(r *http.Request) string {
// Get IP from X-FORWARDED-FOR header
ips := r.Header.Get("X-FORWARDED-FOR")
splitIps := strings.Split(ips, ",")
for _, ip := range splitIps {
netIP := net.ParseIP(ip)
if netIP != nil {
return ip
}
}
// Get IP from the X-REAL-IP header
ip := r.Header.Get("X-REAL-IP")
netIP := net.ParseIP(ip)
if netIP != nil {
return ip
}
// Get IP from RemoteAddr
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return "Unknown IP"
}
netIP = net.ParseIP(ip)
if netIP != nil {
return ip
}
return "Unknown IP"
}

View File

@@ -0,0 +1,66 @@
package logging
import (
"Gokapi/internal/models"
"Gokapi/internal/test"
"Gokapi/internal/test/testconfiguration"
"fmt"
"io/ioutil"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
)
func TestMain(m *testing.M) {
testconfiguration.Create(false)
exitVal := m.Run()
testconfiguration.Delete()
os.Exit(exitVal)
}
func TestGetIpAddress(t *testing.T) {
r := httptest.NewRequest("GET", "/test", nil)
test.IsEqualString(t, getIpAddress(r), "192.0.2.1")
r = httptest.NewRequest("GET", "/test", nil)
r.RemoteAddr = "127.0.0.1:1234"
test.IsEqualString(t, getIpAddress(r), "127.0.0.1")
r.RemoteAddr = "invalid"
test.IsEqualString(t, getIpAddress(r), "Unknown IP")
r.Header.Add("X-REAL-IP", "1.1.1.1")
test.IsEqualString(t, getIpAddress(r), "1.1.1.1")
r.Header.Add("X-FORWARDED-FOR", "1.1.1.2")
test.IsEqualString(t, getIpAddress(r), "1.1.1.2")
}
func TestInit(t *testing.T) {
Init("test")
test.IsEqualString(t, logPath, "test/log.txt")
}
func TestAddString(t *testing.T) {
test.FileDoesNotExist(t, "test/log.txt")
AddString("Hello")
// Need sleep, as AddString() is non-blocking
time.Sleep(500 * time.Millisecond)
test.FileExists(t, "test/log.txt")
content, _ := ioutil.ReadFile("test/log.txt")
test.IsEqualBool(t, strings.Contains(string(content), "UTC Hello"), true)
}
func TestAddDownload(t *testing.T) {
file := models.File{
Id: "testId",
Name: "testName",
}
r := httptest.NewRequest("GET", "/test", nil)
r.Header.Set("User-Agent", "testAgent")
r.Header.Add("X-REAL-IP", "1.1.1.1")
AddDownload(&file, r)
// Need sleep, as AddDownload() is non-blocking
time.Sleep(500 * time.Millisecond)
content, _ := ioutil.ReadFile("test/log.txt")
fmt.Println(string(content))
test.IsEqualBool(t, strings.Contains(string(content), "UTC Download: Filename testName, IP 1.1.1.1, ID testId, Useragent testAgent"), true)
}

View File

@@ -8,6 +8,7 @@ import (
"Gokapi/internal/configuration"
"Gokapi/internal/configuration/downloadstatus"
"Gokapi/internal/helper"
"Gokapi/internal/logging"
"Gokapi/internal/models"
"Gokapi/internal/storage/cloudstorage/aws"
"bytes"
@@ -170,6 +171,7 @@ func ServeFile(file models.File, w http.ResponseWriter, r *http.Request, forceDo
settings.Files[file.Id] = file
dataDir := settings.DataDir
configuration.Release()
logging.AddDownload(&file, r)
// If file is not stored on AWS
if file.AwsBucket == "" {

View File

@@ -90,7 +90,7 @@ func IsNil(t MockT, got error) {
func FileExists(t MockT, name string) {
t.Helper()
if !fileExists(name) {
t.Errorf("Assertion failed, file does not exist: %s, want: nil.", name)
t.Errorf("Assertion failed, file does not exist: %s, want: Exists.", name)
}
}
@@ -98,7 +98,7 @@ func FileExists(t MockT, name string) {
func FileDoesNotExist(t MockT, name string) {
t.Helper()
if fileExists(name) {
t.Errorf("Assertion failed, file exist: %s, want: nil.", name)
t.Errorf("Assertion failed, file exist: %s, want: Does not exist", name)
}
}

View File

@@ -120,9 +120,7 @@ func isValidKey(key string, modifyTime bool) bool {
return false
}
settings := configuration.GetServerSettings()
defer func() {
configuration.Release()
}()
defer configuration.Release()
savedKey, ok := settings.ApiKeys[key]
if ok && savedKey.Id != "" {
if modifyTime {

View File

@@ -24,7 +24,7 @@ func IsValidSession(w http.ResponseWriter, r *http.Request) bool {
sessionString := cookie.Value
if sessionString != "" {
settings := configuration.GetServerSettings()
defer func() { configuration.ReleaseAndSave() }()
defer configuration.ReleaseAndSave()
_, ok := (settings.Sessions)[sessionString]
if ok {
return useSession(w, sessionString, &settings.Sessions)
@@ -56,7 +56,7 @@ func CreateSession(w http.ResponseWriter, sessions *map[string]models.Session) {
if sessions == nil {
settings := configuration.GetServerSettings()
sessions = &settings.Sessions
defer func() { configuration.ReleaseAndSave() }()
defer configuration.ReleaseAndSave()
}
sessionString := helper.GenerateRandomString(60)
(*sessions)[sessionString] = models.Session{