diff --git a/accept.go b/accept.go index 4c4dba3..a93a7ad 100644 --- a/accept.go +++ b/accept.go @@ -32,7 +32,7 @@ func (c *Client) ListenI2P(dest string) (net.Listener, error) { } fmt.Println("Listening on destination:", c.Base32()+".b32.i2p") - c, err = c.NewClient() + c, err = c.NewClient(c.id) if err != nil { return nil, err } diff --git a/client.go b/client.go index 3403b46..7dcc9aa 100644 --- a/client.go +++ b/client.go @@ -67,6 +67,12 @@ var SAMsigTypes = []string{ "SIGNATURE_TYPE=EdDSA_SHA512_Ed25519", } +var ValidSAMCommands = []string{ + "HELLO", + "SESSION", + "STREAM", +} + var ( i2pB64enc *base64.Encoding = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~") i2pB32enc *base32.Encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567") @@ -143,7 +149,7 @@ func NewClientFromOptions(opts ...func(*Client) error) (*Client, error) { c.reduceIdleTime = 300000 c.reduceIdleQuantity = 1 c.closeIdle = true - c.closeIdleTime = 60000000 + c.closeIdleTime = 600000 c.debug = false c.sigType = SAMsigTypes[4] c.id = 0 @@ -157,6 +163,7 @@ func NewClientFromOptions(opts ...func(*Client) error) (*Client, error) { return nil, err } } + c.id = c.NewID() conn, err := net.DialTimeout("tcp", c.samaddr(), 3*time.Minute) if err != nil { return nil, err @@ -220,8 +227,10 @@ func (c *Client) Close() error { return c.SamConn.Close() } -// NewClient generates an exact copy of the client with the same options -func (c *Client) NewClient() (*Client, error) { +// NewClient generates an exact copy of the client with the same options, but +// re-does all the handshaky business so that Dial can pick up right where it +// left off, should the need arise. +func (c *Client) NewClient(id int32) (*Client, error) { return NewClientFromOptions( SetHost(c.host), SetPort(c.port), @@ -242,7 +251,6 @@ func (c *Client) NewClient() (*Client, error) { SetCloseIdle(c.closeIdle), SetCloseIdleTime(c.closeIdleTime), SetCompression(c.compression), - setlastaddr(c.lastaddr), - setid(c.id), + setid(id), ) } diff --git a/dial.go b/dial.go index 4820a6e..ecbf6b4 100644 --- a/dial.go +++ b/dial.go @@ -25,12 +25,10 @@ func (c *Client) DialContext(ctx context.Context, network, addr string) (net.Con }() select { case err := <-errCh: - c.Close() return nil, err case conn := <-connCh: return conn, nil case <-ctx.Done(): - c.Close() return nil, ctx.Err() } } @@ -38,16 +36,16 @@ func (c *Client) DialContext(ctx context.Context, network, addr string) (net.Con func (c *Client) dialCheck(addr string) (int32, bool) { if c.lastaddr == "invalid" { return c.NewID(), true - } else if addr == "" { - return c.id, false + // } else if addr == "" { + // return c.id, false } else if c.lastaddr != addr { return c.NewID(), true } - return c.id, false + return c.NewID(), false } func (c *Client) Dial(network, addr string) (net.Conn, error) { - return c.DialContext(context.Background(), network, addr) + return c.DialContextFree(network, addr) } // Dial implements the net.Dial function and can be used for http.Transport @@ -60,16 +58,17 @@ func (c *Client) DialContextFree(network, addr string) (net.Conn, error) { } addr, err := c.Lookup(addr) if err != nil { + log.Printf("LOOKUP DIALER ERROR %s %s", addr, err) return nil, err } - // var test bool - c.id, _ = c.dialCheck(addr) + // var test bool + //c.id, _ = c.dialCheck(addr) + // log.Println("Address indicates we need to dial a new session.") // if test { c.destination, err = c.CreateStreamSession(c.id, c.destination) if err != nil { - c.id += 1 - c, err = c.NewClient() + c, err = c.NewClient(c.id + 1) if err != nil { return nil, err } @@ -78,15 +77,16 @@ func (c *Client) DialContextFree(network, addr string) (net.Conn, error) { return nil, err } } - c, err = c.NewClient() + // } + c, err = c.NewClient(c.id) if err != nil { return nil, err } c.lastaddr = addr - //} err = c.StreamConnect(c.id, addr) if err != nil { return nil, err } + //} return c.SamConn, nil } diff --git a/options_test.go b/options_test.go index 7301818..70006d1 100644 --- a/options_test.go +++ b/options_test.go @@ -2,8 +2,6 @@ package goSam -/* - import ( "fmt" "math" @@ -188,4 +186,3 @@ func TestOptionPortInt(t *testing.T) { fmt.Printf("\t address64- %s \t", client.Base64()) fmt.Printf("\t address- %s \t", client.Base32()) } -*/ diff --git a/replyParser.go b/replyParser.go index 6d72f77..d7e6d15 100644 --- a/replyParser.go +++ b/replyParser.go @@ -82,9 +82,7 @@ func parseReply(line string) (*Reply, error) { } else { kvPair := strings.SplitN(v, "=", 2) if kvPair != nil { - if len(kvPair) == 1 { - return nil, fmt.Errorf("Malformed key-value-pair len 1.\n%s\n", kvPair) - } else if len(kvPair) != 2 { + if len(kvPair) != 2 { return nil, fmt.Errorf("Malformed key-value-pair len != 2.\n%s\n", kvPair) } }