Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
109830a970 | ||
![]() |
d8c42f6a0a | ||
![]() |
9f0d7f348c | ||
![]() |
f464873c93 | ||
![]() |
a2f2e6786a | ||
![]() |
704f2971b5 | ||
![]() |
aef996d60a | ||
![]() |
529cdd6063 | ||
![]() |
33f3caa3f9 | ||
![]() |
5a43ad260d | ||
![]() |
cd744bfe49 |
7
Makefile
7
Makefile
@@ -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)"
|
||||
|
@@ -89,3 +89,5 @@ Public domain.
|
||||
|
||||
* Kalle Vedin `kalle.vedin@fripost.org`
|
||||
* Unknown Name (majestrate)
|
||||
* idk
|
||||
* qiwenmin
|
||||
|
@@ -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
15
debian/changelog
vendored
@@ -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 ]
|
||||
|
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
2
emit.go
2
emit.go
@@ -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
97
helper/helper.go
Normal 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
|
||||
}
|
@@ -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 {
|
||||
|
@@ -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() {
|
||||
|
Reference in New Issue
Block a user