forked from farcaster-project/farcaster-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opts.rs
153 lines (136 loc) · 4.78 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
// LNP Node: node running lightning network protocol and generalized lightning
// channels.
// Written in 2020 by
// Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// 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::{Clap, ValueHint};
use std::net::SocketAddr;
use std::path::PathBuf;
use std::str::FromStr;
use std::{fs, io};
use internet2::PartialNodeAddr;
use microservices::shell::LogLevel;
#[cfg(any(target_os = "linux"))]
pub const FARCASTER_DATA_DIR: &str = "~/.farcaster";
#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))]
pub const FARCASTER_DATA_DIR: &str = "~/.farcaster";
#[cfg(target_os = "macos")]
pub const FARCASTER_DATA_DIR: &str = "~/Library/Application Support/Farcaster";
#[cfg(target_os = "windows")]
pub const FARCASTER_DATA_DIR: &str = "~\\AppData\\Local\\Farcaster";
#[cfg(target_os = "ios")]
pub const FARCASTER_DATA_DIR: &str = "~/Documents";
#[cfg(target_os = "android")]
pub const FARCASTER_DATA_DIR: &str = ".";
pub const FARCASTER_MSG_SOCKET_NAME: &str = "lnpz:{data_dir}/msg.rpc?api=esb";
pub const FARCASTER_CTL_SOCKET_NAME: &str = "lnpz:{data_dir}/ctl.rpc?api=esb";
pub const FARCASTER_TOR_PROXY: &str = "127.0.0.1:9050";
pub const FARCASTER_KEY_FILE: &str = "{data_dir}/key.dat";
/// Shared options used by different binaries
#[derive(Clap, Clone, PartialEq, Eq, Debug)]
pub struct Opts {
/// Data directory path
///
/// Path to the directory that contains Farcaster Node data, and where ZMQ
/// RPC socket files are located.
#[clap(
short,
long,
global = true,
default_value = FARCASTER_DATA_DIR,
env = "FARCASTER_DATA_DIR",
value_hint = ValueHint::DirPath
)]
pub data_dir: PathBuf,
/// Set verbosity level
///
/// Can be used multiple times to increase verbosity.
#[clap(short, long, global = true, parse(from_occurrences))]
pub verbose: u8,
/// 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 = "FARCASTER_TOR_PROXY",
value_hint = ValueHint::Hostname
)]
pub tor_proxy: Option<Option<SocketAddr>>,
/// ZMQ socket name/address to forward all incoming protocol messages
///
/// Internal interface for transmitting P2P network messages. Defaults
/// to `msg.rpc` file inside `--data-dir` directory.
#[clap(
short = 'm',
long,
global = true,
env = "FARCASTER_MSG_SOCKET",
value_hint = ValueHint::FilePath,
default_value = FARCASTER_MSG_SOCKET_NAME
)]
pub msg_socket: PartialNodeAddr,
/// ZMQ socket name/address for daemon control interface
///
/// Internal interface for control PRC protocol communications. Defaults
/// to `ctl.rpc` file inside `--data-dir` directory.
#[clap(
short = 'x',
long,
global = true,
env = "FARCASTER_CTL_SOCKET",
value_hint = ValueHint::FilePath,
default_value = FARCASTER_CTL_SOCKET_NAME
)]
pub ctl_socket: PartialNodeAddr,
}
/// Token used in services
#[derive(Clap, Clone, PartialEq, Eq, Debug)]
pub struct TokenString {
/// Token used to authentify calls
#[clap(long, env = "FARCASTER_TOKEN")]
pub token: String,
}
impl FromStr for TokenString {
type Err = io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(TokenString {
token: s.to_string(),
})
}
}
impl Opts {
pub fn process(&mut self) {
LogLevel::from_verbosity_flag_count(self.verbose).apply();
let mut me = self.clone();
me.data_dir = PathBuf::from(
shellexpand::tilde(&me.data_dir.to_string_lossy().to_string()).to_string(),
);
fs::create_dir_all(&me.data_dir).expect("Unable to access data directory");
for s in vec![&mut self.msg_socket, &mut self.ctl_socket] {
match s {
PartialNodeAddr::ZmqIpc(path, ..) | PartialNodeAddr::Posix(path) => {
me.process_dir(path);
}
_ => {}
}
}
}
pub fn process_dir(&self, path: &mut String) {
*path = path.replace("{data_dir}", &self.data_dir.to_string_lossy());
*path = shellexpand::tilde(path).to_string();
}
}