first commit
This commit is contained in:
76
client/Reader/MainForm.Designer.cs
generated
Normal file
76
client/Reader/MainForm.Designer.cs
generated
Normal file
@@ -0,0 +1,76 @@
|
||||
namespace Reader
|
||||
{
|
||||
partial class MainForm
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.txtFolder = new System.Windows.Forms.TextBox();
|
||||
this.btnBrowse = new System.Windows.Forms.Button();
|
||||
this.btnRead = new System.Windows.Forms.Button();
|
||||
this.dgv = new System.Windows.Forms.DataGridView();
|
||||
((System.ComponentModel.ISupportInitialize)(this.dgv)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
// txtFolder
|
||||
this.txtFolder.Location = new System.Drawing.Point(12, 30);
|
||||
this.txtFolder.Name = "txtFolder";
|
||||
this.txtFolder.Size = new System.Drawing.Size(360, 23);
|
||||
// btnBrowse
|
||||
this.btnBrowse.Location = new System.Drawing.Point(378, 28);
|
||||
this.btnBrowse.Name = "btnBrowse";
|
||||
this.btnBrowse.Size = new System.Drawing.Size(75, 25);
|
||||
this.btnBrowse.Text = "浏览文件夹";
|
||||
this.btnBrowse.UseVisualStyleBackColor = true;
|
||||
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
|
||||
// btnRead
|
||||
this.btnRead.Location = new System.Drawing.Point(378, 170);
|
||||
this.btnRead.Name = "btnRead";
|
||||
this.btnRead.Size = new System.Drawing.Size(75, 27);
|
||||
this.btnRead.Text = "开始读取";
|
||||
this.btnRead.UseVisualStyleBackColor = true;
|
||||
this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
|
||||
// dgv
|
||||
this.dgv.AllowUserToAddRows = false;
|
||||
this.dgv.AllowUserToDeleteRows = false;
|
||||
this.dgv.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||
this.dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
|
||||
new System.Windows.Forms.DataGridViewTextBoxColumn() { Name = "colFile", HeaderText = "文件名" },
|
||||
new System.Windows.Forms.DataGridViewTextBoxColumn() { Name = "colUpload", HeaderText = "上传信息" },
|
||||
new System.Windows.Forms.DataGridViewTextBoxColumn() { Name = "colDownload", HeaderText = "下载信息" },
|
||||
new System.Windows.Forms.DataGridViewTextBoxColumn() { Name = "colTime", HeaderText = "暗码写入时间" }
|
||||
});
|
||||
this.dgv.Location = new System.Drawing.Point(12, 62);
|
||||
this.dgv.Name = "dgv";
|
||||
this.dgv.ReadOnly = true;
|
||||
this.dgv.RowTemplate.Height = 25;
|
||||
this.dgv.Size = new System.Drawing.Size(441, 100);
|
||||
// MainForm
|
||||
this.ClientSize = new System.Drawing.Size(465, 209);
|
||||
this.Controls.Add(this.txtFolder);
|
||||
this.Controls.Add(this.btnBrowse);
|
||||
this.Controls.Add(this.dgv);
|
||||
this.Controls.Add(this.btnRead);
|
||||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
|
||||
this.MaximizeBox = false;
|
||||
this.Name = "MainForm";
|
||||
this.Text = "Reader - 读取暗码";
|
||||
((System.ComponentModel.ISupportInitialize)(this.dgv)).EndInit();
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
}
|
||||
|
||||
private System.Windows.Forms.TextBox txtFolder;
|
||||
private System.Windows.Forms.Button btnBrowse;
|
||||
private System.Windows.Forms.Button btnRead;
|
||||
private System.Windows.Forms.DataGridView dgv;
|
||||
}
|
||||
}
|
||||
98
client/Reader/MainForm.cs
Normal file
98
client/Reader/MainForm.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Reader
|
||||
{
|
||||
public partial class MainForm : Form
|
||||
{
|
||||
private static readonly HttpClient http = new HttpClient();
|
||||
private const string ServerUrl = "http://localhost:5000";
|
||||
|
||||
public MainForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void btnBrowse_Click(object sender, EventArgs e)
|
||||
{
|
||||
using var fbd = new FolderBrowserDialog();
|
||||
if (fbd.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
txtFolder.Text = fbd.SelectedPath;
|
||||
}
|
||||
}
|
||||
|
||||
private async void btnRead_Click(object sender, EventArgs e)
|
||||
{
|
||||
var folder = txtFolder.Text;
|
||||
if (string.IsNullOrWhiteSpace(folder) || !Directory.Exists(folder))
|
||||
{
|
||||
MessageBox.Show("请选择有效的文件夹。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
dgv.Rows.Clear();
|
||||
var files = Directory.GetFiles(folder, "*", SearchOption.AllDirectories);
|
||||
var shaList = new System.Collections.Generic.List<string>();
|
||||
var fileMap = new System.Collections.Generic.Dictionary<string, string>();
|
||||
foreach (var f in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
var sha = await Task.Run(() => ComputeSha256(f));
|
||||
shaList.Add(sha);
|
||||
fileMap[sha] = f;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
if (shaList.Count == 0)
|
||||
{
|
||||
MessageBox.Show("没有找到文件。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
return;
|
||||
}
|
||||
var payload = new { shas = shaList };
|
||||
var content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
|
||||
var resp = await http.PostAsync(ServerUrl + "/read", content);
|
||||
if (!resp.IsSuccessStatusCode)
|
||||
{
|
||||
MessageBox.Show("读取失败: " + await resp.Content.ReadAsStringAsync(), "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
var text = await resp.Content.ReadAsStringAsync();
|
||||
using var doc = JsonDocument.Parse(text);
|
||||
var root = doc.RootElement;
|
||||
var data = root.GetProperty("data");
|
||||
foreach (var sha in shaList)
|
||||
{
|
||||
var fileName = Path.GetFileName(fileMap[sha]);
|
||||
if (data.TryGetProperty(sha, out var item) && item.ValueKind != JsonValueKind.Null)
|
||||
{
|
||||
var up = item.GetProperty("upload_code").GetString() ?? "";
|
||||
var down = item.GetProperty("download_code").GetString() ?? "";
|
||||
var created = item.GetProperty("created_at").GetString() ?? "";
|
||||
dgv.Rows.Add(fileName, up, down, created);
|
||||
}
|
||||
else
|
||||
{
|
||||
dgv.Rows.Add(fileName, "无", "无", "无");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string ComputeSha256(string filePath)
|
||||
{
|
||||
using var sha = SHA256.Create();
|
||||
using var stream = File.OpenRead(filePath);
|
||||
var hash = sha.ComputeHash(stream);
|
||||
var sb = new StringBuilder();
|
||||
foreach (var b in hash) sb.Append(b.ToString("x2"));
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
17
client/Reader/Program.cs
Normal file
17
client/Reader/Program.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Reader
|
||||
{
|
||||
static class Program
|
||||
{
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
Application.SetHighDpiMode(HighDpiMode.SystemAware);
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new MainForm());
|
||||
}
|
||||
}
|
||||
}
|
||||
9
client/Reader/Reader.csproj
Normal file
9
client/Reader/Reader.csproj
Normal file
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
95
client/Writer/MainForm.Designer.cs
generated
Normal file
95
client/Writer/MainForm.Designer.cs
generated
Normal file
@@ -0,0 +1,95 @@
|
||||
namespace Writer
|
||||
{
|
||||
partial class MainForm
|
||||
{
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.txtFile = new System.Windows.Forms.TextBox();
|
||||
this.btnBrowse = new System.Windows.Forms.Button();
|
||||
this.txtUpload = new System.Windows.Forms.TextBox();
|
||||
this.txtDownload = new System.Windows.Forms.TextBox();
|
||||
this.lblFile = new System.Windows.Forms.Label();
|
||||
this.lblUpload = new System.Windows.Forms.Label();
|
||||
this.lblDownload = new System.Windows.Forms.Label();
|
||||
this.btnWrite = new System.Windows.Forms.Button();
|
||||
this.SuspendLayout();
|
||||
// txtFile
|
||||
this.txtFile.Location = new System.Drawing.Point(12, 30);
|
||||
this.txtFile.Name = "txtFile";
|
||||
this.txtFile.Size = new System.Drawing.Size(360, 23);
|
||||
// btnBrowse
|
||||
this.btnBrowse.Location = new System.Drawing.Point(378, 28);
|
||||
this.btnBrowse.Name = "btnBrowse";
|
||||
this.btnBrowse.Size = new System.Drawing.Size(75, 25);
|
||||
this.btnBrowse.Text = "浏览";
|
||||
this.btnBrowse.UseVisualStyleBackColor = true;
|
||||
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
|
||||
// txtUpload
|
||||
this.txtUpload.Location = new System.Drawing.Point(12, 80);
|
||||
this.txtUpload.Name = "txtUpload";
|
||||
this.txtUpload.Size = new System.Drawing.Size(441, 23);
|
||||
// txtDownload
|
||||
this.txtDownload.Location = new System.Drawing.Point(12, 130);
|
||||
this.txtDownload.Name = "txtDownload";
|
||||
this.txtDownload.Size = new System.Drawing.Size(441, 23);
|
||||
// labels
|
||||
this.lblFile.AutoSize = true;
|
||||
this.lblFile.Location = new System.Drawing.Point(12, 12);
|
||||
this.lblFile.Name = "lblFile";
|
||||
this.lblFile.Size = new System.Drawing.Size(59, 15);
|
||||
this.lblFile.Text = "目标文件";
|
||||
this.lblUpload.AutoSize = true;
|
||||
this.lblUpload.Location = new System.Drawing.Point(12, 62);
|
||||
this.lblUpload.Name = "lblUpload";
|
||||
this.lblUpload.Size = new System.Drawing.Size(83, 15);
|
||||
this.lblUpload.Text = "上传暗码(上传)";
|
||||
this.lblDownload.AutoSize = true;
|
||||
this.lblDownload.Location = new System.Drawing.Point(12, 112);
|
||||
this.lblDownload.Name = "lblDownload";
|
||||
this.lblDownload.Size = new System.Drawing.Size(83, 15);
|
||||
this.lblDownload.Text = "下载暗码(下载)";
|
||||
// btnWrite
|
||||
this.btnWrite.Location = new System.Drawing.Point(378, 170);
|
||||
this.btnWrite.Name = "btnWrite";
|
||||
this.btnWrite.Size = new System.Drawing.Size(75, 27);
|
||||
this.btnWrite.Text = "开始写入";
|
||||
this.btnWrite.UseVisualStyleBackColor = true;
|
||||
this.btnWrite.Click += new System.EventHandler(this.btnWrite_Click);
|
||||
// MainForm
|
||||
this.ClientSize = new System.Drawing.Size(465, 209);
|
||||
this.Controls.Add(this.txtFile);
|
||||
this.Controls.Add(this.btnBrowse);
|
||||
this.Controls.Add(this.txtUpload);
|
||||
this.Controls.Add(this.txtDownload);
|
||||
this.Controls.Add(this.lblFile);
|
||||
this.Controls.Add(this.lblUpload);
|
||||
this.Controls.Add(this.lblDownload);
|
||||
this.Controls.Add(this.btnWrite);
|
||||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
|
||||
this.MaximizeBox = false;
|
||||
this.Name = "MainForm";
|
||||
this.Text = "Writer - 写入暗码";
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
}
|
||||
|
||||
private System.Windows.Forms.TextBox txtFile;
|
||||
private System.Windows.Forms.Button btnBrowse;
|
||||
private System.Windows.Forms.TextBox txtUpload;
|
||||
private System.Windows.Forms.TextBox txtDownload;
|
||||
private System.Windows.Forms.Label lblFile;
|
||||
private System.Windows.Forms.Label lblUpload;
|
||||
private System.Windows.Forms.Label lblDownload;
|
||||
private System.Windows.Forms.Button btnWrite;
|
||||
}
|
||||
}
|
||||
72
client/Writer/MainForm.cs
Normal file
72
client/Writer/MainForm.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Writer
|
||||
{
|
||||
public partial class MainForm : Form
|
||||
{
|
||||
private static readonly HttpClient http = new HttpClient();
|
||||
private const string ServerUrl = "http://localhost:5000";
|
||||
|
||||
public MainForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void btnBrowse_Click(object sender, EventArgs e)
|
||||
{
|
||||
using var ofd = new OpenFileDialog();
|
||||
if (ofd.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
txtFile.Text = ofd.FileName;
|
||||
}
|
||||
}
|
||||
|
||||
private async void btnWrite_Click(object sender, EventArgs e)
|
||||
{
|
||||
var file = txtFile.Text;
|
||||
if (string.IsNullOrWhiteSpace(file) || !File.Exists(file))
|
||||
{
|
||||
MessageBox.Show("请选择有效的文件。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
var upload = txtUpload.Text ?? string.Empty;
|
||||
var download = txtDownload.Text ?? string.Empty;
|
||||
try
|
||||
{
|
||||
var sha = await Task.Run(() => ComputeSha256(file));
|
||||
var payload = new { sha = sha, upload_code = upload, download_code = download };
|
||||
var content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
|
||||
var resp = await http.PostAsync(ServerUrl + "/write", content);
|
||||
if (resp.IsSuccessStatusCode)
|
||||
{
|
||||
MessageBox.Show("写入暗码成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("写入失败: " + await resp.Content.ReadAsStringAsync(), "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("发生错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private static string ComputeSha256(string filePath)
|
||||
{
|
||||
using var sha = SHA256.Create();
|
||||
using var stream = File.OpenRead(filePath);
|
||||
var hash = sha.ComputeHash(stream);
|
||||
var sb = new StringBuilder();
|
||||
foreach (var b in hash) sb.Append(b.ToString("x2"));
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
17
client/Writer/Program.cs
Normal file
17
client/Writer/Program.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Writer
|
||||
{
|
||||
static class Program
|
||||
{
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
Application.SetHighDpiMode(HighDpiMode.SystemAware);
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new MainForm());
|
||||
}
|
||||
}
|
||||
}
|
||||
9
client/Writer/Writer.csproj
Normal file
9
client/Writer/Writer.csproj
Normal file
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user