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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use std::process::exit;
use getopts::Options;
use openssl::ssl::{SslMethod, SslContext, SslVerifyMode};
use openssl::x509::X509FileType;
use mqtt3::{LastWill, SubscribeTopic, QoS, Protocol};
use super::command::{Command, SubscribeCommand, PublishCommand};
pub struct CLI {
program: String,
command: String,
arguments: Vec<String>
}
impl CLI {
pub fn new<C: IntoIterator<Item=String>>(args: C) -> CLI {
let mut args: Vec<String> = args.into_iter().collect();
let program = args.remove(0);
let command = if args.len() < 1 {
"help".to_string()
} else {
args.remove(0)
};
CLI {
program: program,
command: command,
arguments: args
}
}
pub fn parse(&self) -> Box<Command> {
match self.command.as_str() {
"subscribe" | "sub" => Box::new(self.subscribe_parse()),
"publish" | "pub" => Box::new(self.publish_parse()),
"help" | _ => {
self.print_usage();
exit(0);
}
}
}
pub fn publish_parse(&self) -> PublishCommand {
let default = PublishCommand::default();
let mut opts = Options::new();
opts.optopt("t", "", "The topic name which payload data is published.", "topic");
opts.optopt("m", "", "Message payload to send", "message");
opts.optopt("f", "", "Send file as the payload", "file");
opts.optopt("q", "", "Quality of service level", "qos");
opts.optflag("r", "", "Message should be retained");
opts.optopt("a", "", "Address to connect to. Defaults to localhost", "address");
opts.optopt("p", "", "Port to connect to. Defaults to 1883", "port");
opts.optopt("k", "", "Keep alive the link with the server then try to send ping request. Defaults to 60", "seconds");
opts.optopt("i", "", "Specifies a client id", "client_id");
opts.optopt("u", "", "Specifies a username with which to authenticate to", "username");
opts.optopt("P", "", "Specifies a password with which to authenticate to", "password");
opts.optopt("v", "", "MQTT protocol version. Can be 3.1 or 3.1.1", "version");
opts.optflag("d", "", "Show debug messages");
opts.optopt("", "tls", "Enables TLS and sets protocol version. Can be tlsv1, tlsv1.1, tlsv1.2", "");
opts.optopt("", "cafile", "Specifies the file that contains trusted CA certificates.", "file");
opts.optopt("", "key", "Path to private key", "path");
opts.optopt("", "cert", "Path to certificate", "path");
opts.optflag("", "no-verify", "Disables client cert requests");
opts.optflag("h", "help", "Display this message");
let matches = match opts.parse(&self.arguments[..]) {
Ok(m) => { m }
Err(f) => {
self.cli_error(f.to_string());
}
};
if matches.opt_present("h") {
self.publish_print_usage(opts);
exit(0);
};
let topic = matches.opt_str("t").unwrap_or_else(|| self.cli_error("Please set the topic name"));
let message = matches.opt_str("m");
let file = matches.opt_str("f");
if message.is_some() && file.is_some() {
self.cli_error("Shouldn't set both message and file together");
};
let qos = matches.opt_str("q").map_or(QoS::AtLeastOnce, |s| self.parse_qos(s));
let retain = matches.opt_present("r");
let address = matches.opt_str("a").unwrap_or(default.address);
let port = if matches.opt_present("p") {
match matches.opt_str("p").unwrap().parse::<u16>() {
Ok(v) => v,
Err(_) => {
self.cli_error("port format error");
}
}
} else {
default.port
};
let client_id = matches.opt_str("i");
let username = matches.opt_str("u");
let password = matches.opt_str("P");
let debug = matches.opt_present("d");
let keep_alive = if matches.opt_present("k") {
match matches.opt_str("k").unwrap().parse::<u16>() {
Ok(v) => v,
Err(_) => {
self.cli_error("keep alive format error");
}
}
} else {
default.keep_alive
};
let protocol = if matches.opt_present("v") {
match matches.opt_str("v").unwrap().as_ref() {
"3.1" => Protocol::MQIsdp(3),
"3.1.1" => Protocol::MQTT(4),
_ => {
self.cli_error("unsupported protocol version");
}
}
} else {
default.protocol
};
let cafile = matches.opt_str("cafile");
let key = matches.opt_str("key");
let cert = matches.opt_str("cert");
let ssl_method = if matches.opt_present("tls") {
match matches.opt_str("tls").unwrap().as_ref() {
"1" => Some(SslMethod::Tlsv1),
"1.1" => Some(SslMethod::Tlsv1_1),
"1.2" => Some(SslMethod::Tlsv1_2),
_ => {
self.cli_error("unsupported TLS version")
}
}
} else {
None
};
let verify_mode = if matches.opt_present("no-verify") {
SslVerifyMode::from_bits_truncate(0)
} else {
SslVerifyMode::from_bits_truncate(1)
};
let ssl_context = ssl_method.map(|ssl| {
let mut context = SslContext::new(ssl).unwrap();
context.set_verify(verify_mode, None);
if let Some(ref cafile_path) = cafile {
context.set_CA_file(cafile_path).unwrap();
}
if let Some(ref key_path) = key {
context.set_private_key_file(key_path, X509FileType::PEM).unwrap();
}
if let Some(ref cert_path) = cert {
context.set_certificate_file(cert_path, X509FileType::PEM).unwrap();
}
context
});
PublishCommand {
topic: topic,
message: message,
file: file,
qos: qos,
retain: retain,
address: address,
port: port,
keep_alive: keep_alive,
debug: debug,
protocol: protocol,
client_id: client_id,
username: username,
password: password,
ssl_context: ssl_context
}
}
pub fn subscribe_parse(&self) -> SubscribeCommand {
let default = SubscribeCommand::default();
let mut opts = Options::new();
opts.optopt("a", "", "Address to connect to. Defaults to localhost", "address");
opts.optopt("p", "", "Port to connect to. Defaults to 1883", "port");
opts.optopt("q", "", "Maximum quality of service level", "qos");
opts.optopt("k", "", "Keep alive the link with the server then try to send ping request. Defaults to 60", "seconds");
opts.optflag("r", "", "Reconnect automatically if connection was broken.");
opts.optopt("i", "", "Specifies a client id", "client_id");
opts.optopt("u", "", "Specifies a username with which to authenticate to", "username");
opts.optopt("P", "", "Specifies a password with which to authenticate to", "password");
opts.optopt("v", "", "MQTT protocol version. Can be 3.1 or 3.1.1", "version");
opts.optflag("c", "", "Set 'clean session' flag");
opts.optflag("d", "", "Show debug messages");
opts.optflag("s", "", "Prevent to show a connection messages");
opts.optopt("", "will-message", "Message for the client Will", "");
opts.optopt("", "will-topic", "Topic for the client Will", "");
opts.optopt("", "will-qos", "QoS level for the client Will", "");
opts.optopt("", "will-retain", "Make the client Will retained", "");
opts.optopt("", "tls", "Enables TLS and sets protocol version. Can be tlsv1, tlsv1.1, tlsv1.2", "");
opts.optopt("", "cafile", "Specifies the file that contains trusted CA certificates.", "file");
opts.optopt("", "key", "Path to private key", "path");
opts.optopt("", "cert", "Path to certificate", "path");
opts.optflag("", "no-verify", "Disables client cert requests");
opts.optflag("h", "help", "Display this message");
let matches = match opts.parse(&self.arguments[..]) {
Ok(m) => { m }
Err(f) => {
self.cli_error(f.to_string());
}
};
if matches.opt_present("h") {
self.subscribe_print_usage(opts);
exit(0);
};
let address = matches.opt_str("a").unwrap_or(default.address);
let port = if matches.opt_present("p") {
match matches.opt_str("p").unwrap().parse::<u16>() {
Ok(v) => v,
Err(_) => {
self.cli_error("port format error");
}
}
} else {
default.port
};
let client_id = matches.opt_str("i");
let clean_session = matches.opt_present("c");
let username = matches.opt_str("u");
let password = matches.opt_str("P");
let topic_filters = Vec::new();
let limit = None;
let will_topic = matches.opt_str("will-topic");
let will_message = matches.opt_str("will-message");
let will_qos = matches.opt_str("will-qos");
let will_retain = matches.opt_present("will-retain");
let debug = matches.opt_present("d");
let silence = matches.opt_present("s");
let keep_alive = if matches.opt_present("k") {
match matches.opt_str("k").unwrap().parse::<u16>() {
Ok(v) => v,
Err(_) => {
self.cli_error("keep alive format error");
}
}
} else {
default.keep_alive
};
let reconnect = matches.opt_present("r");
let protocol = if matches.opt_present("v") {
match matches.opt_str("v").unwrap().as_ref() {
"3.1" => Protocol::MQIsdp(3),
"3.1.1" => Protocol::MQTT(4),
_ => {
self.cli_error("unsupported protocol version");
}
}
} else {
default.protocol
};
let log_file = None;
let last_will = if will_topic.is_some() && will_message.is_some() {
Some(LastWill {
topic: will_topic.unwrap(),
message: will_message.unwrap(),
qos: will_qos.map_or(QoS::AtMostOnce, |s| self.parse_qos(s)),
retain: will_retain
})
} else {
if !will_topic.is_none() || !will_topic.is_none() {
self.cli_error("both will-topic and will-message required");
};
None
};
let cafile = matches.opt_str("cafile");
let key = matches.opt_str("key");
let cert = matches.opt_str("cert");
let ssl_method = if matches.opt_present("tls") {
match matches.opt_str("tls").unwrap().as_ref() {
"1" => Some(SslMethod::Tlsv1),
"1.1" => Some(SslMethod::Tlsv1_1),
"1.2" => Some(SslMethod::Tlsv1_2),
_ => {
self.cli_error("unsupported TLS version")
}
}
} else {
None
};
let verify_mode = if matches.opt_present("no-verify") {
SslVerifyMode::from_bits_truncate(0)
} else {
SslVerifyMode::from_bits_truncate(1)
};
let ssl_context = ssl_method.map(|ssl| {
let mut context = SslContext::new(ssl).unwrap();
context.set_verify(verify_mode, None);
if let Some(ref cafile_path) = cafile {
context.set_CA_file(cafile_path).unwrap();
}
if let Some(ref key_path) = key {
context.set_private_key_file(key_path, X509FileType::PEM).unwrap();
}
if let Some(ref cert_path) = cert {
context.set_certificate_file(cert_path, X509FileType::PEM).unwrap();
}
context
});
let retain = false;
let qos = matches.opt_str("q").map_or(QoS::ExactlyOnce, |s| self.parse_qos(s));
let topics = if !matches.free.is_empty() {
matches.free.iter().map(|topic| SubscribeTopic { topic_path: topic.clone(), qos: qos} ).collect()
} else {
default.topics.iter().map(|topic| SubscribeTopic { topic_path: topic.topic_path.clone(), qos: qos} ).collect()
};
SubscribeCommand {
topics: topics,
address: address,
port: port,
clean_session: clean_session,
last_will: last_will,
log_file: log_file,
debug: debug,
silence: silence,
keep_alive: keep_alive,
reconnect: reconnect,
protocol: protocol,
client_id: client_id,
username: username,
password: password,
limit: limit,
retain: retain,
topic_filters: topic_filters,
ssl_context: ssl_context
}
}
fn print_usage(&self) {
let mut brief = "mqttc is a simple MQTT client that provides to publish message or subscribe to topics.\n\n".to_string();
brief = brief + format!("Usage:\n {} command\n {} --help\n\n", self.program, self.program).as_str();
brief = brief + "Commands:\n";
brief = brief + " publish/pub \tPublish message to a topic\n";
brief = brief + " subscribe/sub \tSubscribe to topics\n\n";
print!("{}", brief);
}
pub fn publish_print_usage(&self, opts: Options) {
let brief = format!("Usage: {} publish [OPTIONS]", self.program);
print!("{}", opts.usage(&brief));
}
pub fn subscribe_print_usage(&self, opts: Options) {
let brief = format!("Usage: {} subscribe [OPTIONS] [TOPICS...]", self.program);
print!("{}", opts.usage(&brief));
}
fn parse_qos(&self, s: String) -> QoS {
match s.parse::<u8>() {
Ok(v) => {
match QoS::from_u8(v) {
Ok(qos) => qos,
Err(_) => {
self.cli_error("unsupported qos value");
}
}
},
Err(_) => {
self.cli_error("qos format error");
}
}
}
fn cli_error<M: AsRef<str>>(&self, msg: M) -> ! {
println!("{}: {}", self.program, msg.as_ref());
exit(64);
}
}