2019-05-07 15:29:57 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
2019-06-25 19:25:31 +01:00
|
|
|
package wizard
|
2019-05-07 15:29:57 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
2020-11-10 22:00:54 +00:00
|
|
|
"strconv"
|
2020-05-19 03:56:53 +01:00
|
|
|
"strings"
|
2019-05-07 15:29:57 +01:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
2021-01-13 14:08:38 +00:00
|
|
|
"golang.org/x/term"
|
2019-05-07 15:29:57 +01:00
|
|
|
|
2020-03-23 11:38:24 +00:00
|
|
|
"storj.io/common/storj"
|
|
|
|
)
|
2019-05-07 15:29:57 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// PromptForAccessName handles user input for access name to be used with wizards.
|
2020-01-09 12:34:52 +00:00
|
|
|
func PromptForAccessName() (string, error) {
|
2020-05-19 03:56:53 +01:00
|
|
|
_, err := fmt.Printf("Choose an access name (use lowercase letters) [\"default\"]: ")
|
2020-01-09 12:34:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accessName string
|
|
|
|
n, err := fmt.Scanln(&accessName)
|
|
|
|
if err != nil && n != 0 {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if accessName == "" {
|
|
|
|
return "default", nil
|
|
|
|
}
|
2020-05-19 03:56:53 +01:00
|
|
|
|
|
|
|
if accessName != strings.ToLower(accessName) {
|
|
|
|
return "", errs.New("Please only use lowercase letters for access name.")
|
|
|
|
}
|
|
|
|
|
2020-01-09 12:34:52 +00:00
|
|
|
return accessName, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// PromptForSatellite handles user input for a satellite address to be used with wizards.
|
2019-06-25 18:29:52 +01:00
|
|
|
func PromptForSatellite(cmd *cobra.Command) (string, error) {
|
2020-03-23 11:38:24 +00:00
|
|
|
satellites := []string{
|
2021-04-14 04:41:47 +01:00
|
|
|
"12EayRS2V1kEsWESU9QMRseFhdxYxKicsiFmxrsLZHeLUtdps3S@us1.storj.io:7777",
|
|
|
|
"12L9ZFwhzVpuEKMUNUqkaTLGzwY9G24tbiigLiXpmZWKwmcNDDs@eu1.storj.io:7777",
|
|
|
|
"121RTSDpyNZVcEU84Ticf2L1ntiuUimbWgfATz21tuvgk3vzoA6@ap1.storj.io:7777",
|
2020-03-23 11:38:24 +00:00
|
|
|
}
|
2019-06-25 19:25:31 +01:00
|
|
|
|
2019-07-18 18:01:04 +01:00
|
|
|
_, err := fmt.Print("Select your satellite:\n")
|
2019-05-07 15:29:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-06-25 19:25:31 +01:00
|
|
|
|
|
|
|
for iterator, value := range satellites {
|
2020-03-23 11:38:24 +00:00
|
|
|
nodeURL, err := storj.ParseNodeURL(value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
host, _, err := net.SplitHostPort(nodeURL.Address)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fmt.Printf("\t[%d] %s\n", iterator+1, host)
|
2019-06-25 19:25:31 +01:00
|
|
|
if err != nil {
|
2021-05-14 16:05:42 +01:00
|
|
|
return "", nil //nolint: nilerr // we'll skip the prompt, when there's an error
|
2019-06-25 19:25:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 11:38:24 +00:00
|
|
|
_, err = fmt.Print(`Enter number or satellite address as "<nodeid>@<address>:<port>" [1]: `)
|
2019-06-25 19:25:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-05-07 15:29:57 +01:00
|
|
|
var satelliteAddress string
|
|
|
|
n, err := fmt.Scanln(&satelliteAddress)
|
|
|
|
if err != nil {
|
2020-03-23 11:38:24 +00:00
|
|
|
if n != 0 {
|
2019-05-07 15:29:57 +01:00
|
|
|
return "", err
|
|
|
|
}
|
2020-03-23 11:38:24 +00:00
|
|
|
// fmt.Scanln cannot handle empty input
|
|
|
|
satelliteAddress = satellites[0]
|
2019-05-07 15:29:57 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 11:38:24 +00:00
|
|
|
if len(satelliteAddress) == 0 {
|
2019-05-07 15:29:57 +01:00
|
|
|
return "", errs.New("satellite address cannot be empty")
|
2020-03-23 11:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(satelliteAddress) == 1 {
|
2020-11-10 22:00:54 +00:00
|
|
|
satIdx, err := strconv.Atoi(satelliteAddress)
|
|
|
|
if err != nil {
|
|
|
|
return "", errs.New("invalid satellite address option")
|
|
|
|
}
|
|
|
|
|
|
|
|
if satIdx < 1 || satIdx > len(satellites) {
|
|
|
|
return "", errs.New("invalid satellite address option")
|
2019-05-07 15:29:57 +01:00
|
|
|
}
|
2020-11-10 22:00:54 +00:00
|
|
|
|
|
|
|
satelliteAddress = satellites[satIdx-1]
|
2019-05-07 15:29:57 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 11:38:24 +00:00
|
|
|
nodeURL, err := storj.ParseNodeURL(satelliteAddress)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodeURL.ID.IsZero() {
|
|
|
|
return "", errs.New(`missing node id, satellite address must be in the format "<nodeid>@<address>:<port>"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return satelliteAddress, nil
|
2019-05-07 15:29:57 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// PromptForAPIKey handles user input for an API key to be used with wizards.
|
2019-05-07 15:29:57 +01:00
|
|
|
func PromptForAPIKey() (string, error) {
|
|
|
|
_, err := fmt.Print("Enter your API key: ")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var apiKey string
|
|
|
|
n, err := fmt.Scanln(&apiKey)
|
|
|
|
if err != nil && n != 0 {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if apiKey == "" {
|
|
|
|
return "", errs.New("API key cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiKey, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// PromptForEncryptionPassphrase handles user input for an encryption passphrase to be used with wizards.
|
2019-07-29 08:17:49 +01:00
|
|
|
func PromptForEncryptionPassphrase() (string, error) {
|
2020-09-23 11:54:00 +01:00
|
|
|
_, err := fmt.Print(`Data is encrypted on the network, with an encryption passphrase
|
|
|
|
stored on your local machine. Enter a passphrase you'd like to use.
|
|
|
|
Enter your encryption passphrase: `)
|
2019-05-07 15:29:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-01-13 14:08:38 +00:00
|
|
|
encKey, err := term.ReadPassword(int(os.Stdin.Fd()))
|
2019-05-07 15:29:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_, err = fmt.Println()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:07:07 +01:00
|
|
|
if len(encKey) == 0 {
|
|
|
|
return "", errs.New("Encryption passphrase cannot be empty")
|
|
|
|
}
|
|
|
|
|
2019-05-07 15:29:57 +01:00
|
|
|
_, err = fmt.Print("Enter your encryption passphrase again: ")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-01-13 14:08:38 +00:00
|
|
|
repeatedEncKey, err := term.ReadPassword(int(os.Stdin.Fd()))
|
2019-05-07 15:29:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_, err = fmt.Println()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(encKey, repeatedEncKey) {
|
2019-06-25 18:29:52 +01:00
|
|
|
return "", errs.New("encryption passphrase does not match")
|
2019-05-07 15:29:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(encKey), nil
|
|
|
|
}
|
2020-04-15 13:57:45 +01:00
|
|
|
|
|
|
|
// PromptForTracing handles user input for consent to turn on tracing to be used with wizards.
|
|
|
|
func PromptForTracing() (bool, error) {
|
|
|
|
_, err := fmt.Printf(`
|
2021-04-14 04:41:47 +01:00
|
|
|
With your permission, Storj can automatically collect analytics information from your uplink CLI to help improve the quality and performance of our products. This information is sent only with your consent and is submitted anonymously to Storj Labs: (y/n)
|
2020-04-15 13:57:45 +01:00
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var userConsent string
|
|
|
|
n, err := fmt.Scanln(&userConsent)
|
|
|
|
if err != nil {
|
|
|
|
if n != 0 {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// fmt.Scanln cannot handle empty input
|
|
|
|
userConsent = "n"
|
|
|
|
}
|
|
|
|
|
|
|
|
switch userConsent {
|
|
|
|
case "y", "yes", "Y", "Yes":
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|