OpenVPN / openvpn-gui

OpenVPN GUI is a graphical frontend for OpenVPN running on Windows 7 / 8 / 10. It creates an icon in the notification area from which you can control OpenVPN to start/stop your VPN tunnels, view the log and do other useful things.
Other
1.44k stars 403 forks source link

Disconnect button #650

Closed KoalafiedDev closed 11 months ago

KoalafiedDev commented 1 year ago

How can I create a disconnect button in the system tray, like the one in the picture below? This is a modification someone made, but it's an older version of OpenVPN GUI.

image

selvanair commented 1 year ago

What version of the GUI are you using?

There is already a disconnect entry in the context menu. If you have multiple configs you will find one in the submenu of each config, otherwise in the main menu: Connect, Disconnect, Reconnect and Show Status in that order. Just right click on the tray icon and see.

KoalafiedDev commented 1 year ago

It's regarding version 11.44.00. The latest version. I'm aware that there's a disconnect entry in the context menu under each config, but this is a must have for my client.

I think I got the solution for what I need:

I added this line twice in the tray.c file: AppendMenu(hMenu, MF_STRING, IDM_DISCONNECTMENU, LoadLocalizedString(IDS_MENU_DISCONNECT));

At line 232

hMenuImport = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, IDM_DISCONNECTMENU, LoadLocalizedString(IDS_MENU_DISCONNECT));
AppendMenu(hMenu, MF_POPUP, (UINT_PTR) hMenuImport, LoadLocalizedString(IDS_MENU_IMPORT));
AppendMenu(hMenuImport, MF_STRING, IDM_IMPORT_FILE, LoadLocalizedString(IDS_MENU_IMPORT_FILE));
AppendMenu(hMenuImport, MF_STRING, IDM_IMPORT_AS, LoadLocalizedString(IDS_MENU_IMPORT_AS));
AppendMenu(hMenuImport, MF_STRING, IDM_IMPORT_URL, LoadLocalizedString(IDS_MENU_IMPORT_URL));

AppendMenu(hMenu, MF_STRING, IDM_SETTINGS, LoadLocalizedString(IDS_MENU_SETTINGS));
AppendMenu(hMenu, MF_STRING, IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE));

At line 300:

hMenuImport = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, IDM_DISCONNECTMENU, LoadLocalizedString(IDS_MENU_DISCONNECT));
AppendMenu(hMenu, MF_POPUP, (UINT_PTR) hMenuImport, LoadLocalizedString(IDS_MENU_IMPORT));
AppendMenu(hMenuImport, MF_STRING, IDM_IMPORT_FILE, LoadLocalizedString(IDS_MENU_IMPORT_FILE));
AppendMenu(hMenuImport, MF_STRING, IDM_IMPORT_AS, LoadLocalizedString(IDS_MENU_IMPORT_AS));
AppendMenu(hMenuImport, MF_STRING, IDM_IMPORT_URL, LoadLocalizedString(IDS_MENU_IMPORT_URL));

AppendMenu(hMenu, MF_STRING, IDM_SETTINGS, LoadLocalizedString(IDS_MENU_SETTINGS));
AppendMenu(hMenu, MF_STRING, IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE));

I'm going to test if this is going to work.

selvanair commented 1 year ago

It appears you want a disconnect menu item in the main section even when there are multiple configurations. As multiple configs also allows multiple active connections at the same time, its not clear which one user would want to disconnect using this global menu item -- so this is not a "feature" we would add.

As for your local changes, the one at line 232 is not needed as it applies only when there is one config as there is already a global Disconnect entry in that case. For the one around line 300, note that the config pointer is passed in dwMenuData which is set only for config-specific sub-menus.

KoalafiedDev commented 1 year ago

Thank you for the clarification. What I understand from my client is that the disconnect button should close all connections. So if they use multiple connections at the same time, the button should disconnect all of them. How can I achieve this? Probably by changing the code a bit around line 300, as you specified in the post above, but I'm not very familiar with C, unfortunately.

selvanair commented 1 year ago

So if they use multiple connections at the same time, the button should disconnect all of them. How can I achieve this? Probably by changing the code a bit around line 300, as you specified in the post above,

For that you would need to define a new id, say, IDM_DISCONNECT_ALL, make a menu entry named something like "Disconnect All" for it, and handle it by calling StopAllOpenVPN(false). Add this entry only for the case where number of configs exceeds 1.

but I'm not very familiar with C, unfortunately.

Can't help about that. You may have to hire someone. Or you can make a shortcut for C:\Program Files\openvpn\bin\openvpn-gui.exe --command disconnect_all and name it OpenVPN-Disconnect.

KoalafiedDev commented 1 year ago

Thank you for your clear instructions, and all your help! I will work this out.