Skip to content

Commit

Permalink
ui audit log
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Sep 14, 2023
1 parent 41bd3f1 commit 539019e
Show file tree
Hide file tree
Showing 19 changed files with 334 additions and 37 deletions.
30 changes: 30 additions & 0 deletions app/geoip.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package app

import (
"mtui/types"
"net"
"net/http"
"path"
"strings"

"github.com/oschwald/geoip2-golang"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -66,3 +69,30 @@ func (r *GeoipResolver) Resolve(ipstr string) *GeoipResult {

return result
}

func (a *App) ResolveLogGeoIP(l *types.Log, r *http.Request) {
if r == nil {
// nothing to work with
return
}

fwdfor := r.Header.Get("X-Forwarded-For")
if fwdfor != "" {
// behind reverse proxy
parts := strings.Split(fwdfor, ",")
l.IPAddress = &parts[0]
} else {
// direct access
parts := strings.Split(r.RemoteAddr, ":")
l.IPAddress = &parts[0]
}

if a.GeoipResolver != nil && l.IPAddress != nil {
geoip := a.GeoipResolver.Resolve(*l.IPAddress)
if geoip != nil {
l.GeoCity = &geoip.City
l.GeoCountry = &geoip.ISOCountry
l.GeoASN = &geoip.ASN
}
}
}
2 changes: 1 addition & 1 deletion events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func Setup(app *app.App) error {
go metricLoop(app, app.Bridge.AddHandler(command.COMMAND_METRICS))
go statsLoop(app.WSEvents, app.Bridge.AddHandler(command.COMMAND_STATS))
go logLoop(app, app.GeoipResolver, app.Bridge.AddHandler(command.COMMAND_LOG))
go logLoop(app, app.Bridge.AddHandler(command.COMMAND_LOG))

return nil
}
12 changes: 2 additions & 10 deletions events/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"mtui/types"
)

func logLoop(a *app.App, geoipresolver *app.GeoipResolver, ch chan *bridge.CommandResponse) {
func logLoop(a *app.App, ch chan *bridge.CommandResponse) {
for cmd := range ch {
log := &types.Log{}
err := json.Unmarshal(cmd.Data, log)
Expand All @@ -17,15 +17,7 @@ func logLoop(a *app.App, geoipresolver *app.GeoipResolver, ch chan *bridge.Comma
return
}

if log.IPAddress != nil {
geoip := geoipresolver.Resolve(*log.IPAddress)
if geoip != nil {
log.GeoCity = &geoip.City
log.GeoCountry = &geoip.ISOCountry
log.GeoASN = &geoip.ASN
}
}

a.ResolveLogGeoIP(log, nil)
err = a.Repos.LogRepository.Insert(log)
if err != nil {
fmt.Printf("DB error: %s\n", err.Error())
Expand Down
18 changes: 17 additions & 1 deletion public/js/components/pages/Log.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,21 @@ export default {
"logfile-verbose"
];
} else {
return [];
return [
"login",
"signup",
"skin",
"settings",
"filebrowser",
"maintenance",
"lua",
"chatcommand",
"feature",
"system",
"engine",
"mods",
"oauth"
];
}
}
},
Expand Down Expand Up @@ -113,6 +127,7 @@ export default {
format_time: format_time
},
watch: {
"category": "update_count",
"event": "update_count",
"from": "update_count",
"to": "update_count",
Expand All @@ -134,6 +149,7 @@ export default {
<label>Category</label>
<select class="form-control" v-model="category">
<option value="minetest">Minetest</option>
<option value="ui">UI</option>
</select>
</div>
<div class="col-md-2">
Expand Down
13 changes: 13 additions & 0 deletions web/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package web

import (
"mtui/app"
"mtui/types"
"mtui/types/command"
"net/http"
)

type Api struct {
Expand All @@ -22,5 +24,16 @@ func (api *Api) Setup() error {
go api.TanSetListener(api.app.Bridge.AddHandler(command.COMMAND_TAN_REMOVE))
go api.StatsEventListener(api.app.Bridge.AddHandler(command.COMMAND_STATS))

api.CreateUILogEntry(&types.Log{
Event: "system",
Message: "mtui started",
}, nil)

return nil
}

func (api *Api) CreateUILogEntry(l *types.Log, r *http.Request) {
l.Category = types.CategoryUI
api.app.ResolveLogGeoIP(l, r)
api.app.Repos.LogRepository.Insert(l)
}
9 changes: 9 additions & 0 deletions web/chat_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web

import (
"encoding/json"
"fmt"
"mtui/types"
"mtui/types/command"
"net/http"
Expand All @@ -26,4 +27,12 @@ func (a *Api) ExecuteChatcommand(w http.ResponseWriter, r *http.Request, claims
resp := &command.ExecuteChatCommandResponse{}
err = a.app.Bridge.ExecuteCommand(command.COMMAND_CHATCMD, req, resp, time.Second*5)
Send(w, resp, err)

// create log entry
a.CreateUILogEntry(&types.Log{
Username: claims.Username,
Event: "chatcommand",
Message: fmt.Sprintf("User '%s' executes the chatcommand: '%s'", claims.Username, req.Command),
}, r)

}
9 changes: 9 additions & 0 deletions web/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web

import (
"bytes"
"fmt"
"io"
"mtui/types"
"net/http"
Expand Down Expand Up @@ -53,4 +54,12 @@ func (a *Api) SetConfig(w http.ResponseWriter, r *http.Request, c *types.Claims)
SendError(w, 500, err.Error())
return
}

// create log entry
a.CreateUILogEntry(&types.Log{
Username: c.Username,
Event: "lua",
Message: fmt.Sprintf("User '%s' sets the config '%s' to '%s'", c.Username, key, e.Value),
}, r)

}
12 changes: 12 additions & 0 deletions web/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web

import (
"encoding/json"
"fmt"
"mtui/app"
"mtui/types"
"net/http"
Expand Down Expand Up @@ -44,4 +45,15 @@ func (a *Api) SetFeature(w http.ResponseWriter, r *http.Request, claims *types.C

err = a.app.Repos.FeatureRepository.Set(feature)
Send(w, feature, err)

// create log entry
action := "disables"
if feature.Enabled {
action = "enables"
}
a.CreateUILogEntry(&types.Log{
Username: claims.Username,
Event: "feature",
Message: fmt.Sprintf("User '%s' %s the feature '%s'", claims.Username, action, feature.Name),
}, r)
}
Loading

0 comments on commit 539019e

Please sign in to comment.