2017-04-09 22:16:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-09-26 13:44:26 +00:00
|
|
|
"github.com/status-im/status-go/geth/account"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-04-09 22:16:05 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
|
|
|
"gopkg.in/urfave/cli.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-05-03 14:24:48 +00:00
|
|
|
// WhisperEchoModeFlag enables/disables Echo mode (arguments are printed for diagnostics)
|
2017-04-09 22:16:05 +00:00
|
|
|
WhisperEchoModeFlag = cli.BoolTFlag{
|
|
|
|
Name: "echo",
|
|
|
|
Usage: "Echo mode, prints some arguments for diagnostics (default: true)",
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// WhisperBootstrapNodeFlag marks node as not actively listening for incoming connections
|
2017-04-09 22:16:05 +00:00
|
|
|
WhisperBootstrapNodeFlag = cli.BoolTFlag{
|
|
|
|
Name: "bootstrap",
|
|
|
|
Usage: "Don't actively connect to peers, wait for incoming connections (default: true)",
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// WhisperNotificationServerNodeFlag enables/disables Push Notifications services
|
2017-04-09 22:16:05 +00:00
|
|
|
WhisperNotificationServerNodeFlag = cli.BoolFlag{
|
|
|
|
Name: "notify",
|
|
|
|
Usage: "Node is capable of sending Push Notifications",
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// WhisperForwarderNodeFlag enables/disables message forwarding
|
|
|
|
// (when neither sends nor decrypts envelopes, just forwards them)
|
2017-04-09 22:16:05 +00:00
|
|
|
WhisperForwarderNodeFlag = cli.BoolFlag{
|
|
|
|
Name: "forward",
|
|
|
|
Usage: "Only forward messages, neither send nor decrypt messages",
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// WhisperMailserverNodeFlag enables/disables Inboxing services
|
2017-04-09 22:16:05 +00:00
|
|
|
WhisperMailserverNodeFlag = cli.BoolFlag{
|
|
|
|
Name: "mailserver",
|
|
|
|
Usage: "Delivers expired messages on demand",
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// WhisperIdentityFile is path to file containing private key of the node (for asymmetric encryption)
|
2017-04-12 18:26:54 +00:00
|
|
|
WhisperIdentityFile = cli.StringFlag{
|
|
|
|
Name: "identity",
|
2017-05-03 14:24:48 +00:00
|
|
|
Usage: "Protocol identity file (private key used for asymmetric encryption)",
|
2017-04-12 18:26:54 +00:00
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// WhisperPasswordFile is password used to do a symmetric encryption
|
2017-04-12 18:26:54 +00:00
|
|
|
WhisperPasswordFile = cli.StringFlag{
|
2017-04-09 22:16:05 +00:00
|
|
|
Name: "password",
|
2017-04-12 18:26:54 +00:00
|
|
|
Usage: "Password file (password is used for symmetric encryption)",
|
2017-04-09 22:16:05 +00:00
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// WhisperPortFlag defines port on which Whisper protocol is listening
|
2017-04-09 22:16:05 +00:00
|
|
|
WhisperPortFlag = cli.IntFlag{
|
|
|
|
Name: "port",
|
|
|
|
Usage: "Whisper node's listening port",
|
|
|
|
Value: params.WhisperPort,
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// WhisperPoWFlag is the minimum PoW required by the node
|
2017-04-09 22:16:05 +00:00
|
|
|
WhisperPoWFlag = cli.Float64Flag{
|
|
|
|
Name: "pow",
|
|
|
|
Usage: "PoW for messages to be added to queue, in float format",
|
|
|
|
Value: params.WhisperMinimumPoW,
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// WhisperTTLFlag defines node's default TTL for envelopes
|
2017-04-09 22:16:05 +00:00
|
|
|
WhisperTTLFlag = cli.IntFlag{
|
|
|
|
Name: "ttl",
|
|
|
|
Usage: "Time to live for messages, in seconds",
|
|
|
|
Value: params.WhisperTTL,
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// WhisperInjectTestAccounts if set, then test accounts will be imported
|
|
|
|
// into node's key store, and then will be injected as key pairs (identities)
|
|
|
|
// into the Whisper as well.
|
2017-05-02 14:35:37 +00:00
|
|
|
WhisperInjectTestAccounts = cli.BoolTFlag{
|
|
|
|
Name: "injectaccounts",
|
|
|
|
Usage: "Whether test account should be injected or not (default: true)",
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
|
|
|
// FirebaseAuthorizationKey path to file containing FCM password
|
2017-04-12 18:26:54 +00:00
|
|
|
FirebaseAuthorizationKey = cli.StringFlag{
|
|
|
|
Name: "firebaseauth",
|
|
|
|
Usage: "FCM Authorization Key used for sending Push Notifications",
|
|
|
|
}
|
2017-04-09 22:16:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
wnodeCommand = cli.Command{
|
|
|
|
Action: wnode,
|
|
|
|
Name: "wnode",
|
|
|
|
Usage: "Starts Whisper/5 node",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
WhisperEchoModeFlag,
|
|
|
|
WhisperBootstrapNodeFlag,
|
|
|
|
WhisperNotificationServerNodeFlag,
|
|
|
|
WhisperForwarderNodeFlag,
|
|
|
|
WhisperMailserverNodeFlag,
|
2017-04-12 18:26:54 +00:00
|
|
|
WhisperIdentityFile,
|
|
|
|
WhisperPasswordFile,
|
2017-04-09 22:16:05 +00:00
|
|
|
WhisperPoWFlag,
|
|
|
|
WhisperPortFlag,
|
|
|
|
WhisperTTLFlag,
|
2017-05-02 14:35:37 +00:00
|
|
|
WhisperInjectTestAccounts,
|
2017-04-12 18:26:54 +00:00
|
|
|
FirebaseAuthorizationKey,
|
2017-05-16 07:42:30 +00:00
|
|
|
HTTPEnabledFlag,
|
|
|
|
HTTPPortFlag,
|
2017-04-09 22:16:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// version displays app version
|
|
|
|
func wnode(ctx *cli.Context) error {
|
|
|
|
config, err := makeWhisperNodeConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can not parse config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wnodePrintHeader(config)
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
// import test accounts
|
|
|
|
if ctx.BoolT(WhisperInjectTestAccounts.Name) {
|
2017-05-16 12:09:52 +00:00
|
|
|
if err = common.ImportTestAccount(filepath.Join(config.DataDir, "keystore"), "test-account1.pk"); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
if err = common.ImportTestAccount(filepath.Join(config.DataDir, "keystore"), "test-account2.pk"); err != nil {
|
2017-05-03 14:24:48 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
if err := statusAPI.StartNode(config); err != nil {
|
2017-04-09 22:16:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
// inject test accounts into Whisper
|
|
|
|
if ctx.BoolT(WhisperInjectTestAccounts.Name) {
|
2017-05-16 12:09:52 +00:00
|
|
|
testConfig, _ := common.LoadTestConfig()
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = injectAccountIntoWhisper(testConfig.Account1.Address, testConfig.Account1.Password); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = injectAccountIntoWhisper(testConfig.Account2.Address, testConfig.Account2.Password); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 22:16:05 +00:00
|
|
|
// wait till node has been stopped
|
2017-05-16 12:09:52 +00:00
|
|
|
node, err := statusAPI.NodeManager().Node()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
node.Wait()
|
2017-04-09 22:16:05 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
// wnodePrintHeader prints command header
|
2017-04-09 22:16:05 +00:00
|
|
|
func wnodePrintHeader(nodeConfig *params.NodeConfig) {
|
|
|
|
fmt.Println("Starting Whisper/5 node..")
|
|
|
|
|
|
|
|
whisperConfig := nodeConfig.WhisperConfig
|
|
|
|
|
|
|
|
if whisperConfig.EchoMode {
|
|
|
|
fmt.Printf("Whisper Config: %s\n", whisperConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeWhisperNodeConfig parses incoming CLI options and returns node configuration object
|
|
|
|
func makeWhisperNodeConfig(ctx *cli.Context) (*params.NodeConfig, error) {
|
|
|
|
nodeConfig, err := makeNodeConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-16 07:42:30 +00:00
|
|
|
nodeConfig.LightEthConfig.Enabled = false
|
|
|
|
|
2017-04-09 22:16:05 +00:00
|
|
|
whisperConfig := nodeConfig.WhisperConfig
|
|
|
|
|
|
|
|
whisperConfig.Enabled = true
|
2017-04-12 18:26:54 +00:00
|
|
|
whisperConfig.IdentityFile = ctx.String(WhisperIdentityFile.Name)
|
|
|
|
whisperConfig.PasswordFile = ctx.String(WhisperPasswordFile.Name)
|
2017-04-09 22:16:05 +00:00
|
|
|
whisperConfig.EchoMode = ctx.BoolT(WhisperEchoModeFlag.Name)
|
|
|
|
whisperConfig.BootstrapNode = ctx.BoolT(WhisperBootstrapNodeFlag.Name)
|
|
|
|
whisperConfig.ForwarderNode = ctx.Bool(WhisperForwarderNodeFlag.Name)
|
|
|
|
whisperConfig.NotificationServerNode = ctx.Bool(WhisperNotificationServerNodeFlag.Name)
|
|
|
|
whisperConfig.MailServerNode = ctx.Bool(WhisperMailserverNodeFlag.Name)
|
|
|
|
whisperConfig.Port = ctx.Int(WhisperPortFlag.Name)
|
|
|
|
whisperConfig.TTL = ctx.Int(WhisperTTLFlag.Name)
|
|
|
|
whisperConfig.MinimumPoW = ctx.Float64(WhisperPoWFlag.Name)
|
|
|
|
|
2017-04-12 18:26:54 +00:00
|
|
|
if whisperConfig.MailServerNode && len(whisperConfig.PasswordFile) == 0 {
|
2017-04-09 22:16:05 +00:00
|
|
|
return nil, errors.New("mail server requires --password to be specified")
|
|
|
|
}
|
|
|
|
|
2017-04-12 18:26:54 +00:00
|
|
|
if whisperConfig.NotificationServerNode && len(whisperConfig.IdentityFile) == 0 {
|
|
|
|
return nil, errors.New("notification server requires either --identity file to be specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(whisperConfig.PasswordFile) > 0 { // make sure that we can load password file
|
|
|
|
if whisperConfig.PasswordFile, err = filepath.Abs(whisperConfig.PasswordFile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := whisperConfig.ReadPasswordFile(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(whisperConfig.IdentityFile) > 0 { // make sure that we can load identity file
|
|
|
|
if whisperConfig.IdentityFile, err = filepath.Abs(whisperConfig.IdentityFile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := whisperConfig.ReadIdentityFile(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
firebaseConfig := whisperConfig.FirebaseConfig
|
|
|
|
firebaseConfig.AuthorizationKeyFile = ctx.String(FirebaseAuthorizationKey.Name)
|
|
|
|
if len(firebaseConfig.AuthorizationKeyFile) > 0 { // make sure authorization key can be loaded
|
|
|
|
if firebaseConfig.AuthorizationKeyFile, err = filepath.Abs(firebaseConfig.AuthorizationKeyFile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := firebaseConfig.ReadAuthorizationKeyFile(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-09 22:16:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 07:42:30 +00:00
|
|
|
// RPC configuration
|
|
|
|
if !ctx.Bool(HTTPEnabledFlag.Name) {
|
|
|
|
nodeConfig.HTTPHost = "" // HTTP RPC is disabled
|
|
|
|
}
|
|
|
|
nodeConfig.HTTPPort = ctx.Int(HTTPPortFlag.Name)
|
|
|
|
|
2017-04-09 22:16:05 +00:00
|
|
|
return nodeConfig, nil
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
|
|
|
|
// injectAccountIntoWhisper adds key pair into Whisper. Similar to Select/Login,
|
|
|
|
// but allows multiple accounts to be injected.
|
|
|
|
func injectAccountIntoWhisper(address, password string) error {
|
2017-05-16 12:09:52 +00:00
|
|
|
nodeManager := statusAPI.NodeManager()
|
2017-05-02 14:35:37 +00:00
|
|
|
keyStore, err := nodeManager.AccountKeyStore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:44:26 +00:00
|
|
|
acct, err := common.ParseAccountString(address)
|
2017-05-02 14:35:37 +00:00
|
|
|
if err != nil {
|
2017-09-26 13:44:26 +00:00
|
|
|
return account.ErrAddressToAccountMappingFailure
|
2017-05-02 14:35:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 13:44:26 +00:00
|
|
|
_, accountKey, err := keyStore.AccountDecryptedKey(acct, password)
|
2017-05-02 14:35:37 +00:00
|
|
|
if err != nil {
|
2017-09-26 13:44:26 +00:00
|
|
|
return fmt.Errorf("%s: %v", account.ErrAccountToKeyMappingFailure.Error(), err)
|
2017-05-02 14:35:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
whisperService, err := nodeManager.WhisperService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
if _, err = whisperService.AddKeyPair(accountKey.PrivateKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-02 14:35:37 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|