2022-09-25 22:04:44 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-10-24 00:12:48 +03:00
|
|
|
"encoding/json"
|
2022-09-25 22:04:44 +03:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/DataDrake/cli-ng/v2/cmd"
|
|
|
|
"github.com/hyprspace/hyprspace/config"
|
2022-11-11 21:54:57 +02:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
2023-10-27 04:17:42 +03:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-09-26 01:00:05 +03:00
|
|
|
"github.com/multiformats/go-multibase"
|
2022-09-25 22:04:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Init creates a configuration for a Hyprspace Interface.
|
|
|
|
var Init = cmd.Sub{
|
|
|
|
Name: "init",
|
|
|
|
Alias: "i",
|
|
|
|
Short: "Initialize An Interface Config",
|
|
|
|
Run: InitRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitRun handles the execution of the init command.
|
|
|
|
func InitRun(r *cmd.Root, c *cmd.Sub) {
|
2023-10-27 01:32:18 +03:00
|
|
|
ifName := r.Flags.(*GlobalFlags).InterfaceName
|
|
|
|
if ifName == "" {
|
|
|
|
ifName = "hyprspace"
|
|
|
|
}
|
2022-09-25 22:04:44 +03:00
|
|
|
|
|
|
|
configPath := r.Flags.(*GlobalFlags).Config
|
|
|
|
if configPath == "" {
|
2023-10-27 01:32:18 +03:00
|
|
|
configPath = "/etc/hyprspace/" + ifName + ".json"
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
2023-10-27 01:32:18 +03:00
|
|
|
privKey, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 256)
|
2022-09-25 22:04:44 +03:00
|
|
|
checkErr(err)
|
|
|
|
|
2023-10-27 01:32:18 +03:00
|
|
|
keyBytes, err := crypto.MarshalPrivateKey(privKey)
|
2022-09-25 22:04:44 +03:00
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
// Setup an initial default command.
|
|
|
|
new := config.Config{
|
2023-10-27 01:32:18 +03:00
|
|
|
EncodedPrivateKey: multibase.MustNewEncoder(multibase.Base58BTC).Encode(keyBytes),
|
|
|
|
EncodedListenAddresses: []string{
|
|
|
|
"/ip4/0.0.0.0/tcp/8001",
|
|
|
|
"/ip4/0.0.0.0/udp/8001/quic-v1",
|
|
|
|
"/ip6/::/tcp/8001",
|
|
|
|
"/ip6/::/udp/8001/quic-v1",
|
2022-09-25 22:04:44 +03:00
|
|
|
},
|
2023-10-24 00:12:48 +03:00
|
|
|
Peers: make([]config.Peer, 0),
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
2023-10-24 00:12:48 +03:00
|
|
|
out, err := json.MarshalIndent(&new, "", " ")
|
2022-09-25 22:04:44 +03:00
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
err = os.MkdirAll(filepath.Dir(configPath), os.ModePerm)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
f, err := os.Create(configPath)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
_, err = f.Write(out)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
fmt.Printf("Initialized new config at %s\n", configPath)
|
2023-10-27 04:17:42 +03:00
|
|
|
peerId, err := peer.IDFromPrivateKey(privKey)
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Add this entry to your other peers:")
|
|
|
|
fmt.Println("{")
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err == nil {
|
|
|
|
fmt.Printf(" \"name\": \"%s\",\n", hostname)
|
|
|
|
}
|
|
|
|
fmt.Printf(" \"id\": \"%s\"\n", peerId)
|
|
|
|
fmt.Println("}")
|
|
|
|
}
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|