Make sendMessage() validate replies

This commit is contained in:
urgentquest
2025-03-07 01:35:21 +00:00
parent dedd16f352
commit c910edae6b
7 changed files with 59 additions and 63 deletions

22
auth.go
View File

@ -1,47 +1,33 @@
package gosam
import "fmt"
// SetupAuth sends the AUTH ENABLE command and immediately sets up a new Username and
// Password from the arguments
func (c *Client) SetupAuth(user, password string) error {
r, err := c.sendCmd("AUTH ENABLE\n")
_, err := c.sendCmd("AUTH ENABLE\n")
if err != nil {
return err
}
if r.Topic != "AUTH" {
return fmt.Errorf("SetupAuth Unknown Reply: %+v\n", r)
}
r, err = c.sendCmd("AUTH %s %s\n", user, password)
_, err = c.sendCmd("AUTH %s %s\n", user, password)
if err != nil {
return err
}
if r.Topic != "AUTH" {
return fmt.Errorf("SetupAuth Unknown Reply: %+v\n", r)
}
return nil
}
// TeardownAuth sends the AUTH DISABLE command but does not remove the Username and
// Password from the client PasswordManager
func (c *Client) TeardownAuth() error {
r, err := c.sendCmd("AUTH DISABLE\n")
_, err := c.sendCmd("AUTH DISABLE\n")
if err != nil {
return err
}
if r.Topic != "AUTH" {
return fmt.Errorf("TeardownAuth Unknown Reply: %+v\n", r)
}
return nil
}
func (c *Client) RemoveAuthUser(user string) error {
r, err := c.sendCmd("AUTH REMOVE %s\n", user)
_, err := c.sendCmd("AUTH REMOVE %s\n", user)
if err != nil {
return err
}
if r.Topic != "AUTH" {
return fmt.Errorf("RemoveAuthUser Unknown Reply: %+v\n", r)
}
return nil
}

View File

@ -226,11 +226,7 @@ func (c *Client) hello() error {
return err
}
if r.Topic != "HELLO" {
return fmt.Errorf("Client Hello Unknown Reply: %+v\n", r)
}
if r.Pairs["RESULT"] != "OK" {
if !r.IsOk() {
return fmt.Errorf("Handshake did not succeed\nReply:%+v\n", r)
}
@ -248,7 +244,37 @@ func (c *Client) sendCmd(str string, args ...interface{}) (*Reply, error) {
return nil, err
}
return parseReply(line)
r, err := parseReply(line)
if err != nil {
return nil, err
}
if err := c.validateResponse(str, r); err != nil {
return nil, fmt.Errorf("unrecogized reply: %+v\n%v", r, err)
}
return r, nil
}
func (c *Client) validateResponse(command string, reply *Reply) error {
expectedTypesMap := map[string]string{
"HELLO": "REPLY",
"DEST": "REPLY",
"NAMING": "REPLY",
"SESSION": "STATUS",
"STREAM": "STATUS",
}
commandTopic := strings.SplitN(command, "", 1)[0]
if commandTopic != reply.Topic {
return fmt.Errorf("unrecogized reply topic. expecting: %v, got: %v", commandTopic, reply.Topic)
}
if expectedTypesMap[commandTopic] != reply.Type {
return fmt.Errorf("unrecogized reply type. expecting: %v, got: %v", expectedTypesMap[commandTopic], reply.Type)
}
return nil
}
// Close the underlying socket to SAM

View File

@ -46,9 +46,6 @@ func (c *Client) NewDestination(kind ...string) (string, string, error) {
if err != nil {
return "", "", err
}
if r.Topic != "DEST" {
return "", "", fmt.Errorf("NewDestination Unknown Reply: %+v\n", r)
}
return r.Pairs["PRIV"], r.Pairs["PUB"], nil
}

View File

@ -15,14 +15,8 @@ func (c *Client) Lookup(name string) (string, error) {
return "", nil
}
// TODO: move check into sendCmd()
if r.Topic != "NAMING" || r.Type != "REPLY" {
return "", fmt.Errorf("Naming Unknown Reply: %s, %s\n", r.Topic, r.Type)
}
result := r.Pairs["RESULT"]
if result != "OK" {
return "", ReplyError{result, r}
if !r.IsOk() {
return "", ReplyError{r.GetResult(), r}
}
if r.Pairs["NAME"] != name {

View File

@ -38,6 +38,21 @@ type Reply struct {
Pairs map[string]string
}
func (r *Reply) IsOk() bool {
return r.Pairs["RESULT"] == ResultOk
}
func (r *Reply) GetResult() string {
result, ok := r.Pairs["RESULT"]
if !ok {
// TODO Add some debug output
return ""
}
return result
}
func parseReply(line string) (*Reply, error) {
line = strings.TrimSpace(line)
parts := strings.Split(line, " ")

View File

@ -1,7 +1,7 @@
package gosam
import (
"fmt"
// "math"
"math/rand"
"time"
@ -33,13 +33,7 @@ func (c *Client) CreateSession(style, dest string) (string, error) {
return "", err
}
// TODO: move check into sendCmd()
if r.Topic != "SESSION" || r.Type != "STATUS" {
return "", fmt.Errorf("Session Unknown Reply: %+v\n", r)
}
result := r.Pairs["RESULT"]
if result != "OK" {
if !r.IsOk() {
return "", ReplyError{ResultKeyNotFound, r}
}
c.destination = r.Pairs["DESTINATION"]

View File

@ -1,9 +1,5 @@
package gosam
import (
"fmt"
)
// StreamConnect asks SAM for a TCP-Like connection to dest, has to be called on a new Client
func (c *Client) StreamConnect(dest string) error {
if dest == "" {
@ -14,14 +10,8 @@ func (c *Client) StreamConnect(dest string) error {
return err
}
// TODO: move check into sendCmd()
if r.Topic != "STREAM" || r.Type != "STATUS" {
return fmt.Errorf("Stream Connect Unknown Reply: %+v\n", r)
}
result := r.Pairs["RESULT"]
if result != "OK" {
return ReplyError{result, r}
if !r.IsOk() {
return ReplyError{r.GetResult(), r}
}
return nil
@ -34,14 +24,8 @@ func (c *Client) StreamAccept() (*Reply, error) {
return nil, err
}
// TODO: move check into sendCmd()
if r.Topic != "STREAM" || r.Type != "STATUS" {
return nil, fmt.Errorf("Stream Accept Unknown Reply: %+v\n", r)
}
result := r.Pairs["RESULT"]
if result != "OK" {
return nil, ReplyError{result, r}
if !r.IsOk() {
return nil, ReplyError{r.GetResult(), r}
}
return r, nil