Switch to oops, simplify error handling in common

This commit is contained in:
eyedeekay
2025-05-27 16:40:15 -04:00
parent d2f1de4094
commit aa9fe1ef62
11 changed files with 115 additions and 89 deletions

View File

@ -1,8 +1,8 @@
fmt: fmt:
find . -name '*.go' -exec gofumpt -w -s -extra {} \; find . -name '*.go' -exec gofumpt -w -s -extra {} \;
export DEBUG_I2P=warn export DEBUG_I2P=debug
export WARNFAIL_I2P=true #export WARNFAIL_I2P=true
common-test: common-test:
go test --tags nettest -v ./common/... go test --tags nettest -v ./common/...
@ -20,7 +20,7 @@ raw-test:
test-logs: test-logs:
make common-test 2> common-err.log 1> common-out.log make common-test 2> common-err.log 1> common-out.log
make datagram-test 2> datagram-err.log 1> datagram-out.log
make stream-test 2> stream-err.log 1> stream-out.log make stream-test 2> stream-err.log 1> stream-out.log
make datagram-test 2> datagram-err.log 1> datagram-out.log
make raw-test 2> raw-err.log 1> raw-out.log make raw-test 2> raw-err.log 1> raw-out.log
make primary-test 2> primary-err.log 1> primary-out.log make primary-test 2> primary-err.log 1> primary-out.log

View File

