Parse config-auto directory for persistent connections

- Parse the config-auto folder used by automatic service
  and mark these profiles as persistent.

- These connections are marked as auto_connect to try
attaching to them at start up with periodic retry in
case the daemon or service are restarted.

Signed-off-by: Selva Nair <selva.nair@gmail.com>
pull/519/head
Selva Nair 2 years ago
parent 8f0dbbc8a3
commit 477c61ff83

@ -505,6 +505,38 @@ HandleCopyDataMessage(const COPYDATASTRUCT *copy_data)
return TRUE; /* indicate we handled the message */ return TRUE; /* indicate we handled the message */
} }
/* If automatic service is running, check whether we are
* attached to the management i/f of persistent daemons
* and re-attach if necessary. The timer is reset to
* call this routine again after a delay. Periodic check
* is required as we get detached if the daemon gets restarted
* by the service or we do a disconnect.
*/
static void CALLBACK
ManagePersistent(HWND hwnd, UINT UNUSED msg, UINT_PTR id, DWORD UNUSED now)
{
CheckServiceStatus(false);
if (o.service_state == service_connected)
{
for (int i = 0; i < o.num_configs; i++)
{
if (o.conn[i].flags & FLAG_DAEMON_PERSISTENT
&& o.conn[i].auto_connect
&& o.conn[i].state == disconnected)
{
/* disable auto-connect to avoid repeated re-connect
* after unrecoverable errors. Re-enabled on successful
* connect.
*/
o.conn[i].auto_connect = false;
StartOpenVPN(&o.conn[i]); /* attach to the management i/f */
}
}
}
/* schedule to call again after 10 sec */
SetTimer(hwnd, id, 10000, ManagePersistent);
}
/* This function is called by the Windows function DispatchMessage() */ /* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{ {
@ -550,6 +582,9 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
SendMessage(hwnd, WM_CLOSE, 0, 0); SendMessage(hwnd, WM_CLOSE, 0, 0);
break; break;
} }
/* A timer to periodically tend to persistent connections */
SetTimer(hwnd, 0, 10000, ManagePersistent);
break; break;
case WM_NOTIFYICONTRAY: case WM_NOTIFYICONTRAY:

@ -124,6 +124,11 @@ OnReady(connection_t *c, UNUSED char *msg)
/* ask for the current state, especially useful when the daemon was prestarted */ /* ask for the current state, especially useful when the daemon was prestarted */
ManagementCommand(c, "state", OnStateChange, regular); ManagementCommand(c, "state", OnStateChange, regular);
if (c->flags & FLAG_DAEMON_PERSISTENT) /* find the current state */
{
c->auto_connect = true;
}
} }

@ -110,6 +110,13 @@ AddConfigFileToList(int config, const TCHAR *filename, const TCHAR *config_dir)
if (CheckKeyFileWriteAccess (c)) if (CheckKeyFileWriteAccess (c))
c->flags |= FLAG_ALLOW_CHANGE_PASSPHRASE; c->flags |= FLAG_ALLOW_CHANGE_PASSPHRASE;
#endif #endif
if (wcsstr(config_dir, o.config_auto_dir))
{
c->flags |= FLAG_DAEMON_PERSISTENT;
_sntprintf_0(c->log_path, _T("%ls\\%ls.log"), o.global_log_dir, c->config_name);
/* set to auto-connect -- this attempts to attach to them on startup */
c->auto_connect = true;
}
/* Check if connection should be autostarted */ /* Check if connection should be autostarted */
for (i = 0; i < o.num_auto_connect; ++i) for (i = 0; i < o.num_auto_connect; ++i)
@ -402,7 +409,7 @@ BuildFileList()
static bool issue_warnings = true; static bool issue_warnings = true;
int recurse_depth = 20; /* maximum number of levels below config_dir to recurse into */ int recurse_depth = 20; /* maximum number of levels below config_dir to recurse into */
int flags = 0; int flags = 0;
int root = 0; int root0 = 0;
int max_configs = (1<<16) - o.mgmt_port_offset; int max_configs = (1<<16) - o.mgmt_port_offset;
if (o.silent_connection) if (o.silent_connection)
@ -418,22 +425,32 @@ BuildFileList()
o.num_configs = 0; o.num_configs = 0;
o.num_groups = 0; o.num_groups = 0;
flags |= FLAG_ADD_CONFIG_GROUPS; flags |= FLAG_ADD_CONFIG_GROUPS;
root = NewConfigGroup(L"ROOT", -1, flags); /* -1 indicates no parent */ root0 = NewConfigGroup(L"ROOT", -1, flags); /* -1 indicates no parent */
} }
else else
root = 0; root0 = 0;
if (issue_warnings) if (issue_warnings)
{ {
flags |= FLAG_WARN_DUPLICATES | FLAG_WARN_MAX_CONFIGS; flags |= FLAG_WARN_DUPLICATES | FLAG_WARN_MAX_CONFIGS;
} }
BuildFileList0 (o.config_dir, recurse_depth, root, flags); BuildFileList0 (o.config_dir, recurse_depth, root0, flags);
root = NewConfigGroup(L"System Profiles", root, flags);
int root1 = NewConfigGroup(L"System Profiles", root0, flags);
if (!IsSamePath(o.global_config_dir, o.config_dir)) if (!IsSamePath(o.global_config_dir, o.config_dir))
BuildFileList0 (o.global_config_dir, recurse_depth, root, flags); {
BuildFileList0 (o.global_config_dir, recurse_depth, root1, flags);
}
if (o.service_state == service_connected)
{
root1 = NewConfigGroup(L"Persistent Profiles", root0, flags);
if (!IsSamePath(o.config_auto_dir, o.config_dir))
{
BuildFileList0 (o.config_auto_dir, recurse_depth, root1, flags);
}
}
if (o.num_configs == 0 && issue_warnings) if (o.num_configs == 0 && issue_warnings)
ShowLocalizedMsg(IDS_NFO_NO_CONFIGS, o.config_dir, o.global_config_dir); ShowLocalizedMsg(IDS_NFO_NO_CONFIGS, o.config_dir, o.global_config_dir);

