mirror of
https://github.com/go-i2p/goSam.git
synced 2025-07-16 18:03:12 -04:00
Make sendMessage() validate replies
This commit is contained in:
22
auth.go
22
auth.go
@ -1,47 +1,33 @@
|
|||||||
package gosam
|
package gosam
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// SetupAuth sends the AUTH ENABLE command and immediately sets up a new Username and
|
// SetupAuth sends the AUTH ENABLE command and immediately sets up a new Username and
|
||||||
// Password from the arguments
|
// Password from the arguments
|
||||||
func (c *Client) SetupAuth(user, password string) error {
|
func (c *Client) SetupAuth(user, password string) error {
|
||||||
r, err := c.sendCmd("AUTH ENABLE\n")
|
_, err := c.sendCmd("AUTH ENABLE\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if r.Topic != "AUTH" {
|
_, err = c.sendCmd("AUTH %s %s\n", user, password)
|
||||||
return fmt.Errorf("SetupAuth Unknown Reply: %+v\n", r)
|
|
||||||
}
|
|
||||||
r, err = c.sendCmd("AUTH %s %s\n", user, password)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if r.Topic != "AUTH" {
|
|
||||||
return fmt.Errorf("SetupAuth Unknown Reply: %+v\n", r)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TeardownAuth sends the AUTH DISABLE command but does not remove the Username and
|
// TeardownAuth sends the AUTH DISABLE command but does not remove the Username and
|
||||||
// Password from the client PasswordManager
|
// Password from the client PasswordManager
|
||||||
func (c *Client) TeardownAuth() error {
|
func (c *Client) TeardownAuth() error {
|
||||||
r, err := c.sendCmd("AUTH DISABLE\n")
|
_, err := c.sendCmd("AUTH DISABLE\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if r.Topic != "AUTH" {
|
|
||||||
return fmt.Errorf("TeardownAuth Unknown Reply: %+v\n", r)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) RemoveAuthUser(user string) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if r.Topic != "AUTH" {
|
|
||||||
return fmt.Errorf("RemoveAuthUser Unknown Reply: %+v\n", r)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
38
client.go
38
client.go
@ -226,11 +226,7 @@ func (c *Client) hello() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Topic != "HELLO" {
|
if !r.IsOk() {
|
||||||
return fmt.Errorf("Client Hello Unknown Reply: %+v\n", r)
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Pairs["RESULT"] != "OK" {
|
|
||||||
return fmt.Errorf("Handshake did not succeed\nReply:%+v\n", r)
|
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 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
|
// Close the underlying socket to SAM
|
||||||
|
3
dest.go
3
dest.go
@ -46,9 +46,6 @@ func (c *Client) NewDestination(kind ...string) (string, string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
if r.Topic != "DEST" {
|
|
||||||
return "", "", fmt.Errorf("NewDestination Unknown Reply: %+v\n", r)
|
|
||||||
}
|
|
||||||
return r.Pairs["PRIV"], r.Pairs["PUB"], nil
|
return r.Pairs["PRIV"], r.Pairs["PUB"], nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
10
naming.go
10
naming.go
@ -15,14 +15,8 @@ func (c *Client) Lookup(name string) (string, error) {
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move check into sendCmd()
|
if !r.IsOk() {
|
||||||
if r.Topic != "NAMING" || r.Type != "REPLY" {
|
return "", ReplyError{r.GetResult(), r}
|
||||||
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.Pairs["NAME"] != name {
|
if r.Pairs["NAME"] != name {
|
||||||
|
@ -38,6 +38,21 @@ type Reply struct {
|
|||||||
Pairs map[string]string
|
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) {
|
func parseReply(line string) (*Reply, error) {
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
parts := strings.Split(line, " ")
|
parts := strings.Split(line, " ")
|
||||||
|
10
sessions.go
10
sessions.go
@ -1,7 +1,7 @@
|
|||||||
package gosam
|
package gosam
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
// "math"
|
// "math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
@ -33,13 +33,7 @@ func (c *Client) CreateSession(style, dest string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move check into sendCmd()
|
if !r.IsOk() {
|
||||||
if r.Topic != "SESSION" || r.Type != "STATUS" {
|
|
||||||
return "", fmt.Errorf("Session Unknown Reply: %+v\n", r)
|
|
||||||
}
|
|
||||||
|
|
||||||
result := r.Pairs["RESULT"]
|
|
||||||
if result != "OK" {
|
|
||||||
return "", ReplyError{ResultKeyNotFound, r}
|
return "", ReplyError{ResultKeyNotFound, r}
|
||||||
}
|
}
|
||||||
c.destination = r.Pairs["DESTINATION"]
|
c.destination = r.Pairs["DESTINATION"]
|
||||||
|
24
stream.go
24
stream.go
@ -1,9 +1,5 @@
|
|||||||
package gosam
|
package gosam
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// StreamConnect asks SAM for a TCP-Like connection to dest, has to be called on a new Client
|
// 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 {
|
func (c *Client) StreamConnect(dest string) error {
|
||||||
if dest == "" {
|
if dest == "" {
|
||||||
@ -14,14 +10,8 @@ func (c *Client) StreamConnect(dest string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move check into sendCmd()
|
if !r.IsOk() {
|
||||||
if r.Topic != "STREAM" || r.Type != "STATUS" {
|
return ReplyError{r.GetResult(), r}
|
||||||
return fmt.Errorf("Stream Connect Unknown Reply: %+v\n", r)
|
|
||||||
}
|
|
||||||
|
|
||||||
result := r.Pairs["RESULT"]
|
|
||||||
if result != "OK" {
|
|
||||||
return ReplyError{result, r}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -34,14 +24,8 @@ func (c *Client) StreamAccept() (*Reply, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move check into sendCmd()
|
if !r.IsOk() {
|
||||||
if r.Topic != "STREAM" || r.Type != "STATUS" {
|
return nil, ReplyError{r.GetResult(), r}
|
||||||
return nil, fmt.Errorf("Stream Accept Unknown Reply: %+v\n", r)
|
|
||||||
}
|
|
||||||
|
|
||||||
result := r.Pairs["RESULT"]
|
|
||||||
if result != "OK" {
|
|
||||||
return nil, ReplyError{result, r}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return r, nil
|
return r, nil
|
||||||
|
Reference in New Issue
Block a user