@ -3,13 +3,13 @@ package common
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"io" "io"
"net" "net"
"os" "os"
"strings" "strings"
"github.com/go-i2p/i2pkeys" "github.com/go-i2p/i2pkeys"
"github.com/samber/oops"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -93,13 +93,13 @@ func (sam *SAM) NewKeys(sigType ...string) (i2pkeys.I2PKeys, error) {
} }
if _, err := sam.Conn.Write([]byte("DEST GENERATE " + sigtmp + "\n")); err != nil { if _, err := sam.Conn.Write([]byte("DEST GENERATE " + sigtmp + "\n")); err != nil {
log.WithError(err).Error("Failed to write DEST GENERATE command") log.WithError(err).Error("Failed to write DEST GENERATE command")
return i2pkeys.I2PKeys{}, fmt.Errorf("error with writing in SAM: %w", err) return i2pkeys.I2PKeys{}, oops.Errorf("error with writing in SAM: %w", err)
} }
buf := make([]byte, 8192) buf := make([]byte, 8192)
n, err := sam.Conn.Read(buf) n, err := sam.Conn.Read(buf)
if err != nil { if err != nil {
log.WithError(err).Error("Failed to read SAM response for key generation") log.WithError(err).Error("Failed to read SAM response for key generation")
return i2pkeys.I2PKeys{}, fmt.Errorf("error with reading in SAM: %w", err) return i2pkeys.I2PKeys{}, oops.Errorf("error with reading in SAM: %w", err)
} }
s := bufio.NewScanner(bytes.NewReader(buf[:n])) s := bufio.NewScanner(bytes.NewReader(buf[:n]))
s.Split(bufio.ScanWords) s.Split(bufio.ScanWords)
@ -117,7 +117,7 @@ func (sam *SAM) NewKeys(sigType ...string) (i2pkeys.I2PKeys, error) {
priv = text[5:] priv = text[5:]
} else { } else {
log.Error("Failed to parse keys from SAM response") log.Error("Failed to parse keys from SAM response")
return i2pkeys.I2PKeys{}, fmt.Errorf("Failed to parse keys.") return i2pkeys.I2PKeys{}, oops.Errorf("Failed to parse keys.")
} }
} }
log.Debug("Successfully generated new keys") log.Debug("Successfully generated new keys")
@ -174,13 +174,13 @@ func (sam *SAM) NewGenericSessionWithSignatureAndPorts(style, id, from, to strin
if i == 15 { if i == 15 {
log.Error("Failed to write SESSION CREATE message after 15 attempts") log.Error("Failed to write SESSION CREATE message after 15 attempts")
conn.Close() conn.Close()
return nil, fmt.Errorf("writing to SAM failed") return nil, oops.Errorf("writing to SAM failed")
} }
n, err := conn.Write(scmsg[m:]) n, err := conn.Write(scmsg[m:])
if err != nil { if err != nil {
log.WithError(err).Error("Failed to write to SAM connection") log.WithError(err).Error("Failed to write to SAM connection")
conn.Close() conn.Close()
return nil, fmt.Errorf("writing to connection failed: %w", err) return nil, oops.Errorf("writing to connection failed: %w", err)
} }
m += n m += n
} }
@ -189,7 +189,7 @@ func (sam *SAM) NewGenericSessionWithSignatureAndPorts(style, id, from, to strin
if err != nil { if err != nil {
log.WithError(err).Error("Failed to read SAM response") log.WithError(err).Error("Failed to read SAM response")
conn.Close() conn.Close()
return nil, fmt.Errorf("reading from connection failed: %w", err) return nil, oops.Errorf("reading from connection failed: %w", err)
} }
text := string(buf[:n]) text := string(buf[:n])
log.WithField("response", text).Debug("Received SAM response") log.WithField("response", text).Debug("Received SAM response")
@ -197,30 +197,30 @@ func (sam *SAM) NewGenericSessionWithSignatureAndPorts(style, id, from, to strin
if keys.String() != text[len(SESSION_OK):len(text)-1] { if keys.String() != text[len(SESSION_OK):len(text)-1] {
log.Error("SAM created a tunnel with different keys than requested") log.Error("SAM created a tunnel with different keys than requested")
conn.Close() conn.Close()
return nil, fmt.Errorf("SAMv3 created a tunnel with keys other than the ones we asked it for") return nil, oops.Errorf("SAMv3 created a tunnel with keys other than the ones we asked it for")
} }
log.Debug("Successfully created new session") log.Debug("Successfully created new session")
return conn, nil //&StreamSession{id, conn, keys, nil, sync.RWMutex{}, nil}, nil return conn, nil //&StreamSession{id, conn, keys, nil, sync.RWMutex{}, nil}, nil
} else if text == SESSION_DUPLICATE_ID { } else if text == SESSION_DUPLICATE_ID {
log.Error("Duplicate tunnel name") log.Error("Duplicate tunnel name")
conn.Close() conn.Close()
return nil, fmt.Errorf("Duplicate tunnel name") return nil, oops.Errorf("Duplicate tunnel name")
} else if text == SESSION_DUPLICATE_DEST { } else if text == SESSION_DUPLICATE_DEST {
log.Error("Duplicate destination") log.Error("Duplicate destination")
conn.Close() conn.Close()
return nil, fmt.Errorf("Duplicate destination") return nil, oops.Errorf("Duplicate destination")
} else if text == SESSION_INVALID_KEY { } else if text == SESSION_INVALID_KEY {
log.Error("Invalid key for SAM session") log.Error("Invalid key for SAM session")
conn.Close() conn.Close()
return nil, fmt.Errorf("Invalid key - SAM session") return nil, oops.Errorf("Invalid key - SAM session")
} else if strings.HasPrefix(text, SESSION_I2P_ERROR) { } else if strings.HasPrefix(text, SESSION_I2P_ERROR) {
log.WithField("error", text[len(SESSION_I2P_ERROR):]).Error("I2P error") log.WithField("error", text[len(SESSION_I2P_ERROR):]).Error("I2P error")
conn.Close() conn.Close()
return nil, fmt.Errorf("I2P error " + text[len(SESSION_I2P_ERROR):]) return nil, oops.Errorf("I2P error " + text[len(SESSION_I2P_ERROR):])
} else { } else {
log.WithField("reply", text).Error("Unable to parse SAMv3 reply") log.WithField("reply", text).Error("Unable to parse SAMv3 reply")
conn.Close() conn.Close()
return nil, fmt.Errorf("Unable to parse SAMv3 reply: " + text) return nil, oops.Errorf("Unable to parse SAMv3 reply: " + text)
} }
} }

View File

