Skip to content

Commit

Permalink
check cached xban db on every api request / make non-existing xban.db…
Browse files Browse the repository at this point in the history
… a non-error
  • Loading branch information
BuckarooBanzay committed Dec 29, 2024
1 parent e60686e commit 9b4fbf1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 33 deletions.
19 changes: 11 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path"
"sync/atomic"

cache "github.com/Code-Hex/go-generics-cache"
"github.com/minetest-go/mtdb"
"gorm.io/gorm"
)
Expand All @@ -40,6 +41,7 @@ type App struct {
ServiceEngine *dockerservice.DockerService
ServiceMatterbridge *dockerservice.DockerService
ServiceMapserver *dockerservice.DockerService
offline_xban_cache *cache.Cache[string, *types.XBanEntry]
}

const default_world_mt_content = `
Expand All @@ -64,14 +66,15 @@ func Create(cfg *types.Config) (*App, error) {
}

app := &App{
WorldDir: cfg.WorldDir,
Bridge: bridge.New(),
WSEvents: eventbus.NewEventBus(),
Config: cfg,
Mediaserver: mediaserver.New(),
GeoipResolver: NewGeoIPResolver(path.Join(cfg.WorldDir, "mmdb"), cfg.GeoIPAPI),
Version: Version,
maintenanceMode: &atomic.Bool{},
WorldDir: cfg.WorldDir,
Bridge: bridge.New(),
WSEvents: eventbus.NewEventBus(),
Config: cfg,
Mediaserver: mediaserver.New(),
GeoipResolver: NewGeoIPResolver(path.Join(cfg.WorldDir, "mmdb"), cfg.GeoIPAPI),
Version: Version,
maintenanceMode: &atomic.Bool{},
offline_xban_cache: cache.New[string, *types.XBanEntry](),
}

if app.Version == "" {
Expand Down
15 changes: 15 additions & 0 deletions app/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"embed"
"encoding/json"
"fmt"
"mtui/db"
"mtui/types"
)
Expand Down Expand Up @@ -62,3 +63,17 @@ func PopulateFeatures(repo *db.FeatureRepository, enabled_features []string) err

return nil
}

func (app *App) IsFeatureEnabled(name string) bool {
f, err := app.Repos.FeatureRepository.GetByName(name)
if err != nil {
fmt.Printf("feature query error: %v\n", err)
return false
}

if f != nil {
return f.Enabled
} else {
return false
}
}
15 changes: 14 additions & 1 deletion app/xban.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import (
"os"
"path"
"time"

cache "github.com/Code-Hex/go-generics-cache"
)

func (app *App) GetXBanDatabase() (*types.XBanDatabase, error) {
data, err := os.ReadFile(path.Join(app.WorldDir, "xban.db"))
if err != nil {
return nil, fmt.Errorf("readfile error: %v", err)
// file not found or other file-related error
return nil, nil
}

xdb := &types.XBanDatabase{}
Expand All @@ -24,19 +27,29 @@ func (app *App) GetXBanDatabase() (*types.XBanDatabase, error) {

// returns the xban entry from the on-disk xban db
func (app *App) GetOfflineXBanEntry(playername string) (*types.XBanEntry, error) {
cached_entry, ok := app.offline_xban_cache.Get(playername)
if ok {
return cached_entry, nil
}

xdb, err := app.GetXBanDatabase()
if err != nil {
return nil, fmt.Errorf("xban db error: %v", err)
}
if xdb == nil {
return nil, nil
}

for _, e := range xdb.Entries {
if e.Names[playername] {
// entry found
app.offline_xban_cache.Set(playername, e, cache.WithExpiration(time.Minute))
return e, nil
}
}

// entry not found
app.offline_xban_cache.Set(playername, nil, cache.WithExpiration(time.Minute))
return nil, nil
}

Expand Down
12 changes: 6 additions & 6 deletions minetest.conf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
beerchat.matterbridge_token = my-token
server_name = mtui-xxx
server_announce = false
mapserver.url = http://mtui_mapserver:8080
name = mtui_dev
secure.http_mods = mtui,beerchat,mapserver
profiler.default_report_format = json
max_users = 13
profiler.load = true
mtui.url = http://ui:8080
name = mtui_dev
secure.http_mods = mtui,beerchat,mapserver
mtui.key = mykey
server_name = mtui-xxx
server_announce = false
mtui.url = http://ui:8080
beerchat.matterbridge_url = http://mtui_matterbridge:4242
beerchat.matterbridge_token = my-token
18 changes: 0 additions & 18 deletions web/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/golang-jwt/jwt/v5"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
"github.com/sirupsen/logrus"
)

type LoginRequest struct {
Expand Down Expand Up @@ -84,23 +83,6 @@ func (a *Api) GetLogin(w http.ResponseWriter, r *http.Request) {
}

func (a *Api) updateToken(w http.ResponseWriter, id int64, username string) (*types.Claims, error) {
f, err := a.app.Repos.FeatureRepository.GetByName(types.FEATURE_XBAN)
if err != nil {
return nil, fmt.Errorf("could not get feature: %v", err)
}

if f.Enabled {
// consult xban database
entry, err := a.app.GetOfflineXBanEntry(username)
if err != nil {
// just log in error-case, otherwise the login will be blocked
logrus.WithError(err).Error("could not get xban entry on login")
}
if entry != nil && entry.Banned {
return nil, fmt.Errorf("player is banned (reason: '%s')", entry.Reason)
}
}

privs, err := a.app.DBContext.Privs.GetByID(id)
if err != nil {
return nil, err
Expand Down
12 changes: 12 additions & 0 deletions web/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ func (api *Api) GetClaims(r *http.Request) (*types.Claims, error) {
return nil, errors.New("internal error")
}

if api.app.IsFeatureEnabled(types.FEATURE_XBAN) {
// query xban db
xban, err := api.app.GetOfflineXBanEntry(claims.Username)
if err != nil {
return nil, fmt.Errorf("offline xban error: %v", err)
}

if xban != nil && xban.Banned {
return nil, fmt.Errorf("banned player, reason: '%s'", xban.Reason)
}
}

return claims, nil
}

Expand Down

0 comments on commit 9b4fbf1

Please sign in to comment.