From ec4c682ad85e0d2197b48f82847ac5ac6a65630e Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 5 Sep 2023 15:29:34 +0800 Subject: [PATCH 1/2] Create a `RandomComputerId` to `CliAnalyticsCollectInputDto`. --- .../Volo/Abp/Cli/Auth/AuthService.cs | 19 +++++++++++++++++ .../Volo/Abp/Cli/CliPaths.cs | 1 + .../Analyticses/CliAnalyticsCollect.cs | 21 ++++++++++++++++++- .../CliAnalyticsCollectInputDto.cs | 2 ++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs index ec2074434af..292b9556451 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs @@ -90,6 +90,8 @@ public async Task LoginAsync(string userName, string password, string organizati var accessToken = await AuthenticationService.GetAccessTokenAsync(configuration); File.WriteAllText(CliPaths.AccessToken, accessToken, Encoding.UTF8); + + await WriteRandomComputerIdAsync(); } public async Task DeviceLoginAsync() @@ -105,6 +107,8 @@ public async Task DeviceLoginAsync() var accessToken = await AuthenticationService.GetAccessTokenAsync(configuration); File.WriteAllText(CliPaths.AccessToken, accessToken, Encoding.UTF8); + + await WriteRandomComputerIdAsync(); } public async Task LogoutAsync() @@ -125,6 +129,8 @@ public async Task LogoutAsync() File.Delete(CliPaths.Lic); } + + await WriteRandomComputerIdAsync(); } public async Task CheckMultipleOrganizationsAsync(string username) @@ -177,4 +183,17 @@ public static bool IsLoggedIn() { return File.Exists(CliPaths.AccessToken); } + + private async Task WriteRandomComputerIdAsync() + { + var loginInfo = await GetLoginInfoAsync(); + if (loginInfo != null && loginInfo.Id.HasValue) + { + File.WriteAllText(CliPaths.RandomComputerId, loginInfo.Id.Value.ToString("D"), Encoding.UTF8); + } + else + { + File.WriteAllText(CliPaths.RandomComputerId, Guid.NewGuid().ToString("D"), Encoding.UTF8); + } + } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliPaths.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliPaths.cs index 99ba8fbe7e9..5153f5285fc 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliPaths.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliPaths.cs @@ -10,6 +10,7 @@ public static class CliPaths public static string Log => Path.Combine(AbpRootPath, "cli", "logs"); public static string Root => Path.Combine(AbpRootPath, "cli"); public static string AccessToken => Path.Combine(AbpRootPath, "cli", "access-token.bin"); + public static string RandomComputerId => Path.Combine(AbpRootPath, "cli", "random-computer-id.bin"); public static string Memory => Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!, "memory.bin"); public static string Build => Path.Combine(AbpRootPath, "build"); public static string Lic => Path.Combine(Path.GetTempPath(), Encoding.ASCII.GetString(new byte[] { 65, 98, 112, 76, 105, 99, 101, 110, 115, 101, 46, 98, 105, 110 })); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs index fc8505d831b..d26282c4cd3 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs @@ -1,9 +1,11 @@ using System; +using System.IO; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.Cli.Auth; using Volo.Abp.Cli.Http; using Volo.Abp.DependencyInjection; using Volo.Abp.Http; @@ -19,22 +21,39 @@ public class CliAnalyticsCollect : ICliAnalyticsCollect, ITransientDependency private readonly ILogger _logger; private readonly IRemoteServiceExceptionHandler _remoteServiceExceptionHandler; private readonly CliHttpClientFactory _cliHttpClientFactory; + private readonly IAuthService _authService; public CliAnalyticsCollect( ICancellationTokenProvider cancellationTokenProvider, IJsonSerializer jsonSerializer, IRemoteServiceExceptionHandler remoteServiceExceptionHandler, - CliHttpClientFactory cliHttpClientFactory) + CliHttpClientFactory cliHttpClientFactory, + IAuthService authService) { _cancellationTokenProvider = cancellationTokenProvider; _jsonSerializer = jsonSerializer; _remoteServiceExceptionHandler = remoteServiceExceptionHandler; _cliHttpClientFactory = cliHttpClientFactory; + _authService = authService; _logger = NullLogger.Instance; } public async Task CollectAsync(CliAnalyticsCollectInputDto input) { + if (input.RandomComputerId.IsNullOrWhiteSpace()) + { + if (!File.Exists(CliPaths.RandomComputerId)) + { + var randomComputerId = Guid.NewGuid().ToString("D"); + input.RandomComputerId = randomComputerId; + File.WriteAllText(CliPaths.RandomComputerId, randomComputerId, Encoding.UTF8); + } + else + { + input.RandomComputerId = File.ReadAllText(CliPaths.RandomComputerId, Encoding.UTF8); + } + } + var postData = _jsonSerializer.Serialize(input); var url = $"{CliUrls.WwwAbpIo}api/clianalytics/collect"; diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollectInputDto.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollectInputDto.cs index 396cecb525e..2e47455896b 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollectInputDto.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollectInputDto.cs @@ -23,4 +23,6 @@ public class CliAnalyticsCollectInputDto public string Language { get; set; } public string Ip { get; set; } + + public string RandomComputerId { get; set; } } From df8a43a03706193539cc347c7f63c6f623e74ee1 Mon Sep 17 00:00:00 2001 From: Engincan VESKE <43685404+EngincanV@users.noreply.github.com> Date: Tue, 5 Sep 2023 16:46:53 +0300 Subject: [PATCH 2/2] Update CliAnalyticsCollect.cs --- .../Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs index d26282c4cd3..66568ffb4d1 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Analyticses/CliAnalyticsCollect.cs @@ -21,20 +21,17 @@ public class CliAnalyticsCollect : ICliAnalyticsCollect, ITransientDependency private readonly ILogger _logger; private readonly IRemoteServiceExceptionHandler _remoteServiceExceptionHandler; private readonly CliHttpClientFactory _cliHttpClientFactory; - private readonly IAuthService _authService; public CliAnalyticsCollect( ICancellationTokenProvider cancellationTokenProvider, IJsonSerializer jsonSerializer, IRemoteServiceExceptionHandler remoteServiceExceptionHandler, - CliHttpClientFactory cliHttpClientFactory, - IAuthService authService) + CliHttpClientFactory cliHttpClientFactory) { _cancellationTokenProvider = cancellationTokenProvider; _jsonSerializer = jsonSerializer; _remoteServiceExceptionHandler = remoteServiceExceptionHandler; _cliHttpClientFactory = cliHttpClientFactory; - _authService = authService; _logger = NullLogger.Instance; }