-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
264 lines (223 loc) · 8.83 KB
/
MainWindow.xaml.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace whois_scrapper
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public String proxyFile;
public String domainFile;
public bool running;
public bool cleanUrl = false;
static Object locker = new Object();
private int domainsQuantity;
BackgroundWorker worker = new BackgroundWorker();
public static int threadCount = 0;
public MainWindow()
{
InitializeComponent();
running = false;
}
//Start scrapping process
private void Button_Click(object sender, RoutedEventArgs e)
{
//Start or stop
if (!running)
{
//Check if the proxy file is set
if (proxyFile != null)
{
//Load proxies
Proxies.loadProxies(proxyFile);
//Set output file name
if(chkWHOIS.IsChecked == true && chkWeb.IsChecked == false)
{
FilesControl.fileName = "\\whoisOutput.txt";
whois.doWhois = true;
whois.doWeb = false;
}
else if (chkWHOIS.IsChecked == false && chkWeb.IsChecked == true)
{
FilesControl.fileName = "\\layersOutput.txt";
whois.doWhois = false;
whois.doWeb = true;
}
else if (chkWHOIS.IsChecked == true && chkWeb.IsChecked == true)
{
FilesControl.fileName = "\\whoislayersOutput.txt";
whois.doWhois = true;
whois.doWeb = true;
}
//Start Scrapping process
progressBar.Value = 0;
runProcess(domainFile);
}
else
{
MessageBox.Show("You have to choose the proxies file before scrapping.", "Proxy file needed", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
} else
{
worker.CancelAsync();
running = false;
btnScrap.Content = "Stopping!";
btnScrap.IsEnabled = false;
}
}
//Select domains to scrap in .txt
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//File dialog
Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
//.txt as default file ext
dialog.DefaultExt = ".txt";
Nullable<bool> result = dialog.ShowDialog();
if(result == true)
{
txtDomain.Text = dialog.FileName;
domainFile = dialog.FileName;
if(domainFile != null && proxyFile != null)
{
btnScrap.IsEnabled = true;
}
}
}
//Select proxies .txt file
private void btnSetProxy_Click(object sender, RoutedEventArgs e)
{
//File dialog
Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
//.txt as default file ext
dialog.DefaultExt = ".txt";
Nullable<bool> result = dialog.ShowDialog();
if (result == true)
{
proxyFile = dialog.FileName;
txtProxy.Text = dialog.FileName;
if (domainFile != null && proxyFile != null)
{
btnScrap.IsEnabled = true;
}
}
}
//Create separate thread for the scrap process
public void runProcess(String domainsFile)
{
List<string> domains = new List<string>(File.ReadAllLines(domainsFile));
Crawler.levels = Int32.Parse(txtLevels.Text);
Crawler.pages = Int32.Parse(txtPages.Text);
int scrappedDomains = 0;
domainsQuantity = domains.Count;
labelDomains.Content = "0 of " + domainsQuantity + " domains scrapped!";
progressBar.Maximum = domainsQuantity;
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
running = true;
btnScrap.Content = "Stop";
int threadNumber = Int32.Parse(txtThreads.Text);
ThreadPool.SetMinThreads(threadNumber, threadNumber);
ThreadPool.SetMaxThreads(threadNumber + 5, threadNumber + 9);
worker.DoWork += delegate (object s, DoWorkEventArgs args)
{
String file = (string)args.Argument;
domains.AsParallel().ForAllInApproximateOrder(threadNumber, domain =>
{
if(worker.CancellationPending == false)
{
lock(locker)
{
threadCount++;
}
//Scrape domain WHOIS
String cleanedDomain = WhoisServers.cleanDomain(domain);
List<String> domainEmails = whois.Lookup(cleanedDomain);
//Iterate all WHOIS emails and save it
foreach (String email in domainEmails)
{
String newLine;
if(cleanUrl == true)
{
newLine = email + "|http://" + cleanedDomain;
} else
{
newLine = email + "|" + domain;
}
//Create and save emails in the output file "domain.com:email"
FilesControl.writeToFile(newLine);
}
lock(locker)
{
scrappedDomains++;
}
worker.ReportProgress(scrappedDomains);
lock(locker)
{
threadCount--;
}
}
else {
worker.ReportProgress(scrappedDomains);
}
});
};
worker.ProgressChanged += delegate (object s, ProgressChangedEventArgs args)
{
//if(threadCount >= 0)
//{
lblT.Content = threadCount + " threads running";
//}
labelDomains.Content = args.ProgressPercentage + " of " + domainsQuantity + " domains";
progressBar.Value = args.ProgressPercentage;
//Scrap cancelled
if(running == false && threadCount == 0)
{
btnScrap.Content = "Scrap";
btnScrap.IsEnabled = true;
}
//Scrap finished
if(args.ProgressPercentage == domainsQuantity)
{
running = false;
btnScrap.IsEnabled = true;
btnScrap.Content = "Scrap";
labelDomains.Content = "Finished! " + domainsQuantity + " domains scrapped!";
}
};
worker.RunWorkerAsync(domainsFile);
}
//Only accepts numbers on threads TextBox
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
private void chkCleaned_Checked(object sender, RoutedEventArgs e)
{
cleanUrl = true;
}
private void chkCleaned_Unchecked(object sender, RoutedEventArgs e)
{
cleanUrl = false;
}
}
}