-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
149 lines (134 loc) · 5.17 KB
/
Copy pathmain.rs
File metadata and controls
149 lines (134 loc) · 5.17 KB
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
/*
* This software is Copyright (c) 2021 The Regents of the University of
* California. All Rights Reserved. Permission to copy, modify, and distribute this
* software and its documentation for academic research and education purposes,
* without fee, and without a written agreement is hereby granted, provided that
* the above copyright notice, this paragraph and the following three paragraphs
* appear in all copies. Permission to make use of this software for other than
* academic research and education purposes may be obtained by contacting:
*
* Office of Innovation and Commercialization
* 9500 Gilman Drive, Mail Code 0910
* University of California
* La Jolla, CA 92093-0910
* (858) 534-5815
* invent@ucsd.edu
*
* This software program and documentation are copyrighted by The Regents of the
* University of California. The software program and documentation are supplied
* "as is", without any accompanying services from The Regents. The Regents does
* not warrant that the operation of the program will be uninterrupted or
* error-free. The end-user understands that the program was developed for research
* purposes and is advised not to rely exclusively on the program for any reason.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
* PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
* THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
* IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE
* MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
*/
use cc_host_mapper::*;
use clap::Parser;
use dialoguer::{Confirm, Input};
#[derive(Parser)]
struct Opts {
/// Output file name
#[arg(short, long)]
output: Option<String>,
/// Number of threads to be used for crawling
#[arg(short, long)]
threads: Option<usize>,
/// Index wanted to crawl from
#[arg(short, long)]
index_id: Option<String>,
/// Dump cluster index to CSV file
#[arg(short, long)]
dump_cluster_idx: bool,
}
fn main() {
let opts: Opts = Opts::parse();
let mut index_list: Vec<Index> = retrieve_indices();
// Sort the list first to get newest-first order
index_list.sort();
let ids = &index_list
.iter()
.cloned()
.map(|x| x.id)
.collect::<Vec<String>>();
let ids_str = ids.join(",");
let mut selected_index;
match opts.index_id {
Some(index_id) => {
selected_index = match index_list.iter().find(|x| x.id == index_id) {
Some(index) => index.clone(),
None => panic!("index id {} not found", index_id),
}
}
None => {
selected_index = index_list[0].to_owned();
if !Confirm::new()
.with_prompt(format!("Do you want to crawl index {}?", selected_index.id))
.default(false)
.interact()
.unwrap()
{
// we don't want to go with the most recent
if !Confirm::new()
.with_prompt("Do you want to crawl another index?".to_string())
.default(false)
.interact()
.unwrap()
{
// we don't want to select one
println!("nevermind then :)");
return;
} else {
// select one index from list
let input: String = Input::new()
.with_prompt(format!(
"Select from the following index IDs:\n{}",
ids_str.as_str()
))
.interact_text()
.unwrap();
match index_list.iter().find(|x| x.id == input) {
Some(index) => selected_index = index.clone(),
None => return,
}
}
}
}
}
let output_file_name = match opts.output {
Some(output) => output,
None => {
format!(
"mapping-{}.csv.gz",
selected_index.id.as_str().to_lowercase()
)
}
};
if opts.dump_cluster_idx {
println!("dumping cluster.idx to csv file");
let host_pointers = read_cluster_idx(&selected_index.id.to_owned());
let mut writer = get_writer(&format!(
"cluster-idx-{}.csv.gz",
selected_index.id.as_str().to_lowercase()
));
for item in host_pointers {
writeln!(writer, "{}", item.to_csv()).unwrap();
}
return;
}
println!("Will start crawling {} now...", selected_index.id);
crawl_host_ip_mapping(
selected_index.id.to_owned(),
output_file_name.to_owned(),
opts.threads,
);
}