From 6932c5e7109330d8cc75afca8980c80e457ef275 Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Wed, 20 Jul 2022 20:58:04 -0400 Subject: [PATCH] Change the logic of releasing persistent connections in OnHold (i) State is changed to detached before auto-starting so that OnHold() will see state = resuming and keep the hold. State is set to disconnected instead of detached on detach so that manual starts will release the hold automatically. End result: While connecting automatically, do not release if management-hold is on. But while started manually, release from hold so that connection can complete without further user action. In normal use of automatic service, one would not add management hold into the config. However, if the user disconnects the connection the GUI puts it on hold, and we do not want to auto-start it after a lock-unlock or some other automatic action. (ii) Also, currently, for persistent connections, the status window is not shown automatically which feels unnatural in real use. Instead, popup the status window when connection is manually initiated. Its not popped up when automatically attached to or if silent_connection is on. Only persistent connections are affected by the change. fixup: config file list is not recreated from scratch when enable_persistent == 2 (auto attach mode) to avoid losing info such as auto_connect = false on detached connections. Signed-off-by: Selva Nair --- main.c | 5 +++-- openvpn.c | 13 ++++++++----- openvpn_config.c | 8 ++++++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index 115e5fe..d7b3049 100644 --- a/main.c +++ b/main.c @@ -387,7 +387,7 @@ AutoStartConnections() for (i = 0; i < o.num_configs; i++) { - if (o.conn[i].auto_connect) + if (o.conn[i].auto_connect && !(o.conn[i].flags & FLAG_DAEMON_PERSISTENT)) StartOpenVPN(&o.conn[i]); } @@ -529,6 +529,7 @@ ManagePersistent(HWND hwnd, UINT UNUSED msg, UINT_PTR id, DWORD UNUSED now) * connect. */ o.conn[i].auto_connect = false; + o.conn[i].state = detached; /* this is required to retain management-hold on re-attach */ StartOpenVPN(&o.conn[i]); /* attach to the management i/f */ } } @@ -617,7 +618,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM break; } /* A timer to periodically tend to persistent connections */ - SetTimer(hwnd, 0, 10000, ManagePersistent); + SetTimer(hwnd, 0, 100, ManagePersistent); break; diff --git a/openvpn.c b/openvpn.c index 1cd8d32..322c6b6 100644 --- a/openvpn.c +++ b/openvpn.c @@ -1347,7 +1347,6 @@ OnStop(connection_t *c, UNUSED char *msg) if (c->flags & FLAG_DAEMON_PERSISTENT) { /* user initiated disconnection -- stay detached and do not auto-reconnect */ - c->state = detached; c->auto_connect = false; } CheckAndSetTrayIcon(); @@ -1358,7 +1357,7 @@ OnStop(connection_t *c, UNUSED char *msg) case onhold: /* stop triggered while on hold -- possibly the daemon exited. Treat same as detaching */ case detaching: - c->state = detached; + c->state = disconnected; CheckAndSetTrayIcon(); SendMessage(c->hwndStatus, WM_CLOSE, 0, 0); break; @@ -2136,9 +2135,13 @@ ThreadOpenVPNStatus(void *p) wait_event = c->hProcess; } - /* For persistent connections, popup the status win only if we're in manual mode */ - if (o.silent_connection == 0 && ((c->flags & FLAG_DAEMON_PERSISTENT) == 0 || o.enable_persistent == 1)) - ShowWindow(c->hwndStatus, SW_SHOW); + /* For persistent connections, popup the status win only if we're connecting manually */ + BOOL show_status_win = (o.silent_connection == 0); + if ((c->flags & FLAG_DAEMON_PERSISTENT) && c->state == resuming) + { + show_status_win = false; + } + ShowWindow(c->hwndStatus, show_status_win ? SW_SHOW : SW_HIDE); /* Load echo msg histroy from registry */ echo_msg_load(c); diff --git a/openvpn_config.c b/openvpn_config.c index 8b3a5aa..f105739 100644 --- a/openvpn_config.c +++ b/openvpn_config.c @@ -421,9 +421,13 @@ BuildFileList() /* * If no connections are active reset num_configs and rescan * to make a new list. Else we keep all current configs and - * rescan to add any new one's found + * rescan to add any new one's found. + * Do the same when persistent connections are in auto-attach mode, + * to avoid over-writing their status info such as auto_connect=false + * after manual detach. */ - if (!o.num_groups || CountConnState(disconnected) == o.num_configs) + if (!o.num_groups + || (CountConnState(disconnected) == o.num_configs && o.enable_persistent != 2)) { o.num_configs = 0; o.num_groups = 0;