@ -1,10 +1,10 @@
package common package common
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
"github.com/samber/oops"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -22,7 +22,7 @@ func SetType(s string) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("style", s).Error("Invalid session style") log.WithField("style", s).Error("Invalid session style")
return fmt.Errorf("Invalid session STYLE=%s, must be STREAM, DATAGRAM, or RAW", s) return oops.Errorf("Invalid session STYLE=%s, must be STREAM, DATAGRAM, or RAW", s)
} }
} }
@ -32,14 +32,14 @@ func SetSAMAddress(s string) func(*SAMEmit) error {
sp := strings.Split(s, ":") sp := strings.Split(s, ":")
if len(sp) > 2 { if len(sp) > 2 {
log.WithField("address", s).Error("Invalid SAM address") log.WithField("address", s).Error("Invalid SAM address")
return fmt.Errorf("Invalid address string: %s", sp) return oops.Errorf("Invalid address string: %s", sp)
} }
if len(sp) == 2 { if len(sp) == 2 {
var err error var err error
c.I2PConfig.SamPort, err = strconv.Atoi(sp[1]) c.I2PConfig.SamPort, err = strconv.Atoi(sp[1])
if err != nil { if err != nil {
log.WithField("port", sp[1]).Error("Invalid SAM port") log.WithField("port", sp[1]).Error("Invalid SAM port")
return fmt.Errorf("Invalid SAM Port %s; non-number", sp[1]) return oops.Errorf("Invalid SAM Port %s; non-number", sp[1])
} }
} }
c.I2PConfig.SamHost = sp[0] c.I2PConfig.SamHost = sp[0]
@ -66,7 +66,7 @@ func SetSAMPort(s string) func(*SAMEmit) error {
port, err := strconv.Atoi(s) port, err := strconv.Atoi(s)
if err != nil { if err != nil {
log.WithField("port", s).Error("Invalid SAM port: non-number") log.WithField("port", s).Error("Invalid SAM port: non-number")
return fmt.Errorf("Invalid SAM Port %s; non-number", s) return oops.Errorf("Invalid SAM Port %s; non-number", s)
} }
if port < 65536 && port > -1 { if port < 65536 && port > -1 {
c.I2PConfig.SamPort = port c.I2PConfig.SamPort = port
@ -74,7 +74,7 @@ func SetSAMPort(s string) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("port", port).Error("Invalid SAM port") log.WithField("port", port).Error("Invalid SAM port")
return fmt.Errorf("Invalid port") return oops.Errorf("Invalid port")
} }
} }
@ -96,7 +96,7 @@ func SetInLength(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("inLength", u).Error("Invalid inbound tunnel length") log.WithField("inLength", u).Error("Invalid inbound tunnel length")
return fmt.Errorf("Invalid inbound tunnel length") return oops.Errorf("Invalid inbound tunnel length")
} }
} }
@ -109,7 +109,7 @@ func SetOutLength(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("outLength", u).Error("Invalid outbound tunnel length") log.WithField("outLength", u).Error("Invalid outbound tunnel length")
return fmt.Errorf("Invalid outbound tunnel length") return oops.Errorf("Invalid outbound tunnel length")
} }
} }
@ -122,7 +122,7 @@ func SetInVariance(i int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("inVariance", i).Error("Invalid inbound tunnel variance") log.WithField("inVariance", i).Error("Invalid inbound tunnel variance")
return fmt.Errorf("Invalid inbound tunnel length") return oops.Errorf("Invalid inbound tunnel length")
} }
} }
@ -135,7 +135,7 @@ func SetOutVariance(i int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("outVariance", i).Error("Invalid outbound tunnel variance") log.WithField("outVariance", i).Error("Invalid outbound tunnel variance")
return fmt.Errorf("Invalid outbound tunnel variance") return oops.Errorf("Invalid outbound tunnel variance")
} }
} }
@ -148,7 +148,7 @@ func SetInQuantity(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("inQuantity", u).Error("Invalid inbound tunnel quantity") log.WithField("inQuantity", u).Error("Invalid inbound tunnel quantity")
return fmt.Errorf("Invalid inbound tunnel quantity") return oops.Errorf("Invalid inbound tunnel quantity")
} }
} }
@ -161,7 +161,7 @@ func SetOutQuantity(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("outQuantity", u).Error("Invalid outbound tunnel quantity") log.WithField("outQuantity", u).Error("Invalid outbound tunnel quantity")
return fmt.Errorf("Invalid outbound tunnel quantity") return oops.Errorf("Invalid outbound tunnel quantity")
} }
} }
@ -174,7 +174,7 @@ func SetInBackups(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("inBackups", u).Error("Invalid inbound tunnel backup quantity") log.WithField("inBackups", u).Error("Invalid inbound tunnel backup quantity")
return fmt.Errorf("Invalid inbound tunnel backup quantity") return oops.Errorf("Invalid inbound tunnel backup quantity")
} }
} }
@ -187,7 +187,7 @@ func SetOutBackups(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("outBackups", u).Error("Invalid outbound tunnel backup quantity") log.WithField("outBackups", u).Error("Invalid outbound tunnel backup quantity")
return fmt.Errorf("Invalid outbound tunnel backup quantity") return oops.Errorf("Invalid outbound tunnel backup quantity")
} }
} }
@ -316,7 +316,7 @@ func SetReduceIdleTime(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("minutes", u).Error("Invalid reduce idle timeout") log.WithField("minutes", u).Error("Invalid reduce idle timeout")
return fmt.Errorf("Invalid reduce idle timeout(Measured in minutes) %v", u) return oops.Errorf("Invalid reduce idle timeout(Measured in minutes) %v", u)
} }
} }
@ -330,7 +330,7 @@ func SetReduceIdleTimeMs(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("milliseconds", u).Error("Invalid reduce idle timeout") log.WithField("milliseconds", u).Error("Invalid reduce idle timeout")
return fmt.Errorf("Invalid reduce idle timeout(Measured in milliseconds) %v", u) return oops.Errorf("Invalid reduce idle timeout(Measured in milliseconds) %v", u)
} }
} }
@ -343,7 +343,7 @@ func SetReduceIdleQuantity(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("quantity", u).Error("Invalid reduce tunnel quantity") log.WithField("quantity", u).Error("Invalid reduce tunnel quantity")
return fmt.Errorf("Invalid reduce tunnel quantity") return oops.Errorf("Invalid reduce tunnel quantity")
} }
} }
@ -373,7 +373,7 @@ func SetCloseIdleTime(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("minutes", u).Error("Invalid close idle timeout") log.WithField("minutes", u).Error("Invalid close idle timeout")
return fmt.Errorf("Invalid close idle timeout(Measured in minutes) %v", u) return oops.Errorf("Invalid close idle timeout(Measured in minutes) %v", u)
} }
} }
@ -386,7 +386,7 @@ func SetCloseIdleTimeMs(u int) func(*SAMEmit) error {
log.WithField("closeIdleTimeMs", u).Debug("Set close idle time in milliseconds") log.WithField("closeIdleTimeMs", u).Debug("Set close idle time in milliseconds")
return nil return nil
} }
return fmt.Errorf("Invalid close idle timeout(Measured in milliseconds) %v", u) return oops.Errorf("Invalid close idle timeout(Measured in milliseconds) %v", u)
} }
} }
@ -406,7 +406,7 @@ func SetAccessListType(s string) func(*SAMEmit) error {
log.Debug("Set access list type to none") log.Debug("Set access list type to none")
return nil return nil
} }
return fmt.Errorf("Invalid Access list type (whitelist, blacklist, none)") return oops.Errorf("Invalid Access list type (whitelist, blacklist, none)")
} }
} }

