Add fmt make target, incorporate address length fix from qiwenmin, add a shortcut for saving your keys

This commit is contained in:
idk
2020-10-18 00:58:45 -04:00
parent 5a43ad260d
commit 33f3caa3f9
6 changed files with 61 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
USER_GH=eyedeekay
VERSION=0.32.3
VERSION=0.32.31
packagename=sam3
echo:
@@ -22,3 +22,5 @@ copier:
echo '#! /usr/bin/env sh' > deb/copy.sh
echo 'for f in $$(ls); do scp $$f/*.deb user@192.168.99.106:~/DEBIAN_PKGS/$$f/main/; done' >> deb/copy.sh
fmt:
find . -name '*.go' -exec gofmt -w -s {} \;

View File

@@ -108,6 +108,10 @@ func (s *DatagramSession) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
}
}
func (s *DatagramSession) Accept() (net.Conn, error) {
return s, nil
}
func (s *DatagramSession) Read(b []byte) (n int, err error) {
rint, _, rerr := s.ReadFrom(b)
return rint, rerr
@@ -147,6 +151,10 @@ func (s *DatagramSession) LocalAddr() net.Addr {
return s.LocalI2PAddr()
}
func (s *DatagramSession) Addr() net.Addr {
return s.LocalI2PAddr()
}
func (s *DatagramSession) Lookup(name string) (a net.Addr, err error) {
var sam *SAM
sam, err = NewSAM(s.samAddr)

View File

@@ -22,7 +22,7 @@ func SetType(s string) func(*SAMEmit) error {
c.Style = s
return nil
}
return fmt.Errorf("Invalid session STYLE=%s, must be STREAM, DATAGRAM, or RAW")
return fmt.Errorf("Invalid session STYLE=%s, must be STREAM, DATAGRAM, or RAW", s)
}
}

View File

@@ -76,7 +76,7 @@ func (e *SAMEmit) ConnectBytes(dest string) []byte {
func (e *SAMEmit) Accept() string {
return fmt.Sprintf(
"STREAM ACCEPT ID=%s",
"STREAM ACCEPT ID=%s %s %s",
e.I2PConfig.ID(),
e.I2PConfig.FromPort(),
e.I2PConfig.ToPort(),

View File

@@ -7,6 +7,7 @@ import (
"encoding/base64"
"errors"
"io"
"os"
"strings"
)
@@ -31,6 +32,18 @@ func NewKeys(addr I2PAddr, both string) I2PKeys {
return I2PKeys{addr, both}
}
// fileExists checks if a file exists and is not a directory before we
// try using it to prevent further errors.
func fileExists(filename string) (bool, error) {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
return !info.IsDir(), nil
}
// load keys from non standard format
func LoadKeysIncompat(r io.Reader) (k I2PKeys, err error) {
var buff bytes.Buffer
@@ -42,12 +55,40 @@ func LoadKeysIncompat(r io.Reader) (k I2PKeys, err error) {
return
}
// load keys from non-standard format by specifying a text file.
// If the file does not exist, generate keys, otherwise, fail
// closed.
func LoadKeys(r string) (I2PKeys, error) {
exists, err := fileExists(r)
if err != nil {
return I2PKeys{}, err
}
if exists {
fi, err := os.Open(r)
if err != nil {
return I2PKeys{}, err
}
defer fi.Close()
return LoadKeysIncompat(fi)
}
return I2PKeys{}, err
}
// store keys in non standard format
func StoreKeysIncompat(k I2PKeys, w io.Writer) (err error) {
_, err = io.WriteString(w, k.Address.Base64()+"\n"+k.Both)
return
}
func StoreKeys(k I2PKeys, r string) error {
fi, err := os.Open(r)
if err != nil {
return err
}
defer fi.Close()
return StoreKeysIncompat(k, fi)
}
// Returns the public keys of the I2PKeys.
func (k I2PKeys) Addr() I2PAddr {
return k.Address

View File

@@ -150,7 +150,7 @@ func Test_StreamingServerClient(t *testing.T) {
}
func ExampleStreamSession() {
// Creates a new StreamingSession, dials to zzz.i2p and gets a SAMConn
// Creates a new StreamingSession, dials to idk.i2p and gets a SAMConn
// which behaves just like a normal net.Conn.
const samBridge = "127.0.0.1:7656"
@@ -172,7 +172,7 @@ func ExampleStreamSession() {
fmt.Println(err.Error())
return
}
someone, err := sam.Lookup("zzz.i2p")
someone, err := sam.Lookup("idk.i2p")
if err != nil {
fmt.Println(err.Error())
return
@@ -192,17 +192,17 @@ func ExampleStreamSession() {
buf := make([]byte, 4096)
n, err := conn.Read(buf)
if !strings.Contains(strings.ToLower(string(buf[:n])), "http") && !strings.Contains(strings.ToLower(string(buf[:n])), "html") {
fmt.Printf("Probably failed to StreamSession.DialI2P(zzz.i2p)? It replied %d bytes, but nothing that looked like http/html", n)
log.Printf("Probably failed to StreamSession.DialI2P(zzz.i2p)? It replied %d bytes, but nothing that looked like http/html", n)
fmt.Printf("Probably failed to StreamSession.DialI2P(idk.i2p)? It replied %d bytes, but nothing that looked like http/html", n)
log.Printf("Probably failed to StreamSession.DialI2P(idk.i2p)? It replied %d bytes, but nothing that looked like http/html", n)
} else {
fmt.Println("Read HTTP/HTML from zzz.i2p")
log.Println("Read HTTP/HTML from zzz.i2p")
fmt.Println("Read HTTP/HTML from idk.i2p")
log.Println("Read HTTP/HTML from idk.i2p")
}
return
// Output:
//Sending HTTP GET /
//Read HTTP/HTML from zzz.i2p
//Read HTTP/HTML from idk.i2p
}
func ExampleStreamListener() {