@ -75,6 +75,8 @@ ExpandOptions (void)
{ {
ExpandString (o.exe_path, _countof(o.exe_path)); ExpandString (o.exe_path, _countof(o.exe_path));
ExpandString (o.config_dir, _countof(o.config_dir)); ExpandString (o.config_dir, _countof(o.config_dir));
ExpandString (o.global_config_dir, _countof(o.global_config_dir));
ExpandString (o.config_auto_dir, _countof(o.config_auto_dir));
ExpandString (o.log_dir, _countof(o.log_dir)); ExpandString (o.log_dir, _countof(o.log_dir));
ExpandString (o.editor, _countof(o.editor)); ExpandString (o.editor, _countof(o.editor));
ExpandString (o.log_viewer, _countof(o.log_viewer)); ExpandString (o.log_viewer, _countof(o.log_viewer));

@ -193,6 +193,8 @@ typedef struct {
TCHAR exe_path[MAX_PATH]; TCHAR exe_path[MAX_PATH];
TCHAR install_path[MAX_PATH]; TCHAR install_path[MAX_PATH];
TCHAR global_config_dir[MAX_PATH]; TCHAR global_config_dir[MAX_PATH];
TCHAR config_auto_dir[MAX_PATH];
TCHAR global_log_dir[MAX_PATH];
TCHAR priority_string[64]; TCHAR priority_string[64];
TCHAR ovpn_admin_group[MAX_NAME]; TCHAR ovpn_admin_group[MAX_NAME];
DWORD disable_save_passwords; DWORD disable_save_passwords;

@ -118,6 +118,18 @@ GetGlobalRegistryKeys()
_sntprintf_0(o.global_config_dir, _T("%lsconfig"), o.install_path); _sntprintf_0(o.global_config_dir, _T("%lsconfig"), o.install_path);
} }
if (!regkey || !GetRegistryValue(regkey, _T("autostart_config_dir"), o.config_auto_dir, _countof(o.config_auto_dir)))
{
/* use default = openvpnpath\config-auto */
_sntprintf_0(o.config_auto_dir, L"%lsconfig-auto", o.install_path);
}
if (!regkey || !GetRegistryValue(regkey, _T("log_dir"), o.global_log_dir, _countof(o.global_log_dir)))
{
/* use default = openvpnpath\log */
_sntprintf_0(o.global_log_dir, L"%lslog", o.install_path);
}
if (!regkey || !GetRegistryValue(regkey, _T("ovpn_admin_group"), o.ovpn_admin_group, _countof(o.ovpn_admin_group))) if (!regkey || !GetRegistryValue(regkey, _T("ovpn_admin_group"), o.ovpn_admin_group, _countof(o.ovpn_admin_group)))
{ {
_tcsncpy(o.ovpn_admin_group, OVPN_ADMIN_GROUP, _countof(o.ovpn_admin_group)-1); _tcsncpy(o.ovpn_admin_group, OVPN_ADMIN_GROUP, _countof(o.ovpn_admin_group)-1);

Loading…
Cancel
Save