From b97558c06ac8d70b2a779ab6503a71882ab2da9f Mon Sep 17 00:00:00 2001 From: idk Date: Thu, 10 Mar 2022 00:51:20 -0500 Subject: [PATCH] repurpose the useless cmd application nobody used into a place to put the i2pkeys library outside of both the SAM libraries --- I2PAddr_test.go | 12 +++++++++++ Lookup.go | 53 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/I2PAddr_test.go b/I2PAddr_test.go index 5049561..4637bf3 100644 --- a/I2PAddr_test.go +++ b/I2PAddr_test.go @@ -19,3 +19,15 @@ func Test_Basic(t *testing.T) { } fmt.Println(keys.String()) } + +func Test_Basic_Lookup(t *testing.T) { + fmt.Println("Test_Basic") + fmt.Println("\tAttaching to SAM at " + yoursam) + keys, err := Lookup("idk.i2p") + if err != nil { + fmt.Println(err.Error()) + t.Fail() + return + } + fmt.Println(keys.String()) +} diff --git a/Lookup.go b/Lookup.go index 4b6bddb..052531e 100644 --- a/Lookup.go +++ b/Lookup.go @@ -1,14 +1,47 @@ package i2pkeys -import "net" +import ( + "fmt" + "net" + "strings" +) -type SimpleLookup struct { -} - -func Lookup(i2paddr string) net.Addr { - return nil -} - -func LookupI2P(i2paddr string) *I2PAddr { - return nil +func Lookup(addr string) (*I2PAddr, error) { + conn, err := net.Dial("tcp", "127.0.0.1:7656") + if err != nil { + return nil, err + } + defer conn.Close() + _, err = conn.Write([]byte("HELLO VERSION MIN=3.1 MAX=3.1\n")) + if err != nil { + return nil, err + } + buf := make([]byte, 4096) + n, err := conn.Read(buf) + if err != nil { + return nil, err + } + if n < 1 { + return nil, fmt.Errorf("no data received") + } + if strings.Contains(string(buf[:n]), "RESULT=OK") { + _, err = conn.Write([]byte(fmt.Sprintf("NAMING LOOKUP NAME=%s\n", addr))) + if err != nil { + return nil, err + } + n, err = conn.Read(buf) + if err != nil { + return nil, err + } + if n < 1 { + return nil, fmt.Errorf("no destination data received") + } + value := strings.Split(string(buf[:n]), "VALUE=")[1] + addr, err := NewI2PAddrFromString(value) + if err != nil { + return nil, err + } + return &addr, err + } + return nil, fmt.Errorf("no result received") }