-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (100 loc) · 4.93 KB
/
Copy pathProgram.cs
File metadata and controls
115 lines (100 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Security.Cryptography;
using ApacheMinaSSHD.NET.Wrapper;
using ApacheMinaSSHD.NET.Wrapper.Factories;
using Renci.SshNet;
using Renci.SshNet.Sftp;
var rootPath = Path.Combine(AppContext.BaseDirectory, "sftp-root");
var hostKeyPath = Path.Combine(AppContext.BaseDirectory, "hostkey.ser");
Directory.CreateDirectory(rootPath);
// ── Start server ──────────────────────────────────────────
var server = AMNetSshServer.SetUpDefaultServer();
server.Host = "127.0.0.1";
server.Port = 0; // OS-assigned port
server.Config.ApplyProductionDefaults();
server.Config.ApplyModernAlgorithmDefaults();
server.SetFixedPasswordAuthenticator("demo", "demo");
server.setKeyPairProvider(new AMNetSimpleGeneratorHostKeyProvider(hostKeyPath));
server.setFileSystemFactory(new AMNetVirtualFileSystemFactory(rootPath));
server.setSubsystemFactories(new AMNetSftpSubsystemFactory());
server.setCommandFactory(new AMNetScpCommandFactory());
server.Start();
var sshPort = server.Port;
Console.WriteLine($"SFTP/SCP server listening on 127.0.0.1:{sshPort}");
Console.WriteLine();
// ── Create a test file for uploads ────────────────────────
var uploadDir = Directory.CreateDirectory(Path.Combine(rootPath, "upload-test"));
var localTemp = Path.Combine(Path.GetTempPath(), $"sftp-test-{Guid.NewGuid()}.bin");
var original = RandomNumberGenerator.GetBytes(16 * 1024);
File.WriteAllBytes(localTemp, original);
Console.WriteLine($"Test file: {localTemp} ({original.Length} bytes, SHA256={Convert.ToHexString(SHA256.HashData(original))})");
Console.WriteLine();
// ══════════════════════════════════════════════════════════
// SFTP CLIENT DEMO
// ══════════════════════════════════════════════════════════
Console.WriteLine("═══ SFTP Client ═══");
using (var sftp = new SftpClient("127.0.0.1", sshPort, "demo", "demo"))
{
sftp.Connect();
Console.WriteLine($"Connected via SFTP (protocol v{sftp.ProtocolVersion})");
// List root directory
var entries = sftp.ListDirectory("/").ToList();
Console.WriteLine($"Root contains {entries.Count} entries:");
foreach (var e in entries.OrderBy(e => e.Name))
{
Console.WriteLine($" {(e.IsDirectory ? "DIR" : "FILE")} {e.Name} {e.Length} B");
}
// Upload file
var remotePath = $"/upload-test/sample-{Guid.NewGuid()}.bin";
await using (var fs = File.OpenRead(localTemp))
{
sftp.UploadFile(fs, remotePath);
}
var stat = sftp.Get(remotePath);
Console.WriteLine($"Uploaded via SFTP: {remotePath} ({stat.Length} bytes)");
// Download and verify
var downloadTarget = Path.Combine(Path.GetTempPath(), $"sftp-download-{Guid.NewGuid()}.bin");
await using (var fs = File.Create(downloadTarget))
{
sftp.DownloadFile(remotePath, fs);
}
var downloaded = File.ReadAllBytes(downloadTarget);
var match = CryptographicOperations.FixedTimeEquals(original, downloaded);
Console.WriteLine($"Downloaded and verified: {match}");
// Clean up downloaded copy
File.Delete(downloadTarget);
sftp.Disconnect();
}
Console.WriteLine();
// ══════════════════════════════════════════════════════════
// SCP CLIENT DEMO
// ══════════════════════════════════════════════════════════
Console.WriteLine("═══ SCP Client ═══");
using (var scp = new ScpClient("127.0.0.1", sshPort, "demo", "demo"))
{
scp.Connect();
Console.WriteLine("Connected via SCP");
// Upload file via SCP
var scpRemotePath = $"/upload-test/scp-sample-{Guid.NewGuid()}.bin";
using (var fs = File.OpenRead(localTemp))
{
scp.Upload(fs, scpRemotePath);
}
Console.WriteLine($"Uploaded via SCP: {scpRemotePath}");
// Download and verify
var scpDownload = Path.Combine(Path.GetTempPath(), $"scp-download-{Guid.NewGuid()}.bin");
using (var fs = File.Create(scpDownload))
{
scp.Download(scpRemotePath, fs);
}
var scpData = File.ReadAllBytes(scpDownload);
var scpMatch = CryptographicOperations.FixedTimeEquals(original, scpData);
Console.WriteLine($"Downloaded via SCP and verified: {scpMatch}");
File.Delete(scpDownload);
scp.Disconnect();
}
Console.WriteLine();
// ── Cleanup ───────────────────────────────────────────────
File.Delete(localTemp);
Directory.Delete(uploadDir.FullName, recursive: true);
server.Stop();
Console.WriteLine("Done.");