/// <summary>
        /// Creates a new miner instance.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="extraNonce"></param>
        /// <param name="connection"></param>
        /// <param name="pool"></param>
        /// <param name="minerManager"></param>
        /// <param name="storageLayer"></param>
        public StratumMiner(int id, UInt32 extraNonce, IConnection connection, IPool pool, IMinerManager minerManager, IStorageLayer storageLayer)
        {
            Id = id; // the id of the miner.
            ExtraNonce = extraNonce;
            Connection = connection; // the underlying connection.
            Pool = pool;
            _minerManager = minerManager;
            _storageLayer = storageLayer;

            Subscribed = false; // miner has to subscribe.
            Authenticated = false; // miner has to authenticate.

            _logger = Log.ForContext<StratumMiner>().ForContext("Component", pool.Config.Coin.Name);
            _packetLogger = LogManager.PacketLogger.ForContext<StratumMiner>().ForContext("Component", pool.Config.Coin.Name);

            _rpcResultHandler = callback =>
            {
                var asyncData = ((JsonRpcStateAsync)callback); // get the async data.
                var result = asyncData.Result + "\n"; // read the result.
                var response = Encoding.UTF8.GetBytes(result); // set the response.

                Connection.Send(response); // send the response.

                _packetLogger.Verbose("tx: {0}", result.PrettifyJson());
            };
        }
        public IObservable <IOnPremiseConnectorRequest> OnRequestReceived(Guid linkId, string connectionId, bool autoAck)
        {
            return(CreateConsumerObservable <OnPremiseConnectorRequest>($"{_REQUEST_QUEUE_PREFIX}{linkId}", autoAck, (request, deliveryTag) =>
            {
                if (autoAck)
                {
                    return;
                }

                switch (request.AcknowledgmentMode)
                {
                case AcknowledgmentMode.Auto:
                    lock (_model)
                    {
                        _model.BasicAck(deliveryTag, false);
                    }

                    _logger?.Debug("Request was automatically acknowledged. request-id={RequestId}", request.RequestId);
                    break;

                case AcknowledgmentMode.Default:
                case AcknowledgmentMode.Manual:
                    request.AcknowledgeId = deliveryTag.ToString();
                    request.AcknowledgeOriginId = _originId;
                    _logger?.Verbose("Request acknowledge id was set. request-id={RequestId}, acknowledge-id={AcknowledgeId}", request.RequestId, request.AcknowledgeId);
                    break;
                }
            }));
        }
        private string GetPathValue(string settingName, ILogger logger)
        {
            var value = GetValue(settingName);

            if (String.IsNullOrEmpty(value))
            {
                return(null);
            }

            if (!Path.IsPathRooted(value))
            {
                var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

                // we have a relative path, so we need to combine it with current execution directory
                logger?.Verbose($"Configured path for {{SettingName}} is relative ({{{settingName}}}) to base path {{BasePath}} ", settingName, value, basePath);

                value = Path.GetFullPath(Path.Combine(basePath, value));
                logger?.Verbose($"Converted path for {{SettingName}} is absolute: {{{settingName}}}", settingName, value);
            }
            else
            {
                logger?.Verbose($"Configured path for {{SettingName}} is absolute: {{{settingName}}}", settingName, value);
            }

            return(value);
        }
Exemple #4
0
        static async Task <HttpResponseMessage> InnerSendAsyncWithLog(HttpClient client,
                                                                      Func <Task <HttpRequestMessage> > getRequest,
                                                                      HttpRequestMessage request,
                                                                      HttpCompletionOption completion, ILogger log)
        {
            if (request == null)
            {
                request = await getRequest().ConfigureAwait(false);
            }

            var url = request.RequestUri;

            var timer = Stopwatch.StartNew();

            log?.Verbose("{Method} {Server}{Path} sending",
                         request.Method, FormatHostPart(url), request.RequestUri.PathAndQuery);
            var response = await client.SendAsync(request, completion);

            var errorContent = response.IsSuccessStatusCode
        ? null
        : response.Content == null
          ? ""
          : await response.Content.ReadAsStringAsync();

            log?.Verbose("{Method} {Server}{Path} {Status} in {Duration}. {ErrorMessage}",
                         request.Method, FormatHostPart(request.RequestUri), request.RequestUri.PathAndQuery, response.StatusCode, timer.Elapsed.Humanize(2),
                         errorContent);

            return(response);
        }
Exemple #5
0
        protected void DeclareAndBind()
        {
            Dictionary <string, object> arguments = null;

            if (Configuration.QueueExpiration == TimeSpan.Zero)
            {
                Logger?.Verbose("Declaring queue. exchange-name={ExchangeName}, queue-name={QueueName}, channel-id={ChannelId}", Exchange, QueueName, ChannelId);
            }
            else
            {
                Logger?.Verbose("Declaring queue. exchange-name={ExchangeName}, queue-name={QueueName}, channel-id={ChannelId}, expiration={QueueExpiration}", Exchange, QueueName, ChannelId, Configuration.QueueExpiration);
                arguments = new Dictionary <string, object>()
                {
                    ["x-expires"] = (int)Configuration.QueueExpiration.TotalMilliseconds
                };
            }

            try
            {
                _model.QueueDeclare(QueueName, true, false, false, arguments);
                _model.QueueBind(QueueName, Exchange, RoutingKey);
            }
            catch (Exception ex)
            {
                Logger?.Error(ex, "Declaring queue failed - possible expiration change. exchange-name={ExchangeName}, queue-name={QueueName}, channel-id={ChannelId}", Exchange, QueueName, ChannelId);
                throw;
            }
        }
