Imports System.IO
Public Class Form1
Public Sub pror(ByRef strProcessToKill As String)
Dim proc() As Process = Process.GetProcesses
For p As Integer = 0 To proc.GetUpperBound(0)
If proc(p).ProcessName = strProcessToKill Then
proc(p).Kill()
End If
Next
End Sub
Public Shared Function FormatFileSize(ByVal Size As Long) As String
Try
Dim KB As Integer = 1024
Dim MB As Integer = KB * KB
' Return size of file in kilobytes.
If Size < KB Then
Return (Size.ToString("D") & " bytes")
Else
Select Case Size / KB
Case Is < 1000
Return (Size / KB).ToString("N") & "KB"
Case Is < 1000000
Return (Size / MB).ToString("N") & "MB"
Case Is < 10000000
Return (Size / MB / KB).ToString("N") & "GB"
End Select
End If
Catch ex As Exception
Return Size.ToString
End Try
End Function
Dim i, k As Integer
Public Function lines(ByVal file_path As String, ByVal line2read As Integer) As String
Dim line(1000) As String
i = 0
FileOpen(1, file_path, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
Do Until EOF(1)
line(i) = LineInput(1)
i += 1
Loop
FileClose(1)
Return line(line2read)
End Function
Dim log_show As Integer = 0
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
intro.Dispose()
intro.Close()
Me.Dispose()
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label5.ForeColor = Color.Maroon
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If log_show = 0 Then
log_show = 1
GroupBox3.Location = New Point(7, 255)
Me.Size = New System.Drawing.Size(384, 451)
Panel1.Size = New System.Drawing.Size(372, 396)
Else
log_show = 0
GroupBox3.Location = New Point(7, 270)
Me.Size = New System.Drawing.Size(384, 288)
Panel1.Size = New System.Drawing.Size(372, 234)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "Connect" Then
Button1.Enabled = False
Label5.Text = "Connecting"
Label5.Location = New Point(Int((GroupBox4.Width - Label5.Width) / 2), 18)
Shell("bin\openvpn --client --dev tun --remote x.x.x.x --proto udp --port 9200 --lport 53 --persist-key --persist-tun --ca crt\torrent_UDP.crt --comp-lzo --mute 3 --tun-mtu 1500 --auth-user-pass crt\account.txt --reneg-sec 0 --keepalive 10 120 --route-method exe --route-delay 2 --verb 3 --log crt\log.txt --status crt\stat.db 1", AppWinStyle.Hide)
Timer1.Enabled = True
connecting_Timer.Enabled = True
speed_Timer.Enabled = True
speed2_Timer.Enabled = True
ElseIf Button1.Text = "Stop" Then
pror("openvpn")
stopping_while_connecting_Timer.Enabled = True
connecting_Timer.Enabled = False
Button1.Enabled = False
Button1.Text = "Pls wait..."
Else
speed_Timer.Enabled = False
speed2_Timer.Enabled = False
disconnecting_Timer.Enabled = True
Button1.Text = "Disconnecting"
Button1.Enabled = False
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim str, str2 As String
str = lines("crt\stat.db", 4)
str2 = lines("crt\stat.db", 5)
If str <> "" Then
Label9.Text = FormatFileSize(Int32.Parse(Mid(str, 20))) 'FormatFileSize(bitt)
End If
If str2 <> "" Then
Label10.Text = FormatFileSize(Int32.Parse(Mid(str2, 21))) 'FormatFileSize(bitt)
End If
End Sub
Dim disconnecting_Timer_ctr As Integer
Private Sub disconnecting_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles disconnecting_Timer.Tick
disconnecting_Timer_ctr += 1
If disconnecting_Timer_ctr >= 5 Then
pror("openvpn")
Timer1.Enabled = False
Label5.ForeColor = Color.Maroon
Label5.Text = "Disconnected"
Label5.Location = New Point(Int((GroupBox4.Width - Label5.Width) / 2), 18)
Button1.Text = "Connect"
Button1.Enabled = True
disconnecting_Timer_ctr = 0
disconnecting_Timer.Enabled = False
End If
End Sub
Dim last_reciv As Integer = 0
Dim last_sent As Integer = 0
Dim time_out As Integer = 0
Dim speed_down As Integer = 0
Dim recon As Integer = 0
Dim recon_time_out As Integer = 0
Dim retry_in As Integer = 10
Private Sub speed_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles speed_Timer.Tick
Dim str As String
Dim current_reciv As Integer
str = lines("crt\stat.db", 4)
If str <> "" Then
current_reciv = Int(Int32.Parse(Mid(str, 20)))
speed_down = Int(current_reciv - last_reciv)
last_reciv = current_reciv
'If speed_down = 0 Then
'time_out += 1
'Label13.Text = time_out
'End If
End If
'If time_out = 20 Then
'imer2.Enabled = False
' speed_Timer.Enabled = False
'speed2_Timer.Enabled = False
'disconnecting_Timer.Enabled = True
' Button1.Text = "Disconnecting"
' Button1.Enabled = False
' MsgBox("Connection Failed!")
'End If
If speed_down > 0 Then
' time_out = 0
Label4.Text = FormatFileSize(speed_down)
End If
End Sub
Private Sub speed2_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles speed2_Timer.Tick
Dim str As String
Dim current_sent, speed_up As Integer
str = lines("crt\stat.db", 5)
If str <> "" Then
current_sent = Int32.Parse(Mid(str, 21))
speed_up = current_sent - last_sent
last_sent = current_sent
End If
If speed_up > 0 Then
Label12.Text = FormatFileSize(speed_up)
End If
End Sub
Private Sub connecting_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connecting_Timer.Tick
Dim str As String
Dim j As Long
str = lines("crt\stat.db", 6)
If str <> "" Then
j = Int32.Parse(Mid(str, 17))
End If
If i > 12 Then
Button1.Enabled = False
Button1.Text = "Pls wait..."
End If
If k <= 60 Then
If i <= 12 And j < 1 Then
k += 1
Button1.Text = "Stop"
Label5.Text = "Connecting"
Button1.Enabled = True
ElseIf i > 12 And j > 0 Then
k = 0
Button1.Text = "Disconnect"
Label5.ForeColor = Color.Green
Label5.Text = "Connected"
Button1.Enabled = True
connecting_Timer.Enabled = False
chck_Timer.Enabled = True
End If
Else
pror("openvpn")
Button1.Text = "Connect"
Label5.Text = "Disconnected"
Button1.Enabled = True
connecting_Timer.Enabled = False
k = 0
MsgBox("Connection Failed!")
End If
Label5.Location = New Point(Int((GroupBox4.Width - Label5.Width) / 2), 18)
End Sub
Private Sub chck_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chck_Timer.Tick
If speed_down = 0 Then
time_out += 1
Else
time_out = 0
End If
Label13.Text = time_out
If time_out = 30 Then
recon += 1
pror("openvpn")
reconnecting_Timer.Enabled = True
chck_Timer.Enabled = False
time_out = 0
End If
End Sub
Private Sub recon_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recon_Timer.Tick
Dim str As String
Dim j As Long
str = lines("crt\stat.db", 6)
If str <> "" Then
j = Int32.Parse(Mid(str, 17))
End If
If i > 12 Then
Button1.Enabled = False
Button1.Text = "Pls wait..."
End If
If k <= 60 Then
If i <= 12 And j < 1 Then
k += 1
Button1.Text = "Stop"
Label5.Text = "Reconnecting"
Button1.Enabled = True
ElseIf i > 12 And j > 0 Then
k = 0
Button1.Text = "Disconnect"
Label5.ForeColor = Color.Green
Label5.Text = "Connected"
Button1.Enabled = True
recon_Timer.Enabled = False
reconnecting_Timer.Enabled = False
chck_Timer.Enabled = True
End If
Else
pror("openvpn")
Button1.Text = "Connect"
Label5.Text = "Disconnected"
Button1.Enabled = True
recon_Timer.Enabled = False
k = 0
MsgBox("Connection Failed!")
End If
Label5.Location = New Point(Int((GroupBox4.Width - Label5.Width) / 2), 18)
End Sub
Dim stopping_while_connecting As Integer = 0
Private Sub stopping_while_connecting_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopping_while_connecting_Timer.Tick
If stopping_while_connecting >= 3 Then
speed_Timer.Enabled = False
speed2_Timer.Enabled = False
Timer1.Enabled = False
Label5.Text = "Disconnected"
Label5.Location = New Point(Int((GroupBox4.Width - Label5.Width) / 2), 18)
Button1.Text = "Connect"
Button1.Enabled = True
stopping_while_connecting = 0
stopping_while_connecting_Timer.Enabled = False
k = 0
Else
stopping_while_connecting += 1
End If
End Sub
Dim reconnecting As Integer = 10
Dim reconnecting_state As Integer = 10
Private Sub reconnecting_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reconnecting_Timer.Tick
If reconnecting = 0 Then
Shell("bin\openvpn --client --dev tun --remote x.x.x.x --proto udp --port 9200 --lport 53 --persist-key --persist-tun --ca crt\torrent_UDP.crt --comp-lzo --mute 3 --tun-mtu 1500 --auth-user-pass crt\account.txt --reneg-sec 0 --keepalive 10 120 --route-method exe --route-delay 2 --verb 3 --log crt\log.txt --status crt\stat.db 1", AppWinStyle.Hide)
recon_Timer.Enabled = True
reconnecting = 10
reconnecting_Timer.Enabled = false
Else
Button1.Text = "Retry in " & reconnecting
reconnecting_state = 1
reconnecting -= 1
End If
End Sub
End Class