Skip to content

Commit

Permalink
add I2PStreamSession to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
eyedeekay committed Nov 22, 2020
1 parent 704f297 commit a2f2e67
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,39 @@ func I2PListener(name, samaddr, keyspath string) (*sam3.StreamListener, error) {
}
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, error
}

0 comments on commit a2f2e67

Please sign in to comment.