Check player name (#74)

master
HimbeerserverDE 2021-04-06 15:53:08 +02:00
parent 1f293cc5ca
commit c372e1a8d4
No known key found for this signature in database
GPG Key ID: 1A651504791E6A8B
2 changed files with 31 additions and 0 deletions

26
init.go
View File

@ -8,6 +8,7 @@ import (
"io"
"log"
"net"
"regexp"
"strings"
"time"
@ -284,6 +285,31 @@ func Init(c, c2 *Conn, ignMedia, noAccessDenied bool, fin chan *Conn) {
return
}
msg := c2.Addr().String() + " tried to connect with "
if len(c2.Username()) == 0 {
c2.CloseWith(AccessDeniedWrongName, "", false)
fin <- c
log.Print(msg + "empty name")
return
} else if len(c2.Username()) > MaxPlayerNameLength {
c2.CloseWith(AccessDeniedWrongCharsInName, "", false)
fin <- c
log.Print(msg + "too long name")
return
}
ok, err := regexp.MatchString(PlayerNameChars, c2.Username())
if err != nil {
log.Print(err)
}
if !ok || err != nil {
c2.CloseWith(AccessDeniedWrongCharsInName, "", false)
fin <- c
log.Print(c2.Addr().String() + " tried to connect with invalid name")
return
}
// Send HELLO
data := make([]byte, 13+len(c2.Username()))
data[0] = uint8(0x00)

View File

@ -2,6 +2,11 @@ package main
import "sync"
const (
MaxPlayerNameLength = 20
PlayerNameChars = "[a-zA-Z0-9-_]"
)
var onlinePlayers map[string]bool
var onlinePlayerMu sync.RWMutex