diff --git a/README.rst b/README.rst index 75d8013..ddb3208 100644 --- a/README.rst +++ b/README.rst @@ -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 *************************************************** diff --git a/main.c b/main.c index ba71d5c..c5c7843 100644 --- a/main.c +++ b/main.c @@ -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""); } } } diff --git a/options.c b/options.c index 07c26d1..b12dadf 100644 --- a/options.c +++ b/options.c @@ -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]) { diff --git a/res/openvpn-gui-res-en.rc b/res/openvpn-gui-res-en.rc index 5e371dc..698eb5b 100644 --- a/res/openvpn-gui-res-en.rc +++ b/res/openvpn-gui-res-en.rc @@ -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\