Skip to content

Commit

Permalink
More of the great + purge, where we make all our formatters consisten…
Browse files Browse the repository at this point in the history
…t. Oh also it looks like past idk was like 'hmm, I don't know for sure if I should leave NewKeys in sam3 now that I've moved i2pkeys to it's own library. That won't be confusing to future idk, who will never have a week like future idk is having right now.' Screw that guy(past idk).
  • Loading branch information
eyedeekay committed Dec 8, 2024
1 parent bfbe7f6 commit 8279daa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
14 changes: 7 additions & 7 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"errors"
"fmt"
"strings"

"github.com/go-i2p/i2pkeys"
Expand Down Expand Up @@ -36,8 +37,8 @@ func NewFullSAMResolver(address string) (*SAMResolver, error) {
// addresses, 3) by asking peers in the I2P network.
func (sam *SAMResolver) Resolve(name string) (i2pkeys.I2PAddr, error) {
log.WithField("name", name).Debug("Resolving name")

if _, err := sam.conn.Write([]byte("NAMING LOOKUP NAME=" + name + "\r\n")); err != nil {
query := []byte(fmt.Sprintf("NAMING LOOKUP NAME=%s\n", name))
if _, err := sam.conn.Write(query); err != nil {
log.WithError(err).Error("Failed to write to SAM connection")
sam.Close()
return i2pkeys.I2PAddr(""), err
Expand All @@ -56,31 +57,30 @@ func (sam *SAMResolver) Resolve(name string) (i2pkeys.I2PAddr, error) {
s := bufio.NewScanner(bytes.NewReader(buf[13:n]))
s.Split(bufio.ScanWords)

errStr := ""
for s.Scan() {
text := s.Text()
log.WithField("text", text).Debug("Parsing SAM response token")
// log.Println("SAM3", text)
if text == "RESULT=OK" {
continue
} else if text == "RESULT=INVALID_KEY" {
errStr += "Invalid key - resolver."
log.Error("Invalid key in resolver")
return i2pkeys.I2PAddr(""), fmt.Errorf("Invalid key - resolver")
} else if text == "RESULT=KEY_NOT_FOUND" {
errStr += "Unable to resolve " + name
log.WithField("name", name).Error("Unable to resolve name")
return i2pkeys.I2PAddr(""), fmt.Errorf("Unable to resolve %s", name)
} else if text == "NAME="+name {
continue
} else if strings.HasPrefix(text, "VALUE=") {
addr := i2pkeys.I2PAddr(text[6:])
log.WithField("addr", addr).Debug("Name resolved successfully")
return i2pkeys.I2PAddr(text[6:]), nil
} else if strings.HasPrefix(text, "MESSAGE=") {
errStr += " " + text[8:]
log.WithField("message", text[8:]).Warn("Received message from SAM")
return i2pkeys.I2PAddr(""), fmt.Errorf("Received message from SAM: %s", text[8:])
} else {
continue
}
}
return i2pkeys.I2PAddr(""), errors.New(errStr)
return i2pkeys.I2PAddr(""), fmt.Errorf("Unable to resolve %s", name)
}
4 changes: 2 additions & 2 deletions sam3.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ func (sam *SAM) newGenericSessionWithSignatureAndPorts(style, id, from, to strin
} 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, fmt.Errorf("I2P error %s", 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, fmt.Errorf("Unable to parse SAMv3 reply: %s", text)
}
}

Expand Down
13 changes: 7 additions & 6 deletions streamListener.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sam3
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"net"
"strconv"
Expand Down Expand Up @@ -106,7 +106,8 @@ func (l *StreamListener) AcceptI2P() (*SAMConn, error) {
log.Debug("Connected to SAM bridge")
// we connected to sam
// send accept() command
_, err = io.WriteString(s.conn, "STREAM ACCEPT ID="+l.id+" SILENT=false\n")
acceptFmt := fmt.Sprintf("STREAM ACCEPT ID=%s SILENT=false", l.id)
_, err = io.WriteString(s.conn, acceptFmt)
if err != nil {
log.WithError(err).Error("Failed to send STREAM ACCEPT command")
s.Close()
Expand All @@ -127,15 +128,15 @@ func (l *StreamListener) AcceptI2P() (*SAMConn, error) {
destline, err := rd.ReadString(10)
if err != nil {
if err == io.EOF {
err = errors.New("connection closed after OK")
err = fmt.Errorf("connection closed after OK")
}
err = errors.New("error reading destination: " + err.Error())
err = fmt.Errorf("error reading destination: %s", err.Error())
}
if err == nil {
// Validate destination format
dest := ExtractDest(destline)
if !strings.HasPrefix(dest, "") {
err = errors.New("invalid destination format")
err = fmt.Errorf("invalid destination format")
}
l.session.from = ExtractPairString(destline, "FROM_PORT")
l.session.to = ExtractPairString(destline, "TO_PORT")
Expand All @@ -160,7 +161,7 @@ func (l *StreamListener) AcceptI2P() (*SAMConn, error) {
} else {
log.WithField("line", line).Error("Invalid SAM response")
s.Close()
err = errors.New("invalid sam line: " + line)
err = fmt.Errorf("invalid sam line: %s", line)
}
} else {
log.WithError(err).Error("Failed to connect to SAM bridge")
Expand Down
4 changes: 2 additions & 2 deletions suggestedOptions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sam3

import (
"fmt"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -88,8 +89,7 @@ func GenerateOptionString(opts []string) string {
log.Debug("i2cp.leaseSetEncType already present in options")
return optStr
}
finalOpts := optStr + " i2cp.leaseSetEncType=4,0"
finalOpts := fmt.Sprintf("%s i2cp.leaseSetEncType=4,0", optStr)
log.WithField("finalOptions", finalOpts).Debug("Added default i2cp.leaseSetEncType to options")
return finalOpts
// return optStr + " i2cp.leaseSetEncType=4,0"
}

0 comments on commit 8279daa

Please sign in to comment.