2022-10-15 14:28:03 +00:00
|
|
|
package garage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ed25519"
|
2022-10-28 22:09:18 +00:00
|
|
|
"crypto/rand"
|
2022-10-15 14:28:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2022-10-28 22:09:18 +00:00
|
|
|
// RemotePeer describes all information necessary to connect to a given garage
|
|
|
|
// node.
|
|
|
|
type RemotePeer struct {
|
|
|
|
ID string
|
2022-10-15 14:28:03 +00:00
|
|
|
IP string
|
|
|
|
RPCPort int
|
|
|
|
S3APIPort int
|
|
|
|
}
|
|
|
|
|
2022-10-28 22:09:18 +00:00
|
|
|
// LocalPeer describes the configuration of a local garage instance.
|
|
|
|
type LocalPeer struct {
|
|
|
|
RemotePeer
|
2022-10-15 14:28:03 +00:00
|
|
|
|
2022-10-28 22:09:18 +00:00
|
|
|
AdminPort int
|
|
|
|
}
|
2022-10-15 14:28:03 +00:00
|
|
|
|
2022-10-28 22:09:18 +00:00
|
|
|
// GeneratePeerKey generates and returns a public/private key pair for a garage
|
|
|
|
// instance.
|
|
|
|
func GeneratePeerKey() (pubKey, privKey []byte) {
|
|
|
|
pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
|
2022-10-15 14:28:03 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pubKey, privKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPCAddr returns the address of the peer's RPC port.
|
2022-10-28 22:09:18 +00:00
|
|
|
func (p RemotePeer) RPCAddr() string {
|
2022-10-15 14:28:03 +00:00
|
|
|
return net.JoinHostPort(p.IP, strconv.Itoa(p.RPCPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPCPeerAddr returns the full peer address (e.g. "id@ip:port") of the garage
|
|
|
|
// node for use in communicating over RPC.
|
2022-10-28 22:09:18 +00:00
|
|
|
func (p RemotePeer) RPCPeerAddr() string {
|
|
|
|
return fmt.Sprintf("%s@%s", p.ID, p.RPCAddr())
|
2022-10-15 14:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// S3APIAddr returns the address of the peer's S3 API port.
|
2022-10-28 22:09:18 +00:00
|
|
|
func (p RemotePeer) S3APIAddr() string {
|
2022-10-15 14:28:03 +00:00
|
|
|
return net.JoinHostPort(p.IP, strconv.Itoa(p.S3APIPort))
|
|
|
|
}
|
2022-10-28 22:09:18 +00:00
|
|
|
|
|
|
|
// AdminAddr returns the address of the peer's S3 API port.
|
|
|
|
func (p LocalPeer) AdminAddr() string {
|
|
|
|
return net.JoinHostPort(p.IP, strconv.Itoa(p.AdminPort))
|
|
|
|
}
|