revert back to pointers

This commit is contained in:
idk
2019-02-09 17:01:29 -05:00
parent 554202709f
commit ab5663c157
4 changed files with 129 additions and 24 deletions

View File

@ -81,14 +81,14 @@ func DestHashFromString(str string) (dhash I2PDestHash, err error) {
}
// get string representation of i2p dest hash
func (h I2PDestHash) String() string {
func (h *I2PDestHash) String() string {
b32addr := make([]byte, 56)
i2pB32enc.Encode(b32addr, h[:])
return string(b32addr[:52]) + ".b32.i2p"
}
// Returns "I2P"
func (h I2PDestHash) Network() string {
func (h *I2PDestHash) Network() string {
return "I2P"
}
@ -135,7 +135,7 @@ func FiveHundredAs() I2PAddr {
s += "A"
}
r, _ := NewI2PAddrFromString(s)
return r
return r
}
// Creates a new I2P address from a byte array. The inverse of ToBytes().
@ -172,7 +172,7 @@ func (addr I2PAddr) Base32() (str string) {
return addr.DestHash().String()
}
func (addr I2PAddr) DestHash() (h I2PDestHash) {
func (addr I2PAddr) DestHash() (h *I2PDestHash) {
hash := sha256.New()
b, _ := addr.ToBytes()
hash.Write(b)

105
README.md.asc Normal file
View File

@ -0,0 +1,105 @@
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
# README #
go library for the I2P [SAMv3.0](https://geti2p.net/en/docs/api/samv3) bridge, used to build anonymous/pseudonymous end-to-end encrypted sockets.
This library is much better than ccondom (that use BOB), much more stable and much easier to maintain.
## Support/TODO ##
**What works:**
* Utils
* Resolving domain names to I2P destinations
* .b32.i2p hashes
* Generating keys/i2p destinations
* Streaming
* DialI2P() - Connecting to stuff in I2P
* Listen()/Accept() - Handling incomming connections
* Implements net.Conn and net.Listener
* Datagrams
* Implements net.PacketConn
* Raw datagrams
* Like datagrams, but without addresses
**Does not work:**
* Everything works! :D
* Probably needs some real-world testing
## Documentation ##
* Latest version-documentation:
* set your GOPATH
* Enter `godoc -http=:8081` into your terminal and hit enter.
* Goto http://localhost:8081, click packages, and navigate to sam3
## Examples ##
```go
package main
import (
"github.com/majestrate/i2p-tools/sam3"
"fmt"
)
const yoursam = "127.0.0.1:7656" // sam bridge
func client(server I2PAddr) {
sam, _ := NewSAM(yoursam)
keys, _ := sam.NewKeys()
stream, _ := sam.NewStreamSession("clientTun", keys, Options_Small)
fmt.Println("Client: Connecting to " + server.Base32())
conn, _ := stream.DialI2P(server)
conn.Write([]byte("Hello world!"))
return
}
func main() {
sam, _ := NewSAM(yoursam)
keys, _ := sam.NewKeys()
go client(keys.Addr())
stream, _ := sam.NewStreamSession("serverTun", keys, Options_Medium)
listener, _ := stream.Listen()
conn, _ := listener.Accept()
buf := make([]byte, 4096)
n, _ := conn.Read(buf)
fmt.Println("Server received: " + string(buf[:n]))
}
```
The above will write to the terminal:
```text
Client: Connecting to zjnvfh4hs3et5vtz35ogwzrws26zvwkcad5uo5esecvg4qpk5b4a.b32.i2p
Server received: Hello world!
```
Error handling was omitted in the above code for readability.
## Testing ##
* `go test` runs the whole suite (takes 90+ sec to perform!)
* `go test -short` runs the shorter variant, does not connect to anything
## License ##
Public domain.
## Author ##
* Kalle Vedin `kalle.vedin@fripost.org`
* Unknown Name (majestrate)
-----BEGIN PGP SIGNATURE-----
iQEzBAEBCgAdFiEEcNIGBzi++AUjrK/311wDs5teFOEFAlxfTbUACgkQ11wDs5te
FOHRHwf+IvUudGs75maj3pAZslmKvR4S8oiWo4uDFIwi0QyCXBz/Ce0YWIgNYV/2
SR1I5hkmIJGXpTOhx5/xxAb62QAg1AcCO0nLgq4avd5TQJp2nS5PRdlS5teqvpZZ
P6NrjxEZpdj1Ke2xFn9IFseh6XM7JKuFi8/EH6BkbAWW7rdaJKdJdfdajGPgmnZL
xJ0EywI0Imw+3eJPxgvATCrGVMDvxtwSjTVNubrZCCf8Cl3T2u5mt13c5HTKr6PO
pMa3QZF6zXnZKLLofJ8mywrvFyGoX3Uku0yXjlMx7zX0QQRBUPXJ5cPwuL8jdkZb
yCXwni5u1mHsIgtVZRWAcm3OxBXA6A==
=gt5S
-----END PGP SIGNATURE-----

View File

@ -13,51 +13,51 @@ type SAMConn struct {
}
// Implements net.Conn
func (sc SAMConn) Read(buf []byte) (int, error) {
func (sc *SAMConn) Read(buf []byte) (int, error) {
n, err := sc.conn.Read(buf)
return n, err
}
// Implements net.Conn
func (sc SAMConn) Write(buf []byte) (int, error) {
func (sc *SAMConn) Write(buf []byte) (int, error) {
n, err := sc.conn.Write(buf)
return n, err
}
// Implements net.Conn
func (sc SAMConn) Close() error {
func (sc *SAMConn) Close() error {
return sc.conn.Close()
}
func (sc SAMConn) LocalAddr() net.Addr {
func (sc *SAMConn) LocalAddr() net.Addr {
return sc.localAddr()
}
// Implements net.Conn
func (sc SAMConn) localAddr() I2PAddr {
func (sc *SAMConn) localAddr() I2PAddr {
return sc.laddr
}
func (sc SAMConn) RemoteAddr() net.Addr {
func (sc *SAMConn) RemoteAddr() net.Addr {
return sc.remoteAddr()
}
// Implements net.Conn
func (sc SAMConn) remoteAddr() I2PAddr {
func (sc *SAMConn) remoteAddr() I2PAddr {
return sc.raddr
}
// Implements net.Conn
func (sc SAMConn) SetDeadline(t time.Time) error {
func (sc *SAMConn) SetDeadline(t time.Time) error {
return sc.conn.SetDeadline(t)
}
// Implements net.Conn
func (sc SAMConn) SetReadDeadline(t time.Time) error {
func (sc *SAMConn) SetReadDeadline(t time.Time) error {
return sc.conn.SetReadDeadline(t)
}
// Implements net.Conn
func (sc SAMConn) SetWriteDeadline(t time.Time) error {
func (sc *SAMConn) SetWriteDeadline(t time.Time) error {
return sc.conn.SetWriteDeadline(t)
}

View File

@ -60,14 +60,14 @@ func (s *SAM) NewDatagramSession(id string, keys I2PKeys, options []string, udpP
return &DatagramSession{s.address, id, conn, udpconn, keys, rUDPAddr}, nil
}
func (s DatagramSession) B32() string {
func (s *DatagramSession) B32() string {
return s.keys.Addr().Base32()
}
// Reads one datagram sent to the destination of the DatagramSession. Returns
// the number of bytes read, from what address it was sent, or an error.
// implements net.PacketConn
func (s DatagramSession) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
func (s *DatagramSession) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
// extra bytes to read the remote address of incomming datagram
buf := make([]byte, len(b)+4096)
@ -104,7 +104,7 @@ func (s DatagramSession) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
// Sends one signed datagram to the destination specified. At the time of
// writing, maximum size is 31 kilobyte, but this may change in the future.
// Implements net.PacketConn.
func (s DatagramSession) WriteTo(b []byte, addr net.Addr) (n int, err error) {
func (s *DatagramSession) WriteTo(b []byte, addr net.Addr) (n int, err error) {
header := []byte("3.0 " + s.id + " " + addr.String() + "\n")
msg := append(header, b...)
n, err = s.udpconn.WriteToUDP(msg, s.rUDPAddr)
@ -112,7 +112,7 @@ func (s DatagramSession) WriteTo(b []byte, addr net.Addr) (n int, err error) {
}
// Closes the DatagramSession. Implements net.PacketConn
func (s DatagramSession) Close() error {
func (s *DatagramSession) Close() error {
err := s.conn.Close()
err2 := s.udpconn.Close()
if err != nil {
@ -122,16 +122,16 @@ func (s DatagramSession) Close() error {
}
// Returns the I2P destination of the DatagramSession.
func (s DatagramSession) LocalI2PAddr() I2PAddr {
func (s *DatagramSession) LocalI2PAddr() I2PAddr {
return s.keys.Addr()
}
// Implements net.PacketConn
func (s DatagramSession) LocalAddr() net.Addr {
func (s *DatagramSession) LocalAddr() net.Addr {
return s.LocalI2PAddr()
}
func (s DatagramSession) Lookup(name string) (a net.Addr, err error) {
func (s *DatagramSession) Lookup(name string) (a net.Addr, err error) {
var sam *SAM
sam, err = NewSAM(s.samAddr)
if err == nil {
@ -144,16 +144,16 @@ func (s DatagramSession) Lookup(name string) (a net.Addr, err error) {
// Sets read and write deadlines for the DatagramSession. Implements
// net.PacketConn and does the same thing. Setting write deadlines for datagrams
// is seldom done.
func (s DatagramSession) SetDeadline(t time.Time) error {
func (s *DatagramSession) SetDeadline(t time.Time) error {
return s.udpconn.SetDeadline(t)
}
// Sets read deadline for the DatagramSession. Implements net.PacketConn
func (s DatagramSession) SetReadDeadline(t time.Time) error {
func (s *DatagramSession) SetReadDeadline(t time.Time) error {
return s.udpconn.SetReadDeadline(t)
}
// Sets the write deadline for the DatagramSession. Implements net.Packetconn.
func (s DatagramSession) SetWriteDeadline(t time.Time) error {
func (s *DatagramSession) SetWriteDeadline(t time.Time) error {
return s.udpconn.SetWriteDeadline(t)
}