using System; using xNet.Net; using System.IO; using System.Text; using System.Net.Sockets; using System.Text.RegularExpressions; namespace Library.Network { public class BrowserTOR { public class result { public string url { get; internal set; } public string html { get; internal set; } public HttpStatusCode StatusCode { get; internal set; } public Exception exception { get; internal set; } public bool ReplyStatus { get; internal set; } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////############# Обычные get запросы с куками контакта #############///// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private static int CountGet = 0; private static Socks5ProxyClient socks = Socks5ProxyClient.Parse("127.0.0.1:9050"); public result get(string url, Encoding encoding, string Referer = null, bool UsingTor = false) { HttpRequest request = null; result get = new result(); try { request = new HttpRequest(); request.UserAgent = "Safari/5.05 (Windows; U; Windows NT 6.0; ru; rv:1.9.2.13) Net/02022012 Safari/5.05.112"; request.KeepAlive = true; request.Referer = Referer; request.MaximumAutomaticRedirections = 3; request.ReadWriteTimeout = 1000 * 30; request.ConnectTimeout = 1000 * 30; //Прокси тора if (UsingTor) { if (((CountGet++) % 40) == 0) { RefreshTor(DateTime.Now); } request.Proxy = socks; } //Получаем HTML страницу и записывае (ответ сервера и URL) var Response = request.Get(url); get.url = Response.Address.AbsoluteUri; get.StatusCode = Response.StatusCode; get.html = Response.ToString(); get.ReplyStatus = true; } catch (Exception ex) { get.exception = ex; ex = null; } finally { //Чистим ресурсы url = null; if (request != null) { request.Dispose(); request = null; } } //Возвращаем результат return get; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////############# Смена IP адреса для тора #############///// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private int countConnection = 0; private static DateTime LastTime = DateTime.Now; public void RefreshTor(DateTime time) { //Конектимся к тору и обновляемм IP if (countConnection < 10 && (time - LastTime).Seconds < 30) { try { //Подключение к тору bool good = false; Socket server = null; try { server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Connect("127.0.0.1", 9051); } catch { } //Авторизация по паролю server.Send(Encoding.ASCII.GetBytes("AUTHENTICATE \"passwd\"\n")); if (GetDataReceive(server).Contains("250")) { //сигнал о завершении смены цепочки серверов server.Send(Encoding.ASCII.GetBytes("SETEVENTS SIGNAL\r\n")); server.Send(Encoding.ASCII.GetBytes("SIGNAL NEWNYM\r\n")); if (GetDataReceive(server).Contains("250")) { good = true; } } //Чистим ресурсы server.Shutdown(SocketShutdown.Both); server.Close(); server.Dispose(); server = null; if (!good) { //Если смена IP не удалась, то пробуем еще раз countConnection++; RefreshTor(LastTime); } } catch { } //Ждем System.Threading.Thread.Sleep(1000 * 3); LastTime = time; } } private string GetDataReceive(Socket server) { //Обрабатываем данные byte[] data = new byte[1024]; int receivedDataLength = server.Receive(data); string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength); //Чистим ресурсы и возврашаем результат server = null; data = null; return stringData; } } }