Require client name to be specified when logging in

This commit is contained in:
Trevor Slocum 2023-10-23 00:15:13 -07:00
parent 27b011cf5b
commit 07e9c3dc1e
3 changed files with 42 additions and 20 deletions

View file

@ -20,10 +20,11 @@ When sending a password to the server, replace spaces with underscores.
- This (or `loginjson`) must be the first command sent when a client connects to bgammon.
- Aliases: `l`
- `loginjson [username] [password]`
- `loginjson <client name> [username] [password]`
- Log in to bgammon and enable JSON formatted responses.
- All client applications should use the `loginjson` command to log in, as JSON
formatted responses are more easily parsed by computers.
- The name of the client must be specified.
- Aliases: `lj`
- `json <on/off>`

View file

@ -164,18 +164,36 @@ func (c *serverClient) Terminate(reason string) {
func logClientRead(msg []byte) {
msgLower := bytes.ToLower(msg)
if bytes.HasPrefix(msgLower, []byte("login ")) || bytes.HasPrefix(msgLower, []byte("l ")) || bytes.HasPrefix(msgLower, []byte("loginjson ")) || bytes.HasPrefix(msgLower, []byte("lj ")) {
loginJSON := bytes.HasPrefix(msgLower, []byte("loginjson ")) || bytes.HasPrefix(msgLower, []byte("lj "))
if bytes.HasPrefix(msgLower, []byte("login ")) || bytes.HasPrefix(msgLower, []byte("l ")) || loginJSON {
split := bytes.Split(msg, []byte(" "))
var clientName []byte
var username []byte
var password []byte
l := len(split)
if l > 1 {
username = split[1]
if loginJSON {
clientName = split[1]
} else {
username = split[1]
}
if l > 2 {
password = []byte("*******")
if loginJSON {
username = split[2]
} else {
password = []byte("*******")
}
if l > 3 {
if loginJSON {
password = []byte("*******")
}
}
}
}
log.Printf("<- %s %s %s", split[0], username, password)
if len(clientName) == 0 {
clientName = []byte("unspecified")
}
log.Printf("<- %s %s %s %s", split[0], clientName, username, password)
} else if !bytes.HasPrefix(msgLower, []byte("list")) && !bytes.HasPrefix(msgLower, []byte("ls")) && !bytes.HasPrefix(msgLower, []byte("pong")) {
log.Printf("<- %s", msg)
}

View file

@ -315,7 +315,18 @@ COMMANDS:
var username []byte
var password []byte
readUsername := func() bool {
username = params[0]
if cmd.client.json {
if len(params) > 1 {
username = params[1]
}
} else {
if len(params) > 0 {
username = params[0]
}
}
if len(bytes.TrimSpace(username)) == 0 {
username = s.randomUsername()
}
if onlyNumbers.Match(username) {
cmd.client.Terminate("Invalid username: must contain at least one non-numeric character.")
return false
@ -325,20 +336,12 @@ COMMANDS:
}
return true
}
switch len(params) {
case 0:
username = s.randomUsername()
case 1:
if !readUsername() {
s.clientsLock.Unlock()
continue
}
default:
if !readUsername() {
s.clientsLock.Unlock()
continue
}
password = bytes.ReplaceAll(bytes.Join(params[1:], []byte(" ")), []byte("_"), []byte(" "))
if !readUsername() {
s.clientsLock.Unlock()
continue
}
if len(params) > 2 {
password = bytes.ReplaceAll(bytes.Join(params[2:], []byte(" ")), []byte("_"), []byte(" "))
}
s.clientsLock.Unlock()