'
' Erstellt mit SharpDevelop.
' Benutzer: Tobi
' Datum: 27.01.2018
' Zeit: 12:25
' 
' Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
'
Public Partial Class MainForm
	Dim mInstalVerzeichnis As String
	Dim mAppPath As String
	Dim mText As String
	Dim mText1 As String
	Dim mText2 As String
	Dim mPosAnfang As Integer
	Dim mPosEnde As Integer
	Dim mUrl As String
	Dim mPid As String
	Dim mStart As Integer
	Dim mPausenlaenge As Integer
	Dim dateiexistiert As Boolean
	Dim zaehler As Integer
	Dim stringlaenge As Integer
	Dim mFilename As String
	Dim mAuthtoken As String
	Dim mDokumentVerzeichnis As String
	
	Public Sub New()
		' The Me.InitializeComponent call is required for Windows Forms designer support.
		Me.InitializeComponent()
		
		'
		' TODO : Add constructor code after InitializeComponents
		'
	End Sub
	
	Public Function Rechts(ByVal sText As String, _
		ByVal nLen As Integer) As String
		If nLen > sText.Length Then nLen = sText.Length
		Return (sText.Substring(sText.Length - nLen))
	End Function
	
	Public Function Links(ByVal sText As String, _
		ByVal nLen As Integer) As String
		If nLen > sText.Length Then nLen = sText.Length
		Return (sText.Substring(0, nLen))
	End Function
	
	Sub BtLocaltunnelStartClick(sender As Object, e As EventArgs)
		LocalTunnelStarten()
	End Sub
	
	Sub LocalTunnelStarten()
		progressBar1.Value = 1
		progressBar1.Visible = True
		progressBar1.Update
		system.threading.thread.sleep(2000)
		progressBar1.Value += 1
		Cursor.Current = System.Windows.Forms.Cursors.WaitCursor		
		mPid = Shell (mInstalVerzeichnis & "ngrok.exe http 80")
		system.threading.thread.sleep(3000) 
		btLocaltunnelStart.Text = "Bitte warten"
		btLocaltunnelStart.ForeColor = Color.Red
		btLocaltunnelStart.Update
		lblText3.Visible = False
		lblText4.Visible = False
		lblNeuerAuthtoken.Visible = False
		progressBar1.Value += 2
		mText = mInstalVerzeichnis & "curl.exe http://localhost:4040/api/tunnels > " & mInstalVerzeichnis & "meinlocaltunnel.txt"	
		IO.File.WriteAllText(mInstalVerzeichnis & "meincurl.bat",mText)
		system.threading.thread.sleep(2000)
		progressBar1.Value += 1
		Shell (mInstalVerzeichnis & "meincurl.bat")
		system.threading.thread.sleep(2000)
		progressBar1.Value += 1
		system.threading.thread.sleep(2000)
		progressBar1.Value += 1
		dateiexistiert = False
		zaehler = 0
		While dateiexistiert = False
			Try
				zaehler = zaehler + 1
				mText = IO.File.ReadAllText(mInstalVerzeichnis & "meinlocaltunnel.txt")
				stringlaenge = mText.Length
				dateiexistiert = True	
			Catch
			End Try
			System.Threading.Thread.Sleep(200)
		End While
		progressBar1.Value += 1
		Kill (mInstalVerzeichnis & "meinlocaltunnel.txt")
		Kill (mInstalVerzeichnis & "meincurl.bat")
		progressBar1.Value += 1
		If mText.Length = 36 Then
			MessageBox.Show("Es konnte keine Verbindung zum Tunnelserver hergestellt werden!", _
				"Verbindungsfehler", _
				MessageBoxButtons.OK, _
				MessageBoxIcon.Exclamation, _
				MessageBoxDefaultButton.Button1)
			btLocaltunnelStart.Text = "Local - Tunnel starten"
			btLocaltunnelStart.ForeColor = Color.Black
			progressBar1.Visible = False
			Shell ("taskkill /pid " & mPid)
		Else
			mPosAnfang= mText.IndexOf("https://") 
			mText = mText.Substring(mPosAnfang, mText.Length - mPosAnfang )
			mPosEnde = mText.IndexOf("ngrok-free.app") + 14
			mUrl = mText.Substring(0, mPosEnde)
			Clipboard.SetText(mUrl)
			llblURL.Text = mURL
			llblURL.Visible = True
			lblText1.Visible = True
			lblText2.Visible=True
			lblText3.Visible=True
			btCopyClipboard.Visible = True
			btEnde.Text = "Local - Tunnel beenden"
			btLocaltunnelStart.Text = "Local - Tunnel starten"
			btLocaltunnelStart.ForeColor = Color.Black
			btLocaltunnelStart.Enabled = False
			progressBar1.Visible = False
			mText = mUrl
			IO.File.WriteAllText(mInstalVerzeichnis & "aktuelleurl.txt",mText)
			Cursor.Current = System.Windows.Forms.Cursors.Default
			
			lblAuthtokenGespeichert.Visible = True
			btNeuerAuthtoken.Visible = True
			mFilename = Environ$("USERPROFILE") & "\.ngrok2\ngrok.yml"
			If System.IO.File.Exists(mFileName) = True Then
				mAuthtoken = IO.File.ReadAllText(mFileName)
				mAuthtoken = mAuthtoken.Substring(11, mAuthtoken.Length - 11)
				lblAuthtokenGespeichert.Text = mAuthtoken
			End If
		End If
	End Sub
	
	Sub MainFormLoad(sender As Object, e As EventArgs)
		With Screen.PrimaryScreen.WorkingArea
			Me.Location = New Point(.Left + (.Right - .Left - Me.Width) / 2, .Top + ( _
				.Bottom - .Top - Me.Height) / 2)
		End With
		mAppPath = Application.StartupPath
		If Rechts(mAppPath, 1) = "\" Then
			mInstalVerzeichnis = mAppPath
		Else
			mInstalVerzeichnis = mAppPath & "\"
		End If
		'mInstalVerzeichnis = "D:\S20\Programme\Localtunnel\"
		mDokumentVerzeichnis = Links(mInstalVerzeichnis,mInstalVerzeichnis.Length - 23) &  "\Hilfe_und_Dokumentation\"
	End Sub
	
	Sub BtEndeClick(sender As Object, e As EventArgs)  
		Try
			Shell ("taskkill /pid " & mPid)
		Catch ex As Exception
		End Try
		Try
			Kill (mInstalVerzeichnis & "aktuelleurl.txt")
		Catch
		End Try
		End
	End Sub
	
	Sub MainFormFormClosing(sender As Object, e As FormClosingEventArgs)
		Try
			Shell ("taskkill /pid " & mPid)
		Catch ex As Exception
		End Try
		Try
			Kill (mInstalVerzeichnis & "aktuelleurl.txt")
		Catch
		End Try
		End
	End Sub
	
	Sub BtCopyClipboardClick(sender As Object, e As EventArgs)
		Clipboard.SetText(mUrl)
	End Sub
	
	Sub BtNeuerAuthtokenClick(sender As Object, e As EventArgs)
		lblText4.Visible = True
		lblNeuerAuthtoken.Text = Clipboard.GetText()
		If Links(lblNeuerAuthtoken.Text,8) = "https://" Then
			MessageBox.Show("Bitte kopieren Sie zuvor den neuen Authtoken in die Zwischenablage !", _
				"NEUER Authtoken fehlt", _
				MessageBoxButtons.OK, _
				MessageBoxIcon.Exclamation, _
				MessageBoxDefaultButton.Button1)
		Else
			Shell (mInstalVerzeichnis & "ngrok.exe authtoken " & lblNeuerAuthtoken.Text)
			lblText3.Visible = True
			lblText3.Update
			lblNeuerAuthtoken.Visible = True
			lblNeuerAuthtoken.Update
			btLocaltunnelStart.Text = "Local - Tunnel wird erneut mit neuem Authtoken gestartet"
			btLocaltunnelStart.ForeColor = Color.Red
			btLocaltunnelStart.Enabled = True
			btLocaltunnelStart.Update
			Try
				Shell ("taskkill /pid " & mPid)
			Catch ex As Exception
			End Try
			LocalTunnelStarten()	
		End If
	End Sub
	
	Sub BtDokumentClick(sender As Object, e As EventArgs)
		Process.Start(mDokumentVerzeichnis & "Zugriff auf eigene Webseiten per Internet.pdf")
	End Sub
	
	Sub LblURLMouseClick(sender As Object, e As MouseEventArgs)
		
	End Sub
	
		
	Sub LlblURLMouseClick(sender As Object, e As MouseEventArgs)
		Process.Start(llblURL.text)
	End Sub
End Class