View File

@ -1,9 +1,10 @@
package common package common
import ( import (
"fmt"
"net" "net"
"strings" "strings"
"github.com/samber/oops"
) )
// Creates a new controller for the I2P routers SAM bridge. // Creates a new controller for the I2P routers SAM bridge.
@ -14,19 +15,19 @@ func OldNewSAM(address string) (*SAM, error) {
conn, err := net.Dial("tcp", address) conn, err := net.Dial("tcp", address)
if err != nil { if err != nil {
log.WithError(err).Error("Failed to dial SAM address") log.WithError(err).Error("Failed to dial SAM address")
return nil, fmt.Errorf("error dialing to address '%s': %w", address, err) return nil, oops.Errorf("error dialing to address '%s': %w", address, err)
} }
if _, err := conn.Write(s.SAMEmit.HelloBytes()); err != nil { if _, err := conn.Write(s.SAMEmit.HelloBytes()); err != nil {
log.WithError(err).Error("Failed to write hello message") log.WithError(err).Error("Failed to write hello message")
conn.Close() conn.Close()
return nil, fmt.Errorf("error writing to address '%s': %w", address, err) return nil, oops.Errorf("error writing to address '%s': %w", address, err)
} }
buf := make([]byte, 256) buf := make([]byte, 256)
n, err := conn.Read(buf) n, err := conn.Read(buf)
if err != nil { if err != nil {
log.WithError(err).Error("Failed to read SAM response") log.WithError(err).Error("Failed to read SAM response")
conn.Close() conn.Close()
return nil, fmt.Errorf("error reading onto buffer: %w", err) return nil, oops.Errorf("error reading onto buffer: %w", err)
} }
if strings.Contains(string(buf[:n]), HELLO_REPLY_OK) { if strings.Contains(string(buf[:n]), HELLO_REPLY_OK) {
log.Debug("SAM hello successful") log.Debug("SAM hello successful")
@ -35,17 +36,17 @@ func OldNewSAM(address string) (*SAM, error) {
s.SAMResolver, err = NewSAMResolver(&s) s.SAMResolver, err = NewSAMResolver(&s)
if err != nil { if err != nil {
log.WithError(err).Error("Failed to create SAM resolver") log.WithError(err).Error("Failed to create SAM resolver")
return nil, fmt.Errorf("error creating resolver: %w", err) return nil, oops.Errorf("error creating resolver: %w", err)
} }
return &s, nil return &s, nil
} else if string(buf[:n]) == HELLO_REPLY_NOVERSION { } else if string(buf[:n]) == HELLO_REPLY_NOVERSION {
log.Error("SAM bridge does not support SAMv3") log.Error("SAM bridge does not support SAMv3")
conn.Close() conn.Close()
return nil, fmt.Errorf("That SAM bridge does not support SAMv3.") return nil, oops.Errorf("That SAM bridge does not support SAMv3.")
} else { } else {
log.WithField("response", string(buf[:n])).Error("Unexpected SAM response") log.WithField("response", string(buf[:n])).Error("Unexpected SAM response")
conn.Close() conn.Close()
return nil, fmt.Errorf("%s", string(buf[:n])) return nil, oops.Errorf("%s", string(buf[:n]))
} }
} }
@ -55,26 +56,25 @@ func NewSAM(address string) (*SAM, error) {
conn, err := connectToSAM(address) conn, err := connectToSAM(address)
if err != nil { if err != nil {
logger.WithError(err).Error("Failed to connect to SAM bridge")
return nil, err return nil, err
} }
defer func() {
if err != nil {
conn.Close()
}
}()
s := &SAM{ s := &SAM{
Conn: conn, Conn: conn,
} }
if err = sendHelloAndValidate(conn, s); err != nil { if err = sendHelloAndValidate(conn, s); err != nil {
logger.WithError(err).Error("Failed to send hello and validate SAM connection")
conn.Close()
return nil, err return nil, err
} }
s.SAMEmit.I2PConfig.SetSAMAddress(address) s.SAMEmit.I2PConfig.SetSAMAddress(address)
if s.SAMResolver, err = NewSAMResolver(s); err != nil { if s.SAMResolver, err = NewSAMResolver(s); err != nil {
return nil, fmt.Errorf("failed to create SAM resolver: %w", err) logger.WithError(err).Error("Failed to create SAM resolver")
return nil, oops.Errorf("failed to create SAM resolver: %w", err)
} }
return s, nil return s, nil

View File

@ -1,28 +1,29 @@
package common package common
import ( import (
"fmt"
"net" "net"
"strings" "strings"
"github.com/samber/oops"
) )
func connectToSAM(address string) (net.Conn, error) { func connectToSAM(address string) (net.Conn, error) {
conn, err := net.Dial("tcp", address) conn, err := net.Dial("tcp", address)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to connect to SAM bridge at %s: %w", address, err) return nil, oops.Errorf("failed to connect to SAM bridge at %s: %w", address, err)
} }
return conn, nil return conn, nil
} }
func sendHelloAndValidate(conn net.Conn, s *SAM) error { func sendHelloAndValidate(conn net.Conn, s *SAM) error {
if _, err := conn.Write(s.SAMEmit.HelloBytes()); err != nil { if _, err := conn.Write(s.SAMEmit.HelloBytes()); err != nil {
return fmt.Errorf("failed to send hello message: %w", err) return oops.Errorf("failed to send hello message: %w", err)
} }
buf := make([]byte, 256) buf := make([]byte, 256)
n, err := conn.Read(buf) n, err := conn.Read(buf)
if err != nil { if err != nil {
return fmt.Errorf("failed to read SAM response: %w", err) return oops.Errorf("failed to read SAM response: %w", err)
} }
response := string(buf[:n]) response := string(buf[:n])
@ -31,8 +32,8 @@ func sendHelloAndValidate(conn net.Conn, s *SAM) error {
log.Debug("SAM hello successful") log.Debug("SAM hello successful")
return nil return nil
case response == HELLO_REPLY_NOVERSION: case response == HELLO_REPLY_NOVERSION:
return fmt.Errorf("SAM bridge does not support SAMv3") return oops.Errorf("SAM bridge does not support SAMv3")
default: default:
return fmt.Errorf("unexpected SAM response: %s", response) return oops.Errorf("unexpected SAM response: %s", response)
} }
} }

