From aa9fe1ef62986aa7303e372e20fd70a457475b42 Mon Sep 17 00:00:00 2001 From: eyedeekay Date: Tue, 27 May 2025 16:40:15 -0400 Subject: [PATCH] Switch to oops, simplify error handling in common --- Makefile | 6 +++--- common/SAM.go | 26 +++++++++++++------------- common/emit-options.go | 40 ++++++++++++++++++++-------------------- common/new.go | 26 +++++++++++++------------- common/sam3.go | 13 +++++++------ common/util.go | 4 ++-- emit-options.go | 40 ++++++++++++++++++++-------------------- go.mod | 10 +++++++++- go.sum | 21 +++++++++++++++++++-- primary/dialers.go | 4 ++-- stream/dialers.go | 14 +++++++------- 11 files changed, 115 insertions(+), 89 deletions(-) diff --git a/Makefile b/Makefile index b9ae3a9..67e8584 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ fmt: find . -name '*.go' -exec gofumpt -w -s -extra {} \; -export DEBUG_I2P=warn -export WARNFAIL_I2P=true +export DEBUG_I2P=debug +#export WARNFAIL_I2P=true common-test: go test --tags nettest -v ./common/... @@ -20,7 +20,7 @@ raw-test: test-logs: 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 datagram-test 2> datagram-err.log 1> datagram-out.log make raw-test 2> raw-err.log 1> raw-out.log make primary-test 2> primary-err.log 1> primary-out.log diff --git a/common/SAM.go b/common/SAM.go index 8f7b7fb..b587ee4 100644 --- a/common/SAM.go +++ b/common/SAM.go @@ -3,13 +3,13 @@ package common import ( "bufio" "bytes" - "fmt" "io" "net" "os" "strings" "github.com/go-i2p/i2pkeys" + "github.com/samber/oops" "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 { 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) n, err := sam.Conn.Read(buf) if err != nil { 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.Split(bufio.ScanWords) @@ -117,7 +117,7 @@ func (sam *SAM) NewKeys(sigType ...string) (i2pkeys.I2PKeys, error) { priv = text[5:] } else { 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") @@ -174,13 +174,13 @@ func (sam *SAM) NewGenericSessionWithSignatureAndPorts(style, id, from, to strin if i == 15 { log.Error("Failed to write SESSION CREATE message after 15 attempts") conn.Close() - return nil, fmt.Errorf("writing to SAM failed") + return nil, oops.Errorf("writing to SAM failed") } n, err := conn.Write(scmsg[m:]) if err != nil { log.WithError(err).Error("Failed to write to SAM connection") conn.Close() - return nil, fmt.Errorf("writing to connection failed: %w", err) + return nil, oops.Errorf("writing to connection failed: %w", err) } m += n } @@ -189,7 +189,7 @@ func (sam *SAM) NewGenericSessionWithSignatureAndPorts(style, id, from, to strin if err != nil { log.WithError(err).Error("Failed to read SAM response") 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]) 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] { log.Error("SAM created a tunnel with different keys than requested") 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") return conn, nil //&StreamSession{id, conn, keys, nil, sync.RWMutex{}, nil}, nil } else if text == SESSION_DUPLICATE_ID { log.Error("Duplicate tunnel name") conn.Close() - return nil, fmt.Errorf("Duplicate tunnel name") + return nil, oops.Errorf("Duplicate tunnel name") } else if text == SESSION_DUPLICATE_DEST { log.Error("Duplicate destination") conn.Close() - return nil, fmt.Errorf("Duplicate destination") + return nil, oops.Errorf("Duplicate destination") } else if text == SESSION_INVALID_KEY { log.Error("Invalid key for SAM session") 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) { log.WithField("error", text[len(SESSION_I2P_ERROR):]).Error("I2P error") 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 { log.WithField("reply", text).Error("Unable to parse SAMv3 reply") conn.Close() - return nil, fmt.Errorf("Unable to parse SAMv3 reply: " + text) + return nil, oops.Errorf("Unable to parse SAMv3 reply: " + text) } } diff --git a/common/emit-options.go b/common/emit-options.go index ee9f6f8..28304f2 100644 --- a/common/emit-options.go +++ b/common/emit-options.go @@ -1,10 +1,10 @@ package common import ( - "fmt" "strconv" "strings" + "github.com/samber/oops" "github.com/sirupsen/logrus" ) @@ -22,7 +22,7 @@ func SetType(s string) func(*SAMEmit) error { return nil } 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, ":") if len(sp) > 2 { 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 { var err error c.I2PConfig.SamPort, err = strconv.Atoi(sp[1]) if err != nil { 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] @@ -66,7 +66,7 @@ func SetSAMPort(s string) func(*SAMEmit) error { port, err := strconv.Atoi(s) if err != nil { 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 { c.I2PConfig.SamPort = port @@ -74,7 +74,7 @@ func SetSAMPort(s string) func(*SAMEmit) error { return nil } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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") 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") return nil } - return fmt.Errorf("Invalid Access list type (whitelist, blacklist, none)") + return oops.Errorf("Invalid Access list type (whitelist, blacklist, none)") } } diff --git a/common/new.go b/common/new.go index cdfb911..3bb4452 100644 --- a/common/new.go +++ b/common/new.go @@ -1,9 +1,10 @@ package common import ( - "fmt" "net" "strings" + + "github.com/samber/oops" ) // 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) if err != nil { 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 { log.WithError(err).Error("Failed to write hello message") 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) n, err := conn.Read(buf) if err != nil { log.WithError(err).Error("Failed to read SAM response") 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) { log.Debug("SAM hello successful") @@ -35,17 +36,17 @@ func OldNewSAM(address string) (*SAM, error) { s.SAMResolver, err = NewSAMResolver(&s) if err != nil { 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 } else if string(buf[:n]) == HELLO_REPLY_NOVERSION { log.Error("SAM bridge does not support SAMv3") 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 { log.WithField("response", string(buf[:n])).Error("Unexpected SAM response") 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) if err != nil { + logger.WithError(err).Error("Failed to connect to SAM bridge") return nil, err } - defer func() { - if err != nil { - conn.Close() - } - }() s := &SAM{ Conn: conn, } if err = sendHelloAndValidate(conn, s); err != nil { + logger.WithError(err).Error("Failed to send hello and validate SAM connection") + conn.Close() return nil, err } s.SAMEmit.I2PConfig.SetSAMAddress(address) 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 diff --git a/common/sam3.go b/common/sam3.go index 0454789..974937c 100644 --- a/common/sam3.go +++ b/common/sam3.go @@ -1,28 +1,29 @@ package common import ( - "fmt" "net" "strings" + + "github.com/samber/oops" ) func connectToSAM(address string) (net.Conn, error) { conn, err := net.Dial("tcp", address) 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 } func sendHelloAndValidate(conn net.Conn, s *SAM) error { 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) n, err := conn.Read(buf) 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]) @@ -31,8 +32,8 @@ func sendHelloAndValidate(conn net.Conn, s *SAM) error { log.Debug("SAM hello successful") return nil case response == HELLO_REPLY_NOVERSION: - return fmt.Errorf("SAM bridge does not support SAMv3") + return oops.Errorf("SAM bridge does not support SAMv3") default: - return fmt.Errorf("unexpected SAM response: %s", response) + return oops.Errorf("unexpected SAM response: %s", response) } } diff --git a/common/util.go b/common/util.go index e82d183..351b5df 100644 --- a/common/util.go +++ b/common/util.go @@ -1,13 +1,13 @@ package common import ( - "fmt" "math/rand" "net" "strconv" "strings" "time" + "github.com/samber/oops" "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) } diff --git a/emit-options.go b/emit-options.go index dbf7948..2618014 100644 --- a/emit-options.go +++ b/emit-options.go @@ -2,10 +2,10 @@ package sam3 import ( - "fmt" "strconv" "strings" + "github.com/samber/oops" "github.com/sirupsen/logrus" ) @@ -29,7 +29,7 @@ func SetType(s string) func(*SAMEmit) error { return nil } 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, ":") if len(sp) > 2 { 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 { port, err := strconv.Atoi(sp[1]) if err != nil { 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 } @@ -73,7 +73,7 @@ func SetSAMPort(s string) func(*SAMEmit) error { port, err := strconv.Atoi(s) if err != nil { 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 { c.I2PConfig.SamPort = port @@ -81,7 +81,7 @@ func SetSAMPort(s string) func(*SAMEmit) error { return nil } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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") 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") return nil } - return fmt.Errorf("Invalid Access list type (whitelist, blacklist, none)") + return oops.Errorf("Invalid Access list type (whitelist, blacklist, none)") } } diff --git a/go.mod b/go.mod index 741118b..0c23975 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,15 @@ go 1.23.5 require ( github.com/go-i2p/i2pkeys v0.33.92 github.com/go-i2p/logger v0.0.0-20241123010126-3050657e5d0c + github.com/samber/oops v1.18.0 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 +) diff --git a/go.sum b/go.sum index 70135d9..681b63b 100644 --- a/go.sum +++ b/go.sum @@ -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/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/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/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/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= 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.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.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= 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/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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/primary/dialers.go b/primary/dialers.go index f8fee6c..6ddb359 100644 --- a/primary/dialers.go +++ b/primary/dialers.go @@ -1,12 +1,12 @@ package primary import ( - "fmt" "net" "strings" "github.com/go-i2p/go-sam-go/common" "github.com/go-i2p/go-sam-go/datagram" + "github.com/samber/oops" "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) } 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 diff --git a/stream/dialers.go b/stream/dialers.go index 1cf4db2..57b576d 100644 --- a/stream/dialers.go +++ b/stream/dialers.go @@ -4,7 +4,6 @@ import ( "bufio" "bytes" "context" - "fmt" "io" "net" "strings" @@ -12,6 +11,7 @@ import ( "github.com/go-i2p/go-sam-go/common" "github.com/go-i2p/i2pkeys" + "github.com/samber/oops" "github.com/sirupsen/logrus" ) @@ -110,27 +110,27 @@ func (s *StreamSession) DialI2P(addr i2pkeys.I2PAddr) (*StreamConn, error) { case ResultCantReachPeer: log.Error("Can't reach peer") conn.Close() - return nil, fmt.Errorf("Can not reach peer") + return nil, oops.Errorf("Can not reach peer") case ResultI2PError: log.Error("I2P internal error") conn.Close() - return nil, fmt.Errorf("I2P internal error") + return nil, oops.Errorf("I2P internal error") case ResultInvalidKey: log.Error("Invalid key - Stream Session") conn.Close() - return nil, fmt.Errorf("Invalid key - Stream Session") + return nil, oops.Errorf("Invalid key - Stream Session") case ResultInvalidID: log.Error("Invalid tunnel ID") conn.Close() - return nil, fmt.Errorf("Invalid tunnel ID") + return nil, oops.Errorf("Invalid tunnel ID") case ResultTimeout: log.Error("Connection timeout") conn.Close() - return nil, fmt.Errorf("Timeout") + return nil, oops.Errorf("Timeout") default: log.WithField("error", scanner.Text()).Error("Unknown error") 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()")