Fix a bunch of errors that could potentially happen because of changes I made to the Dialer

This commit is contained in:
idk
2021-04-15 17:21:41 -04:00
parent d1d2663c42
commit e278de3a66
9 changed files with 95 additions and 107 deletions

View File

@ -24,8 +24,7 @@ func (c *Client) Listen() (net.Listener, error) {
// with Accept // with Accept
func (c *Client) ListenI2P(dest string) (net.Listener, error) { func (c *Client) ListenI2P(dest string) (net.Listener, error) {
var err error var err error
c.id = c.NewID() c.destination, err = c.CreateStreamSession(dest)
c.destination, err = c.CreateStreamSession(c.id, dest)
d := c.destination d := c.destination
if err != nil { if err != nil {
return nil, err return nil, err
@ -52,7 +51,7 @@ func (c *Client) Accept() (net.Conn, error) {
if c.id == 0 { if c.id == 0 {
return c.AcceptI2P() return c.AcceptI2P()
} }
resp, err := c.StreamAccept(c.id) resp, err := c.StreamAccept()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -92,6 +92,7 @@ func NewClient(addr string) (*Client, error) {
func (c *Client) NewID() int32 { func (c *Client) NewID() int32 {
if c.id == 0 { if c.id == 0 {
c.id = rand.Int31n(math.MaxInt32) c.id = rand.Int31n(math.MaxInt32)
fmt.Printf("Initializing new ID: %d\n", c.id)
} }
return c.id return c.id
} }
@ -178,7 +179,7 @@ func NewClientFromOptions(opts ...func(*Client) error) (*Client, error) {
} }
func (p *Client) ID() string { func (p *Client) ID() string {
return fmt.Sprintf("%d", p.id) return fmt.Sprintf("%d", p.NewID())
} }
func (p *Client) Addr() net.Addr { func (p *Client) Addr() net.Addr {

View File

@ -6,6 +6,8 @@ import "testing"
import ( import (
"fmt" "fmt"
"math"
"math/rand"
"time" "time"
//"log" //"log"
"net/http" "net/http"
@ -18,48 +20,40 @@ func HelloServer(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
} }
var client *Client
func setup(t *testing.T) {
var err error
// these tests expect a running SAM brige on this address
client, err = NewClientFromOptions(SetDebug(true))
if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}
}
func TestCompositeClient(t *testing.T) { func TestCompositeClient(t *testing.T) {
listener, err := sam.I2PListener("testservice", "127.0.0.1:7656", "testkeys") listener, err := sam.I2PListener("testservice"+fmt.Sprintf("%d", rand.Int31n(math.MaxInt32)), "127.0.0.1:7656", "testkeys")
if err != nil { if err != nil {
t.Fatalf("Listener() Error: %q\n", err) t.Fatalf("Listener() Error: %q\n", err)
} }
defer listener.Close()
http.HandleFunc("/", HelloServer) http.HandleFunc("/", HelloServer)
go http.Serve(listener, nil) go http.Serve(listener, nil)
listener2, err := sam.I2PListener("testservice2", "127.0.0.1:7656", "testkeys2") listener2, err := sam.I2PListener("testservice"+fmt.Sprintf("%d", rand.Int31n(math.MaxInt32)), "127.0.0.1:7656", "testkeys2")
if err != nil { if err != nil {
t.Fatalf("Listener() Error: %q\n", err) t.Fatalf("Listener() Error: %q\n", err)
} }
defer listener2.Close()
// http.HandleFunc("/", HelloServer) // http.HandleFunc("/", HelloServer)
go http.Serve(listener2, nil) go http.Serve(listener2, nil)
listener3, err := sam.I2PListener("testservice3", "127.0.0.1:7656", "testkeys3") listener3, err := sam.I2PListener("testservice"+fmt.Sprintf("%d", rand.Int31n(math.MaxInt32)), "127.0.0.1:7656", "testkeys3")
if err != nil { if err != nil {
t.Fatalf("Listener() Error: %q\n", err) t.Fatalf("Listener() Error: %q\n", err)
} }
defer listener3.Close()
// http.HandleFunc("/", HelloServer) // http.HandleFunc("/", HelloServer)
go http.Serve(listener3, nil) go http.Serve(listener3, nil)
client, err = NewClientFromOptions(SetDebug(true)) sam, err := NewClientFromOptions(SetDebug(false))
if err != nil { if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err) t.Fatalf("NewDefaultClient() Error: %q\n", err)
} }
tr := &http.Transport{ tr := &http.Transport{
Dial: client.Dial, Dial: sam.Dial,
} }
client := &http.Client{Transport: tr} client := &http.Client{Transport: tr}
defer sam.Close()
time.Sleep(time.Second * 30) time.Sleep(time.Second * 30)
go func() { go func() {
resp, err := client.Get("http://" + listener.Addr().(i2pkeys.I2PAddr).Base32()) resp, err := client.Get("http://" + listener.Addr().(i2pkeys.I2PAddr).Base32())
@ -67,45 +61,50 @@ func TestCompositeClient(t *testing.T) {
t.Fatalf("Get Error: %q\n", err) t.Fatalf("Get Error: %q\n", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
t.Log("Get returned ", resp)
}() }()
time.Sleep(time.Second * 15)
go func() { go func() {
resp, err := client.Get("http://" + listener2.Addr().(i2pkeys.I2PAddr).Base32()) resp, err := client.Get("http://" + listener2.Addr().(i2pkeys.I2PAddr).Base32())
if err != nil { if err != nil {
t.Fatalf("Get Error: %q\n", err) t.Fatalf("Get Error: %q\n", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
t.Log("Get returned ", resp)
}() }()
time.Sleep(time.Second * 15)
go func() { go func() {
resp, err := client.Get("http://" + listener3.Addr().(i2pkeys.I2PAddr).Base32()) resp, err := client.Get("http://" + listener3.Addr().(i2pkeys.I2PAddr).Base32())
if err != nil { if err != nil {
t.Fatalf("Get Error: %q\n", err) t.Fatalf("Get Error: %q\n", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
t.Log("Get returned ", resp)
}() }()
time.Sleep(time.Second * 15) time.Sleep(time.Second * 45)
} }
func teardown(t *testing.T) { func TestClientHello(t *testing.T) {
client, err := NewClientFromOptions(SetDebug(false))
if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}
t.Log(client.Base32())
if err := client.Close(); err != nil { if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err) t.Fatalf("client.Close() Error: %q\n", err)
} }
} }
func TestClientHello(t *testing.T) {
setup(t)
t.Log(client.Base32())
teardown(t)
}
func TestNewDestination(t *testing.T) { func TestNewDestination(t *testing.T) {
setup(t) client, err := NewClientFromOptions(SetDebug(false))
if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}
t.Log(client.Base32()) t.Log(client.Base32())
if _, err := client.NewDestination(SAMsigTypes[3]); err != nil { if s, p, err := client.NewDestination(SAMsigTypes[3]); err != nil {
t.Error(err) t.Error(err)
} else {
t.Log(s, p)
}
if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err)
} }
teardown(t)
} }

28
dial.go
View File

@ -64,36 +64,20 @@ func (c *Client) DialStreamingContextFree(addr string) (net.Conn, error) {
return nil, err return nil, err
} }
c.destination, err = c.CreateStreamSession(c.id, c.destination) if c.destination == "" {
if err != nil { c.destination, err = c.CreateStreamSession(c.destination)
c.Close()
d, err := c.NewClient(c.id + 1) /**/
if err != nil { if err != nil {
return nil, err return nil, err
} }
d.destination, err = d.CreateStreamSession(d.id, c.destination) }
d, err := c.NewClient(c.NewID())
if err != nil { if err != nil {
return nil, err return nil, err
} }
d, err = d.NewClient(d.id) err = d.StreamConnect(addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// d.lastaddr = addr
err = d.StreamConnect(d.id, addr)
if err != nil {
return nil, err
}
c = d
return d.SamConn, nil return d.SamConn, nil
} }
c, err = c.NewClient(c.id)
if err != nil {
return nil, err
}
err = c.StreamConnect(c.id, addr)
if err != nil {
return nil, err
}
return c.SamConn, nil
}

View File

@ -17,7 +17,7 @@ func (c *Client) Lookup(name string) (string, error) {
// TODO: move check into sendCmd() // TODO: move check into sendCmd()
if r.Topic != "NAMING" || r.Type != "REPLY" { if r.Topic != "NAMING" || r.Type != "REPLY" {
return "", fmt.Errorf("Naming Unknown Reply: %+v\n", r) return "", fmt.Errorf("Naming Unknown Reply: %s, %s\n", r.Topic, r.Type)
} }
result := r.Pairs["RESULT"] result := r.Pairs["RESULT"]

View File

@ -10,8 +10,10 @@ import (
func TestClientLookupInvalid(t *testing.T) { func TestClientLookupInvalid(t *testing.T) {
var err error var err error
setup(t) client, err := NewClientFromOptions(SetDebug(false))
defer teardown(t) if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}
addr, err := client.Lookup(`!(@#)`) addr, err := client.Lookup(`!(@#)`)
if addr != "" || err == nil { if addr != "" || err == nil {
@ -25,6 +27,9 @@ func TestClientLookupInvalid(t *testing.T) {
if repErr.Result != ResultKeyNotFound { if repErr.Result != ResultKeyNotFound {
t.Errorf("client.Lookup() should throw an ResultKeyNotFound error.\nGot:%+v%s%s\n", repErr, "!=", ResultKeyNotFound) t.Errorf("client.Lookup() should throw an ResultKeyNotFound error.\nGot:%+v%s%s\n", repErr, "!=", ResultKeyNotFound)
} }
if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err)
}
} }
func TestClientLookupValid(t *testing.T) { func TestClientLookupValid(t *testing.T) {

View File

@ -31,12 +31,12 @@ func (c *Client) validCmd(str string, args ...interface{}) (string, error) {
func (c *Client) validCreate() (string, error) { func (c *Client) validCreate() (string, error) {
id := rand.Int31n(math.MaxInt32) id := rand.Int31n(math.MaxInt32)
result, err := c.validCmd("SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s %s\n", id, "abc.i2p", client.allOptions()) result, err := c.validCmd("SESSION CREATE STYLE=STREAM ID=%d DESTINATION=%s %s\n", id, "abc.i2p", c.allOptions())
return result, err return result, err
} }
func TestOptionAddrString(t *testing.T) { func TestOptionAddrString(t *testing.T) {
client, err := NewClientFromOptions(SetAddr("127.0.0.1:7656"), SetDebug(true)) client, err := NewClientFromOptions(SetAddr("127.0.0.1:7656"), SetDebug(false))
if err != nil { if err != nil {
t.Fatalf("NewClientFromOptions() Error: %q\n", err) t.Fatalf("NewClientFromOptions() Error: %q\n", err)
} }
@ -45,17 +45,17 @@ func TestOptionAddrString(t *testing.T) {
} else { } else {
t.Log(result) t.Log(result)
} }
dest, _ := client.CreateStreamSession(client.NewID(), "") _, err = client.CreateStreamSession("")
if err := client.Close(); err != nil { if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err) t.Fatalf("client.Close() Error: %q\n", err)
} }
fmt.Printf("\t destination- %s \n", dest) /* fmt.Printf("\t destination- %s \n", dest)
fmt.Printf("\t address64- %s \t", client.Base64()) fmt.Printf("\t address64- %s \t", client.Base64())
fmt.Printf("\t address- %s \t", client.Base32()) fmt.Printf("\t address- %s \t", client.Base32())*/
} }
func TestOptionAddrStringLh(t *testing.T) { func TestOptionAddrStringLh(t *testing.T) {
client, err := NewClientFromOptions(SetAddr("localhost:7656"), SetDebug(true)) client, err := NewClientFromOptions(SetAddr("localhost:7656"), SetDebug(false))
if err != nil { if err != nil {
t.Fatalf("NewClientFromOptions() Error: %q\n", err) t.Fatalf("NewClientFromOptions() Error: %q\n", err)
} }
@ -64,17 +64,17 @@ func TestOptionAddrStringLh(t *testing.T) {
} else { } else {
t.Log(result) t.Log(result)
} }
dest, _ := client.CreateStreamSession(client.NewID(), "") _, err = client.CreateStreamSession("")
if err := client.Close(); err != nil { if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err) t.Fatalf("client.Close() Error: %q\n", err)
} }
fmt.Printf("\t destination- %s \n", dest) /* fmt.Printf("\t destination- %s \n", dest)
fmt.Printf("\t address64- %s \t", client.Base64()) fmt.Printf("\t address64- %s \t", client.Base64())
fmt.Printf("\t address- %s \t", client.Base32()) fmt.Printf("\t address- %s \t", client.Base32())*/
} }
func TestOptionAddrSlice(t *testing.T) { func TestOptionAddrSlice(t *testing.T) {
client, err := NewClientFromOptions(SetAddr("127.0.0.1", "7656"), SetDebug(true)) client, err := NewClientFromOptions(SetAddr("127.0.0.1", "7656"), SetDebug(false))
if err != nil { if err != nil {
t.Fatalf("NewClientFromOptions() Error: %q\n", err) t.Fatalf("NewClientFromOptions() Error: %q\n", err)
} }
@ -83,17 +83,17 @@ func TestOptionAddrSlice(t *testing.T) {
} else { } else {
t.Log(result) t.Log(result)
} }
dest, _ := client.CreateStreamSession(client.NewID(), "") _, err = client.CreateStreamSession("")
if err := client.Close(); err != nil { if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err) t.Fatalf("client.Close() Error: %q\n", err)
} }
fmt.Printf("\t destination- %s \n", dest) /* fmt.Printf("\t destination- %s \n", dest)
fmt.Printf("\t address64- %s \t", client.Base64()) fmt.Printf("\t address64- %s \t", client.Base64())
fmt.Printf("\t address- %s \t", client.Base32()) fmt.Printf("\t address- %s \t", client.Base32())*/
} }
func TestOptionAddrMixedSlice(t *testing.T) { func TestOptionAddrMixedSlice(t *testing.T) {
client, err := NewClientFromOptions(SetAddrMixed("127.0.0.1", 7656), SetDebug(true)) client, err := NewClientFromOptions(SetAddrMixed("127.0.0.1", 7656), SetDebug(false))
if err != nil { if err != nil {
t.Fatalf("NewClientFromOptions() Error: %q\n", err) t.Fatalf("NewClientFromOptions() Error: %q\n", err)
} }
@ -102,13 +102,13 @@ func TestOptionAddrMixedSlice(t *testing.T) {
} else { } else {
t.Log(result) t.Log(result)
} }
dest, _ := client.CreateStreamSession(client.NewID(), "") _, err = client.CreateStreamSession("")
if err := client.Close(); err != nil { if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err) t.Fatalf("client.Close() Error: %q\n", err)
} }
fmt.Printf("\t destination- %s \n", dest) /* fmt.Printf("\t destination- %s \n", dest)
fmt.Printf("\t address64- %s \t", client.Base64()) fmt.Printf("\t address64- %s \t", client.Base64())
fmt.Printf("\t address- %s \t", client.Base32()) fmt.Printf("\t address- %s \t", client.Base32())*/
} }
func TestOptionHost(t *testing.T) { func TestOptionHost(t *testing.T) {
@ -124,7 +124,7 @@ func TestOptionHost(t *testing.T) {
SetInBackups(2), SetInBackups(2),
SetOutBackups(2), SetOutBackups(2),
SetEncrypt(true), SetEncrypt(true),
SetDebug(true), SetDebug(false),
SetUnpublished(true), SetUnpublished(true),
SetReduceIdle(true), SetReduceIdle(true),
SetReduceIdleTime(300001), SetReduceIdleTime(300001),
@ -140,13 +140,13 @@ func TestOptionHost(t *testing.T) {
} else { } else {
t.Log(result) t.Log(result)
} }
dest, _ := client.CreateStreamSession(client.NewID(), "") _, err = client.CreateStreamSession("")
if err := client.Close(); err != nil { if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err) t.Fatalf("client.Close() Error: %q\n", err)
} }
fmt.Printf("\t destination- %s \n", dest) /* fmt.Printf("\t destination- %s \n", dest)
fmt.Printf("\t address64- %s \t", client.Base64()) fmt.Printf("\t address64- %s \t", client.Base64())
fmt.Printf("\t address- %s \t", client.Base32()) fmt.Printf("\t address- %s \t", client.Base32())*/
} }
func TestOptionPortInt(t *testing.T) { func TestOptionPortInt(t *testing.T) {
@ -162,7 +162,7 @@ func TestOptionPortInt(t *testing.T) {
SetInBackups(2), SetInBackups(2),
SetOutBackups(2), SetOutBackups(2),
SetEncrypt(true), SetEncrypt(true),
SetDebug(true), SetDebug(false),
SetUnpublished(true), SetUnpublished(true),
SetReduceIdle(true), SetReduceIdle(true),
SetReduceIdleTime(300001), SetReduceIdleTime(300001),
@ -178,11 +178,11 @@ func TestOptionPortInt(t *testing.T) {
} else { } else {
t.Log(result) t.Log(result)
} }
dest, _ := client.CreateStreamSession(client.NewID(), "") _, err = client.CreateStreamSession("")
if err := client.Close(); err != nil { if err := client.Close(); err != nil {
t.Fatalf("client.Close() Error: %q\n", err) t.Fatalf("client.Close() Error: %q\n", err)
} }
fmt.Printf("\t destination- %s \n", dest) /* fmt.Printf("\t destination- %s \n", dest)
fmt.Printf("\t address64- %s \t", client.Base64()) fmt.Printf("\t address64- %s \t", client.Base64())
fmt.Printf("\t address- %s \t", client.Base32()) fmt.Printf("\t address- %s \t", client.Base32())*/
} }

