Skip to content

Commit

Permalink
feat: use persistent to reuse cert
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Jan 9, 2025
1 parent 5da91ac commit bf63025
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/autotls/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
autotls
p2p-forge-certs/
identity.key
49 changes: 49 additions & 0 deletions examples/autotls/identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

// Borrowed from https://github.com/libp2p/go-libp2p-relay-daemon/blob/master/identity.go

import (
"os"

"github.com/libp2p/go-libp2p/core/crypto"
)

// LoadIdentity reads a private key from the given path and, if it does not
// exist, generates a new one.
func LoadIdentity(keyPath string) (crypto.PrivKey, error) {
if _, err := os.Stat(keyPath); err == nil {
return ReadIdentity(keyPath)
} else if os.IsNotExist(err) {
logger.Infof("Generating peer identity in %s\n", keyPath)
return GenerateIdentity(keyPath)
} else {
return nil, err
}
}

// ReadIdentity reads a private key from the given path.
func ReadIdentity(path string) (crypto.PrivKey, error) {
bytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}

return crypto.UnmarshalPrivateKey(bytes)
}

// GenerateIdentity writes a new random private key to the given path.
func GenerateIdentity(path string) (crypto.PrivKey, error) {
privk, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 0)
if err != nil {
return nil, err
}

bytes, err := crypto.MarshalPrivateKey(privk)
if err != nil {
return nil, err
}

err = os.WriteFile(path, bytes, 0400)

return privk, err
}
24 changes: 16 additions & 8 deletions examples/autotls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ import (
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"
)

var logger = log.Logger("example")
var logger = log.Logger("autotls-example")

const userAgent = "go-libp2p/example/autotls"
const identityKeyFile = "identity.key"

func main() {
// Create a background context
ctx := context.Background()

log.SetLogLevel("*", "error")
log.SetLogLevel("example", "debug") // Set the log level for the example to debug
log.SetLogLevel("basichost", "info") // Set the log level for the basichost package to info
log.SetLogLevel("autotls", "debug") // Set the log level for the autotls-example package to debug
log.SetLogLevel("p2p-forge", "debug") // Set the log level for the p2pforge package to debug
log.SetLogLevel("nat", "debug") // Set the log level for the libp2p nat package to debug
log.SetLogLevel("autotls-example", "debug") // Set the log level for the example to debug
log.SetLogLevel("basichost", "info") // Set the log level for the basichost package to info
log.SetLogLevel("autotls", "debug") // Set the log level for the autotls-example package to debug
log.SetLogLevel("p2p-forge", "debug") // Set the log level for the p2pforge package to debug
log.SetLogLevel("nat", "debug") // Set the log level for the libp2p nat package to debug

certLoaded := make(chan bool, 1) // Create a channel to signal when the cert is loaded

Expand Down Expand Up @@ -74,9 +75,16 @@ func main() {
certManager.Start()
defer certManager.Stop()

// Load or generate a persistent peer identity key
privKey, err := LoadIdentity(identityKeyFile)
if err != nil {
panic(err)
}

opts := []libp2p.Option{
libp2p.DisableRelay(), // Disable relay, since we need a public IP address
libp2p.NATPortMap(), // Attempt to open ports using UPnP for NATed hosts.
libp2p.Identity(privKey), // Use the loaded identity key
libp2p.DisableRelay(), // Disable relay, since we need a public IP address
libp2p.NATPortMap(), // Attempt to open ports using UPnP for NATed hosts.

libp2p.ListenAddrStrings(
// Configure default catch-all listeners for TCP and UDP
Expand Down

0 comments on commit bf63025

Please sign in to comment.