Allow --command action args to work also as --action args

When sending multiple commands to a running instance of the GUI,
simplify usage like

openvpn-gui.exe --command connect vpn1 --command connect vpn2
      --command disconnect vpn3

by omitting --command so that one could write

openvpn-gui.exe --connect vpn1 --connect vpn2 --disconnect vpn3

The form "--command action .. " was introduced originally to indicate that
these actions are only understood if there is an instance of the gui
already running as it just triggers a message for that action to be sent to
the first instance. But it becomes tedious when multiple commands are to
be specified.

This patch makes --action arg  to also work the same way as
--command action arg. A side effect is that some of such options do not
make sense when launching the first instance of the GUI. In such cases,
the option is ignored but an error is logged to the eventlog. In particular,
use of options such as --command disconnect or --disconnect with the first
instance are no longer treated as a fatal error.

The allowed values of action are unchanged:
  connect
  disconnect
  reconnect
  disconnect_all
  exit
  status
  silent_connection
  import
  rescan
out of which connect, silent_connection and import are the only
one's that could be handled by the first instance.

Example:

openvpn-gui.exe --connect vpn1 --connect vpn2

will start vpn1 and vpn2 if OpenVPN-GUI is not already running,
or cause a command to start those sent to a running instance.

Signed-off-by: Selva Nair <selva.nair@gmail.com>
pull/502/head
Selva Nair 2022-05-23 16:45:29 -04:00
parent 6787306b16
commit 3d5592c47b
4 changed files with 76 additions and 49 deletions

View File

@ -150,6 +150,10 @@ it using the command line interface using the following syntax::
openvpn-gui.exe --command *cmd* [*args*]
Or::
openvpn-gui.exe --cmd [*args*]
Currently supported *cmds* are
connect ``config-name``
@ -163,6 +167,10 @@ reconnect ``config-name``
Disconnect and then reconnect the configuration named *config-name*
if connected.
status ``config-name``
Show the status window of the configuration named *config-name*
if connected or is in the process of connecting.
disconnect\_all
Disconnect all active connections.
@ -178,9 +186,10 @@ rescan
import ``path``
Import the config file pointed to by ``path``.
If no running instance of the GUI is found, these commands do nothing
except for *--command connect config-name* which gets interpreted
as *--connect config-name*
If no running instance of the GUI is found, these commands have no
effect except for *connect*, *import*, and *silent_connection* which
get interpreted resepctively as *--connect*, *--import*, and
*--silent_connection*.
Registry Values affecting the OpenVPN GUI operation
***************************************************

8
main.c
View File

