Skip to content

Commit

Permalink
完成GoodPass CLI重构
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeDong32 authored Jul 5, 2023
2 parents a5147b6 + 3c50557 commit 28d1779
Show file tree
Hide file tree
Showing 39 changed files with 3,324 additions and 2,740 deletions.
25 changes: 25 additions & 0 deletions GoodPass-CLI.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33815.320
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoodPass-CLI", "GoodPass-CLI\GoodPass-CLI.csproj", "{461024E6-40E6-422F-93E9-A9D571A750F2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{461024E6-40E6-422F-93E9-A9D571A750F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{461024E6-40E6-422F-93E9-A9D571A750F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{461024E6-40E6-422F-93E9-A9D571A750F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{461024E6-40E6-422F-93E9-A9D571A750F2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0261636B-6072-4CB9-AAE9-89E7DF57015B}
EndGlobalSection
EndGlobal
11 changes: 11 additions & 0 deletions GoodPass-CLI/GoodPass-CLI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>GoodPass_CLI</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
112 changes: 112 additions & 0 deletions GoodPass-CLI/Helpers/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
namespace GoodPass_CLI.Helpers;
public static class ConsoleHelper
{
public static void PrintError(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ForegroundColor = ConsoleColor.Gray;
}

public static void PrintWarning(string message)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(message);
Console.ForegroundColor = ConsoleColor.Gray;
}

public static void PrintGreen(string message)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(message);
Console.ForegroundColor = ConsoleColor.Gray;
}

/// <summary>
/// 密码行保护
/// </summary>
/// <param name="linebefore">保护行位置</param>
/// <param name="mode">1:get命令显示保护,2:add命令显示时保护</param>
public static void PasswordLineProtect(int linebefore, int mode)
{
switch (mode)
{
case 1:
var currentLineCursor1 = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor1 - linebefore);
Console.Write(" {0,-15}\t{1,-40}", "Password", "************");
Console.SetCursorPosition(0, currentLineCursor1);
break;
case 2:
var currentLineCursor2 = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor2 - linebefore);
Console.Write(" {0,-15}\t{1,-40}", "Password", "************");
Console.SetCursorPosition(0, currentLineCursor2);
break;
default:
var currentLineCursor3 = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor3 - linebefore);
Console.Write(" [ Your password has been protected by GoodPass CLI ]");
Console.SetCursorPosition(0, currentLineCursor3);
break;
}

}

public static void PasswordCommandProtect(int linebefore)
{
var currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor - linebefore);
Console.Write(">>> [ Your password has been protected by GoodPass CLI ]");
Console.SetCursorPosition(0, currentLineCursor);
}

public static void CleanConsoleLine(int lineindex)
{
var currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor - lineindex);
ClearCurrentConsoleLine();
Console.SetCursorPosition(0, currentLineCursor);
}

public static void BackspaceToLine(int linebefore)
{
var currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor - linebefore);
Console.SetCursorPosition(0, Console.CursorTop);
}

public static void ClearCurrentConsoleLine()
{
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
}

public static string? ReadPassword()
{
var password = "";
while (true)
{
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine();
break;
}
else if (key.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password = password.Remove(password.Length - 1);
Console.Write("\b \b");
}
}
else
{
password += key.KeyChar;
Console.Write("*");
}
}
return password;
}
}
200 changes: 200 additions & 0 deletions GoodPass-CLI/Models/GPData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
using GoodPass_CLI.Services;

namespace GoodPass_CLI.Models;

