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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use std::{net::IpAddr, path::Path, process::Stdio};
use tokio::process::Command;
use crate::croc::{ClipboardKind, EncryptionCurve, Relay, receive::CrocReceive};
use super::send::CrocSend;
/// A builder around a [`Command`] that allows to set up a `croc` instance.
pub struct Croc {
/// The inner `croc` command.
inner: Command,
/// Use a built-in DNS stub resolver rather than the host operating system
internal_dns: bool,
/// Whether to enable debug mode.
debug: bool,
/// Automatically agree to all prompts.
yes: bool,
/// Whether file compression should be enabled.
compress: bool,
/// Force to use only local connections.
local: bool,
/// Disables all output.
quiet: bool,
/// Whether the code should be copied to the clipboard by `croc`.
clipboard: ClipboardKind,
/// The multicast address to use for local discovery.
multicast: Option<IpAddr>,
/// The encryption curve to be used when encrypting files.
curve: EncryptionCurve,
/// The IP of the sender, if known.
ip: Option<IpAddr>,
/// Address of the relay.
relay: Option<Relay>,
/// The password of the relay.
pass: Option<String>,
}
impl Croc {
/// Creates a [`Croc`] builder.
pub fn new() -> Self {
Self::new_with_path("croc")
}
/// Creates a [`Croc`] builder with the provided executable name.
pub fn new_with_path<P: AsRef<Path>>(name: P) -> Self {
let inner = Command::new(name.as_ref().as_os_str());
let croc = Self {
inner,
internal_dns: false,
debug: false,
yes: true,
compress: true,
local: false,
clipboard: ClipboardKind::default(),
quiet: false,
multicast: None,
curve: EncryptionCurve::default(),
ip: None,
relay: None,
pass: None,
};
croc.no_window().no_stdin().no_stdout().pipe_stderr()
}
/// Sets if `croc` should use a built-in DNS stub resolver rather than the host operating system. (Default: `false`)
pub fn internal_dns(mut self, value: bool) -> Self {
self.internal_dns = value;
self
}
/// Sets the debug mode. (Default: `false`)
pub fn debug(mut self, value: bool) -> Self {
self.debug = value;
self
}
/// Sets the `--yes` argument in `croc`, automatically agreeing to all prompts. (Default: `true`)
pub fn yes(mut self, value: bool) -> Self {
self.yes = value;
self
}
/// Sets whether compression should be enabled or not. (Default: `true`)
pub fn compress(mut self, value: bool) -> Self {
self.compress = value;
self
}
/// Forces `croc` to only use local connections. (Default: `false`)
pub fn local(mut self, value: bool) -> Self {
self.local = value;
self
}
/// Sets whether output should be disabled. (Default: `false`)
pub fn quiet(mut self, value: bool) -> Self {
self.quiet = value;
self
}
/// Sets the copy to clipboard behaviour for `croc`. (Default: [`ClipboardKind::Enabled`])
pub fn clipboard<Kind: Into<ClipboardKind>>(mut self, kind: Kind) -> Self {
self.clipboard = kind.into();
self
}
/// Sets the multicast address for local discovery. (Default: `239.255.255.250`)
pub fn multicast<Ip: Into<IpAddr>>(mut self, address: Ip) -> Self {
self.multicast = Some(address.into());
self
}
/// Sets the encryption curve to be used by `croc`. (Default: [`EncryptionCurve::P256`])
pub fn curve(mut self, curve: EncryptionCurve) -> Self {
self.curve = curve;
self
}
/// Specifies the IP address of the sender, if known. (Default: `None`)
pub fn ip<Ip: Into<IpAddr>>(mut self, ip: Ip) -> Self {
self.ip = Some(ip.into());
self
}
/// Specifies the IP address of the relay. (Default: `204.168.131.42:9009`)
pub fn relay<Ip: Into<IpAddr>>(mut self, ip: Ip, port: u16) -> Self {
self.relay = Some(Relay::new(ip, port));
self
}
/// Specifies the password of the relay. (Default: `pass123`)
pub fn pass<S: ToString>(mut self, pass: S) -> Self {
self.pass = Some(pass.to_string());
self
}
/// Disables croc stdout.
pub fn no_stdout(mut self) -> Self {
self.inner.stdout(Stdio::null());
self
}
/// Disables croc stdin.
pub fn no_stdin(mut self) -> Self {
self.inner.stdin(Stdio::null());
self
}
/// Disables croc stderr.
pub fn no_stderr(mut self) -> Self {
self.inner.stderr(Stdio::null());
self
}
/// Pipes stderr for reading. All metadata croc gives us comes from stderr.
pub fn pipe_stderr(mut self) -> Self {
self.inner.stderr(Stdio::piped());
self
}
/// Pipes stdout for reading. Croc can output received files to stdout.
pub fn pipe_stdout(mut self) -> Self {
self.inner.stdout(Stdio::piped());
self
}
/// Pipes stdin for writing. Used for sending passphrases or other input.
pub fn pipe_stdin(mut self) -> Self {
self.inner.stdin(Stdio::piped());
self
}
/// Disables the console window on Windows.
/// Highly recommended for GUI applications.
pub fn no_window(mut self) -> Self {
self.inner.disable_window();
self
}
/// Sets up croc to send files.
pub fn send<P>(mut self) -> CrocSend<P>
where
P: AsRef<Path>,
{
self.parse_options();
CrocSend::new(self.inner)
}
/// Sets up `croc` to receive files.
pub fn receive(mut self) -> CrocReceive {
self.parse_options();
CrocReceive::new(self.inner)
}
/// Use the settings in `self` to add their respective arguments to the `croc` command.
fn parse_options(&mut self) {
if self.internal_dns {
self.inner.arg("--internal-dns");
}
if self.debug {
self.inner.arg("--debug");
}
if self.yes {
self.inner.arg("--yes");
}
if !self.compress {
self.inner.arg("--no-compress");
}
if self.local {
self.inner.arg("--local");
}
if self.quiet {
self.inner.arg("--quiet");
}
if let Some(address) = self.multicast {
self.inner.arg(format!("--multicast={}", address));
}
if let Some(address) = self.ip {
self.inner.arg(format!("--ip={}", address));
}
if let Some(ref relay) = self.relay {
let key = match relay.address {
IpAddr::V4(_) => "CROC_RELAY",
IpAddr::V6(_) => "CROC_RELAY6",
};
self.inner.env(key, relay.to_string());
}
if let Some(ref pass) = self.pass {
self.inner.env("CROC_PASS", pass);
}
match self.clipboard {
ClipboardKind::Disabled => {
self.inner.arg("--disable-clipboard");
}
ClipboardKind::Extended => {
self.inner.arg("--extended-clipboard");
}
ClipboardKind::Enabled => {}
}
match self.curve {
EncryptionCurve::P256 => {}
ref curve => {
self.inner.arg(format!("--curve={}", curve));
}
}
}
}
trait DisableWindow {
/// Disables the console window on Windows.
/// Highly recommended for GUI applications.
fn disable_window(&mut self);
}
impl DisableWindow for Command {
fn disable_window(&mut self) {
#[cfg(target_os = "windows")]
self.creation_flags(0x08000000);
}
}
impl From<Croc> for Command {
fn from(croc: Croc) -> Self {
croc.inner
}
}
impl Default for Croc {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for Croc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}