From 6275f8b05cc67fbd5b09406c0a94ef0c03b6fc79 Mon Sep 17 00:00:00 2001 From: Eugene K Date: Fri, 18 Dec 2020 11:10:59 -0500 Subject: [PATCH] force session refresh if router closes edge connection --- inc_internal/zt_internal.h | 2 ++ library/channel.c | 21 ++++++++++++++------- library/ziti.c | 4 ++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/inc_internal/zt_internal.h b/inc_internal/zt_internal.h index 339377cf..599ed3a7 100644 --- a/inc_internal/zt_internal.h +++ b/inc_internal/zt_internal.h @@ -197,6 +197,8 @@ struct ziti_ctx { extern "C" { #endif +void ziti_force_session_refresh(ziti_context ztx); + int ziti_close_channels(ziti_context ztx); int ziti_channel_connect(ziti_context ztx, const char *name, const char *url, ch_connect_cb, void *ctx); diff --git a/library/channel.c b/library/channel.c index 6880f599..7aeaed83 100644 --- a/library/channel.c +++ b/library/channel.c @@ -568,17 +568,23 @@ static void async_write(uv_async_t *ar) { static void reconnect_cb(uv_timer_t *t) { ziti_channel_t *ch = t->data; + ziti_context ztx = ch->ctx; - ch->msg_seq = 0; + if(ztx->session == NULL || ztx->session->token == NULL) { + ZITI_LOG(ERROR, "ziti context is not authenticated, delaying re-connect"); + reconnect_channel(ch); + } else { + ch->msg_seq = 0; - uv_connect_t *req = calloc(1, sizeof(uv_connect_t)); - req->data = ch; + uv_connect_t *req = calloc(1, sizeof(uv_connect_t)); + req->data = ch; - ch->state = Connecting; + ch->state = Connecting; - uv_mbed_init(ch->ctx->loop, &ch->connection, ch->ctx->tlsCtx); - ch->connection._stream.data = ch; - uv_mbed_connect(req, &ch->connection, ch->host, ch->port, on_channel_connect_internal); + uv_mbed_init(ch->ctx->loop, &ch->connection, ch->ctx->tlsCtx); + ch->connection._stream.data = ch; + uv_mbed_connect(req, &ch->connection, ch->host, ch->port, on_channel_connect_internal); + } uv_close((uv_handle_t *) t, (uv_close_cb) free); } @@ -621,6 +627,7 @@ static void on_channel_close(ziti_channel_t *ch, ssize_t code) { if (ch->state != Closed) { reconnect_channel(ch); + ziti_force_session_refresh(ztx); } } diff --git a/library/ziti.c b/library/ziti.c index db17077a..6a08a42d 100644 --- a/library/ziti.c +++ b/library/ziti.c @@ -506,6 +506,10 @@ static void session_refresh(uv_timer_t *t) { ziti_ctrl_current_api_session(&ztx->controller, session_cb, req); } +void ziti_force_session_refresh(ziti_context ztx) { + uv_timer_start(&ztx->session_timer, session_refresh, 0, 0); +} + static void ziti_re_auth(ziti_context ztx) { ZITI_LOG(WARN, "starting to re-auth with ctlr[%s]", ztx->opts->controller); uv_timer_stop(&ztx->refresh_timer);