Example #1
0
        public ApiContext(DbContextOptions <ApiContext> options)
            : base(options)
        {
            // Build Default ConnStr
            string endpoint = new NpgsqlConnectionStringBuilder(_dbSection["ConnectionString"]).Host;

            _logger.LogInformation($"Buffer endpoint: {endpoint}");
            uint port = uint.Parse(_dbSection["Port"]);

            // Tunnel:SshClient DatabaseHook
            _client = TunnelProvider.CreateClient();
            (SshClient TunnelClient, ForwardedPortLocal PortLocal)tunnel =
                TunnelProvider.HookDatabase(_client, endpoint, port);
            _client = tunnel.TunnelClient;

            // Check:Active SshClientConnections
            TunnelProvider.ActiveSshConnections(_client);

            // Check:Tunnel WebPortForwardState
            TunnelProvider.SshPortForwardState(_client);

            // Set:Database Connection String
            _connectionStr =
                new NpgsqlConnectionStringBuilder(_dbSection["ConnectionString"])
            {
                Host           = tunnel.PortLocal.BoundHost,
                Port           = (int)tunnel.PortLocal.BoundPort,
                CommandTimeout = 30,
                KeepAlive      = 30,
            }.ToString();
        }