From cfdb60d9327b3864fc36d1121bd5a47aadbd72b4 Mon Sep 17 00:00:00 2001 From: idk Date: Thu, 2 Jun 2022 23:56:49 -0400 Subject: [PATCH] work on datagram support. add the ability to get the private keys from the client. --- client.go | 11 +++++++-- datagram.go | 66 +++++++++++++++++++++++++++++++++++++++++------------ dial.go | 15 ++++++++++-- 3 files changed, 73 insertions(+), 19 deletions(-) diff --git a/client.go b/client.go index fb49404..a406630 100644 --- a/client.go +++ b/client.go @@ -27,8 +27,8 @@ type Client struct { user string pass string - SamConn net.Conn // Control socket - SamDGConn DatagramConn // Datagram socket + SamConn net.Conn // Control socket + SamDGConn net.PacketConn // Datagram socket rd *bufio.Reader // d *Client @@ -206,6 +206,13 @@ func (p *Client) LocalAddr() net.Addr { return p.Addr() } +// LocalKeys returns the local keys of the client as a fully-fledged i2pkeys.I2PKeys +func (p *Client) PrivateAddr() i2pkeys.I2PKeys { + //keys := i2pkeys.I2PAddr(p.Destination()) + keys := i2pkeys.NewKeys(i2pkeys.I2PAddr(p.base64()), p.Destination()) + return keys +} + //return the combined host:port of the SAM bridge func (c *Client) samaddr() string { return fmt.Sprintf("%s:%s", c.host, c.port) diff --git a/datagram.go b/datagram.go index 3860c50..5a0bdd5 100644 --- a/datagram.go +++ b/datagram.go @@ -6,20 +6,56 @@ import ( ) // DatagramConn -type DatagramConn interface { - ReadFrom(p []byte) (n int, addr net.Addr, err error) - Read(b []byte) (n int, err error) - WriteTo(p []byte, addr net.Addr) (n int, err error) - Write(b []byte) (n int, err error) - Close() error - LocalAddr() net.Addr - RemoteAddr() net.Addr - SetDeadline(t time.Time) error - SetReadDeadline(t time.Time) error - SetWriteDeadline(t time.Time) error +type DatagramConn struct { + RWC + conn net.PacketConn + RAddr net.Addr } -/** - * When datagram support is finished, this will compile. - * var conn DatagramConn = &Client{} -**/ +// WrapConn wraps a net.PacketConn in a DatagramConn. +func WrapPacketConn(c net.Conn) *Conn { + wrap := Conn{ + conn: c, + } + wrap.Reader = NewReadLogger("<", c) + wrap.Writer = NewWriteLogger(">", c) + wrap.RWC.c = c + return &wrap +} + +func (d *DatagramConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { + return d.conn.ReadFrom(p) +} +func (d *DatagramConn) Read(b []byte) (n int, err error) { + n, _, err = d.ReadFrom(b) + return n, err +} +func (d *DatagramConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { + return d.conn.WriteTo(p, addr) +} +func (d *DatagramConn) Write(b []byte) (n int, err error) { + n, err = d.WriteTo(b, d.RemoteAddr()) + return n, err +} +func (d *DatagramConn) Close() error { + return d.conn.Close() +} +func (d *DatagramConn) LocalAddr() net.Addr { + return d.conn.LocalAddr() +} +func (d *DatagramConn) RemoteAddr() net.Addr { + return d.RAddr +} +func (d *DatagramConn) SetDeadline(t time.Time) error { + return d.conn.SetDeadline(t) +} +func (d *DatagramConn) SetReadDeadline(t time.Time) error { + return d.conn.SetReadDeadline(t) +} +func (d *DatagramConn) SetWriteDeadline(t time.Time) error { + return d.conn.SetWriteDeadline(t) +} + +var dgt net.PacketConn = &DatagramConn{} + +//func (c *Client) DatagramSend() diff --git a/dial.go b/dial.go index 6ad4127..f38c6dc 100644 --- a/dial.go +++ b/dial.go @@ -39,7 +39,7 @@ func (c *Client) Dial(network, addr string) (net.Conn, error) { return c.DialContext(context.TODO(), network, addr) } -// Dial implements the net.Dial function and can be used for http.Transport +// DialContextFree implements the net.Dial function and can be used for http.Transport func (c *Client) DialContextFree(network, addr string) (net.Conn, error) { if network == "tcp" || network == "tcp6" || network == "tcp4" { return c.DialStreamingContextFree(addr) @@ -55,10 +55,21 @@ func (c *Client) DialContextFree(network, addr string) (net.Conn, error) { // 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) { +func (c *Client) DialDatagramContextFree(addr string) (*DatagramConn, error) { + portIdx := strings.Index(addr, ":") + if portIdx >= 0 { + addr = addr[:portIdx] + } + addr, err := c.Lookup(addr) + if err != nil { + log.Printf("LOOKUP DIALER ERROR %s %s", addr, err) + return nil, err + } + return nil, fmt.Errorf("Datagram support is not finished yet, come back later`") } +// DialStreamingContextFree is a "Dialer" for "Client-Like" Streaming connections. func (c *Client) DialStreamingContextFree(addr string) (net.Conn, error) { portIdx := strings.Index(addr, ":") if portIdx >= 0 {