View File

@ -1,13 +1,13 @@
package common package common
import ( import (
"fmt"
"math/rand" "math/rand"
"net" "net"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/samber/oops"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -95,5 +95,5 @@ func RandPort() (portNumber string, err error) {
} }
} }
return "", fmt.Errorf("unable to find a pair of available tcp and udp ports in %v attempts", maxAttempts) return "", oops.Errorf("unable to find a pair of available tcp and udp ports in %v attempts", maxAttempts)
} }

View File

@ -2,10 +2,10 @@
package sam3 package sam3
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
"github.com/samber/oops"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -29,7 +29,7 @@ func SetType(s string) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("style", s).Error("Invalid session style") log.WithField("style", s).Error("Invalid session style")
return fmt.Errorf("Invalid session STYLE=%s, must be STREAM, DATAGRAM, or RAW", s) return oops.Errorf("Invalid session STYLE=%s, must be STREAM, DATAGRAM, or RAW", s)
} }
} }
@ -39,13 +39,13 @@ func SetSAMAddress(s string) func(*SAMEmit) error {
sp := strings.Split(s, ":") sp := strings.Split(s, ":")
if len(sp) > 2 { if len(sp) > 2 {
log.WithField("address", s).Error("Invalid SAM address") log.WithField("address", s).Error("Invalid SAM address")
return fmt.Errorf("Invalid address string: %s", s) return oops.Errorf("Invalid address string: %s", s)
} }
if len(sp) == 2 { if len(sp) == 2 {
port, err := strconv.Atoi(sp[1]) port, err := strconv.Atoi(sp[1])
if err != nil { if err != nil {
log.WithField("port", sp[1]).Error("Invalid SAM port: non-number") log.WithField("port", sp[1]).Error("Invalid SAM port: non-number")
return fmt.Errorf("Invalid SAM port %s; non-number", sp[1]) return oops.Errorf("Invalid SAM port %s; non-number", sp[1])
} }
c.I2PConfig.SamPort = port c.I2PConfig.SamPort = port
} }
@ -73,7 +73,7 @@ func SetSAMPort(s string) func(*SAMEmit) error {
port, err := strconv.Atoi(s) port, err := strconv.Atoi(s)
if err != nil { if err != nil {
log.WithField("port", s).Error("Invalid SAM port: non-number") log.WithField("port", s).Error("Invalid SAM port: non-number")
return fmt.Errorf("Invalid SAM port %s; non-number", s) return oops.Errorf("Invalid SAM port %s; non-number", s)
} }
if port < 65536 && port > -1 { if port < 65536 && port > -1 {
c.I2PConfig.SamPort = port c.I2PConfig.SamPort = port
@ -81,7 +81,7 @@ func SetSAMPort(s string) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("port", port).Error("Invalid SAM port") log.WithField("port", port).Error("Invalid SAM port")
return fmt.Errorf("Invalid SAM port: out of range") return oops.Errorf("Invalid SAM port: out of range")
} }
} }
@ -103,7 +103,7 @@ func SetInLength(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("inLength", u).Error("Invalid inbound tunnel length") log.WithField("inLength", u).Error("Invalid inbound tunnel length")
return fmt.Errorf("Invalid inbound tunnel length: out of range") return oops.Errorf("Invalid inbound tunnel length: out of range")
} }
} }
@ -116,7 +116,7 @@ func SetOutLength(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("outLength", u).Error("Invalid outbound tunnel length") log.WithField("outLength", u).Error("Invalid outbound tunnel length")
return fmt.Errorf("Invalid outbound tunnel length: out of range") return oops.Errorf("Invalid outbound tunnel length: out of range")
} }
} }
@ -129,7 +129,7 @@ func SetInVariance(i int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("inVariance", i).Error("Invalid inbound tunnel variance") log.WithField("inVariance", i).Error("Invalid inbound tunnel variance")
return fmt.Errorf("Invalid inbound tunnel variance: out of range") return oops.Errorf("Invalid inbound tunnel variance: out of range")
} }
} }
@ -142,7 +142,7 @@ func SetOutVariance(i int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("outVariance", i).Error("Invalid outbound tunnel variance") log.WithField("outVariance", i).Error("Invalid outbound tunnel variance")
return fmt.Errorf("Invalid outbound tunnel variance: out of range") return oops.Errorf("Invalid outbound tunnel variance: out of range")
} }
} }
@ -155,7 +155,7 @@ func SetInQuantity(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("inQuantity", u).Error("Invalid inbound tunnel quantity") log.WithField("inQuantity", u).Error("Invalid inbound tunnel quantity")
return fmt.Errorf("Invalid inbound tunnel quantity: out of range") return oops.Errorf("Invalid inbound tunnel quantity: out of range")
} }
} }
@ -168,7 +168,7 @@ func SetOutQuantity(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("outQuantity", u).Error("Invalid outbound tunnel quantity") log.WithField("outQuantity", u).Error("Invalid outbound tunnel quantity")
return fmt.Errorf("Invalid outbound tunnel quantity: out of range") return oops.Errorf("Invalid outbound tunnel quantity: out of range")
} }
} }
@ -181,7 +181,7 @@ func SetInBackups(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("inBackups", u).Error("Invalid inbound tunnel backup quantity") log.WithField("inBackups", u).Error("Invalid inbound tunnel backup quantity")
return fmt.Errorf("Invalid inbound tunnel backup quantity: out of range") return oops.Errorf("Invalid inbound tunnel backup quantity: out of range")
} }
} }
@ -194,7 +194,7 @@ func SetOutBackups(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("outBackups", u).Error("Invalid outbound tunnel backup quantity") log.WithField("outBackups", u).Error("Invalid outbound tunnel backup quantity")
return fmt.Errorf("Invalid outbound tunnel backup quantity: out of range") return oops.Errorf("Invalid outbound tunnel backup quantity: out of range")
} }
} }
@ -323,7 +323,7 @@ func SetReduceIdleTime(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("minutes", u).Error("Invalid reduce idle timeout") log.WithField("minutes", u).Error("Invalid reduce idle timeout")
return fmt.Errorf("Invalid reduce idle timeout (Measured in minutes) %v", u) return oops.Errorf("Invalid reduce idle timeout (Measured in minutes) %v", u)
} }
} }
@ -337,7 +337,7 @@ func SetReduceIdleTimeMs(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("milliseconds", u).Error("Invalid reduce idle timeout") log.WithField("milliseconds", u).Error("Invalid reduce idle timeout")
return fmt.Errorf("Invalid reduce idle timeout (Measured in milliseconds) %v", u) return oops.Errorf("Invalid reduce idle timeout (Measured in milliseconds) %v", u)
} }
} }
@ -350,7 +350,7 @@ func SetReduceIdleQuantity(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("quantity", u).Error("Invalid reduce tunnel quantity") log.WithField("quantity", u).Error("Invalid reduce tunnel quantity")
return fmt.Errorf("Invalid reduce idle tunnel quantity: out of range") return oops.Errorf("Invalid reduce idle tunnel quantity: out of range")
} }
} }
@ -380,7 +380,7 @@ func SetCloseIdleTime(u int) func(*SAMEmit) error {
return nil return nil
} }
log.WithField("minutes", u).Error("Invalid close idle timeout") log.WithField("minutes", u).Error("Invalid close idle timeout")
return fmt.Errorf("Invalid close idle timeout (Measured in minutes) %v", u) return oops.Errorf("Invalid close idle timeout (Measured in minutes) %v", u)
} }
} }
@ -393,7 +393,7 @@ func SetCloseIdleTimeMs(u int) func(*SAMEmit) error {
log.WithField("closeIdleTimeMs", u).Debug("Set close idle time in milliseconds") log.WithField("closeIdleTimeMs", u).Debug("Set close idle time in milliseconds")
return nil return nil
} }
return fmt.Errorf("Invalid close idle timeout (Measured in milliseconds) %v", u) return oops.Errorf("Invalid close idle timeout (Measured in milliseconds) %v", u)
} }
} }
@ -417,7 +417,7 @@ func SetAccessListType(s string) func(*SAMEmit) error {
log.Debug("Set access list type to none") log.Debug("Set access list type to none")
return nil return nil
} }
return fmt.Errorf("Invalid Access list type (whitelist, blacklist, none)") return oops.Errorf("Invalid Access list type (whitelist, blacklist, none)")
} }
} }

