Как использовать rasphone.exe, чтобы получить соединение vpn, не всегда запрашивая пароль - PullRequest
0 голосов
/ 04 марта 2019

У меня есть приложение, которое подключает Windows VPN (не OpenVPN) с помощью интерфейса rasphone.exe.Я могу успешно установить соединение, но каждый раз запрашивает пароль.Есть ли способ заставить интерфейс запомнить пароль, чтобы в случае потери соединения оно могло автоматически восстанавливаться программно?Как примечание, когда я запускаю процесс для rasphone.exe, я получаю исключения, когда пытаюсь передать ему более 1 параметра.Единственный параметр, который я могу успешно передать - это имя записи, я не могу добавить такие параметры, как -d, -h или -f.

Вот код, который у меня есть:

Imports System.Linq
Imports System.Net.NetworkInformation

Public Class clsVPN

    Public Delegate Sub delPing()
    Public Delegate Sub delConnect()
    Public Delegate Sub delIdle()
    Public Delegate Sub delDisconnect()
    Public Delegate Sub delStatus(blnConnected As Boolean)

    Public Event Ping As delPing
    Public Event Con As delConnect
    Public Event Discon As delDisconnect
    Public Event Idle As delIdle
    Public Event StatusChanged As delStatus

    Public strRASPhone As String = "C:\WINDOWS\system32\rasphone.exe"
    Public strIPAddress As String = ""
    Public strVPNCon As String = ""

    Public blnConnected As Boolean = False

    Dim file As String = "C : \Users\Tom\AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone.pbk"


    Protected Sub OnStatusChanged(blnConnected As Boolean)

        RaiseEvent StatusChanged(blnConnected)

    End Sub

    Protected Sub OnDisconnect()

        RaiseEvent Discon()

    End Sub

    Protected Sub OnPing()

        RaiseEvent Ping()

    End Sub

    Protected Sub OnIdle()

        RaiseEvent Idle()

    End Sub

    Protected Sub OnConnect()

        RaiseEvent Con()

    End Sub

    Public ReadOnly Property Connected() As Boolean

        Get

            Return blnConnected

        End Get

    End Property


    Public Property ConName() As String

        Get

            Return strVPNCon

        End Get

        Set(strValue As String)

            strVPNCon = strValue

        End Set

    End Property

    Public Function Test() As Boolean

        Dim blnSucceed As Boolean = False

        OnPing()

        Dim p As New Ping()

        If p.Send(strIPAddress).Status = IPStatus.Success Then

            blnSucceed = True

        Else

            blnSucceed = False

        End If

        p = Nothing

        If blnSucceed <> blnConnected Then

            blnConnected = blnSucceed

            OnStatusChanged(blnConnected)

        End If

        OnIdle()

        Return blnSucceed

    End Function

    Public Function Connect() As Boolean

        Dim blnSucceed As Boolean = False
        Dim optionstr As String = "-f " & file & " -d "

        OnConnect()

        'MessageBox.Show("strVPNCon = " )

        'Process.Start(strRASPhone, Convert.ToString(" -f ") & file & Convert.ToString(" -d ") _
        ' & strVPNCon)
        optionstr = ""

        Dim wait As Boolean = True

        ProcessExec(strRASPhone, optionstr & strVPNCon, wait)

        Application.DoEvents()

        System.Threading.Thread.Sleep(5000)

        Application.DoEvents()

        blnSucceed = True

        OnIdle()

        Return blnSucceed

    End Function

    Public Function Disconnect() As Boolean

        Dim blnSucceed As Boolean = False
        Dim optionstr As String = "-h "

        OnDisconnect()

        optionstr = ""

        Dim wait As Boolean = True

        ProcessExec(strRASPhone, optionstr & strVPNCon, wait)

        Application.DoEvents()

        System.Threading.Thread.Sleep(8000)

        Application.DoEvents()

        blnSucceed = True

        OnIdle()

        Return blnSucceed

    End Function

    Public Function CheckConnection() As Boolean

        Dim niVPN As NetworkInterface() =
         NetworkInterface.GetAllNetworkInterfaces

        Dim blnExist As Boolean =
         niVPN.AsEnumerable().Any(Function(x) x.Name = ConName)

        If blnExist Then

            'MessageBox.Show("VPN Exists")

        Else

            ' MessageBox.Show("VPN Does Not Exist")

        End If
        Return blnExist

    End Function

    Public Sub ProcessExec(processarg As String, param As String, wait As Boolean)

        ' Start the child process.
        Dim p As New ProcessStartInfo
        ' Redirect the output stream of the child process.
        p.FileName = processarg
        p.Arguments = param
        p.UseShellExecute = True
        p.WindowStyle = ProcessWindowStyle.Normal
        Dim proc As Process = Process.Start(p)
        ' Do Not wait for the child process to exit before
        ' reading to the end of its redirected stream.
        If wait = True Then
            proc.WaitForExit()
        End If

    End Sub

End Class
...