Commit 48be6f05 authored by ceddral's avatar ceddral
Browse files

player: restrict name length

parent fc037ba8
......@@ -11,8 +11,13 @@ import (
//"errors"
)
const (
nameMaxLen = 16
nameWhitelistChars = "a-zA-Z0-9_"
)
var (
nameWhitelist = regexp.MustCompile(".*[^a-zA-Z0-9_].*") // replace all characters if any 'evil' one is present
nameWhitelist = regexp.MustCompile("[^"+nameWhitelistChars+"]")
)
type Context interface {
......@@ -52,11 +57,19 @@ func NewPlayer(conn net.Conn) (p *Player) {
if err != nil {
log.Println(err)
}
if len(nameWhitelist.ReplaceAllString(p.name, "")) == 0 {
reason := ""
if len(p.name) == 0 {
reason = "Must set username. `telnet -l <name>`\n"
} else if len(p.name) > nameMaxLen {
reason = fmt.Sprintf("Username must contain at most %d characters.\n", nameMaxLen)
} else if len(nameWhitelist.ReplaceAllString(p.name, "")) != len(p.name) {
reason = fmt.Sprintf("Username must consist only of %s\n", nameWhitelistChars)
}
if reason != "" {
p.inputKill <- struct{}{}
close(p.inputKill)
close(p.inputEvent)
conn.Write([]byte(fmt.Sprintf("Invalid username >%s<\n", p.name)))
conn.Write([]byte(reason))
conn.Close()
return nil
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment