mirror of
https://github.com/go-i2p/i2pkeys.git
synced 2025-06-07 09:03:28 -04:00
move load and store to own files
This commit is contained in:
96
I2PKeys.go
96
I2PKeys.go
@ -1,13 +1,10 @@
|
||||
package i2pkeys
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto"
|
||||
"encoding/base32"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
@ -57,99 +54,6 @@ func fileExists(filename string) (bool, error) {
|
||||
return !info.IsDir(), nil
|
||||
}
|
||||
|
||||
// LoadKeysIncompat loads keys from a non-standard format
|
||||
func LoadKeysIncompat(r io.Reader) (I2PKeys, error) {
|
||||
log.Debug("Loading keys from reader")
|
||||
var buff bytes.Buffer
|
||||
_, err := io.Copy(&buff, r)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error copying from reader, did not load keys")
|
||||
return I2PKeys{}, fmt.Errorf("error copying from reader: %w", err)
|
||||
}
|
||||
|
||||
parts := strings.Split(buff.String(), "\n")
|
||||
if len(parts) < 2 {
|
||||
err := errors.New("invalid key format: not enough data")
|
||||
log.WithError(err).Error("Error parsing keys")
|
||||
return I2PKeys{}, err
|
||||
}
|
||||
|
||||
k := I2PKeys{I2PAddr(parts[0]), parts[1]}
|
||||
log.WithField("keys", k).Debug("Loaded keys")
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// 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) {
|
||||
log.WithField("filename", r).Debug("Loading keys from file")
|
||||
exists, err := fileExists(r)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error checking if file exists")
|
||||
return I2PKeys{}, err
|
||||
}
|
||||
if !exists {
|
||||
// File doesn't exist so we'll generate new keys
|
||||
log.WithError(err).Debug("File does not exist, attempting to generate new keys")
|
||||
k, err := NewDestination()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error generating new keys")
|
||||
return I2PKeys{}, err
|
||||
}
|
||||
// Save the new keys to the file
|
||||
err = StoreKeys(*k, r)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error saving new keys to file")
|
||||
return I2PKeys{}, err
|
||||
}
|
||||
return *k, nil
|
||||
}
|
||||
fi, err := os.Open(r)
|
||||
if err != nil {
|
||||
log.WithError(err).WithField("filename", r).Error("Error opening file")
|
||||
return I2PKeys{}, fmt.Errorf("error opening file: %w", err)
|
||||
}
|
||||
defer fi.Close()
|
||||
log.WithField("filename", r).Debug("File opened successfully")
|
||||
return LoadKeysIncompat(fi)
|
||||
}
|
||||
|
||||
// store keys in non standard format
|
||||
func StoreKeysIncompat(k I2PKeys, w io.Writer) error {
|
||||
log.Debug("Storing keys")
|
||||
_, err := io.WriteString(w, k.Address.Base64()+"\n"+k.Both)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error writing keys")
|
||||
return fmt.Errorf("error writing keys: %w", err)
|
||||
}
|
||||
log.WithField("keys", k).Debug("Keys stored successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func StoreKeys(k I2PKeys, r string) error {
|
||||
log.WithField("filename", r).Debug("Storing keys to file")
|
||||
if _, err := os.Stat(r); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.WithField("filename", r).Debug("File does not exist, creating new file")
|
||||
fi, err := os.Create(r)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error creating file")
|
||||
return err
|
||||
}
|
||||
defer fi.Close()
|
||||
return StoreKeysIncompat(k, fi)
|
||||
}
|
||||
}
|
||||
fi, err := os.Open(r)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error opening file")
|
||||
return err
|
||||
}
|
||||
defer fi.Close()
|
||||
return StoreKeysIncompat(k, fi)
|
||||
}
|
||||
|
||||
func (k I2PKeys) Network() string {
|
||||
return k.Address.Network()
|
||||
}
|
||||
|
68
LoadKeys.go
Normal file
68
LoadKeys.go
Normal file
@ -0,0 +1,68 @@
|
||||
package i2pkeys
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LoadKeysIncompat loads keys from a non-standard format
|
||||
func LoadKeysIncompat(r io.Reader) (I2PKeys, error) {
|
||||
log.Debug("Loading keys from reader")
|
||||
var buff bytes.Buffer
|
||||
_, err := io.Copy(&buff, r)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error copying from reader, did not load keys")
|
||||
return I2PKeys{}, fmt.Errorf("error copying from reader: %w", err)
|
||||
}
|
||||
|
||||
parts := strings.Split(buff.String(), "\n")
|
||||
if len(parts) < 2 {
|
||||
err := errors.New("invalid key format: not enough data")
|
||||
log.WithError(err).Error("Error parsing keys")
|
||||
return I2PKeys{}, err
|
||||
}
|
||||
|
||||
k := I2PKeys{I2PAddr(parts[0]), parts[1]}
|
||||
log.WithField("keys", k).Debug("Loaded keys")
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// 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) {
|
||||
log.WithField("filename", r).Debug("Loading keys from file")
|
||||
exists, err := fileExists(r)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error checking if file exists")
|
||||
return I2PKeys{}, err
|
||||
}
|
||||
if !exists {
|
||||
// File doesn't exist so we'll generate new keys
|
||||
log.WithError(err).Debug("File does not exist, attempting to generate new keys")
|
||||
k, err := NewDestination()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error generating new keys")
|
||||
return I2PKeys{}, err
|
||||
}
|
||||
// Save the new keys to the file
|
||||
err = StoreKeys(*k, r)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error saving new keys to file")
|
||||
return I2PKeys{}, err
|
||||
}
|
||||
return *k, nil
|
||||
}
|
||||
fi, err := os.Open(r)
|
||||
if err != nil {
|
||||
log.WithError(err).WithField("filename", r).Error("Error opening file")
|
||||
return I2PKeys{}, fmt.Errorf("error opening file: %w", err)
|
||||
}
|
||||
defer fi.Close()
|
||||
log.WithField("filename", r).Debug("File opened successfully")
|
||||
return LoadKeysIncompat(fi)
|
||||
}
|
45
StoreKeys.go
Normal file
45
StoreKeys.go
Normal file
@ -0,0 +1,45 @@
|
||||
package i2pkeys
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// store keys in non standard format
|
||||
func StoreKeysIncompat(k I2PKeys, w io.Writer) error {
|
||||
log.Debug("Storing keys")
|
||||
_, err := io.WriteString(w, k.Address.Base64()+"\n"+k.Both)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error writing keys")
|
||||
return fmt.Errorf("error writing keys: %w", err)
|
||||
}
|
||||
log.WithField("keys", k).Debug("Keys stored successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func StoreKeys(k I2PKeys, r string) error {
|
||||
log.WithField("filename", r).Debug("Storing keys to file")
|
||||
if _, err := os.Stat(r); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.WithField("filename", r).Debug("File does not exist, creating new file")
|
||||
fi, err := os.Create(r)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error creating file")
|
||||
return err
|
||||
}
|
||||
defer fi.Close()
|
||||
return StoreKeysIncompat(k, fi)
|
||||
}
|
||||
// If stat failed for reasons other than file not existing, return the error
|
||||
return err
|
||||
}
|
||||
// File exists - open in write mode to allow overwriting
|
||||
fi, err := os.OpenFile(r, os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Error opening file")
|
||||
return err
|
||||
}
|
||||
defer fi.Close()
|
||||
return StoreKeysIncompat(k, fi)
|
||||
}
|
Reference in New Issue
Block a user