-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
opts.rs
203 lines (182 loc) · 6.46 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// LNP Node: node running lightning network protocol and generalized lightning
// channels.
// Written in 2020-2022 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 std::fmt::Debug;
use std::net::SocketAddr;
use std::path::PathBuf;
use clap::ValueHint;
use internet2::addr::ServiceAddr;
use lnp_rpc::LNP_NODE_RPC_ENDPOINT;
use lnpbp::chain::Chain;
use microservices::shell::shell_setup;
const LNP_NODE_CTL_ENDPOINT: &str = "{data_dir}/ctl";
#[cfg(any(target_os = "linux"))]
pub const LNP_NODE_DATA_DIR: &'static str = "~/.lnp_node/{chain}";
#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))]
pub const LNP_NODE_DATA_DIR: &'static str = "~/.lnp_node/{chain}";
#[cfg(target_os = "macos")]
pub const LNP_NODE_DATA_DIR: &str = "~/Library/Application Support/LNP Node/{chain}";
#[cfg(target_os = "windows")]
pub const LNP_NODE_DATA_DIR: &'static str = "~\\AppData\\Local\\LNP Node\\{chain}";
#[cfg(target_os = "ios")]
pub const LNP_NODE_DATA_DIR: &'static str = "~/Documents/{chain}";
#[cfg(target_os = "android")]
pub const LNP_NODE_DATA_DIR: &'static str = "./{chain}";
pub const LNP_NODE_MSG_ENDPOINT: &str = "{data_dir}/msg";
pub const LNP_NODE_CONFIG: &str = "{data_dir}/lnp_node.toml";
pub const LNP_NODE_TOR_PROXY: &str = "127.0.0.1:9050";
pub const LNP_NODE_KEY_FILE: &str = "{data_dir}/node.key";
/// Marker trait for daemon-specific options
pub trait Options: Clone + Eq + Debug {
/// Daemon-specific configuration extension
type Conf;
/// Returns shared part of options
fn shared(&self) -> &Opts;
/// Constructs daemon-specific configuration object
fn config(&self) -> Self::Conf;
}
/// Shared options used by different binaries
#[derive(Parser, Clone, PartialEq, Eq, Debug)]
pub struct Opts {
/// Set verbosity level.
///
/// Can be used multiple times to increase verbosity.
#[clap(short, long, global = true, parse(from_occurrences))]
pub verbose: u8,
/// Data directory path.
///
/// Path to the directory that contains stored data, and where ZMQ RPC
/// socket files are located.
#[clap(
short,
long,
global = true,
default_value = LNP_NODE_DATA_DIR,
env = "LNP_NODE_DATA_DIR",
value_hint = ValueHint::DirPath
)]
pub data_dir: PathBuf,
/// Path for the configuration file.
///
/// NB: Command-line options override configuration file values.
#[clap(
short,
long,
global = true,
env = "LNP_NODE_CONFIG",
value_hint = ValueHint::FilePath
)]
pub config: Option<PathBuf>,
/// Use Tor.
///
/// If set, specifies SOCKS5 proxy used for Tor connectivity and directs all network
/// traffic through Tor network. If the argument is provided in form of flag, without
/// value, uses `127.0.0.1:9050` as default Tor proxy address.
#[clap(
short = 'T',
long,
alias = "tor",
global = true,
env = "LNP_NODE_TOR_PROXY",
value_hint = ValueHint::Hostname
)]
pub tor_proxy: Option<Option<SocketAddr>>,
/// ZMQ socket for peer message bus used to communicate with LNP node peerd
/// service.
///
/// A user needs to specify this socket usually if it likes to distribute daemons
/// over different server instances. In this case all daemons within the same node
/// must use the same socket address.
///
/// Socket can be either TCP address in form of `<ipv4 | ipv6>:<port>` – or a path
/// to an IPC file.
///
/// Defaults to `msg` file inside `--data-dir` directory.
#[clap(
short = 'M',
long = "msg",
global = true,
env = "LNP_NODE_MSG_ENDPOINT",
default_value = LNP_NODE_MSG_ENDPOINT,
value_hint = ValueHint::FilePath
)]
pub msg_endpoint: ServiceAddr,
/// ZMQ socket for internal service control bus.
///
/// A user needs to specify this socket usually if it likes to distribute daemons
/// over different server instances. In this case all daemons within the same node
/// must use the same socket address.
///
/// Socket can be either TCP address in form of `<ipv4 | ipv6>:<port>` – or a path
/// to an IPC file.
///
/// Defaults to `ctl` file inside `--data-dir` directory, unless `--threaded-daemons`
/// is specified; in that cases parameter in-memory communication protocol is used
/// by default (see ZMQ inproc socket specification).
#[clap(
short = 'X',
long = "ctl",
global = true,
env = "LNP_NODE_CTL_ENDPOINT",
default_value = LNP_NODE_CTL_ENDPOINT,
value_hint = ValueHint::FilePath
)]
pub ctl_endpoint: ServiceAddr,
/// ZMQ socket for LNP Node client-server RPC API.
///
/// Socket can be either TCP address in form of `<ipv4 | ipv6>:<port>` – or a path
/// to an IPC file.
#[clap(
short = 'R',
long = "rpc",
global = true,
default_value = LNP_NODE_RPC_ENDPOINT,
env = "LNP_NODE_RPC_ENDPOINT"
)]
pub rpc_endpoint: ServiceAddr,
/// Blockchain to use
#[clap(
short = 'n',
long,
global = true,
alias = "network",
default_value = "signet",
env = "LNP_NODE_NETWORK"
)]
pub chain: Chain,
/// Electrum server to use.
#[clap(
long,
global = true,
default_value("electrum.blockstream.info"),
env = "LNP_NODE_ELECTRUM_SERVER",
value_hint = ValueHint::Hostname
)]
pub electrum_server: String,
/// Customize Electrum server port number. By default the wallet will use port
/// matching the selected network.
#[clap(long, global = true, env = "LNP_NODE_ELECTRUM_PORT")]
pub electrum_port: Option<u16>,
/// Spawn daemons as threads and not processes
#[clap(short = 't', long = "threaded")]
pub threaded_daemons: bool,
}
impl Opts {
pub fn process(&mut self) {
shell_setup(
self.verbose,
[&mut self.msg_endpoint, &mut self.ctl_endpoint, &mut self.rpc_endpoint],
&mut self.data_dir,
&[("{chain}", self.chain.to_string())],
);
}
}