Advertisement
Guest User

Untitled

a guest
May 27th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1.  
  2. Imports System.Collections.Generic
  3. Imports System.Diagnostics
  4. Imports System.Globalization
  5. Imports System.IO
  6. Imports System.Linq
  7. Imports System.Net
  8. Imports System.Security.Cryptography
  9. Imports System.Text
  10. Imports System.Threading
  11. Imports System.Threading.Tasks
  12.  
  13. Namespace RiotHelper.ProxyChecker
  14. Public Class ProxyChecker
  15.  
  16. #Region "Properties / Fields"
  17.  
  18. Public Property ProxyTimeout() As Integer
  19.  
  20. Public Property ThreadCount() As Integer
  21. Get
  22. Return m_ThreadCount
  23. End Get
  24. Set(value As Integer)
  25. m_ThreadCount = value
  26. End Set
  27. End Property
  28. Private m_ThreadCount As Integer
  29. Public Property Finished() As Boolean
  30. Get
  31. Return m_Finished
  32. End Get
  33. Set(value As Boolean)
  34. m_Finished = value
  35. End Set
  36. End Property
  37. Private m_Finished As Boolean
  38. Public Property Processed() As Double
  39. Get
  40. Return m_Processed
  41. End Get
  42. Set(value As Double)
  43. m_Processed = value
  44. End Set
  45. End Property
  46. Private m_Processed As Double
  47. Public ReadOnly Property Left() As Integer
  48. Get
  49. Return _queue.Count
  50. End Get
  51. End Property
  52.  
  53. Private ReadOnly _queue As New Queue(Of String)()
  54. Private _tasks As Task()
  55.  
  56. Private _stop As Boolean
  57. Private _running As Boolean
  58.  
  59. Private _threadsRunning As Integer = 0
  60.  
  61. #End Region
  62.  
  63. #Region "Events"
  64.  
  65.  
  66. Public Delegate Sub WorkingProxyEventHandler(sender As Object, proxy As String)
  67.  
  68. Public Event WorkingProxy As WorkingProxyEventHandler
  69.  
  70. Public Sub OnWorkingProxy(proxy As String)
  71. RaiseEvent WorkingProxy(Me, proxy)
  72. End Sub
  73.  
  74. #End Region
  75.  
  76. #Region "Constructor"
  77.  
  78. Public Sub New(threadCount__1 As Integer)
  79. ThreadCount = threadCount__1
  80. Finished = False
  81. Processed = 0
  82. End Sub
  83.  
  84. #End Region
  85.  
  86. #Region "Public Functions"
  87.  
  88. Public Sub AddProxy(proxy As String)
  89. SyncLock _queue
  90. _queue.Enqueue(proxy)
  91. End SyncLock
  92. End Sub
  93.  
  94. Public Async Sub Start()
  95.  
  96. If _running Then
  97. Throw New Exception("Checker already running")
  98. End If
  99.  
  100. _running = True
  101. _stop = False
  102.  
  103. _tasks = New Task(ThreadCount - 1) {}
  104.  
  105. For i As Integer = 0 To _tasks.Length - 1
  106. _tasks(i) = New Task(AddressOf CheckerLoop, TaskCreationOptions.LongRunning)
  107. _tasks(i).Start()
  108. Await Task.Delay(150)
  109. Interlocked.Increment(_threadsRunning)
  110. Next
  111.  
  112. End Sub
  113.  
  114. Public Sub [Stop]()
  115. _stop = True
  116. End Sub
  117.  
  118. #End Region
  119.  
  120. Private Sub CheckerLoop()
  121. While Not _stop
  122. Try
  123. Dim proxy As String
  124. SyncLock _queue
  125. proxy = If(_queue.Count > 0, _queue.Dequeue(), Nothing)
  126. End SyncLock
  127.  
  128. If proxy Is Nothing Then
  129. _stop = True
  130. End If
  131.  
  132. Dim success As Boolean = CheckProxy(proxy, False, ProxyTimeout) '10 seconds now. It's in ms ow ok
  133. If success Then
  134. OnWorkingProxy(proxy)
  135. End If
  136.  
  137. Processed += 1
  138. Catch generatedExceptionName As Exception
  139. End Try
  140. End While
  141.  
  142. Interlocked.Decrement(_threadsRunning)
  143. If _threadsRunning <= 0 Then
  144. Finished = True
  145. End If
  146. End Sub
  147.  
  148. Private Function CheckProxy(proxy As String, https As Boolean, timeout As Integer) As Boolean
  149. Try
  150. Dim request As HttpWebRequest
  151. If (https) Then
  152. request = DirectCast(WebRequest.Create("https://www.google.com/"), HttpWebRequest)
  153. Else
  154. request = DirectCast(WebRequest.Create("http://www.google.com/"), HttpWebRequest)
  155. End If
  156.  
  157. request.Proxy = New WebProxy(proxy)
  158. request.Method = "GET"
  159. request.Timeout = timeout
  160. request.ReadWriteTimeout = timeout
  161.  
  162.  
  163.  
  164. Dim startTime = DateTime.Now
  165. Dim responseTask = request.GetResponseAsync()
  166. While responseTask.Status = TaskStatus.WaitingForActivation OrElse responseTask.Status = TaskStatus.WaitingToRun
  167. If startTime.AddSeconds(30) < DateTime.Now Then
  168. Exit While
  169. Else
  170. Thread.Sleep(500)
  171. End If
  172. End While
  173. startTime = DateTime.Now
  174.  
  175. While responseTask.Status = TaskStatus.Running
  176. If startTime.AddSeconds(10) > DateTime.Now Then
  177. Exit While
  178. End If
  179. Thread.Sleep(500)
  180. End While
  181. If responseTask.Status = TaskStatus.RanToCompletion Then
  182. Return True
  183. End If
  184.  
  185. Return False
  186. Catch ex As Exception
  187. End Try
  188. Return False
  189. End Function
  190. End Class
  191. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement