Skip to content

Commit

Permalink
add m_ban
Browse files Browse the repository at this point in the history
  • Loading branch information
inada-s committed Jan 14, 2021
1 parent 28783cf commit 2b7d645
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
11 changes: 10 additions & 1 deletion gdxsv/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type DBAccount struct {
Created time.Time `db:"created" json:"created,omitempty"`
CreatedIP string `db:"created_ip" json:"created_ip,omitempty"`
LastLogin time.Time `db:"last_login" json:"last_login,omitempty"`
LastLoginIP string `db:"last_login_ip" json:"last_login,omitempty"`
LastLoginIP string `db:"last_login_ip" json:"last_login_ip,omitempty"`
System byte `db:"system" json:"system,omitempty"`
}

Expand Down Expand Up @@ -111,6 +111,12 @@ type RankingRecord struct {
DBUser
}

type UserBan struct {
Key string `db:"key" json:"key,omitempty"`
Until time.Time `db:"until" json:"until,omitempty"`
Created time.Time `db:"created" json:"created,omitempty"`
}

// DB is an interface of database operation.
type DB interface {
// Init initializes the database.
Expand Down Expand Up @@ -176,4 +182,7 @@ type DB interface {

// GetString returns a string that corresponds to the key.
GetString(key string) (value string, err error)

// GetBan returns user's ban information.
GetBan(key string) (ban UserBan, err error)
}
12 changes: 12 additions & 0 deletions gdxsv/db_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ CREATE TABLE IF NOT EXISTS strings (
value text,
PRIMARY KEY (key)
);
CREATE TABLE IF NOT EXISTS m_ban (
key text,
until timestamp,
created timestamp,
PRIMARY KEY (key)
);
`

const indexes = `
Expand Down Expand Up @@ -525,3 +531,9 @@ func (db SQLiteDB) GetString(key string) (string, error) {
err := db.QueryRowx(`SELECT value FROM strings WHERE key = ? LIMIT 1`, key).Scan(&value)
return value, err
}

func (db SQLiteDB) GetBan(key string) (UserBan, error) {
var userBan UserBan
err := db.QueryRowx(`SELECT key, until, created FROM m_ban WHERE key = ? LIMIT 1`, key).StructScan(&userBan)
return userBan, err
}
22 changes: 21 additions & 1 deletion gdxsv/lbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"go.uber.org/zap"
Expand Down Expand Up @@ -81,10 +82,29 @@ func (lbs *Lbs) NoBan() {
lbs.noban = true
}

func (lbs *Lbs) IsBan(p *LbsPeer) bool {
ban, err := getDB().GetBan(p.IP())
if err == sql.ErrNoRows {
return false
}
if err != nil {
logger.Warn("GetBan returned err", zap.Error(err))
return false
}
if 0 < time.Until(ban.Until) {
if lbs.noban {
logger.Warn("passed banned user", zap.String("user_id", p.UserID), zap.String("name", p.Name))
return false
}
return true
}
return false
}

func (lbs *Lbs) IsTempBan(p *LbsPeer) bool {
if t, ok := p.app.bannedIPs[p.IP()]; ok && time.Since(t).Minutes() <= 10 {
if lbs.noban {
logger.Warn("passed banned user", zap.String("user_id", p.UserID))
logger.Warn("passed temp banned user", zap.String("user_id", p.UserID), zap.String("name", p.Name))
return false
}
return true
Expand Down
6 changes: 6 additions & 0 deletions gdxsv/lbs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ var _ = register(lbsLoginType, func(p *LbsPeer, m *LbsMessage) {
return
}

if p.app.IsBan(p) {
p.SendMessage(NewServerNotice(lbsShutDown).Writer().
WriteString("<LF=5><BODY><CENTER>YOU ARE BANNED<END>").Msg())
return
}

switch loginType {
case 2:
if p.PlatformInfo["flycast"] != "" {
Expand Down

0 comments on commit 2b7d645

Please sign in to comment.