20250202_1 jhlim
This commit is contained in:
@@ -86,12 +86,25 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="BouncyCastle.Cryptography">
|
||||
<HintPath>..\..\SftpTest\WindowsFormsApp1\bin\Debug\BouncyCastle.Cryptography.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="FluentFTP">
|
||||
<HintPath>..\DDUtilityApp\bin\Debug\FluentFTP.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="JWH.SECS">
|
||||
<HintPath>..\Library\JWH.SECS.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="log4net, Version=2.0.12.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\log4net.2.0.12\lib\net45\log4net.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\SftpTest\WindowsFormsApp1\bin\Debug\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Renci.SshNet">
|
||||
<HintPath>..\..\SftpTest\packages\SSH.NET.2024.2.0\lib\net462\Renci.SshNet.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
@@ -129,7 +142,9 @@
|
||||
<Compile Include="EXTENSIONS\ExtensionXml.cs" />
|
||||
<Compile Include="EXTENSIONS\XLogger.cs" />
|
||||
<Compile Include="NETWORK\AsyncSocket.cs" />
|
||||
<Compile Include="NETWORK\FtpsClient.cs" />
|
||||
<Compile Include="NETWORK\FtpClient.cs" />
|
||||
<Compile Include="NETWORK\SFtpClient.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TIB\ListenerData.cs" />
|
||||
<Compile Include="TIB\ListenerInfo.cs" />
|
||||
|
||||
146
JWH/NETWORK/FtpsClient.cs
Normal file
146
JWH/NETWORK/FtpsClient.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using FluentFTP;
|
||||
|
||||
namespace JWH.NETWORK
|
||||
{
|
||||
public static class FtpsClient
|
||||
{
|
||||
private static FluentFTP.FtpClient _ftpClient;
|
||||
private static string _host;
|
||||
private static int _port = 990; // FTPS 기본 포트
|
||||
private static string _userId;
|
||||
private static string _password;
|
||||
|
||||
public static void Initialize(string host, string userId, string password, int port = 990)
|
||||
{
|
||||
_host = host;
|
||||
_userId = userId;
|
||||
_password = password;
|
||||
_port = port;
|
||||
|
||||
_ftpClient = new FluentFTP.FtpClient(_host, _port, new NetworkCredential(_userId, _password))
|
||||
{
|
||||
//EncryptionMode = FtpEncryptionMode.Explicit,
|
||||
EncryptionMode = FtpEncryptionMode.Implicit,
|
||||
SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
|
||||
ConnectTimeout = 500 // 타임아웃 설정 (0.5초)
|
||||
};
|
||||
_ftpClient.ValidateCertificate += (control, args) => args.Accept = true; // 인증서 검증 비활성화 (운영 확인 필요!)
|
||||
}
|
||||
|
||||
public static void Connect()
|
||||
{
|
||||
if (_ftpClient == null)
|
||||
{
|
||||
throw new InvalidOperationException("FTPS client is not initialized. Call Initialize() first.");
|
||||
}
|
||||
|
||||
if (!_ftpClient.IsConnected)
|
||||
{
|
||||
_ftpClient.Connect();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Disconnect()
|
||||
{
|
||||
if (_ftpClient != null && _ftpClient.IsConnected)
|
||||
{
|
||||
_ftpClient.Disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsFileExists(string path)
|
||||
{
|
||||
Connect();
|
||||
return _ftpClient.FileExists(path);
|
||||
}
|
||||
|
||||
public static bool IsDirExists(string path)
|
||||
{
|
||||
Connect();
|
||||
return _ftpClient.DirectoryExists(path);
|
||||
}
|
||||
|
||||
public static FtpListItem[] GetFtpsList(string path)
|
||||
{
|
||||
Connect();
|
||||
return _ftpClient.GetListing(path);
|
||||
}
|
||||
|
||||
public static object GetObjectInfo(string filePath)
|
||||
{
|
||||
Connect();
|
||||
return _ftpClient.GetObjectInfo(filePath);
|
||||
}
|
||||
|
||||
public static FtpListItem GetFileInfo(string remoteFilePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
FtpsClient.Connect();
|
||||
|
||||
if (!_ftpClient.IsConnected)
|
||||
{
|
||||
Console.WriteLine("FTPS 서버에 연결되지 않았습니다.");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Passive Mode 설정 (필요한 경우)
|
||||
// _ftpClient.SetPassiveMode(true);
|
||||
|
||||
if (!FtpsClient.IsFileExists(remoteFilePath))
|
||||
{
|
||||
Console.WriteLine("파일이 존재하지 않습니다.");
|
||||
return null;
|
||||
}
|
||||
|
||||
var fileInfo = FtpsClient.GetObjectInfo(remoteFilePath) ;
|
||||
if (fileInfo == null)
|
||||
{
|
||||
Console.WriteLine("파일 정보를 가져오는 데 실패했습니다.");
|
||||
}
|
||||
return (FtpListItem)fileInfo;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"파일 정보 조회 오류: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
FtpsClient.Disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool DownloadFtpsFile(string remotePath, string localPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
Connect();
|
||||
_ftpClient.DownloadFile(localPath, remotePath);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Download error: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool UploadFtpsFile(string localPath, string remotePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
Connect();
|
||||
_ftpClient.UploadFile(localPath, remotePath);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Upload error: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
121
JWH/NETWORK/SFtpClient.cs
Normal file
121
JWH/NETWORK/SFtpClient.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using Renci.SshNet;
|
||||
using System;
|
||||
using File = System.IO.File;
|
||||
|
||||
namespace JWH.NETWORK
|
||||
{
|
||||
public static class StpClientWrap
|
||||
{
|
||||
private static SftpClient _sftpClient;
|
||||
private static string _host;
|
||||
private static int _port = 22;
|
||||
private static string _userId;
|
||||
private static string _password;
|
||||
|
||||
public static void Initialize(string host, string userId, string password, int port = 22)
|
||||
{
|
||||
_host = host;
|
||||
_userId = userId;
|
||||
_password = password;
|
||||
_port = port;
|
||||
_sftpClient = new SftpClient(_host, _port, _userId, _password);
|
||||
}
|
||||
|
||||
public static void Connect()
|
||||
{
|
||||
if (_sftpClient == null)
|
||||
{
|
||||
throw new InvalidOperationException("SFTP client is not initialized. Call Initialize() first.");
|
||||
}
|
||||
|
||||
if (!_sftpClient.IsConnected)
|
||||
{
|
||||
_sftpClient.Connect();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Disconnect()
|
||||
{
|
||||
if (_sftpClient != null && _sftpClient.IsConnected)
|
||||
{
|
||||
_sftpClient.Disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsDirExists(string path)
|
||||
{
|
||||
return IsFileExists(path) && _sftpClient.GetAttributes(path).IsDirectory;
|
||||
}
|
||||
|
||||
|
||||
public static bool IsFileExists(string path)
|
||||
{
|
||||
Connect();
|
||||
return _sftpClient.Exists(path);
|
||||
}
|
||||
|
||||
public static Renci.SshNet.Sftp.SftpFileAttributes GetFileInfo(string remoteFilePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
Connect();
|
||||
if (_sftpClient.Exists(remoteFilePath))
|
||||
{
|
||||
return _sftpClient.GetAttributes(remoteFilePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("파일이 존재하지 않습니다.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"파일 정보 조회 오류: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static object GetSftpList(string path)
|
||||
{
|
||||
Connect();
|
||||
return _sftpClient.ListDirectory(path);
|
||||
}
|
||||
|
||||
public static bool DownloadSftpFile(string remotePath, string localPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
Connect();
|
||||
using (var fileStream = File.OpenWrite(localPath))
|
||||
{
|
||||
_sftpClient.DownloadFile(remotePath, fileStream);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Download error: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool UploadSftpFile(string localPath, string remotePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
Connect();
|
||||
using (var fileStream = File.OpenRead(localPath))
|
||||
{
|
||||
_sftpClient.UploadFile(fileStream, remotePath);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Upload error: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user