本文介绍了如何以编程方式断开 OpenVPN 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 WinForms 应用程序来启动和停止 Windows 上的 OpenVPN 连接.我正在尝试实现与 Windows 的 OpenVPN GUI (http://openvpn.se/) 提供的相同功能,但是使用我自己的基于 .NET 的 UI.

I am creating a WinForms application to start and stop an OpenVPN connection on Windows. I am trying to achieve the same functionality as OpenVPN GUI for Windows (http://openvpn.se/) provides but using my own .NET based UI.

我使用以下方法开始连接:

I am starting the connection using the following approach:

Process openVpnProcess = new Process();
openVpnProcess.StartInfo.CreateNoWindow = true;
openVpnProcess.EnableRaisingEvents = true;
openVpnProcess.StartInfo.Arguments = "--config client.ovpn";
openVpnProcess.StartInfo.FileName = "openvpn.exe";
openVpnProcess.StartInfo.WorkingDirectory = @"C:\Program Files\OpenVPN\config";
openVpnProcess.Start();

这会调用 openvpn.exe 并成功建立连接.

This invokes openvpn.exe and the connection is established successfully.

但是,一旦连接建立,我无法确定终止连接的方法.我试过使用 Process.Kill()

I am however unable to determine a way to terminate the connection once it is established. I have tried using Process.Kill()

foreach (var p in Process.GetProcessesByName("openvpn"))
{
    p.Kill();
}

这会杀死进程,但不会恢复初始路由状态.实际上,除非我手动禁用/启用我的 LAN 卡,否则我无法访问网络.

This kills the process, but does not restore the initial routing state. Effectively, I cannot access the network until I manually disable/enable my LAN card.

在建立 VPN 连接之前'openvpn --show-net'的输出:

Output of 'openvpn --show-net' before the VPN connection is established:

SYSTEM ROUTING TABLE
0.0.0.0 0.0.0.0 10.31.0.254 p=0 i=1376258 t=4 pr=3 a=21 h=0 m=1/-1/-1/-1/-1
10.31.0.0 255.255.240.0 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=26 h=0 m=20/-1/-1/-1/-1
10.31.10.235 255.255.255.255 127.0.0.1 p=0 i=1 t=3 pr=2 a=26 h=0 m=20/-1/-1/-1/-1
10.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=26 h=0 m=20/-1/-1/-1/-1
127.0.0.0 255.0.0.0 127.0.0.1 p=0 i=1 t=3 pr=2 a=116753 h=0 m=1/-1/-1/-1/-1
224.0.0.0 240.0.0.0 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=26 h=0 m=20/-1/-1/-1/-1
255.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=26 h=0 m=1/-1/-1/-1/-1
255.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1441796 t=3 pr=2 a=4 h=0 m=1/-1/-1/-1/-1
SYSTEM ADAPTER LIST
TAP-Win32 Adapter V8
  Index = 1441796
  GUID = {013AB57F-DFE6-4FD9-B25E-9589E77DA4EB}
  IP = 0.0.0.0/0.0.0.0
  MAC = 00:ff:01:3a:b5:7f
  GATEWAY =
  DHCP SERV = 172.16.0.0
  DHCP LEASE OBTAINED = Tue Jul 07 16:35:20 2009
  DHCP LEASE EXPIRES  = Wed Jul 07 16:35:20 2010
D-Link DFE-538TX 10/100 Adapter
  Index = 1376258
  GUID = {FB6051A1-E970-4F46-BB85-F442A194BA3D}
  IP = 10.31.10.235/255.255.240.0
  MAC = 00:08:a1:65:70:93
  GATEWAY = 10.31.0.254/0.0.0.0

'openvpn --show-net' 使用 Process.Kill() 关闭 VPN 连接后:

'openvpn --show-net' after VPN connection is closed using Process.Kill():

SYSTEM ROUTING TABLE
10.31.0.0 255.255.240.0 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=106 h=0 m=20/-1/-1/-1/-1
10.31.10.235 255.255.255.255 127.0.0.1 p=0 i=1 t=3 pr=2 a=106 h=0 m=20/-1/-1/-1/-1
10.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=106 h=0 m=20/-1/-1/-1/-1
127.0.0.0 255.0.0.0 127.0.0.1 p=0 i=1 t=3 pr=2 a=116833 h=0 m=1/-1/-1/-1/-1
208.94.64.10 255.255.255.255 10.31.0.254 p=0 i=1376258 t=4 pr=3 a=21 h=0 m=1/-1/-1/-1/-1
224.0.0.0 240.0.0.0 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=106 h=0 m=20/-1/-1/-1/-1
255.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1376258 t=3 pr=2 a=106 h=0 m=1/-1/-1/-1/-1
255.255.255.255 255.255.255.255 10.31.10.235 p=0 i=1441796 t=3 pr=2 a=84 h=0 m=1/-1/-1/-1/-1
SYSTEM ADAPTER LIST
TAP-Win32 Adapter V8
  Index = 1441796
  GUID = {013AB57F-DFE6-4FD9-B25E-9589E77DA4EB}
  IP = 0.0.0.0/0.0.0.0
  MAC = 00:ff:01:3a:b5:7f
  GATEWAY =
  DHCP SERV = 172.16.0.0
  DHCP LEASE OBTAINED = Tue Jul 07 17:02:30 2009
  DHCP LEASE EXPIRES  = Wed Jul 07 17:02:30 2010
D-Link DFE-538TX 10/100 Adapter
  Index = 1376258
  GUID = {FB6051A1-E970-4F46-BB85-F442A194BA3D}
  IP = 10.31.10.235/255.255.240.0
  MAC = 00:08:a1:65:70:93
  GATEWAY =

我也尝试发送进程 WM_CLOSE/WM_QUIT/WM_ENDMESSAGE 消息,但这些没有产生任何结果.

I also tried sending the process WM_CLOSE / WM_QUIT / WM_ENDMESSAGE messages but these did not produce any result.

const int WM_CLOSE = 0x10;
const int WM_QUIT = 0x12;
const int WM_ENDSESSION = 0x0016;

[DllImport("user32.dll")]
public static extern int SendMessage(int hwnd, int msg, int wparam, int lparam);

foreach (var p in Process.GetProcessesByName("openvpn"))
{
    SendMessage(p.Handle.ToInt32(), WM_CLOSE, 0, 0);
    SendMessage(p.Handle.ToInt32(), WM_QUIT, 0, 0);
    SendMessage(p.Handle.ToInt32(), WM_ENDSESSION, 0, 0);
}

有关适当解决方案的更多信息:请参阅.


Further info on the appropriate solution: See instructions in section titled Using the management interface in Controlling a running OpenVPN process.

有关使用 来自 C# 的 Telnet.

推荐答案

我还没有在 Windows 上尝试过这个,但是你可以使用 OpenVPN 管理接口 使用 signal 命令发送 SIGTERM 信号.当然,您需要在配置文件中包含管理接口配置条目.

I haven't tried this on Windows but you can use the OpenVPN Management interface to send a SIGTERM signal with the signal command. You'll need to include the management interface configuration entries in your configuration file of course.

OpenVPN 中的更多信息手册页

您可能想了解 OpenVPN-admin 的工作方式.它在 Windows 和 Linux 下运行,并使用 Mono 开发.

You may want to look at the way OpenVPN-admin is doing things. It's working under Windows and Linux and developed with Mono.

这篇关于如何以编程方式断开 OpenVPN 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 22:33