@ -258,9 +258,9 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance,
{
for (struct action *a = o.action_list.head; a; a = a->next)
{
if (a->type == WM_OVPN_START)
if (a->type == WM_OVPN_START || a->type == WM_OVPN_SILENT)
{
PrintDebug(L"Instance 1: Called with --command connect xxx. Treating it as --connect xxx");
; /* pass these could get set by --connect or --silent_connection */
}
else if (a->type == WM_OVPN_IMPORT)
{
@ -268,8 +268,8 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance,
}
else
{
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Called with --command when no previous instance available (action type = %d arg = %s", a->type, a->arg ? a->arg : L"");
exit(OVPN_EXITCODE_ERROR);
/* log an error, but do not treat as fatal */
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Called with options relevant only when a previous instance is available (action type = %d arg = %s", a->type, a->arg ? a->arg : L"");
}
}
}

View File

@ -104,6 +104,22 @@ add_action(struct action_list *al, DWORD type, wchar_t *arg)
al->tail = a;
}
/* action commands that could be sent to a running instance */
static const wchar_t *valid_cmds[] =
{
L"connect",
L"disconnect",
L"reconnect",
L"disconnect_all",
L"status",
L"exit",
L"import",
L"silent_connection",
L"rescan",
NULL /* last entry */
};
static int
add_option(options_t *options, int i, TCHAR **p)
{
@ -234,6 +250,8 @@ add_option(options_t *options, int i, TCHAR **p)
{
++i;
options->silent_connection = _ttoi(p[1]) ? 1 : 0;
/* also interpreted by a second instance */
add_action(al, WM_OVPN_SILENT, p[1]);
}
else if (streq(p[0], _T("passphrase_attempts")) && p[1])
{
@ -262,55 +280,52 @@ add_option(options_t *options, int i, TCHAR **p)
}
else if (streq(p[0], _T("command")) && p[1])
{
++i;
/* command to be sent to a running instance */
if (streq(p[1], _T("connect")) && p[2])
int found = 0;
for (int k = 0; valid_cmds[k] && !found; k++)
{
/* Treat this as "--connect profile" in case this is the first instance */
i = add_option(options, i, &p[1]);
found = streq(valid_cmds[k], p[1]);
}
else if (streq(p[1], _T("disconnect")) && p[2])
{
++i;
add_action(al, WM_OVPN_STOP, p[2]);
}
else if (streq(p[1], _T("reconnect")) && p[2])
{
++i;
add_action(al, WM_OVPN_RESTART, p[2]);
}
else if (streq(p[1], _T("status")) && p[2])
{
++i;
add_action(al, WM_OVPN_SHOWSTATUS, p[2]);
}
else if (streq(p[1], L"import") && p[2])
{
++i;
add_action(al, WM_OVPN_IMPORT, p[2]);
}
else if (streq(p[1], _T("silent_connection")))
{
++i;
add_action(al, WM_OVPN_SILENT, p[2] ? p[2] : L"1");
}
else if (streq(p[1], _T("disconnect_all")))
{
add_action(al, WM_OVPN_STOPALL, NULL);
}
else if (streq(p[1], _T("exit")))
{
add_action(al, WM_OVPN_EXIT, NULL);
}
else if (streq(p[1], _T("rescan")))
{
add_action(al, WM_OVPN_RESCAN, NULL);
}
else
if (!found)
{
ShowLocalizedMsg(IDS_ERR_BAD_OPTION, p[0]);
exit(1);
}
++i;
i = add_option(options, i, &p[1]);
}
else if (streq(p[0], _T("disconnect")) && p[1])
{
++i;
/* this option is handled only as an action passed by a second instance */
add_action(al, WM_OVPN_STOP, p[1]);
}
else if (streq(p[0], _T("reconnect")) && p[1])
{
++i;
/* this option is handled only as an action passed by a second instance */
add_action(al, WM_OVPN_RESTART, p[1]);
}
else if (streq(p[0], _T("status")) && p[1])
{
++i;
/* this option is handled only as an action passed by a second instance */
add_action(al, WM_OVPN_SHOWSTATUS, p[1]);
}
else if (streq(p[0], _T("disconnect_all")))
{
/* this option is handled only as an action passed by a second instance */
add_action(al, WM_OVPN_STOPALL, NULL);
}
else if (streq(p[0], _T("exit")))
{
/* this option is handled only as an action passed by a second instance */
add_action(al, WM_OVPN_EXIT, NULL);
}
else if (streq(p[0], _T("rescan")))
{
/* this option is handled only as an action passed by a second instance */
add_action(al, WM_OVPN_RESCAN, NULL);
}
else if (streq(p[0], _T("popup_mute_interval")) && p[1])
{

View File

@ -406,7 +406,10 @@ Supported commands:\n\
status cnn \t: show the status window of config ""cnn"" if connected\n\
silent_connection [0|1]\t: set the silent_connection flag on (1) or off (0)\n\
import path \t\t: Import the config file pointed to by path\n\
rescan \t\t: Rescan config directories for config files\n\
\t\t\tExample: openvpn-gui.exe --command disconnect myconfig\n\
As a shortcut, --command cmd may be simplified to --cmd\n\
e.g., --exit is the same as --command exit.\n\
\n\
Options to override registry settings:\n\
--exe_path\t\t: Path to openvpn.exe.\n\