10
go.mod
View File

@ -5,7 +5,15 @@ go 1.23.5
require ( require (
github.com/go-i2p/i2pkeys v0.33.92 github.com/go-i2p/i2pkeys v0.33.92
github.com/go-i2p/logger v0.0.0-20241123010126-3050657e5d0c github.com/go-i2p/logger v0.0.0-20241123010126-3050657e5d0c
github.com/samber/oops v1.18.0
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
) )
require golang.org/x/sys v0.30.0 // indirect require (
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/samber/lo v1.50.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
)

21
go.sum
View File

@ -5,16 +5,33 @@ github.com/go-i2p/i2pkeys v0.33.92 h1:e2vx3vf7tNesaJ8HmAlGPOcfiGM86jzeIGxh27I9J2
github.com/go-i2p/i2pkeys v0.33.92/go.mod h1:BRURQ/twxV0WKjZlFSKki93ivBi+MirZPWudfwTzMpE= github.com/go-i2p/i2pkeys v0.33.92/go.mod h1:BRURQ/twxV0WKjZlFSKki93ivBi+MirZPWudfwTzMpE=
github.com/go-i2p/logger v0.0.0-20241123010126-3050657e5d0c h1:VTiECn3dFEmUlZjto+wOwJ7SSJTHPLyNprQMR5HzIMI= github.com/go-i2p/logger v0.0.0-20241123010126-3050657e5d0c h1:VTiECn3dFEmUlZjto+wOwJ7SSJTHPLyNprQMR5HzIMI=
github.com/go-i2p/logger v0.0.0-20241123010126-3050657e5d0c/go.mod h1:te7Zj3g3oMeIl8uBXAgO62UKmZ6m6kHRNg1Mm+X8Hzk= github.com/go-i2p/logger v0.0.0-20241123010126-3050657e5d0c/go.mod h1:te7Zj3g3oMeIl8uBXAgO62UKmZ6m6kHRNg1Mm+X8Hzk=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.50.0 h1:XrG0xOeHs+4FQ8gJR97zDz5uOFMW7OwFWiFVzqopKgY=
github.com/samber/lo v1.50.0/go.mod h1:RjZyNk6WSnUFRKK6EyOhsRJMqft3G+pg7dCWHQCWvsc=
github.com/samber/oops v1.18.0 h1:NnoCdxlOg/ajFos8HIC0+dV8S6cZRcrjW1WrfZe+GOc=
github.com/samber/oops v1.18.0/go.mod h1:DcZbba2s+PzSx14vY6HjvhV1FDsGOZ1TJg7T/ZZARBQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/otel v1.29.0 h1:PdomN/Al4q/lN6iBJEN3AwPvUiHPMlt93c8bqTG5Llw=
go.opentelemetry.io/otel v1.29.0/go.mod h1:N/WtXPs1CNCUEx+Agz5uouwCba+i+bJGFicT8SR4NP8=
go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt39JTi4=
go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,12 +1,12 @@
package primary package primary
import ( import (
"fmt"
"net" "net"
"strings" "strings"
"github.com/go-i2p/go-sam-go/common" "github.com/go-i2p/go-sam-go/common"
"github.com/go-i2p/go-sam-go/datagram" "github.com/go-i2p/go-sam-go/datagram"
"github.com/samber/oops"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -21,7 +21,7 @@ func (sam *PrimarySession) Dial(network, addr string) (net.Conn, error) {
return sam.DialTCPI2P(network, network+addr[0:4], addr) return sam.DialTCPI2P(network, network+addr[0:4], addr)
} }
log.WithField("network", network).Error("Invalid network type") log.WithField("network", network).Error("Invalid network type")
return nil, fmt.Errorf("Error: Must specify a valid network type") return nil, oops.Errorf("Error: Must specify a valid network type")
} }
// DialTCP implements x/dialer // DialTCP implements x/dialer

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"fmt"
"io" "io"
"net" "net"
"strings" "strings"
@ -12,6 +11,7 @@ import (
"github.com/go-i2p/go-sam-go/common" "github.com/go-i2p/go-sam-go/common"
"github.com/go-i2p/i2pkeys" "github.com/go-i2p/i2pkeys"
"github.com/samber/oops"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -110,27 +110,27 @@ func (s *StreamSession) DialI2P(addr i2pkeys.I2PAddr) (*StreamConn, error) {
case ResultCantReachPeer: case ResultCantReachPeer:
log.Error("Can't reach peer") log.Error("Can't reach peer")
conn.Close() conn.Close()
return nil, fmt.Errorf("Can not reach peer") return nil, oops.Errorf("Can not reach peer")
case ResultI2PError: case ResultI2PError:
log.Error("I2P internal error") log.Error("I2P internal error")
conn.Close() conn.Close()
return nil, fmt.Errorf("I2P internal error") return nil, oops.Errorf("I2P internal error")
case ResultInvalidKey: case ResultInvalidKey:
log.Error("Invalid key - Stream Session") log.Error("Invalid key - Stream Session")
conn.Close() conn.Close()
return nil, fmt.Errorf("Invalid key - Stream Session") return nil, oops.Errorf("Invalid key - Stream Session")
case ResultInvalidID: case ResultInvalidID:
log.Error("Invalid tunnel ID") log.Error("Invalid tunnel ID")
conn.Close() conn.Close()
return nil, fmt.Errorf("Invalid tunnel ID") return nil, oops.Errorf("Invalid tunnel ID")
case ResultTimeout: case ResultTimeout:
log.Error("Connection timeout") log.Error("Connection timeout")
conn.Close() conn.Close()
return nil, fmt.Errorf("Timeout") return nil, oops.Errorf("Timeout")
default: default:
log.WithField("error", scanner.Text()).Error("Unknown error") log.WithField("error", scanner.Text()).Error("Unknown error")
conn.Close() conn.Close()
return nil, fmt.Errorf("Unknown error: %s : %s", scanner.Text(), string(buf[:n])) return nil, oops.Errorf("Unknown error: %s : %s", scanner.Text(), string(buf[:n]))
} }
} }
log.Panic("Unexpected end of StreamSession.DialI2P()") log.Panic("Unexpected end of StreamSession.DialI2P()")