- Unbreak things by properly validate command topic

- Replace fmt.ErrorF() with errors.New() where no
actual formatting requested
- Hanle edge cases with no username/password in `Client.hello()`
- Make go-staticcheck happier
This commit is contained in:
urgentquest
2025-05-09 00:41:20 +00:00
parent 6f02aa69e8
commit eebe8c5cb4
5 changed files with 52 additions and 41 deletions

View File

@ -222,21 +222,31 @@ 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.IsOk() {
return fmt.Errorf("Handshake did not succeed\nReply:%+v\n", r)
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
}
@ -263,7 +273,7 @@ func (c *Client) sendCmd(str string, args ...interface{}) (*Reply, error) {
}
func validateCommand(str string) error {
topic := strings.SplitN(str, " ", 1)[0]
topic, _, _ := strings.Cut(str, " ")
for _, x := range ValidSAMCommands {
if x == topic {
return nil
@ -282,7 +292,7 @@ func (c *Client) validateReply(command string, reply *Reply) error {
"SESSION": "STATUS",
"STREAM": "STATUS",
}
commandTopic := strings.SplitN(command, " ", 1)[0]
commandTopic, _, _ := strings.Cut(command, " ")
if commandTopic != reply.Topic {
return fmt.Errorf("unrecogized reply topic. expecting: %v, got: %v", commandTopic, reply.Topic)

View File

@ -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

View File

@ -2,7 +2,7 @@ package gosam
import (
"context"
"fmt"
"errors"
"log"
"net"
"strings"
@ -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.

View File

@ -22,7 +22,7 @@ func (c *Client) Lookup(name string) (string, error) {
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")
}

View File

@ -1,6 +1,7 @@
package gosam
import (
"errors"
"fmt"
"strconv"
"strings"
@ -21,11 +22,11 @@ func SetAddr(s ...string) func(*Client) error {
c.port = split[1]
return nil
}
return fmt.Errorf("Invalid port")
return errors.New("invalid port")
}
return fmt.Errorf("Invalid port; non-number")
return errors.New("invalid port; non-number")
}
return fmt.Errorf("Invalid address; use host:port %s", split)
return fmt.Errorf("invalid address; use host:port %s", split)
} else if len(s) == 2 {
if i, err := strconv.Atoi(s[1]); err == nil {
if i < 65536 {
@ -33,11 +34,11 @@ func SetAddr(s ...string) func(*Client) error {
c.port = s[1]
return nil
}
return fmt.Errorf("Invalid port")
return errors.New("invalid port")
}
return fmt.Errorf("Invalid port; non-number")
return errors.New("invalid port; non-number")
} else {
return fmt.Errorf("Invalid address")
return errors.New("invalid address")
}
}
}
@ -50,7 +51,7 @@ func SetAddrMixed(s string, i int) func(*Client) error {
c.port = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
return errors.New("invalid port")
}
}
@ -81,10 +82,10 @@ func SetPass(s string) func(*Client) error {
func SetSAMMinVersion(i int) func(*Client) error {
return func(c *Client) error {
if i < 0 {
return fmt.Errorf("SAM version must be greater than or equal to 0")
return errors.New("SAM version must be greater than or equal to 0")
}
if i > 3 {
return fmt.Errorf("SAM version must be less than or equal to 3")
return errors.New("SAM version must be less than or equal to 3")
}
c.sammin = i
return nil
@ -94,10 +95,10 @@ func SetSAMMinVersion(i int) func(*Client) error {
func SetSAMMaxVersion(i int) func(*Client) error {
return func(c *Client) error {
if i < 0 {
return fmt.Errorf("SAM version must be greater than or equal to 0")
return errors.New("SAM version must be greater than or equal to 0")
}
if i > 3 {
return fmt.Errorf("SAM version must be less than or equal to 3")
return errors.New("SAM version must be less than or equal to 3")
}
c.sammin = i
return nil
@ -125,13 +126,13 @@ func SetPort(s string) func(*Client) error {
return func(c *Client) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid port; non-number")
return errors.New("invalid port; non-number")
}
if port < 65536 && port > -1 {
c.port = s
return nil
}
return fmt.Errorf("Invalid port")
return errors.New("invalid port")
}
}
@ -142,7 +143,7 @@ func SetPortInt(i int) func(*Client) error {
c.port = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
return errors.New("invalid port")
}
}
@ -151,13 +152,13 @@ func SetFromPort(s string) func(*Client) error {
return func(c *Client) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid port; non-number")
return errors.New("invalid port; non-number")
}
if port < 65536 && port > -1 {
c.fromport = s
return nil
}
return fmt.Errorf("Invalid port")
return errors.New("invalid port")
}
}
@ -168,7 +169,7 @@ func SetFromPortInt(i int) func(*Client) error {
c.fromport = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
return errors.New("invalid port")
}
}
@ -177,13 +178,13 @@ func SetToPort(s string) func(*Client) error {
return func(c *Client) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid port; non-number")
return errors.New("invalid port; non-number")
}
if port < 65536 && port > -1 {
c.toport = s
return nil
}
return fmt.Errorf("Invalid port")
return errors.New("invalid port")
}
}
@ -194,7 +195,7 @@ func SetToPortInt(i int) func(*Client) error {
c.fromport = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
return errors.New("invalid port")
}
}
@ -214,7 +215,7 @@ func SetInLength(u uint) func(*Client) error {
c.inLength = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
return errors.New("invalid inbound tunnel length")
}
}
@ -225,7 +226,7 @@ func SetOutLength(u uint) func(*Client) error {
c.outLength = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel length")
return errors.New("invalid outbound tunnel length")
}
}
@ -236,7 +237,7 @@ func SetInVariance(i int) func(*Client) error {
c.inVariance = i
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
return errors.New("invalid inbound tunnel length")
}
}
@ -247,7 +248,7 @@ func SetOutVariance(i int) func(*Client) error {
c.outVariance = i
return nil
}
return fmt.Errorf("Invalid outbound tunnel variance")
return errors.New("invalid outbound tunnel variance")
}
}
@ -258,7 +259,7 @@ func SetInQuantity(u uint) func(*Client) error {
c.inQuantity = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel quantity")
return errors.New("invalid inbound tunnel quantity")
}
}
@ -269,7 +270,7 @@ func SetOutQuantity(u uint) func(*Client) error {
c.outQuantity = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel quantity")
return errors.New("invalid outbound tunnel quantity")
}
}
@ -280,7 +281,7 @@ func SetInBackups(u uint) func(*Client) error {
c.inBackups = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel backup quantity")
return errors.New("invalid inbound tunnel backup quantity")
}
}
@ -291,7 +292,7 @@ func SetOutBackups(u uint) func(*Client) error {
c.outBackups = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel backup quantity")
return errors.New("invalid outbound tunnel backup quantity")
}
}
@ -335,7 +336,7 @@ func SetReduceIdleTime(u uint) func(*Client) error {
c.reduceIdleTime = u
return nil
}
return fmt.Errorf("Invalid reduce idle time %v", u)
return fmt.Errorf("invalid reduce idle time %v", u)
}
}
@ -346,7 +347,7 @@ func SetReduceIdleQuantity(u uint) func(*Client) error {
c.reduceIdleQuantity = u
return nil
}
return fmt.Errorf("Invalid reduced tunnel quantity %v", u)
return fmt.Errorf("invalid reduced tunnel quantity %v", u)
}
}
@ -365,7 +366,7 @@ func SetCloseIdleTime(u uint) func(*Client) error {
c.closeIdleTime = u
return nil
}
return fmt.Errorf("Invalid close idle time %v", u)
return fmt.Errorf("invalid close idle time %v", u)
}
}
@ -401,7 +402,7 @@ func SetSignatureType(s string) func(*Client) error {
return nil
}
}
return fmt.Errorf("Invalid signature type specified at construction time")
return errors.New("invalid signature type specified at construction time")
}
}