Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Collections.Generic
- Imports System.Diagnostics
- Imports System.Globalization
- Imports System.IO
- Imports System.Linq
- Imports System.Net
- Imports System.Security.Cryptography
- Imports System.Text
- Imports System.Threading
- Imports System.Threading.Tasks
- Namespace RiotHelper.ProxyChecker
- Public Class ProxyChecker
- #Region "Properties / Fields"
- Public Property ProxyTimeout() As Integer
- Public Property ThreadCount() As Integer
- Get
- Return m_ThreadCount
- End Get
- Set(value As Integer)
- m_ThreadCount = value
- End Set
- End Property
- Private m_ThreadCount As Integer
- Public Property Finished() As Boolean
- Get
- Return m_Finished
- End Get
- Set(value As Boolean)
- m_Finished = value
- End Set
- End Property
- Private m_Finished As Boolean
- Public Property Processed() As Double
- Get
- Return m_Processed
- End Get
- Set(value As Double)
- m_Processed = value
- End Set
- End Property
- Private m_Processed As Double
- Public ReadOnly Property Left() As Integer
- Get
- Return _queue.Count
- End Get
- End Property
- Private ReadOnly _queue As New Queue(Of String)()
- Private _tasks As Task()
- Private _stop As Boolean
- Private _running As Boolean
- Private _threadsRunning As Integer = 0
- #End Region
- #Region "Events"
- Public Delegate Sub WorkingProxyEventHandler(sender As Object, proxy As String)
- Public Event WorkingProxy As WorkingProxyEventHandler
- Public Sub OnWorkingProxy(proxy As String)
- RaiseEvent WorkingProxy(Me, proxy)
- End Sub
- #End Region
- #Region "Constructor"
- Public Sub New(threadCount__1 As Integer)
- ThreadCount = threadCount__1
- Finished = False
- Processed = 0
- End Sub
- #End Region
- #Region "Public Functions"
- Public Sub AddProxy(proxy As String)
- SyncLock _queue
- _queue.Enqueue(proxy)
- End SyncLock
- End Sub
- Public Async Sub Start()
- If _running Then
- Throw New Exception("Checker already running")
- End If
- _running = True
- _stop = False
- _tasks = New Task(ThreadCount - 1) {}
- For i As Integer = 0 To _tasks.Length - 1
- _tasks(i) = New Task(AddressOf CheckerLoop, TaskCreationOptions.LongRunning)
- _tasks(i).Start()
- Await Task.Delay(150)
- Interlocked.Increment(_threadsRunning)
- Next
- End Sub
- Public Sub [Stop]()
- _stop = True
- End Sub
- #End Region
- Private Sub CheckerLoop()
- While Not _stop
- Try
- Dim proxy As String
- SyncLock _queue
- proxy = If(_queue.Count > 0, _queue.Dequeue(), Nothing)
- End SyncLock
- If proxy Is Nothing Then
- _stop = True
- End If
- Dim success As Boolean = CheckProxy(proxy, False, ProxyTimeout) '10 seconds now. It's in ms ow ok
- If success Then
- OnWorkingProxy(proxy)
- End If
- Processed += 1
- Catch generatedExceptionName As Exception
- End Try
- End While
- Interlocked.Decrement(_threadsRunning)
- If _threadsRunning <= 0 Then
- Finished = True
- End If
- End Sub
- Private Function CheckProxy(proxy As String, https As Boolean, timeout As Integer) As Boolean
- Try
- Dim request As HttpWebRequest
- If (https) Then
- request = DirectCast(WebRequest.Create("https://www.google.com/"), HttpWebRequest)
- Else
- request = DirectCast(WebRequest.Create("http://www.google.com/"), HttpWebRequest)
- End If
- request.Proxy = New WebProxy(proxy)
- request.Method = "GET"
- request.Timeout = timeout
- request.ReadWriteTimeout = timeout
- Dim startTime = DateTime.Now
- Dim responseTask = request.GetResponseAsync()
- While responseTask.Status = TaskStatus.WaitingForActivation OrElse responseTask.Status = TaskStatus.WaitingToRun
- If startTime.AddSeconds(30) < DateTime.Now Then
- Exit While
- Else
- Thread.Sleep(500)
- End If
- End While
- startTime = DateTime.Now
- While responseTask.Status = TaskStatus.Running
- If startTime.AddSeconds(10) > DateTime.Now Then
- Exit While
- End If
- Thread.Sleep(500)
- End While
- If responseTask.Status = TaskStatus.RanToCompletion Then
- Return True
- End If
- Return False
- Catch ex As Exception
- End Try
- Return False
- End Function
- End Class
- End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement