-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathopts.rs
149 lines (135 loc) · 5.19 KB
/
opts.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// LNP Node: node running lightning network protocol and generalized lightning
// channels.
// Written in 2020 by
// Dr. Maxim Orlovsky <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.
use clap::{AppSettings, ArgGroup, Clap, ValueHint};
use std::fs;
use std::net::IpAddr;
use std::path::PathBuf;
use lnpbp::lnp::{FramingProtocol, LocalNode, RemoteNodeAddr};
use lnpbp::strict_encoding::{StrictDecode, StrictEncode};
use crate::opts::LNP_NODE_KEY_FILE;
/// Lightning peer network connection daemon; part of LNP Node
///
/// Daemon listens to incoming connections from the lightning network peers
/// (if started with `--listen` argument) or connects to the remote peer
/// (specified with `--connect` argument) and passes all incoming messages into
/// ZMQ messaging socket (controlled with `--msg-socket` argument, defaulting to
/// `msg.rpc` file inside the data directory from `--data-dir`). It also
/// forwards messages from the same socket to the remote peer.
///
/// The daemon is controlled though ZMQ ctl socket (see `ctl-socket` argument
/// description)
#[derive(Clap, Clone, PartialEq, Eq, Debug)]
#[clap(
name = "peerd",
bin_name = "peerd",
author,
version,
group = ArgGroup::new("action").required(true),
setting = AppSettings::ColoredHelp
)]
pub struct Opts {
// These params are passed through command-line argument or environment
// only since they are instance-specific
/// Start daemon in listening mode binding the provided local address
///
/// Binds to the specified interface and listens for incoming connections,
/// spawning a new thread / forking child process for each new incoming
/// client connecting the opened socket. Whether the child is spawned as a
/// thread or forked as a child process determined by the presence of
/// `--use-threads` flag.
/// If the argument is provided in form of flag, without value, uses
/// `0.0.0.0` as the bind address.
#[clap(short = 'L', long, group = "action", value_hint = ValueHint::Hostname)]
pub listen: Option<Option<IpAddr>>,
/// Connect to a remote peer with the provided address after start
///
/// Connects to the specified remote peer. Peer address should be given as
/// either IPv4, IPv6 or Onion address (v2 or v3); in the former case you
/// will be also required to provide `--tor` argument.
#[clap(short = 'C', long, group = "action")]
pub connect: Option<RemoteNodeAddr>,
/// Customize port used by lightning peer network
///
/// Optional argument specifying local or remote TCP port to use with the
/// address given to `--listen` or `--connect` argument.
#[clap(short, long, default_value = "9735")]
pub port: u16,
/// Overlay peer communications through different transport protocol.
#[clap(
short,
long,
default_value = "tcp",
possible_values = &["tcp", "zmq", "http", "websocket", "smtp"]
)]
pub overlay: FramingProtocol,
/// Node key configuration
#[clap(flatten)]
pub key_opts: KeyOpts,
/// RGB configuration: ignored by this daemon
#[clap(short, long = "rgb20-rpc")]
pub r: Option<String>,
/// These params can be read also from the configuration file, not just
/// command-line args or environment variables
#[clap(flatten)]
pub shared: crate::opts::Opts,
}
/// Node key configuration
#[derive(Clap, Clone, PartialEq, Eq, Debug)]
pub struct KeyOpts {
/// Node key file
///
/// Location for the file containing node private Secp256k1 key
/// (unencrypted)
#[clap(
short,
long,
env = "LNP_NODE_KEY_FILE",
default_value = LNP_NODE_KEY_FILE,
value_hint = ValueHint::FilePath
)]
pub key_file: String,
}
impl Opts {
pub fn process(&mut self) {
self.shared.process();
self.key_opts.process(&self.shared);
}
}
impl KeyOpts {
pub fn process(&mut self, shared: &crate::opts::Opts) {
shared.process_dir(&mut self.key_file);
}
pub fn local_node(&self) -> LocalNode {
if PathBuf::from(self.key_file.clone()).exists() {
LocalNode::strict_decode(fs::File::open(&self.key_file).expect(
&format!(
"Unable to open key file {}; please check that the user \
running the daemon has necessary permissions",
self.key_file
),
))
.expect("Unable to read node code file format")
} else {
let local_node = LocalNode::new();
let key_file = fs::File::create(&self.key_file).expect(&format!(
"Unable to create key file '{}'; please check that the path exists",
self.key_file
));
local_node
.strict_encode(key_file)
.expect("Unable to save generated node kay");
local_node
}
}
}