4 Commits

Author SHA1 Message Date
idk
964219c25f Disable datagram support in the dialer function until it's ready, so I can cut a release tonight. 2022-02-01 21:50:35 -05:00
idk
23c45022b3 tests should run in debug mode. Don't attempt to hot-restore SAM sessions by replacing the net.Conn parts inside if they go down, just let it die and tell the user to restart. 2022-02-01 20:03:13 -05:00
idk
c6d9c0e340 protect the dialer with a mutex 2021-04-15 19:16:11 -04:00
idk
460926afe8 Merge pull request #7 from eyedeekay/no-keys
Don't rely on sam3 for key generation. Cleanup API issues. Improve Stability. Begin to add DG, RAW support.
2021-04-15 14:24:56 -07:00
10 changed files with 58 additions and 29 deletions

View File

@@ -1,6 +1,6 @@
USER_GH=eyedeekay
VERSION=0.32.30
VERSION=0.32.4
packagename=gosam
echo: fmt

View File

@@ -25,18 +25,11 @@ func (c *Client) Listen() (net.Listener, error) {
func (c *Client) ListenI2P(dest string) (net.Listener, error) {
var err error
c.destination, err = c.CreateStreamSession(dest)
d := c.destination
if err != nil {
return nil, err
}
fmt.Println("Listening on destination:", c.Base32()+".b32.i2p")
c, err = c.NewClient(c.id)
if err != nil {
return nil, err
}
c.destination = d
if c.debug {
c.SamConn = WrapConn(c.SamConn)
}

View File

@@ -11,8 +11,10 @@ import (
"math/rand"
"net"
"strings"
// "sync"
"sync"
"time"
samkeys "github.com/eyedeekay/goSam/compat"
)
// A Client represents a single Connection to the SAM bridge
@@ -53,12 +55,14 @@ type Client struct {
compression bool
debug bool
mutex sync.Mutex
//NEVER, EVER modify lastaddr or id yourself. They are used internally only.
id int32
sammin int
sammax int
}
// SAMsigTypes is a slice of the available signature types
var SAMsigTypes = []string{
"SIGNATURE_TYPE=DSA_SHA1",
"SIGNATURE_TYPE=ECDSA_SHA256_P256",
@@ -178,12 +182,22 @@ func NewClientFromOptions(opts ...func(*Client) error) (*Client, error) {
return &c, c.hello()
}
// ID returns a the current ID of the client as a string
func (p *Client) ID() string {
return fmt.Sprintf("%d", p.NewID())
}
// Addr returns the address of the client as a net.Addr
func (p *Client) Addr() net.Addr {
return nil
keys, err := samkeys.DestToKeys(p.Destination())
if err != nil {
return nil
}
return keys.Addr()
}
func (p *Client) LocalAddr() net.Addr {
return p.Addr()
}
//return the combined host:port of the SAM bridge

View File

@@ -1,3 +1,4 @@
//go:build nettest
// +build nettest
package goSam
@@ -45,7 +46,7 @@ func TestCompositeClient(t *testing.T) {
// http.HandleFunc("/", HelloServer)
go http.Serve(listener3, nil)
sam, err := NewClientFromOptions(SetDebug(false))
sam, err := NewClientFromOptions(SetDebug(true))
if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}
@@ -83,7 +84,7 @@ func TestCompositeClient(t *testing.T) {
}
func TestClientHello(t *testing.T) {
client, err := NewClientFromOptions(SetDebug(false))
client, err := NewClientFromOptions(SetDebug(true))
if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}
@@ -94,7 +95,7 @@ func TestClientHello(t *testing.T) {
}
func TestNewDestination(t *testing.T) {
client, err := NewClientFromOptions(SetDebug(false))
client, err := NewClientFromOptions(SetDebug(true))
if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}

View File

@@ -30,11 +30,14 @@ import (
"time"
)
// Conn Read data from the connection, writes data to te connection
// and logs the data in-between.
type Conn struct {
RWC
conn net.Conn
}
// WrapConn wraps a net.Conn in a Conn.
func WrapConn(c net.Conn) *Conn {
wrap := Conn{
conn: c,
@@ -45,23 +48,29 @@ func WrapConn(c net.Conn) *Conn {
return &wrap
}
// LocalAddr returns the local address of the connection.
func (c *Conn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
// RemoteAddr returns the remote address of the connection.
func (c *Conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
// SetDeadline sets the read and write deadlines associated with the connection
func (c *Conn) SetDeadline(t time.Time) error {
log.Println("WARNING: SetDeadline() not sure this works")
return c.conn.SetDeadline(t)
}
// SetReadDeadline sets the read deadline associated with the connection
func (c *Conn) SetReadDeadline(t time.Time) error {
log.Println("WARNING: SetReadDeadline() not sure this works")
return c.conn.SetReadDeadline(t)
}
// SetWriteDeadline sets the write deadline associated with the connection
func (c *Conn) SetWriteDeadline(t time.Time) error {
log.Println("WARNING: SetWriteDeadline() not sure this works")
return c.conn.SetWriteDeadline(t)

View File

@@ -5,6 +5,7 @@ import (
"time"
)
// DatagramConn
type DatagramConn interface {
ReadFrom(p []byte) (n int, addr net.Addr, err error)
Read(b []byte) (n int, err error)
@@ -17,3 +18,9 @@ type DatagramConn interface {
SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
}
/**
var conn DatagramConn = &Client{}
*/

16
dial.go
View File

@@ -2,6 +2,7 @@ package goSam
import (
"context"
"fmt"
"log"
"net"
"strings"
@@ -9,6 +10,8 @@ import (
// DialContext implements the net.DialContext function and can be used for http.Transport
func (c *Client) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
errCh := make(chan error, 1)
connCh := make(chan net.Conn, 1)
go func() {
@@ -29,6 +32,7 @@ func (c *Client) DialContext(ctx context.Context, network, addr string) (net.Con
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (c *Client) Dial(network, addr string) (net.Conn, error) {
@@ -49,8 +53,10 @@ func (c *Client) DialContextFree(network, addr string) (net.Conn, error) {
return c.DialStreamingContextFree(addr)
}
// DialDatagramContextFree is a "Dialer" for "Client-Like" Datagram connections.
// It is also not finished. If you need datagram support right now, use sam3.
func (c *Client) DialDatagramContextFree(addr string) (DatagramConn, error) {
return c.SamDGConn, nil
return nil, fmt.Errorf("Datagram support is not finished yet, come back later`")
}
func (c *Client) DialStreamingContextFree(addr string) (net.Conn, error) {
@@ -71,13 +77,9 @@ func (c *Client) DialStreamingContextFree(addr string) (net.Conn, error) {
}
}
d, err := c.NewClient(c.NewID())
err = c.StreamConnect(addr)
if err != nil {
return nil, err
}
err = d.StreamConnect(addr)
if err != nil {
return nil, err
}
return d.SamConn, nil
return c.SamConn, nil
}

View File

@@ -1,3 +1,4 @@
//go:build nettest
// +build nettest
package goSam
@@ -10,7 +11,7 @@ import (
func TestClientLookupInvalid(t *testing.T) {
var err error
client, err := NewClientFromOptions(SetDebug(false))
client, err := NewClientFromOptions(SetDebug(true))
if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}

View File

@@ -1,3 +1,4 @@
//go:build nettest
// +build nettest
package goSam
@@ -36,7 +37,7 @@ func (c *Client) validCreate() (string, error) {
}
func TestOptionAddrString(t *testing.T) {
client, err := NewClientFromOptions(SetAddr("127.0.0.1:7656"), SetDebug(false))
client, err := NewClientFromOptions(SetAddr("127.0.0.1:7656"), SetDebug(true))
if err != nil {
t.Fatalf("NewClientFromOptions() Error: %q\n", err)
}
@@ -55,7 +56,7 @@ func TestOptionAddrString(t *testing.T) {
}
func TestOptionAddrStringLh(t *testing.T) {
client, err := NewClientFromOptions(SetAddr("localhost:7656"), SetDebug(false))
client, err := NewClientFromOptions(SetAddr("localhost:7656"), SetDebug(true))
if err != nil {
t.Fatalf("NewClientFromOptions() Error: %q\n", err)
}
@@ -74,7 +75,7 @@ func TestOptionAddrStringLh(t *testing.T) {
}
func TestOptionAddrSlice(t *testing.T) {
client, err := NewClientFromOptions(SetAddr("127.0.0.1", "7656"), SetDebug(false))
client, err := NewClientFromOptions(SetAddr("127.0.0.1", "7656"), SetDebug(true))
if err != nil {
t.Fatalf("NewClientFromOptions() Error: %q\n", err)
}
@@ -93,7 +94,7 @@ func TestOptionAddrSlice(t *testing.T) {
}
func TestOptionAddrMixedSlice(t *testing.T) {
client, err := NewClientFromOptions(SetAddrMixed("127.0.0.1", 7656), SetDebug(false))
client, err := NewClientFromOptions(SetAddrMixed("127.0.0.1", 7656), SetDebug(true))
if err != nil {
t.Fatalf("NewClientFromOptions() Error: %q\n", err)
}
@@ -124,7 +125,7 @@ func TestOptionHost(t *testing.T) {
SetInBackups(2),
SetOutBackups(2),
SetEncrypt(true),
SetDebug(false),
SetDebug(true),
SetUnpublished(true),
SetReduceIdle(true),
SetReduceIdleTime(300001),
@@ -162,7 +163,7 @@ func TestOptionPortInt(t *testing.T) {
SetInBackups(2),
SetOutBackups(2),
SetEncrypt(true),
SetDebug(false),
SetDebug(true),
SetUnpublished(true),
SetReduceIdle(true),
SetReduceIdleTime(300001),

View File

@@ -11,8 +11,9 @@ func init() {
rand.Seed(time.Now().UnixNano())
}
// CreateSession creates a new STREAM Session.
// Returns the Id for the new Client.
// CreateSession creates a new Session of type style, with an optional destination.
// an empty destination is interpreted as "TRANSIENT"
// Returns the destination for the new Client or an error.
func (c *Client) CreateSession(style, dest string) (string, error) {
if dest == "" {
dest = "TRANSIENT"