Establish Connection to server using C#

Post your questions about SoftEther VPN software here. Please answer questions if you can afford.
Post Reply
blakepuls
Posts: 1
Joined: Tue Apr 13, 2021 10:44 pm

Establish Connection to server using C#

Post by blakepuls » Tue Apr 13, 2021 10:48 pm

I can't for the life of me figure out a way to programmatically connect to a SoftEther VPN server using C#. If anyone can provide resources, insight, or any help I would greatly appreciate it.

MikeL
Posts: 10
Joined: Fri Jan 05, 2018 11:51 pm

Re: Establish Connection to server using C#

Post by MikeL » Thu Apr 15, 2021 3:17 pm

I haven't tried this with C# but have done it using vbscript. Here are the 3 routines I used. Hopefully you can convert them to C# (good luck with lots of double quotes).

The one thing that you need to be aware of is that when you call the SE VPN command AccountConnect then it returns immediately with a success code. You need to build in logic to check that the connect actually worked. If there's another way I never figured it out.

XXXXXX in vpncmd calls needs to be replaced with the name of your VPN

Hope this helps
Mike

Function ConnectSoftEther

' First let's do a quick sanity check that we are not already connected if we are just return True
If SoftEtherState = Connected Then
ConnectSoftEther = Connected
Exit Function
End If
hostname = "localhost"
Set wshShell = WScript.CreateObject("WScript.Shell")
return = WshShell.Run (("vpncmd " &hostname &" /client /cmd AccountConnect XXXXX"), 2, True)
'If return = 0 Then 'we only know that the Connect command was successfully called - we do not know if the Connect itself worked
If return = 0 Then 'Connect worked
WScript.Sleep 15*1000 ' Give it time to make connection then check if it worked
If SoftEtherState = Connected Then
ConnectSoftEther = Connected
Else
ConnectSoftEther = Disconnected
End If
Else
ConnectSoftEther = Disconnected
End If

End Function


Function SoftEtherState
' Following assumes that I only have one VPN adapter installed - you'd have to extend logic to be more exact

Set objWMIService = GetObject(_
"winmgmts:\\" & strComputer & "\root\cimv2")
SoftEtherState = Disconnected
Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter")

For Each objItem in colItems
If Left(objitem.name,3) = "VPN" Then
If objItem.NetConnectionStatus = 2 Then
SoftEtherState = Connected
Else
SoftEtherState = Disconnected
End If
End If
Next
End Function


Sub DisconnectSoftEther

hostname = "localhost"
Set wshShell = WScript.CreateObject("WScript.Shell")
return = WshShell.Run (("vpncmd " &hostname &" /client /cmd AccountStatusGet XXXXXX"), 2, True) 'Note for call to vpncmd to work SEpath must be in Path Environment Variables
If return = 0 Then 'we have a valid connection with UK-VPN so let's disconnect
return = WshShell.Run (("vpncmd " &hostname &" /client /cmd AccountDisconnect XXXXXXX"), 2, True)
If return = 0 Then 'Disconnect worked
' Add Logic for what you want to do after successful disconnect
Else
' Add logic for what you want to do if disconnect returned non 0
End If
Else
' Add logic for what you want to do if AccountStatusGet call did not return success
End If

End Sub

jpl
Posts: 7
Joined: Mon Jan 25, 2021 11:08 am

Re: Establish Connection to server using C#

Post by jpl » Fri Apr 23, 2021 7:38 am

You have to use vpnncmd commands.
You only need to execute them in c# like this:

public void Execute(string command)
{
try
{

ProcessStartInfo procStartInfo = new ProcessStartInfo("vpncmd", comando);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();

}
catch (IOException e)
{
Console.WriteLine(e.Message.ToString());
}
}
You pass the command to this function and it will be executed.

Post Reply