Exemple #6
0
        public async Task Initialize(ILogger logger)
        {
            _Logger = logger;

            _Logger?.Verbose("Retrieving gateway...");
            var gateway = await m_httpClient.GetAsync(DiscordAPI.Gateway.Bot);

            if (gateway.IsSuccessStatusCode)
            {
                var result = await gateway.Content.ReadAsStringAsync();

                _Logger?.Verbose($"Gateway returned: {result}");

                var json = JObject.Parse(result);

                var url = json["url"].ToString();

                Gateway      = new GatewayManager(url, m_token, _Logger);
                GuildManager = new GuildManager(Gateway, _Logger);

                Gateway.Initialize();
            }
            else
            {
                _Logger?.Error($"Error returning gateway: {gateway.ReasonPhrase}");
            }
        }
        public async Task <IHttpActionResult> Forward()
        {
            var claimsPrincipal = (ClaimsPrincipal)RequestContext.Principal;
            var onPremiseId     = claimsPrincipal.FindFirst(AuthorizationServerProvider.OnPremiseIdClaimName)?.Value;

            Request.GetCallCancelled().Register(() => _logger?.Warning("Disconnect during receiving on-premise response detected. link-id={LinkId}", onPremiseId));

            var requestStream = await Request.Content.ReadAsStreamAsync().ConfigureAwait(false);

            OnPremiseConnectorResponse response = null;

            if (Request.Headers.TryGetValues("X-TTRELAY-METADATA", out var headerValues))
            {
                response = JToken.Parse(headerValues.First()).ToObject <OnPremiseConnectorResponse>();

                using (var stream = _postDataTemporaryStore.CreateResponseStream(response.RequestId))
                {
                    await requestStream.CopyToAsync(stream).ConfigureAwait(false);

                    response.ContentLength = stream.Length;
                }
            }
            else
            {
                // this is a legacy on-premise connector (v1)
                response = await ForwardLegacyResponse(Encoding.UTF8, requestStream).ConfigureAwait(false);
            }

            _logger?.Verbose("Received on-premise response. request-id={RequestId}, content-length={ResponseContentLength}", response.RequestId, response.ContentLength);

            _backendCommunication.SendOnPremiseTargetResponse(response.OriginId, response);

            return(Ok());
        }