public class GPData
{
#region Properties
public string PlatformName
{
get; set;
}

public string? PlatformUrl
{
get; set;
}

public string AccountName
{
get; set;
}

public string EncPassword
{
get; set;
}

private string DecPassword
{
get; set;
}

public DateTime LatestUpdateTime
{
get; set;
}

public string GetPassword;
#endregion

#region Constructors
/// <summary>
/// 默认GPData构造函数
/// </summary>
public GPData()
{
PlatformName = "No Name";
PlatformUrl = null;
AccountName = "No Name";
DecPassword = "DecPassword";
EncPassword = DecPassword;
GetPassword = DecPassword;
LatestUpdateTime = DateTime.Now;
}

/// <summary>
/// GPData的拷贝构造函数
/// </summary>
/// <param name="data">拷贝构造的对象</param>
public GPData(GPData data)
{
PlatformName = data.PlatformName;
PlatformUrl = data.PlatformUrl;
AccountName = data.AccountName;
DecPassword = data.DecPassword;
EncPassword = data.EncPassword;
LatestUpdateTime = data.LatestUpdateTime;
GetPassword = data.DecPassword;
}

/// <summary>
/// GPData的完整构造函数
/// </summary>
public GPData(string platformName, string accountName, string encPassword, DateTime latestUpdateTime)
{
PlatformName = platformName;
PlatformUrl = null;
AccountName = accountName;
EncPassword = encPassword;
DecPassword = GoodPassCryptographicServices.DecryptStr(EncPassword);
GetPassword = DecPassword;
LatestUpdateTime = latestUpdateTime;
}

/// <summary>
/// GPData的含时间的构造函数
/// </summary>
public GPData(string platformName, string? platformUrl, string accountName, string encPassword, DateTime latestUpdateTime)
{
PlatformName = platformName;
PlatformUrl = platformUrl;
AccountName = accountName;
EncPassword = encPassword;
DecPassword = GoodPassCryptographicServices.DecryptStr(EncPassword);
GetPassword = DecPassword;
LatestUpdateTime = latestUpdateTime;
}
#endregion

#region Self Update Method
/// <summary>
/// 数据自更新,预留接口
/// </summary>
public async void SelfUpdate() //预留接口
{
/*Todo:添加数据自升级的相应代码*/
await Task.CompletedTask;
}
#endregion

#region Encrypt/Decrypt Data
/// <summary>
/// 数据解密
/// </summary>
/// <returns>数据解密是否成功</returns>
public bool DataDecrypt()
{
DecPassword = GoodPassCryptographicServices.DecryptStr(EncPassword);
GetPassword = DecPassword;
return true;
}

public bool DataEncrypt()
{
EncPassword = GoodPassCryptographicServices.EncryptStr(DecPassword);
DecPassword = "";
GetPassword = "";
return true;
}
#endregion

#region Change Data Methods
/// <summary>
/// 更改密码
/// </summary>
/// <param name="newPassword">新密码</param>
/// <returns>修改密码结果</returns>
public string ChangePassword(string newPassword)
{
DataDecrypt();
if (newPassword != DecPassword && newPassword != string.Empty && newPassword != null)
{
DecPassword = newPassword;
EncPassword = GoodPassCryptographicServices.EncryptStr(newPassword);
LatestUpdateTime = DateTime.Now;
return "Success";
}
else if (newPassword == DecPassword)
{
return "SamePassword";
}
else if (newPassword == String.Empty || newPassword == null)
{
return "Empty";
}
else
{
return "Unknown Error";
}
}

/// <summary>
/// 修改平台Url
/// </summary>
/// <param name="newUrl">新的Url</param>
/// <returns>修改结果</returns>
public bool ChangeUrl(string? newUrl)
{
if (newUrl == this.PlatformUrl)
{
return false;
}
else
{
PlatformUrl = newUrl;
LatestUpdateTime = DateTime.Now;
return true;
}
}

/// <summary>
/// 修改平台名
/// </summary>
/// <param name="newAccountName">新的平台名</param>
/// <returns>修改结果</returns>
public bool ChangeAccountName(string newAccountName)
{
if (newAccountName == AccountName)
{
return false;
}
else
{
AccountName = newAccountName;
LatestUpdateTime = DateTime.Now;
return true;
}
}
#endregion
}
Loading

0 comments on commit 28d1779

Please sign in to comment.