Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions auth.go
Original file line number Diff line number Diff line change
@@ -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
}
71 changes: 62 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ var SAMsigTypes = []string{

var ValidSAMCommands = []string{
"HELLO",
"DEST",
"NAMING",
"SESSION",
"STREAM",
}
Expand Down Expand Up @@ -220,25 +222,34 @@ func (c *Client) samaddr() string {

// send the initial handshake command and check that the reply is ok
func (c *Client) hello() error {
var r *Reply
var err error

if c.getUser() == "" {
r, err = c.sendCmd("HELLO VERSION MIN=3.%d MAX=3.%d\n", c.sammin, c.sammax)
} else if c.getUser() != "" && c.getPass() == "" {
r, err = c.sendCmd("HELLO VERSION MIN=3.%d MAX=3.%d %s\n", c.sammin, c.sammax, c.getUser())
} else {
r, err = c.sendCmd("HELLO VERSION MIN=3.%d MAX=3.%d %s %s\n", c.sammin, c.sammax, c.getUser(), c.getPass())
}

r, err := c.sendCmd("HELLO VERSION MIN=3.%d MAX=3.%d %s %s\n", c.sammin, c.sammax, c.getUser(), c.getPass())
if err != nil {
return err
}

if r.Topic != "HELLO" {
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)
if !r.IsOk() {
return fmt.Errorf("handshake did not succeed\nReply:%+v", r)
}

return nil
}

// helper to send one command and parse the reply by sam
func (c *Client) sendCmd(str string, args ...interface{}) (*Reply, error) {
func (c *Client) sendCmd(str string, args ...any) (*Reply, error) {
if err := validateCommand(str); err != nil {
return nil, err
}

if _, err := fmt.Fprintf(c.SamConn, str, args...); err != nil {
return nil, err
}
Expand All @@ -248,7 +259,49 @@ 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.validateReply(str, r); err != nil {
return nil, fmt.Errorf("unrecogized reply: %+v\n%v", r, err)
}

return r, nil
}

func validateCommand(str string) error {
topic, _, _ := strings.Cut(str, " ")
for _, x := range ValidSAMCommands {
if x == topic {
return nil
}
}

return fmt.Errorf("unsupported sam command %v", topic)

}

func (c *Client) validateReply(command string, reply *Reply) error {
expectedTypesMap := map[string]string{
"HELLO": "REPLY",
"DEST": "REPLY",
"NAMING": "REPLY",
"SESSION": "STATUS",
"STREAM": "STATUS",
}
commandTopic, _, _ := strings.Cut(command, " ")

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
Expand Down
5 changes: 1 addition & 4 deletions dest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func validateKind(kind string) (string, error) {
if kint >= 0 && kint <= 7 {
return validateKindInner(kind), nil
}
return "SIGNATURE_TYPE=7", fmt.Errorf("Invalid sigType: %s", kind)
return "SIGNATURE_TYPE=7", fmt.Errorf("invalid sigType: %s", kind)
}

// Generate a new destination and return the base64 encoded string
Expand All @@ -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

}
4 changes: 2 additions & 2 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package gosam

import (
"context"
"fmt"
"errors"
"log"
"net"
"strings"
Expand Down Expand Up @@ -66,7 +66,7 @@ func (c *Client) DialDatagramContextFree(addr string) (*DatagramConn, error) {
return nil, err
}

return nil, fmt.Errorf("Datagram support is not finished yet, come back later`")
return nil, errors.New("datagram support is not finished yet, come back later")
}

// DialStreamingContextFree is a "Dialer" for "Client-Like" Streaming connections.
Expand Down
12 changes: 3 additions & 9 deletions naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ 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 {
// somehow different on i2pd
if r.Pairs["NAME"] != "ME" {
return "", fmt.Errorf("Lookup() Replyed to another name.\nWanted:%s\nGot: %+v\n", name, r)
return "", fmt.Errorf("Lookup() Replyed to another name.\nWanted:%s\nGot: %+v", name, r)
}
fmt.Fprintln(os.Stderr, "WARNING: Lookup() Replyed to another name. assuming i2pd c++ fluke")
}
Expand Down
4 changes: 2 additions & 2 deletions naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func TestClientLookupInvalid(t *testing.T) {
if !ok {
t.Fatalf("client.Lookup() should return a ReplyError")
}
if repErr.Result != ResultKeyNotFound {
t.Errorf("client.Lookup() should throw an ResultKeyNotFound error.\nGot:%+v%s%s\n", repErr, "!=", ResultKeyNotFound)
if repErr.Result != ResultKeyNotFound && repErr.Result != ResultInvalidKey {
t.Errorf("client.Lookup() should either throw an ResultKeyNotFound(i2p) or ResultInvalidKey(i2pd) error.\nGot:%+v%s%s\n", repErr, "!=", ResultKeyNotFound)
}
if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err)
Expand Down
Loading