4 Commits
v0.32.4 ... dg

Author SHA1 Message Date
idk
55d40a2238 Use the connection wrapper to implement a Datagram based connection, hopefully 2021-04-15 20:24:43 -04:00
idk
b47be771d5 Use the connection wrapper to implement a Datagram based connection, hopefully 2021-04-15 20:19:55 -04:00
idk
8fcab6ce00 Don't delay the test 2021-04-15 19:35:19 -04:00
idk
3760249a67 Don't delay the test 2021-04-15 19:35:02 -04:00
13 changed files with 64 additions and 77 deletions

View File

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

View File

@@ -25,14 +25,19 @@ 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")
if c.debug {
c.SamConn = WrapConn(c.SamConn)
c, err = c.NewClient(c.id)
if err != nil {
return nil, err
}
c.destination = d
c.SamConn = WrapConn(c.SamConn, c.ID(), fmt.Sprintf("3.%d", c.sammax), c.debug)
return c, nil
}

View File

@@ -13,8 +13,6 @@ import (
"strings"
"sync"
"time"
samkeys "github.com/eyedeekay/goSam/compat"
)
// A Client represents a single Connection to the SAM bridge
@@ -24,9 +22,8 @@ type Client struct {
fromport string
toport string
SamConn net.Conn
SamDGConn DatagramConn
rd *bufio.Reader
SamConn *Conn
rd *bufio.Reader
sigType string
destination string
@@ -62,7 +59,6 @@ type Client struct {
sammax int
}
// SAMsigTypes is a slice of the available signature types
var SAMsigTypes = []string{
"SIGNATURE_TYPE=DSA_SHA1",
"SIGNATURE_TYPE=ECDSA_SHA256_P256",
@@ -174,30 +170,17 @@ func NewClientFromOptions(opts ...func(*Client) error) (*Client, error) {
if err != nil {
return nil, err
}
if c.debug {
conn = WrapConn(conn)
}
c.SamConn = conn
c.SamConn = WrapConn(conn, c.ID(), fmt.Sprintf("3.%d", c.sammax), c.debug)
c.rd = bufio.NewReader(conn)
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 {
keys, err := samkeys.DestToKeys(p.Destination())
if err != nil {
return nil
}
return keys.Addr()
}
func (p *Client) LocalAddr() net.Addr {
return p.Addr()
return nil
}
//return the combined host:port of the SAM bridge

View File

@@ -1,4 +1,3 @@
//go:build nettest
// +build nettest
package goSam
@@ -46,7 +45,7 @@ func TestCompositeClient(t *testing.T) {
// http.HandleFunc("/", HelloServer)
go http.Serve(listener3, nil)
sam, err := NewClientFromOptions(SetDebug(true))
sam, err := NewClientFromOptions(SetDebug(false))
if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}
@@ -63,7 +62,7 @@ func TestCompositeClient(t *testing.T) {
}
defer resp.Body.Close()
}()
time.Sleep(time.Second * 15)
// time.Sleep(time.Second * 15)
go func() {
resp, err := client.Get("http://" + listener2.Addr().(i2pkeys.I2PAddr).Base32())
if err != nil {
@@ -71,7 +70,7 @@ func TestCompositeClient(t *testing.T) {
}
defer resp.Body.Close()
}()
time.Sleep(time.Second * 15)
// time.Sleep(time.Second * 15)
go func() {
resp, err := client.Get("http://" + listener3.Addr().(i2pkeys.I2PAddr).Base32())
if err != nil {
@@ -84,7 +83,7 @@ func TestCompositeClient(t *testing.T) {
}
func TestClientHello(t *testing.T) {
client, err := NewClientFromOptions(SetDebug(true))
client, err := NewClientFromOptions(SetDebug(false))
if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}
@@ -95,7 +94,7 @@ func TestClientHello(t *testing.T) {
}
func TestNewDestination(t *testing.T) {
client, err := NewClientFromOptions(SetDebug(true))
client, err := NewClientFromOptions(SetDebug(false))
if err != nil {
t.Fatalf("NewDefaultClient() Error: %q\n", err)
}

36
conn.go
View File

@@ -30,47 +30,55 @@ 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
id string
version string
conn net.Conn
}
// WrapConn wraps a net.Conn in a Conn.
func WrapConn(c net.Conn) *Conn {
var streamingConn net.Conn = Conn
var datagramConn net.PacketConn = Conn
func WrapConn(c net.Conn, id, version string, debug bool) *Conn {
wrap := Conn{
conn: c,
conn: c,
id: id,
version: version,
}
wrap.Reader = NewReadLogger("<", c)
wrap.Writer = NewWriteLogger(">", c)
wrap.Reader = NewReadLogger("<", debug, c)
wrap.Writer = NewWriteLogger(">", debug, c)
wrap.RWC.c = c
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) ReadFrom(b []byte) (int, net.Addr, error) {
return c.ReadFrom(b)
}
func (c *Conn) WriteTo(b []byte, addr net.Addr) (int, error) {
header := []byte(c.version + " " + c.id + " " + addr.String() + "\n")
msg := append(header, b...)
return c.WriteTo(msg, addr)
}
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,7 +5,6 @@ import (
"time"
)
// DatagramConn
type DatagramConn interface {
ReadFrom(p []byte) (n int, addr net.Addr, err error)
Read(b []byte) (n int, err error)
@@ -18,9 +17,3 @@ type DatagramConn interface {
SetReadDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
}
/**
var conn DatagramConn = &Client{}
*/

13
dial.go
View File

@@ -2,7 +2,6 @@ package goSam
import (
"context"
"fmt"
"log"
"net"
"strings"
@@ -53,10 +52,8 @@ 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 nil, fmt.Errorf("Datagram support is not finished yet, come back later`")
return c.SamConn, nil
}
func (c *Client) DialStreamingContextFree(addr string) (net.Conn, error) {
@@ -77,9 +74,13 @@ func (c *Client) DialStreamingContextFree(addr string) (net.Conn, error) {
}
}
err = c.StreamConnect(addr)
d, err := c.NewClient(c.NewID())
if err != nil {
return nil, err
}
return c.SamConn, nil
err = d.StreamConnect(addr)
if err != nil {
return nil, err
}
return d.SamConn, nil
}

View File

@@ -52,7 +52,6 @@ func (c *Client) forward(client, conn net.Conn) {
}
func (c *Client) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
// if c.lastaddr == "invalid" || c.lastaddr != name {
client, err := c.DialContext(ctx, "", name)
if err != nil {
return ctx, nil, err

View File

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

View File

@@ -1,4 +1,3 @@
//go:build nettest
// +build nettest
package goSam
@@ -37,7 +36,7 @@ func (c *Client) validCreate() (string, error) {
}
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 {
t.Fatalf("NewClientFromOptions() Error: %q\n", err)
}
@@ -56,7 +55,7 @@ func TestOptionAddrString(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 {
t.Fatalf("NewClientFromOptions() Error: %q\n", err)
}
@@ -75,7 +74,7 @@ func TestOptionAddrStringLh(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 {
t.Fatalf("NewClientFromOptions() Error: %q\n", err)
}
@@ -94,7 +93,7 @@ func TestOptionAddrSlice(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 {
t.Fatalf("NewClientFromOptions() Error: %q\n", err)
}
@@ -125,7 +124,7 @@ func TestOptionHost(t *testing.T) {
SetInBackups(2),
SetOutBackups(2),
SetEncrypt(true),
SetDebug(true),
SetDebug(false),
SetUnpublished(true),
SetReduceIdle(true),
SetReduceIdleTime(300001),
@@ -163,7 +162,7 @@ func TestOptionPortInt(t *testing.T) {
SetInBackups(2),
SetOutBackups(2),
SetEncrypt(true),
SetDebug(true),
SetDebug(false),
SetUnpublished(true),
SetReduceIdle(true),
SetReduceIdleTime(300001),

10
rw.go
View File

@@ -36,6 +36,7 @@ Copy of testing/iotest Read- and WriteLogger, but using %q instead of %x for pri
type writeLogger struct {
prefix string
debug bool
w io.Writer
}
@@ -52,12 +53,13 @@ func (l *writeLogger) Write(p []byte) (n int, err error) {
// NewWriteLogger returns a writer that behaves like w except
// that it logs (using log.Printf) each write to standard error,
// printing the prefix and the hexadecimal data written.
func NewWriteLogger(prefix string, w io.Writer) io.Writer {
return &writeLogger{prefix, w}
func NewWriteLogger(prefix string, debug bool, w io.Writer) io.Writer {
return &writeLogger{prefix, debug, w}
}
type readLogger struct {
prefix string
debug bool
r io.Reader
}
@@ -74,8 +76,8 @@ func (l *readLogger) Read(p []byte) (n int, err error) {
// NewReadLogger returns a reader that behaves like r except
// that it logs (using log.Print) each read to standard error,
// printing the prefix and the hexadecimal data written.
func NewReadLogger(prefix string, r io.Reader) io.Reader {
return &readLogger{prefix, r}
func NewReadLogger(prefix string, debug bool, r io.Reader) io.Reader {
return &readLogger{prefix, debug, r}
}
type readHexLogger struct {

6
rwc.go
View File

@@ -35,9 +35,9 @@ type RWC struct {
c io.Closer
}
func WrapRWC(c io.ReadWriteCloser) io.ReadWriteCloser {
rl := NewReadLogger("<", c)
wl := NewWriteLogger(">", c)
func WrapRWC(c io.ReadWriteCloser, debug bool) io.ReadWriteCloser {
rl := NewReadLogger("<", debug, c)
wl := NewWriteLogger(">", debug, c)
return &RWC{
Reader: rl,

View File

@@ -11,9 +11,8 @@ func init() {
rand.Seed(time.Now().UnixNano())
}
// 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.
// CreateSession creates a new STREAM Session.
// Returns the Id for the new Client.
func (c *Client) CreateSession(style, dest string) (string, error) {
if dest == "" {
dest = "TRANSIENT"