-
Notifications
You must be signed in to change notification settings - Fork 1
/
Searcher.cs
266 lines (181 loc) · 8.13 KB
/
Searcher.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
265
266
/*
* Created by SharpDevelop.
* User: Elite
* Date: 7/9/2020
* Time: 10:07 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.IO;
using HtmlAgilityPack;
using System.Threading;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ProxyScraper {
/// <summary>
/// Search for proxy list websites
/// </summary>
public class Searcher {
private string searchQuery = null;
public static List<string> scrapedLinksArchive = new List<string>();
private readonly Regex rg = new Regex(@"""([^""]*)&");
public Searcher (string searchQuery) {
this.searchQuery = searchQuery;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
}
public List<String> search (int start, int end) {
List<String> results = new List<string>();
int iter = start;
string link = "";
while (iter != end) {
if (iter == 1) link = "https://www.google.com/search?q=" + searchQuery;
else link = "https://www.google.com/search?q=" + searchQuery + "&start=" + ((iter-1)*10);
if (iter != end) ++iter;
link = link.Replace(' ', '+');
link = "https://" + link.Substring(8).Replace(":", "%3A");
Program.debug(link);
Thread.Sleep(2500);
byte[] res = new Byte[16384];
Stream s = null;
try {
HttpWebRequest rq = (HttpWebRequest)(WebRequest.Create(link));
HttpWebResponse rp = (HttpWebResponse)rq.GetResponse();
s = rp.GetResponseStream();
}
catch (Exception ex) { /*Banned(too many requests)probably*/ Program.debug(ex.ToString()); Program.cm.updateStatus(Status.IDLE); Thread.Sleep(30000);
Program.cm.updateStatus(Status.SEARCHING);
try {
HttpWebRequest rq = (HttpWebRequest)(WebRequest.Create(link));
rq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
HttpWebResponse rp = (HttpWebResponse)rq.GetResponse();
s = rp.GetResponseStream();
}
catch (Exception ex0) { Program.debug(ex0.ToString()); Program.cm.updateStatus(Status.IDLE); Thread.Sleep(60000); return new List<String>(); }
}
int i = 0;
StringBuilder b = new StringBuilder();
while (true) {
i = s.Read(res, 0, res.Length);
if (i == 0) break;
b.Append(Encoding.ASCII.GetString(res, 0, i));
}
HtmlDocument d = new HtmlDocument();
d.OptionOutputAsXml = true;
d.LoadHtml(b.ToString());
HtmlNode n = d.DocumentNode;
foreach (HtmlNode htn in n.Descendants("a").Where(ppeater3000 => ppeater3000.GetAttributeValue("href", "").StartsWith("/url?q="))) {
string p = this.rg.Match(htn.OuterHtml).ToString();
p = p.Substring(8).Split('&')[0];
if (p.Contains("accounts.google.com")) continue;
this.inputLink(p, results);
foreach (string p0 in this.searchForMorePages(p))
this.inputLink(p0, results);
}
}
Thread.Sleep(4300); //Sleep a bit to avoid getting banned, doesn't mess up gui since this is in a new thread presumably
return results;
}
private List<string> searchForMorePages (string link) {
List<String> newPages = new List<String>();
byte[] res = new Byte[16384];
Stream s = null;
try {
HttpWebRequest rq = (HttpWebRequest)(WebRequest.Create(link));
rq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36";
rq.ServicePoint.Expect100Continue = true;
HttpWebResponse rp = (HttpWebResponse)rq.GetResponse();
s = rp.GetResponseStream();
}
catch (Exception ex) { /*Website doesnt want us on for some reason, probably doesnt matter*/ Program.debug(ex.ToString()); }
StringBuilder b = new StringBuilder();
int i = 0;
try{
while (true) {
i = s.Read(res, 0, res.Length);
if (i == 0) break;
b.Append(Encoding.ASCII.GetString(res, 0, i));
}
}
catch(Exception){return newPages; /*idc*/}
HtmlDocument d = new HtmlDocument();
d.OptionOutputAsXml = true;
d.LoadHtml(b.ToString());
HtmlNode n = d.DocumentNode;
string hreflink = "";
foreach (HtmlNode hnd in n.Descendants().Where(KYSKYSKYSKYSKYSKYSKYSKYSKYSKYKYSKYSKYS => KYSKYSKYSKYSKYSKYSKYSKYSKYSKYKYSKYSKYS.HasAttributes &&
KYSKYSKYSKYSKYSKYSKYSKYSKYSKYKYSKYSKYS.Name.ToLower() == "a" &&
(
KYSKYSKYSKYSKYSKYSKYSKYSKYSKYKYSKYSKYS.InnerText.ToLower().Contains("next") ||
KYSKYSKYSKYSKYSKYSKYSKYSKYSKYKYSKYSKYS.InnerText.Equals("3") ||
KYSKYSKYSKYSKYSKYSKYSKYSKYSKYKYSKYSKYS.InnerText.ToLower().Contains("proxies") ||
KYSKYSKYSKYSKYSKYSKYSKYSKYSKYKYSKYSKYS.InnerText.ToLower().Contains("list")
)
)) {
//File.AppendAllText("./html.txt", Regex.Match(hnd.OuterHtml, ">(.*?)<").Value.Replace("<","").Replace(">","") + "\n");
HtmlAttributeCollection atribs = hnd.Attributes;
hreflink = "";
foreach (HtmlAttribute ha in atribs) {
if (ha.Name != "href") continue;
hreflink = ha.Value;
if (hreflink.StartsWith("#")) continue;
Uri tmpUri = null;
if (Uri.TryCreate(hreflink, UriKind.Absolute, out tmpUri) && (tmpUri.Scheme == Uri.UriSchemeHttp || tmpUri.Scheme == Uri.UriSchemeHttps)) {
if (!(newPages.Contains(hreflink)) && !(hreflink == link))
newPages.Add(hreflink);
goto DoneSplitChecks;
}
else {
if (Uri.TryCreate(hreflink.Replace("\"", ""), UriKind.Absolute, out tmpUri) && (tmpUri.Scheme == Uri.UriSchemeHttp || tmpUri.Scheme == Uri.UriSchemeHttps)) {
if (!(newPages.Contains(hreflink)) && !(hreflink == link))
newPages.Add(hreflink);
goto DoneSplitChecks;
}
}
if (hreflink.StartsWith("/") || hreflink.StartsWith(@"\")) {
string ppgg = new Uri(link).GetLeftPart(UriPartial.Authority) + hreflink;
if (!(newPages.Contains(ppgg)) && !(ppgg == link))
newPages.Add(ppgg);
continue;
}
else /*Idk if this will generate junk, debug sample was small(1 website) and it worked fine..*/ {
string ppgg = new Uri(link).GetLeftPart(UriPartial.Authority) + "/" + hreflink;
if (Uri.TryCreate(ppgg, UriKind.Absolute, out tmpUri) && (tmpUri.Scheme == Uri.UriSchemeHttp || tmpUri.Scheme == Uri.UriSchemeHttps))
if (!(newPages.Contains(ppgg)) && !(ppgg == link))
newPages.Add(ppgg);
}
break;
}
DoneSplitChecks:;
}
return newPages;
}
private void inputLink (string link, List<String> currResults) {
if (!(currResults.Contains(link)) && !(scrapedLinksArchive.Contains(link))) {
Program.debug(link);
currResults.Add(link);
scrapedLinksArchive.Add(link);
}
}
public string getSearchQuery () { return this.searchQuery; }
public List<String> threadedSearch (int start, int end) {
List<String> results = new List<String>();
List<Task> tasks = new List<Task>();
while (start != end+1) {
tasks.Add(Task.Factory.StartNew(()=>{
foreach (string s in search(start, start+1))
results.Add(s);
}));
++start;
}
Task.WaitAll(tasks.ToArray());
foreach (Task tsk in tasks) tsk.Dispose();
return results;
}
}
}