Skip to content

Commit

Permalink
Merge pull request #257 from inada-s/remove-tempban-noban
Browse files Browse the repository at this point in the history
remove tempban impl
  • Loading branch information
inada-s authored Jul 14, 2023
2 parents 85591f4 + 04b50bd commit 18c2b7d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 148 deletions.
4 changes: 2 additions & 2 deletions build_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export GDXSV_BATTLE_ADDR="localhost:9877"
export GDXSV_GCP_PROJECT_ID=""
export GDXSV_GCP_KEY_PATH=""
export GDXSV_MCSFUNC_URL=""
export GDXSV_WEBHOOK_URL=""

exec ./bin/gdxsv -v=3 -noban -pprof=1 lbs
# exec ./bin/gdxsv -v=0 -noban -pprof=3 lbs
exec ./bin/gdxsv -v=3 -pprof=1 lbs
121 changes: 1 addition & 120 deletions gdxsv/lbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ type Lbs struct {
lobbies map[string]map[uint16]*LbsLobby
chEvent chan interface{}
chQuit chan interface{}

noBan bool
noTempBan bool
reload bool
banChecked map[string]bool
bannedIPs map[string]time.Time
reload bool
}

func NewLbs() *Lbs {
Expand All @@ -60,9 +55,6 @@ func NewLbs() *Lbs {
lobbies: make(map[string]map[uint16]*LbsLobby),
chEvent: make(chan interface{}, 64),
chQuit: make(chan interface{}),

banChecked: make(map[string]bool),
bannedIPs: make(map[string]time.Time),
}

for _, pf := range []string{PlatformConsole, PlatformEmuX8664} {
Expand All @@ -79,14 +71,6 @@ func NewLbs() *Lbs {
return app
}

func (lbs *Lbs) NoBan() {
lbs.noBan = true
}

func (lbs *Lbs) NoTempBan() {
lbs.noTempBan = true
}

func (lbs *Lbs) IsBannedEndpoint(p *LbsPeer) bool {
banned, err := getDB().IsBannedEndpoint(p.IP(), p.PlatformInfo["machine_id"])
if err == sql.ErrNoRows {
Expand All @@ -96,10 +80,6 @@ func (lbs *Lbs) IsBannedEndpoint(p *LbsPeer) bool {
logger.Warn("GetBan returned err", zap.Error(err))
return false
}
if banned && lbs.noBan {
logger.Warn("passed banned user", zap.String("ip", p.IP()), zap.String("machine_id", p.PlatformInfo["machine_id"]))
return false
}
return banned
}

Expand All @@ -112,57 +92,9 @@ func (lbs *Lbs) IsBannedAccount(loginKey string) bool {
logger.Warn("IsBannedAccount returned err", zap.Error(err))
return false
}
if banned && lbs.noBan {
logger.Warn("passed banned user", zap.String("login_key", loginKey))
return false
}
return banned
}

func (lbs *Lbs) IsTempBan(p *LbsPeer) bool {
if t, ok := p.app.bannedIPs[p.IP()]; ok && time.Since(t).Minutes() <= 10 {
if lbs.noTempBan {
logger.Warn("passed temp banned user", zap.String("user_id", p.UserID), zap.String("name", p.Name))
return false
}
return true
}
return false
}

func (lbs *Lbs) TempBan(userID string) {
user, err := getDB().GetUser(userID)
if err != nil {
logger.Warn("failed to get banned user", zap.String("user_id", userID), zap.Error(err))
return
}

account, err := getDB().GetAccountByLoginKey(user.LoginKey)
if err != nil {
logger.Warn("failed to get banned user account", zap.String("user_id", userID), zap.Error(err))
return
}

if account.LastLoginIP == "" {
logger.Warn("last login ip is empty", zap.String("user_id", userID))
return
}

logger.Info("temporary ip banned",
zap.String("ip_addr", account.LastLoginIP),
zap.String("user_id", userID),
zap.String("name", user.Name))

lbs.bannedIPs[account.LastLoginIP] = time.Now()

for _, p := range lbs.userPeers {
if p.IP() == account.LastLoginIP {
p.SendMessage(NewServerNotice(lbsShutDown).Writer().
WriteString("<LF=5><BODY><CENTER>TEMPORARY BANNED<END>").Msg())
}
}
}

func (lbs *Lbs) GetLobby(platform, disk string, lobbyID uint16) *LbsLobby {
lobbies, ok := lbs.lobbies[lobbyKey(platform, disk)]
if !ok {
Expand Down Expand Up @@ -436,57 +368,6 @@ func (lbs *Lbs) eventLoop() {
}
}

// temp ban check
for _, g := range sharedData.GetMcsGames() {
if g.State != McsGameStateClosed {
continue
}

if lbs.banChecked[g.BattleCode] {
continue
}

lbs.banChecked[g.BattleCode] = true

if g.McsAddr == McsAddrP2PGame {
// temp ban is currently disabled on p2p game
logger.Info("p2p game ignored")
continue
}

var mcsUsers []*McsUser
stateCount := map[int]int{}
for _, u := range sharedData.GetMcsUsers() {
if g.BattleCode == u.BattleCode {
mcsUsers = append(mcsUsers, u)
stateCount[u.State]++
}
}

if len(mcsUsers) < 4 {
continue
}

// All players joined the game except for one player.
if stateCount[McsUserStateCreated] == 1 {
for _, u := range mcsUsers {
if u.State == McsUserStateCreated {
lbs.TempBan(u.UserID)
}
}
}

// All players joined the game, player disconnected during the game.
if stateCount[McsUserStateCreated] == 0 {
for _, u := range mcsUsers {
switch u.CloseReason {
case "cl_hard_reset", "cl_soft_reset", "cl_hard_quit":
lbs.TempBan(u.UserID)
}
}
}
}

sharedData.RemoveStaleData()

reload := lbs.reload
Expand Down
6 changes: 0 additions & 6 deletions gdxsv/lbs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,6 @@ var _ = register(lbsLoginType, func(p *LbsPeer, m *LbsMessage) {
// 2 : 「登録情報変更」
// 3 : The user come back from battle server

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

if p.app.IsBannedEndpoint(p) {
p.SendMessage(NewServerNotice(lbsShutDown).Writer().
WriteString("<LF=5><BODY><CENTER>YOU ARE BANNED<END>").Msg())
Expand Down
26 changes: 7 additions & 19 deletions gdxsv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ var (
var (
conf Config

cpu = flag.Int("cpu", 2, "setting GOMAXPROCS")
pprof = flag.Int("pprof", 1, "0: disable pprof, 1: enable http pprof, 2: enable blocking profile")
cprof = flag.Int("cprof", 0, "0: disable cloud profiler, 1: enable cloud profiler, 2: also enable mtx profile")
prodlog = flag.Bool("prodlog", false, "use production logging mode")
loglevel = flag.Int("v", 2, "logging level. 1:error, 2:info, 3:debug")
mcsdelay = flag.Duration("mcsdelay", 0, "mcs room delay for network lag emulation")
noban = flag.Bool("noban", false, "disable user ban")
notempban = flag.Bool("notempban", false, "disable temporary ban")
cpu = flag.Int("cpu", 2, "setting GOMAXPROCS")
pprof = flag.Int("pprof", 1, "0: disable pprof, 1: enable http pprof, 2: enable blocking profile")
cprof = flag.Int("cprof", 0, "0: disable cloud profiler, 1: enable cloud profiler, 2: also enable mtx profile")
prodlog = flag.Bool("prodlog", false, "use production logging mode")
loglevel = flag.Int("v", 2, "logging level. 1:error, 2:info, 3:debug")
mcsdelay = flag.Duration("mcsdelay", 0, "mcs room delay for network lag emulation")
)

var (
Expand All @@ -65,7 +63,6 @@ type Config struct {

GCPProjectID string `env:"GDXSV_GCP_PROJECT_ID" envDefault:""`
GCPKeyPath string `env:"GDXSV_GCP_KEY_PATH" envDefault:""`
McsFuncKey string `env:"GDXSV_MCSFUNC_KEY" envDefault:""` // deprecated
McsFuncURL string `env:"GDXSV_MCSFUNC_URL" envDefault:""`
WebhookUrl string `env:"GDXSV_WEBHOOK_URL" envDefault:""`

Expand Down Expand Up @@ -111,10 +108,6 @@ func loadConfig() {
logger.Fatal("config load failed", zap.Error(err))
}

if c.GCPKeyPath == "" && c.McsFuncKey != "" {
c.GCPKeyPath = c.McsFuncKey
}

logger.Info("config loaded", zap.Any("config", c))
conf = c
}
Expand Down Expand Up @@ -189,16 +182,11 @@ func mainLbs() {
defer stop()

lbs := NewLbs()
if *noban {
lbs.NoBan()
}
if *notempban {
lbs.NoTempBan()
}
go lbs.ListenAndServe(stripHost(conf.LobbyAddr))

mcs := NewMcs(*mcsdelay)
go mcs.ListenAndServe(stripHost(conf.BattleAddr))

if conf.LobbyHttpAddr != "" {
lbs.RegisterHTTPHandlers()
go func() {
Expand Down
2 changes: 1 addition & 1 deletion infra/lbs/launch-lbs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ if [[ ! -d $LATEST_TAG/bin ]]; then
"$GDXSV_BIN" -prodlog migratedb >> /var/log/gdxsv-lbs.log 2>&1
fi

exec "$GDXSV_BIN" -notempban -prodlog -cprof=2 lbs >> /var/log/gdxsv-lbs.log 2>&1
exec "$GDXSV_BIN" -prodlog -cprof=2 lbs >> /var/log/gdxsv-lbs.log 2>&1

0 comments on commit 18c2b7d

Please sign in to comment.