11 Commits

Author SHA1 Message Date
idk
109830a970 gofmt 2020-11-23 20:38:35 -05:00
idk
d8c42f6a0a Prep to cut a new release 2020-11-22 12:50:23 -05:00
idk
9f0d7f348c Prep to cut a new release 2020-11-22 12:48:28 -05:00
idk
f464873c93 add I2PStreamSession to helper 2020-11-22 00:08:55 -05:00
idk
a2f2e6786a add I2PStreamSession to helper 2020-11-22 00:05:04 -05:00
idk
704f2971b5 add a "helper" function which just magically creates a listener 2020-11-21 17:44:50 -05:00
idk
aef996d60a update deb changelog 2020-10-18 01:04:40 -04:00
idk
529cdd6063 update deb changelog 2020-10-18 01:03:01 -04:00
idk
33f3caa3f9 Add fmt make target, incorporate address length fix from qiwenmin, add a shortcut for saving your keys 2020-10-18 00:58:45 -04:00
idk
5a43ad260d Merge pull request #3 from qiwenmin/fix-wrong-addr
Fix wrong address.
2020-10-18 04:34:41 +00:00
Qi Wenmin
cd744bfe49 Fix wrong address.
With some addresses, ToBytes() calculates the wrong buffer lenngth
and returns the larger buffer for next step (sha256). So the b32
address will wrong.

For example, when the b64 key is the following:

tr2kQArAdxb7rt3Hr6itisY1Pi7ncWVUYDan2rjB45F1lIFvDPJih5dBa~WfWTvfyt
QKdtFoGHLuZdC8X71R5GXGvh8N0ECBtjhYPG8chFSEO2Cab2SZKYskEqGeOrlOvAQ-
yYaH0xCkarxFdvEbXoV~cfDhHDJcZI3Uc522sLWlB72-uOH9nlzyt-41byGc5GWn5c
CxJZx8UfszUIG9B2QoptrN0xquWvkLxescZ~0JYePTEJTeQFWlfPAiHcuAloAq3yKt
DCY4xcSracQr9ieCmnFJoUsLtzsjSrn7L~WwlI4pYYXxkm2dtzR8cPHJCBUBNMbK~K
TIxLtH0Tt9wI3edHgRMdxNm3EoozpPMXA05Ouni~ehWMhaywiJKTDZcGKDRqXJVm~y
xJGcXiXT2UzdwM~D20KTTUlLmeRqsU5hqpk2kgBIWKFfb8Gunx2hr0wEHsJypUwDQ-
FSGT0Fw0MRGq0QWvwXxroPbJdjsNnYOKCEogHxC4VrmzaLhHlEBQAEAAcAAA==

the right raw key length is 391, but ToBytes() returns a 393 bytes
raw key.

Using DecodeString() can fix this issue.
2020-09-27 08:33:52 +08:00
9 changed files with 179 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
USER_GH=eyedeekay
VERSION=0.32.3
VERSION=0.32.32
packagename=sam3
echo:
@@ -22,3 +22,8 @@ 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 {} \;
upload-linux:
gothub upload -R -u $(USER_GH) -r "$(packagename)" -t $(VERSION) -l `sha256sum ` -n "$(packagename)" -f "$(packagename)"

View File

@@ -89,3 +89,5 @@ Public domain.
* Kalle Vedin `kalle.vedin@fripost.org`
* Unknown Name (majestrate)
* idk
* qiwenmin

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)

15
debian/changelog vendored
View File

@@ -1,3 +1,18 @@
golang-github-eyedeekay-sam3 (0.3.2.32) unreleased; urgency=medium
[ idk ]
* Include "Convenience Functions" in /helper
-- idk <hankhill19580@gmail.com> Sun, 22 Nov 2020 12:49:13 -0500
golang-github-eyedeekay-sam3 (0.3.2.31) unreleased; urgency=medium
[ idk ]
* Incorporate address fix from qiwenmin
* Shortcut for saving keys to text file
-- idk <hankhill19580@gmail.com> Sun, 18 Oct 2020 1:01:03 -0500
golang-github-eyedeekay-sam3 (0.3.2.3) unreleased; urgency=medium
[ idk ]

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(),

97
helper/helper.go Normal file
View File