View File

@ -13,15 +13,15 @@ func init() {
// CreateSession creates a new STREAM Session. // CreateSession creates a new STREAM Session.
// Returns the Id for the new Client. // Returns the Id for the new Client.
func (c *Client) CreateSession(id int32, style, dest string) (string, error) { func (c *Client) CreateSession(style, dest string) (string, error) {
if dest == "" { if dest == "" {
dest = "TRANSIENT" dest = "TRANSIENT"
} }
c.id = id // c.id = id
r, err := c.sendCmd( r, err := c.sendCmd(
"SESSION CREATE STYLE=%s ID=%d DESTINATION=%s %s %s %s %s \n", "SESSION CREATE STYLE=%s ID=%s DESTINATION=%s %s %s %s %s \n",
style, style,
c.id, c.ID(),
dest, dest,
c.from(), c.from(),
c.to(), c.to(),
@ -47,18 +47,18 @@ func (c *Client) CreateSession(id int32, style, dest string) (string, error) {
// CreateStreamSession creates a new STREAM Session. // CreateStreamSession creates a new STREAM Session.
// Returns the Id for the new Client. // Returns the Id for the new Client.
func (c *Client) CreateStreamSession(id int32, dest string) (string, error) { func (c *Client) CreateStreamSession(dest string) (string, error) {
return c.CreateSession(id, "STREAM", dest) return c.CreateSession("STREAM", dest)
} }
// CreateDatagramSession creates a new DATAGRAM Session. // CreateDatagramSession creates a new DATAGRAM Session.
// Returns the Id for the new Client. // Returns the Id for the new Client.
func (c *Client) CreateDatagramSession(id int32, dest string) (string, error) { func (c *Client) CreateDatagramSession(dest string) (string, error) {
return c.CreateSession(id, "DATAGRAM", dest) return c.CreateSession("DATAGRAM", dest)
} }
// CreateRawSession creates a new RAW Session. // CreateRawSession creates a new RAW Session.
// Returns the Id for the new Client. // Returns the Id for the new Client.
func (c *Client) CreateRawSession(id int32, dest string) (string, error) { func (c *Client) CreateRawSession(dest string) (string, error) {
return c.CreateSession(id, "RAW", dest) return c.CreateSession("RAW", dest)
} }

View File

@ -5,11 +5,11 @@ import (
) )
// StreamConnect asks SAM for a TCP-Like connection to dest, has to be called on a new Client // StreamConnect asks SAM for a TCP-Like connection to dest, has to be called on a new Client
func (c *Client) StreamConnect(id int32, dest string) error { func (c *Client) StreamConnect(dest string) error {
if dest == "" { if dest == "" {
return nil return nil
} }
r, err := c.sendCmd("STREAM CONNECT ID=%d DESTINATION=%s %s %s\n", id, dest, c.from(), c.to()) r, err := c.sendCmd("STREAM CONNECT ID=%s DESTINATION=%s %s %s\n", c.ID(), dest, c.from(), c.to())
if err != nil { if err != nil {
return err return err
} }
@ -28,8 +28,8 @@ func (c *Client) StreamConnect(id int32, dest string) error {
} }
// StreamAccept asks SAM to accept a TCP-Like connection // StreamAccept asks SAM to accept a TCP-Like connection
func (c *Client) StreamAccept(id int32) (*Reply, error) { func (c *Client) StreamAccept() (*Reply, error) {
r, err := c.sendCmd("STREAM ACCEPT ID=%d SILENT=false\n", id) r, err := c.sendCmd("STREAM ACCEPT ID=%s SILENT=false\n", c.ID())
if err != nil { if err != nil {
return nil, err return nil, err
} }