Exemple #8
0
        public IOnPremiseConnectorRequest HandleRequest(IOnPremiseConnectorRequest request, HttpRequestMessage message, IPrincipal clientUser, out HttpResponseMessage immediateResponse)
        {
            immediateResponse = null;

            if (_requestInceptor == null)
            {
                return(request);
            }

            _logger?.Verbose("Handling request. request-id={RequestId}", request.RequestId);


            try
            {
                var interceptedRequest = CreateInterceptedRequest(request, message, clientUser);

                immediateResponse = _requestInceptor.OnRequestReceived(interceptedRequest);

                if (immediateResponse != null)
                {
                    immediateResponse.RequestMessage = message;
                }

                return(interceptedRequest);
            }
            catch (Exception ex)
            {
                _logger?.Error(ex, "Error while executing the request interceptor. type-name={InterceptorType}, request-id={RequestId}", _requestInceptor?.GetType().Name, request.RequestId);
            }

            return(request);
        }
        public async Task <IHttpActionResult> Forward()
        {
            var requestStream = await Request.Content.ReadAsStreamAsync().ConfigureAwait(false);

            OnPremiseConnectorResponse response = null;

            if (Request.Headers.TryGetValues("X-TTRELAY-METADATA", out var headerValues))
            {
                response = JToken.Parse(headerValues.First()).ToObject <OnPremiseConnectorResponse>();

                using (var stream = _postDataTemporaryStore.CreateResponseStream(response.RequestId))
                {
                    await requestStream.CopyToAsync(stream).ConfigureAwait(false);

                    response.ContentLength = stream.Length;
                }
            }
            else
            {
                // this is a legacy on-premise connector (v1)
                response = await ForwardLegacyResponse(Encoding.UTF8, requestStream).ConfigureAwait(false);
            }

            _logger?.Verbose("Received on-premise response. request-id={RequestId}, content-length={ResponseContentLength}", response.RequestId, response.ContentLength);

            _backendCommunication.SendOnPremiseTargetResponse(response.OriginId, response);

            return(Ok());
        }
        public HttpResponseMessage BuildFromConnectorResponse(IOnPremiseConnectorResponse response, Link link, string requestId)
        {
            var message = new HttpResponseMessage();

            if (response == null)
            {
                _logger?.Verbose("Received no response. request-id={RequestId}", requestId);

                message.StatusCode = HttpStatusCode.GatewayTimeout;
                message.Content    = new ByteArrayContent(Array.Empty <byte>());
                message.Content.Headers.Add("X-TTRELAY-TIMEOUT", "On-Premise");
            }
            else
            {
                message.StatusCode = response.StatusCode;
                message.Content    = GetResponseContentForOnPremiseTargetResponse(response, link);

                if (response.HttpHeaders.TryGetValue("WWW-Authenticate", out var wwwAuthenticate))
                {
                    message.Headers.Add("WWW-Authenticate", wwwAuthenticate);
                }

                if (IsRedirectStatusCode(response.StatusCode) && response.HttpHeaders.TryGetValue("Location", out var location))
                {
                    message.Headers.Location = new Uri(location, UriKind.RelativeOrAbsolute);
                }
            }

            return(message);
        }
        public IConnection CreateConnection()
        {
            var connectionString = _configuration.RabbitMqConnectionString;

            if (connectionString == null)
            {
                _logger?.Fatal("Not connection string found for RabbitMQ. Can not create a bus. Aborting...");
                throw new ConfigurationErrorsException("Could not find a connection string for RabbitMQ. Please add a connection string in the <connectionStrings> section of the application's configuration file. For example: <add name=\"RabbitMQ\" connectionString=\"host=localhost\" />");
            }

            try
            {
                _factory.Uri = new Uri(connectionString);

                if (_configuration.RabbitMqClusterHosts == null)
                {
                    _logger?.Verbose("Creating RabbitMQ connection. connection-string={RabbitConnectionString}", _configuration.RabbitMqConnectionString);
                    return(_factory.CreateConnection());
                }

                _logger?.Verbose("Creating RabbitMQ cluster connection. connection-string={RabbitConnectionString}, cluster-hosts={RabbitClusterHosts}", _configuration.RabbitMqConnectionString, _configuration.RabbitMqClusterHosts);
                return(_factory.CreateConnection(AmqpTcpEndpoint.ParseMultiple(_configuration.RabbitMqClusterHosts)));
            }
            catch (Exception ex)
            {
                _logger?.Fatal(ex, "Cannot connect to RabbitMQ using the provided configuration.");
                throw;
            }
        }
        public Stream CreateRequestStream(string requestId)
        {
            _logger?.Verbose("Creating stream for storing request body. request-id={RequestId}", requestId);

            var ms = new NotifyingMemoryStream();

            ms.Disposing += (s, e) => _memcachedClient.Store(StoreMode.Set, requestId, (s as NotifyingMemoryStream)?.ToArray(), _storagePeriod);
            return(ms);
        }
        internal void RenewTransactionCallback(object state)
        {
            try
            {
                if (_saveQueue.IsEmpty)
                {
                    return;
                }

                var sw = Stopwatch.StartNew();
                using (var cn = DbConnection())
                {
                    cn.Open();
                    using (var trans = cn.BeginTransaction())
                    {
                        while (_saveQueue.TryDequeue(out WallPostDataChunk chunk))
                        {
                            cn.Query(@"INSERT INTO WallPosts (GroupId, CompressedData) VALUES (@GroupId, @CompressedData);", chunk);
                        }
                        trans.Commit();
                    }
                }
                _log?.Verbose($"Commitinf transaction takes {sw.ElapsedMilliseconds} ms");
            }
            catch (Exception ex)
            {
                _log?.Error(ex, ex.Message);
            }
        }
        private async Task ForwardClientRequestAsync(string connectionId, IOnPremiseConnectorRequest request)
        {
            _logger?.Verbose("Forwarding client request to connection. connection-id={ConnectionId}, request-id={RequestId}, http-method={RequestMethod}, url={RequestUrl}, origin-id={OriginId}, body-length={RequestContentLength}",
                             connectionId, request.RequestId, request.HttpMethod, request.Url, request.OriginId, request.ContentLength);

            await Connection.Send(connectionId, request).ConfigureAwait(false);
        }
        private async Task ForwardClientRequestAsync(string connectionId, IOnPremiseConnectorRequest request)
        {
            try
            {
                _logger?.Verbose("Forwarding client request to connection. connection-id={ConnectionId}, request-id={RequestId}, http-method={RequestMethod}, url={RequestUrl}, origin-id={OriginId}, body-length={RequestContentLength}",
                                 connectionId, request.RequestId, request.HttpMethod, _configuration.LogSensitiveData ? request.Url : request.Url.StripQueryString(), request.OriginId, request.ContentLength);

                var json = JObject.FromObject(request);
                if (request.Properties != null)
                {
                    json.Remove(nameof(IOnPremiseConnectorRequest.Properties));

                    foreach (var kvp in request.Properties)
                    {
                        json[kvp.Key] = JToken.FromObject(kvp.Value);
                    }
                }

                await Connection.Send(connectionId, json).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger?.Error(ex, "An error occured forwarding request to connection. connection-id={ConnectionId}, request={@Request}", connectionId, request);
            }
        }
        internal void PushDataToDataBase(IList <DataChunk> chunkList)
        {
            try
            {
                if ((chunkList?.Count() ?? 0) == 0)
                {
                    return;
                }
                _log?.Verbose($"Saving {chunkList.Count()} records");

                using (var cn = DbConnection())
                {
                    cn.Open();
                    using (var trans = cn.BeginTransaction())
                    {
                        foreach (var chunk in chunkList)
                        {
                            cn.Query(@"INSERT INTO UserGets (VkontakteUserId, Timestamp, CompressedUserGet) VALUES (@VkontakteUserId, @Timestamp, @CompressedUserGet);", chunk);
                        }

                        trans.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                _log?.Error(ex, ex.Message);
            }
        }
Exemple #17
0
        public IHttpActionResult Get(string requestId)
        {
            _logger?.Verbose("Getting request data. request-id={RequestId}", requestId);

            var stream = _temporaryStore.GetRequestStream(requestId);

            if (stream == null)
            {
                _logger?.Warning("No request data found. request-id={RequestId}", requestId);
                return(NotFound());
            }

            return(new ResponseMessageResult(new HttpResponseMessage()
            {
                Content = new StreamContent(stream, 0x10000)
            }));
        }
        public static void CopyRecursiveTo(
            this DirectoryInfo?sourceDirectory,
            DirectoryInfo targetDirectory,
            ILogger?logger = null)
        {
            if (targetDirectory == null)
            {
                throw new ArgumentNullException(nameof(targetDirectory));
            }

            if (sourceDirectory is null)
            {
                return;
            }

            if (sourceDirectory.FullName.Equals(targetDirectory.FullName))
            {
                throw new InvalidOperationException(
                          $"Could not copy from and to the same directory '{sourceDirectory.FullName}'");
            }

            sourceDirectory.Refresh();

            if (!sourceDirectory.Exists)
            {
                logger?.Verbose("Source directory {Source} does not exist", sourceDirectory.FullName);
                return;
            }

            targetDirectory.EnsureExists();

            foreach (FileInfo file in sourceDirectory.EnumerateFiles())
            {
                string targetFilePath = Path.Combine(targetDirectory.FullName, file.Name);

                logger?.Verbose("Copying file '{From}' '{To}'", file.FullName, targetFilePath);

                file.CopyTo(targetFilePath, true);
            }

            foreach (DirectoryInfo subDirectory in sourceDirectory.EnumerateDirectories())
            {
                CopyRecursiveTo(subDirectory,
                                new DirectoryInfo(Path.Combine(targetDirectory.FullName, subDirectory.Name)));
            }
        }
Exemple #19
0
        public async Task <IOnPremiseTargetResponse> GetResponseFromLocalTargetAsync(string url, IOnPremiseTargetRequest request, string relayedRequestHeader)
        {
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            _logger?.Verbose("Requesting response from on-premise web target. request-id={RequestId}, url={RequestUrl}, origin-id={OriginId}", request.RequestId, url, request.OriginId);

            var response = new OnPremiseTargetResponse()
            {
                RequestId      = request.RequestId,
                OriginId       = request.OriginId,
                RequestStarted = DateTime.UtcNow,
            };

            try
            {
                var message = await SendLocalRequestWithTimeoutAsync(url, request, relayedRequestHeader).ConfigureAwait(false);

                response.StatusCode  = message.StatusCode;
                response.HttpHeaders = message.Headers.Union(message.Content.Headers).ToDictionary(kvp => kvp.Key, kvp => String.Join(" ", kvp.Value));
                response.Stream      = await message.Content.ReadAsStreamAsync().ConfigureAwait(false);

                response.HttpResponseMessage = message;
            }
            catch (Exception ex)
            {
                _logger?.Error(ex, "Error requesting response from local target. request-id={RequestId}", request.RequestId);

                response.StatusCode  = HttpStatusCode.GatewayTimeout;
                response.HttpHeaders = new Dictionary <string, string> {
                    ["X-TTRELAY-TIMEOUT"] = "On-Premise Target"
                };
            }

            response.RequestFinished = DateTime.UtcNow;

            _logger?.Verbose("Got web response. request-id={RequestId}, status-code={ResponseStatusCode}", response.RequestId, response.StatusCode);

            return(response);
        }
Exemple #20
0
        private async Task ForwardClientRequestAsync(string connectionId, IOnPremiseConnectorRequest request)
        {
            var uri = new Uri(new Uri("http://localhost"), request.Url);

            _logger?.Verbose("Forwarding client request to connection. connection-id={ConnectionId}, request-id={RequestId}, http-method={RequestMethod}, url={RequestUrl}, origin-id={OriginId}, body-length={RequestContentLength}",
                             connectionId, request.RequestId, request.HttpMethod, _configuration.LogSensitiveData ? uri.PathAndQuery : uri.AbsolutePath, request.OriginId, request.ContentLength);

            await Connection.Send(connectionId, request).ConfigureAwait(false);
        }
        public async Task UnregisterOnPremiseConnectionAsync(string connectionId)
        {
            CheckDisposed();

            await DeactivateOnPremiseConnectionAsync(connectionId);

            _logger?.Verbose("Unregistering connection. connection-id={ConnectionId}", connectionId);
            _connectionContexts.TryRemove(connectionId, out var info);
        }
        public async Task PublishToStream <T>(string providerName, Guid streamId, string streamNamespace, T item)
        {
            EnsureClusterIsRunning();

            _logger?.Verbose("Publishing {Item} to {Provider}://{Namespace}-{Id}", typeof(T), streamNamespace, streamId);

            var requestId   = Guid.NewGuid();
            var streamGrain = GrainFactory.GetGrain <IStreamGrain>(requestId);
            await streamGrain.Publish <T>(providerName, streamId, streamNamespace, item);
        }
        private bool RegisterInterceptors(ContainerBuilder builder, params Type[] interfaceTypes)
        {
            var registered = false;

            foreach (var interfaceType in interfaceTypes)
            {
                _logger?.Verbose("Trying to load interceptor implementation. type={InterceptorInterface}", interfaceType.Name);

                var interceptorType = _customCodeAssemblyLoader.GetType(interfaceType);
                if (interceptorType != null)
                {
                    _logger?.Verbose("Registering interceptor. type={InterceptorType}', interface={InterceptorInterface}", interceptorType.Name, interfaceType.Name);
                    builder.RegisterType(interceptorType).As(interfaceType);
                    registered = true;
                }
            }

            return(registered);
        }
Exemple #24
0
 private void SelectPointerAddresses(GameVersion version)
 {
     Version = version;
     if (version == GameVersion.STEAM)
     {
         difficultyAdjustment = 0x081FA818;
         selectedSlot         = 0x081F2620;
         itemCount            = 0x081F1308;
         hitPoints            = 0x081EB330;
         enemyHitPoints       = 0x081E9A98;
         mapName           = 0x081E9B00;
         bagCount          = 0x081EA150;
         mrEverythingCount = 0x933A378;
         logger?.Verbose("Steam Version Detected!. Setting base addresses");
     }
     else if (version == GameVersion.WINDOWS)
     {
         difficultyAdjustment = 0x0933E618;
         selectedSlot         = 0x09336170;
         itemCount            = 0x093352C0;
         hitPoints            = 0x9373DB8;
         enemyHitPoints       = 0x09417178;
         mapName           = 0x0932F7E8;
         bagCount          = 0x9373DB8;
         mrEverythingCount = 0x933A378;
         logger?.Verbose("Microsoft Store Version Detected!. Setting base addresses");
     }
     else
     {
         difficultyAdjustment = 0x81ED378;
         selectedSlot         = 0x0;
         itemCount            = 0x0;
         hitPoints            = 0x8223758;
         enemyHitPoints       = 0x0;
         mapName           = 0x0825B9F8; //  081DE6A8 or 0825BB38
         bagCount          = 0x0;
         mrEverythingCount = 0x0;
         logger?.Warning("Unknown version Detected!. Setting base addresses. Warning Unknown Version Might Not Work!");
         return;
     }
 }
        public async Task AddOrRenewActiveConnectionAsync(Guid linkId, Guid originId, string connectionId, int connectorVersion, string assemblyVersion)
        {
            _logger?.Verbose("Adding or updating connection. connection-id={ConnectionId}, link-id={LinkId}, connector-version={ConnectorVersion}, connector-assembly-version={ConnectorAssemblyVersion}", connectionId, linkId, connectorVersion, assemblyVersion);

            try
            {
                using (var context = new RelayContext())
                {
                    var activeConnection = await context.ActiveConnections
                                           .FirstOrDefaultAsync(ac => ac.LinkId == linkId && ac.OriginId == originId && ac.ConnectionId == connectionId).ConfigureAwait(false);

                    if (activeConnection != null)
                    {
                        activeConnection.LastActivity     = DateTime.UtcNow;
                        activeConnection.ConnectorVersion = connectorVersion;
                        activeConnection.AssemblyVersion  = assemblyVersion;

                        context.Entry(activeConnection).State = EntityState.Modified;
                    }
                    else
                    {
                        context.ActiveConnections.Add(new DbActiveConnection()
                        {
                            LinkId           = linkId,
                            OriginId         = originId,
                            ConnectionId     = connectionId,
                            ConnectorVersion = connectorVersion,
                            AssemblyVersion  = assemblyVersion,
                            LastActivity     = DateTime.UtcNow,
                        });
                    }

                    await context.SaveChangesAsync().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                _logger?.Error(ex, "Error during adding or renewing an active connection. link-id={LinkId}, connection-id={ConnectionId}, connector-version={ConnectorVersion}, connector-assembly-version={ConnectorAssemblyVersion}", linkId, connectionId, connectorVersion, assemblyVersion);
            }
        }
        private void CleanUp(CancellationToken cancellationToken)
        {
            var timeout = DateTime.UtcNow.Add(-_storagePeriod);

            _logger?.Verbose("Cleaning up old stored temporary data (files). timeout={CreationTimeout}", timeout);

            try
            {
                foreach (var fileName in Directory.GetFiles(_path))
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    try
                    {
                        if (File.GetCreationTimeUtc(fileName) < timeout)
                        {
                            File.Delete(fileName);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger?.Error(ex, "File store cleanup process could not delete file. file-name={FileName}", fileName);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger?.Error(ex, "Error during file store cleanup process");
            }
        }
Exemple #27
0
        public void DeleteLink(Guid linkId)
        {
            _logger?.Verbose("Removing link. link-id={LinkId}", linkId);

            try
            {
                using (var context = new RelayContext())
                {
                    var itemToDelete = new DbLink
                    {
                        Id = linkId,
                    };

                    context.Links.Attach(itemToDelete);
                    context.Links.Remove(itemToDelete);

                    context.SaveChanges();
                }
            }
            catch (DbUpdateConcurrencyException) { }
            catch (Exception ex)
            {
                _logger?.Error(ex, "Error while removing a link. link-id={LinkId}", linkId);
            }
        }
        public static CustomWasapiLoopbackCapture CreateCustomWasapiLoopbackCapture(MMDevice mDevice, bool UseWasapiEventSync, ILogger logger = null)
        {
            int channels = 8 /* mDevice.AudioClient.MixFormat.Channels  // What if Realtek driver report 2 channels as it is the common physical channel count but stream is surround? */;
            CustomWasapiLoopbackCapture audioCapturer = null;

            logger?.Verbose($"Use CustomWasapiCapture enabled");

            for (int i = channels; (i > 0 && audioCapturer == null); i--)
            {
                try
                {
                    audioCapturer = new CustomWasapiLoopbackCapture(mDevice, UseWasapiEventSync, i);

                    // We may crash here based on the channel count.
                    audioCapturer.StartRecording();

                    // This is not instant.
                    audioCapturer.StopRecording();
                    while (audioCapturer.CaptureState != CaptureState.Stopped)
                    {
                        Thread.Sleep(50);                                                        // Find a better way to stop and wait.
                    }
                    logger?.Verbose($"CustomWasapiCapture successfully created for {i} channels with {audioCapturer.WaveFormat.SampleRate}Hz and {audioCapturer.WaveFormat.BitsPerSample}bps");

                    break; // succeeded.
                }
                catch (Exception e)
                {
                    logger?.Warning($"CustomWasapiCapture creation failed for {i} channels with {audioCapturer.WaveFormat.SampleRate}Hz and {audioCapturer.WaveFormat.BitsPerSample}bps.\r\nException:{e}\r\nTrying with one less channel");
                    audioCapturer = null;
                }
            }

            if (audioCapturer == null)
            {
                logger?.Error($"CustomWasapiCapture cannot be created. No valid channel config found. Try disabling third party Audio Engines");
            }

            return(audioCapturer);
        }
Exemple #29
0
        public IConnection CreateConnection()
        {
            var connectionString = _configuration.RabbitMqConnectionString;

            if (connectionString == null)
            {
                _logger?.Fatal("Not connection string found for RabbitMQ. Can not create a bus. Aborting...");
                throw new ConfigurationErrorsException("Could not find a connection string for RabbitMQ. Please add a connection string in the <connectionStrings> section of the application's configuration file. For example: <add name=\"RabbitMQ\" connectionString=\"host=localhost\" />");
            }

            try
            {
                _factory.Uri = new Uri(connectionString);

                if (!_configuration.LogSensitiveData && !String.IsNullOrWhiteSpace(_factory.Uri.UserInfo))
                {
                    connectionString = connectionString.Replace($"{_factory.Uri.UserInfo}@", "*** BLOCKED ***@");
                }

                if (_configuration.RabbitMqAutomaticRecoveryEnabled && _factory is ConnectionFactory connectionFactory)
                {
                    connectionFactory.AutomaticRecoveryEnabled = true;
                }

                if (_configuration.RabbitMqClusterHosts == null)
                {
                    _logger?.Verbose("Creating RabbitMQ connection. connection-string={RabbitConnectionString}, automatic-recovery-enabled={RabbitAutomaticRecoveryEnabled}", connectionString, _configuration.RabbitMqAutomaticRecoveryEnabled);
                    return(_factory.CreateConnection());
                }

                _logger?.Verbose("Creating RabbitMQ cluster connection. connection-string={RabbitConnectionString}, cluster-hosts={RabbitClusterHosts}, automatic-recovery-enabled={RabbitAutomaticRecoveryEnabled}", connectionString, _configuration.RabbitMqClusterHosts, _configuration.RabbitMqAutomaticRecoveryEnabled);

                return(_factory.CreateConnection(AmqpTcpEndpoint.ParseMultiple(_configuration.RabbitMqClusterHosts)));
            }
            catch (Exception ex)
            {
                _logger?.Fatal(ex, "Cannot connect to RabbitMQ using the provided configuration.");
                throw;
            }
        }
        public NinjectCore(IKernel kernel, ILogger logger, ICoreSettings settings)
        {
            _kernel = kernel;
            _logger = logger;
            _settings = settings;

            _logger.Information("Initializing Core.");

            InitializeKernel();
            InitializeExtensions();

            _logger.Verbose("Core initilized.");
        }
Exemple #31
0
        private async Task SendHeartbeatAsync(IOnPremiseConnectionContext connectionContext, CancellationToken token)
        {
            if (connectionContext == null)
            {
                throw new ArgumentNullException(nameof(connectionContext));
            }

            if (connectionContext.NextHeartbeat > DateTime.UtcNow)
            {
                return;
            }

            connectionContext.NextHeartbeat = DateTime.UtcNow.Add(_heartbeatInterval);

            try
            {
                _logger?.Verbose("Sending {PingMethod}. connection-id={ConnectionId}", connectionContext.SupportsHeartbeat ? "Heartbeat" : "Ping (as heartbeat)", connectionContext.ConnectionId);

                var requestId = Guid.NewGuid().ToString();
                var request   = new OnPremiseConnectorRequest()
                {
                    HttpMethod         = connectionContext.SupportsHeartbeat ? "HEARTBEAT" : "PING",
                    Url                = String.Empty,
                    RequestStarted     = DateTime.UtcNow,
                    OriginId           = _backendCommunication.OriginId,
                    RequestId          = requestId,
                    AcknowledgmentMode = AcknowledgmentMode.Auto,
                    HttpHeaders        = connectionContext.SupportsConfiguration ? null : new Dictionary <string, string> {
                        ["X-TTRELAY-HEARTBEATINTERVAL"] = _heartbeatInterval.TotalSeconds.ToString(CultureInfo.InvariantCulture)
                    },
                };

                // wait for the response of the Heartbeat / Ping
                var task = _backendCommunication.GetResponseAsync(requestId, _configuration.ActiveConnectionTimeout);

                // heartbeats do NOT go through the message dispatcher as we want to heartbeat the connections directly
                await connectionContext.RequestAction(request, token).ConfigureAwait(false);

                var response = await task.ConfigureAwait(false);

                if (response != null)
                {
                    await _backendCommunication.RenewLastActivityAsync(connectionContext.ConnectionId).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                _logger?.Error(ex, "Error during sending heartbeat to a client. link-id={LinkId}, connection-id={ConnectionId}, connector-version={ConnectorVersion}", connectionContext.LinkId, connectionContext.ConnectionId, connectionContext.ConnectorVersion);
            }
        }
Exemple #32
0
        public WebServer(INancyBootstrapper webBootstrapper, IConfigManager configManager)
            : base(webBootstrapper)
        {
            var config = configManager.WebServerConfig;
            BindIP = config.BindInterface;
            Port = config.Port;

            _logger = Log.ForContext<WebServer>();

            if (config.Enabled)
                Start();
            else
                _logger.Verbose("Skipping web-server initialization as it disabled.");
        }
 /// <summary>
 /// Helper to run the delay policy and output additional information.
 /// </summary>
 /// <param name="logger"></param>
 /// <param name="cont"></param>
 /// <param name="policy"></param>
 /// <param name="maxRetry"></param>
 /// <param name="k"></param>
 /// <param name="ex"></param>
 /// <param name="ct"></param>
 /// <returns></returns>
 private static async Task DelayOrThrow(ILogger logger, Func <Exception, bool> cont,
                                        Func <int, Exception, int> policy, int maxRetry, int k, Exception ex,
                                        CancellationToken ct)
 {
     if (k > maxRetry || !cont(ex))
     {
         logger?.Verbose(ex, "Give up after {k}", k);
         throw ex;
     }
     if (ex is TemporarilyBusyException tbx && tbx.RetryAfter != null)
     {
         var delay = tbx.RetryAfter.Value;
         Log(logger, k, (int)delay.TotalMilliseconds, ex);
         await Task.Delay(delay, ct);
     }
        public BluetoothProfileActivator(IBluetoothService bluetoothService,
            IPersonalizationSettings personalizationSettings,
            ISchedulerProvider schedulerProvider,
            ILoggerFactory loggerFactory)
        {
            _bluetoothService = bluetoothService;
            _personalizationSettings = personalizationSettings;
            _schedulerProvider = schedulerProvider;
            _logger = loggerFactory.CreateLogger(GetType());
            _bluetoothEventLoop = _schedulerProvider.CreateEventLoopScheduler("BluetoothActivator");
            _logger.Verbose("BluetoothProfileActivator.ctor();");

            _profileActivated = _bluetoothService.IdentitiesActivated(_bluetoothEventLoop)
                .Retry()
                .Repeat()
                .Log(_logger, "IdentitiesActivated")
                .Select(Translate)
                .Publish();

            //HACK: Avoid work in the ctor. Can be smart here using SelectMany on the IsEnabled + Connecting when ProfileActivated is subscribed to. -LC
            if (_bluetoothService.IsSupported && IsEnabled)
                _connection.Disposable = _profileActivated.Connect();
        }
 protected override void Log(ILogger logger, string format, params object[] args)
 {
     logger.Verbose(format, args);
 }
Exemple #36
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FileController"/> class.
 /// </summary>
 /// <param name="hostingEnvironment">
 /// The hosting environment.
 /// </param>
 /// <param name="files">A list of files</param>
 public FileController(IApplicationEnvironment hostingEnvironment)
 {
     log = Log.ForContext<FileController>();
     fileUploadPath = Path.Combine(hostingEnvironment.ApplicationBasePath, "Uploads");
     log.Verbose("File handler created with base path: {@basepath}", fileUploadPath);
 }
Exemple #37
0
        public static bool MigrateAssetIfNeeded(ILogger log, string assetFullPath)
        {
            // Determine if asset was Yaml or not
            var assetFileExtension = Path.GetExtension(assetFullPath);
            if (assetFileExtension == null)
                return false;

            assetFileExtension = assetFileExtension.ToLowerInvariant();

            var serializer = AssetSerializer.FindSerializer(assetFileExtension);
            if (!(serializer is AssetYamlSerializer))
                return false;

            // We've got a Yaml asset, let's get expected and serialized versions
            var serializedVersion = 0;
            int expectedVersion;
            Type assetType;

            // Read from Yaml file the asset version and its type (to get expected version)
            // Note: It tries to read as few as possible (SerializedVersion is expected to be right after Id, so it shouldn't try to read further than that)
            using (var streamReader = new StreamReader(assetFullPath))
            {
                var yamlEventReader = new EventReader(new Parser(streamReader));

                // Skip header
                yamlEventReader.Expect<StreamStart>();
                yamlEventReader.Expect<DocumentStart>();
                var mappingStart = yamlEventReader.Expect<MappingStart>();

                var yamlSerializerSettings = YamlSerializer.GetSerializerSettings();
                var tagTypeRegistry = yamlSerializerSettings.TagTypeRegistry;
                assetType = tagTypeRegistry.TypeFromTag(mappingStart.Tag);

                expectedVersion = AssetRegistry.GetCurrentFormatVersion(assetType);

                Scalar assetKey;
                while ((assetKey = yamlEventReader.Allow<Scalar>()) != null)
                {
                    // Only allow Id before SerializedVersion
                    if (assetKey.Value == "Id")
                    {
                        yamlEventReader.Skip();
                        continue;
                    }
                    if (assetKey.Value == "SerializedVersion")
                    {
                        serializedVersion = Convert.ToInt32(yamlEventReader.Expect<Scalar>().Value, CultureInfo.InvariantCulture);
                        break;
                    }
                }
            }

            if (serializedVersion > expectedVersion)
            {
                // Try to open an asset newer than what we support (probably generated by a newer Paradox)
                throw new InvalidOperationException(string.Format("Asset of type {0} has been serialized with newer version {1}, but only version {2} is supported. Was this asset created with a newer version of Paradox?", assetType, serializedVersion, expectedVersion));
            }

            if (serializedVersion < expectedVersion)
            {
                // Perform asset upgrade
                log.Verbose("{0} needs update, from version {1} to version {2}", Path.GetFullPath(assetFullPath), serializedVersion, expectedVersion);

                // Load the asset as a YamlNode object
                var input = new StringReader(File.ReadAllText(assetFullPath));
                var yamlStream = new YamlStream();
                yamlStream.Load(input);
                var yamlRootNode = (YamlMappingNode)yamlStream.Documents[0].RootNode;

                // Check if there is any asset updater
                var assetUpgraders = AssetRegistry.GetAssetUpgraders(assetType);
                if (assetUpgraders == null)
                {
                    throw new InvalidOperationException(string.Format("Asset of type {0} should be updated from version {1} to {2}, but no asset migration path was found", assetType, serializedVersion, expectedVersion));
                }

                // Instantiate asset updaters
                var currentVersion = serializedVersion;
                while (currentVersion != expectedVersion)
                {
                    int targetVersion;
                    // This will throw an exception if no upgrader is available for the given version, exiting the loop in case of error.
                    var upgrader = assetUpgraders.GetUpgrader(currentVersion, out targetVersion);
                    upgrader.Upgrade(currentVersion, targetVersion, log, yamlRootNode);
                    currentVersion = targetVersion;
                }

                // Make sure asset is updated to latest version
                YamlNode serializedVersionNode;
                var newSerializedVersion = 0;
                if (yamlRootNode.Children.TryGetValue(new YamlScalarNode("SerializedVersion"), out serializedVersionNode))
                {
                    newSerializedVersion = Convert.ToInt32(((YamlScalarNode)serializedVersionNode).Value);
                }

                if (newSerializedVersion != expectedVersion)
                {
                    throw new InvalidOperationException(string.Format("Asset of type {0} was migrated, but still its new version {1} doesn't match expected version {2}.", assetType, newSerializedVersion, expectedVersion));
                }

                log.Info("{0} updated from version {1} to version {2}", Path.GetFullPath(assetFullPath), serializedVersion, expectedVersion);

                var preferredIndent = YamlSerializer.GetSerializerSettings().PreferredIndent;

                // Save asset back to disk
                using (var streamWriter = new StreamWriter(assetFullPath))
                    yamlStream.Save(streamWriter, true, preferredIndent);

                return true;
            }

            return false;
        }
 protected override void Log(ILogger logger, Exception exception, string format, params object[] args)
 {
     logger.Verbose(exception, format, args);
 }
        //private async Task<ServiceDto> CallConsul(HttpContext context, ILogger log, string serviceName)
        //{
        //    try
        //    {
        //        var data = new ServiceDto();
        //        log.Verbose("Searching service in Consul...");

        //        using (var client = new ConsulClient())
        //        {
        //            var consulCatalog = await client.Catalog.Service(serviceName);

        //            if (consulCatalog.Response.Length > 0)
        //            {
        //                var service = consulCatalog.Response[0];
        //                data.Address = IPAddress.Parse(service.Address);
        //                data.Port = service.ServicePort;

        //                log.Information("Sending request to {Host}:{port}...", service.Address, data.Port);
        //            }
        //            else
        //            {
        //                log.Warning("Service not found!");
        //                context.Response.Send(404);
        //            }
        //        }

        //        return data;
        //    }
        //    catch (Exception e)
        //    {
        //        log.Error(e, "Cannot call Consul");
        //        context.Response.Send(500);
        //        return null;
        //    }
        //}

        private static async Task CallService(HttpContext context, HttpRequest request, ILogger log, ServiceInfo serviceInfo)
        {
            IPAddress serviceAdress = IPAddress.Parse(serviceInfo.Address);
            int servicePort = serviceInfo.Port;

            using (context)
            {
                try
                {
                    using (var client = new TcpClient())
                    {
                        client.Connect(serviceAdress, servicePort);

                        using (var realService = client.GetStream())
                        {
                            log.Verbose("Sending request...");

                            await request.RedirectToAsync(realService);

                            log.Verbose("Waiting response...");

                            await context.Response.RedirectFromAsync(realService);

                            log.Information("Finished!");
                        }
                    }
                }
                catch (Exception e)
                {
                    log.Error(e, "Cannot call service");
                    context.Response.Send(500);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pool"></param>
        /// <param name="minerManager"></param>
        public GetworkMiner(int id, IPool pool, IMinerManager minerManager)
        {
            Id = id; // the id of the miner.
            Pool = pool;
            _minerManager = minerManager;

            Authenticated = false; // miner has to authenticate.

            Software = MinerSoftware.Unknown;
            SoftwareVersion = new Version();

            _logger = Log.ForContext<GetworkMiner>().ForContext("Component", pool.Config.Coin.Name);
            _packetLogger = LogManager.PacketLogger.ForContext<GetworkMiner>().ForContext("Component", pool.Config.Coin.Name);

            _rpcResultHandler = callback =>
            {
                var asyncData = ((JsonRpcStateAsync) callback);
                var result = asyncData.Result;
                var response = Encoding.UTF8.GetBytes(result);
                var context = (GetworkContext) asyncData.AsyncState;

                context.Response.ContentType = "application/json";
                context.Response.ContentEncoding = Encoding.UTF8;
                context.Response.ContentLength64 = response.Length;
                context.Response.OutputStream.Write(response, 0, response.Length);

                _packetLogger.Verbose("tx: {0}", result.PrettifyJson());
            };
        }