@@ -0,0 +1,97 @@
package sam
import (
"io/ioutil"
"log"
"net"
"os"
"github.com/eyedeekay/sam3"
"github.com/eyedeekay/sam3/i2pkeys"
)
func NetListener(name, samaddr, keyspath string) (net.Listener, error) {
return I2PListener(name, samaddr, keyspath)
}
// I2PListener is a convenience function which takes a SAM tunnel name, a SAM address and a filename.
// If the file contains I2P keys, it will create a service using that address. If the file does not
// exist, keys will be generated and stored in that file.
func I2PListener(name, samaddr, keyspath string) (*sam3.StreamListener, error) {
log.Printf("Starting and registering I2P service, please wait a couple of minutes...")
sam, err := sam3.NewSAM(samaddr)
if err != nil {
log.Fatalf("error connecting to SAM to %s: %s", samaddr, err)
}
var keys *i2pkeys.I2PKeys
if _, err := os.Stat(keyspath + ".i2p.private"); os.IsNotExist(err) {
f, err := os.Create(keyspath + ".i2p.private")
if err != nil {
log.Fatalf("unable to open I2P keyfile for writing: %s", err)
}
defer f.Close()
tkeys, err := sam.NewKeys()
if err != nil {
log.Fatalf("unable to generate I2P Keys, %s", err)
}
keys = &tkeys
err = i2pkeys.StoreKeysIncompat(*keys, f)
if err != nil {
log.Fatalf("unable to save newly generated I2P Keys, %s", err)
}
} else {
tkeys, err := i2pkeys.LoadKeys(keyspath + ".i2p.private")
if err != nil {
log.Fatalf("unable to load I2P Keys: %e", err)
}
keys = &tkeys
}
stream, err := sam.NewStreamSession(name, *keys, sam3.Options_Medium)
if err != nil {
log.Fatalf("error creating I2P streaming connection %s: %s, %s.", name, err, *keys)
}
listener, err := stream.Listen()
err = ioutil.WriteFile(keyspath+".i2p.public.txt", []byte(keys.Addr().Base32()), 0644)
if err != nil {
log.Fatalf("error storing I2P base32 address in adjacent text file, %s", err)
}
return listener, err
}
// I2PStreamSession is a convenience function which returns a sam3.StreamSession instead
// of a sam3.StreamListener. It also takes care of setting a persisitent key on behalf
// of the user.
func I2PStreamSession(name, samaddr, keyspath string) (*sam3.StreamSession, error) {
log.Printf("Starting and registering I2P service, please wait a couple of minutes...")
sam, err := sam3.NewSAM(samaddr)
if err != nil {
log.Fatalf("error connecting to SAM to %s: %s", samaddr, err)
}
var keys *i2pkeys.I2PKeys
if _, err := os.Stat(keyspath + ".i2p.private"); os.IsNotExist(err) {
f, err := os.Create(keyspath + ".i2p.private")
if err != nil {
log.Fatalf("unable to open I2P keyfile for writing: %s", err)
}
defer f.Close()
tkeys, err := sam.NewKeys()
if err != nil {
log.Fatalf("unable to generate I2P Keys, %s", err)
}
keys = &tkeys
err = i2pkeys.StoreKeysIncompat(*keys, f)
if err != nil {
log.Fatalf("unable to save newly generated I2P Keys, %s", err)
}
} else {
tkeys, err := i2pkeys.LoadKeys(keyspath + ".i2p.private")
if err != nil {
log.Fatalf("unable to load I2P Keys: %e", err)
}
keys = &tkeys
}
stream, err := sam.NewStreamSession(name, *keys, sam3.Options_Medium)
return stream, err
}

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
@@ -160,11 +201,7 @@ func NewI2PAddrFromBytes(addr []byte) (I2PAddr, error) {
// Turns an I2P address to a byte array. The inverse of NewI2PAddrFromBytes().
func (addr I2PAddr) ToBytes() ([]byte, error) {
buf := make([]byte, i2pB64enc.DecodedLen(len(addr)))
if _, err := i2pB64enc.Decode(buf, []byte(addr)); err != nil {
return buf, errors.New("Address is not base64-encoded")
}
return buf, nil
return i2pB64enc.DecodeString(string(addr))
}
func (addr I2PAddr) Bytes() []byte {

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() {