From b23aa91a93a70853710d518640d8d9517c2ceae7 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Fri, 23 Dec 2022 20:20:29 +0100 Subject: [PATCH] Replaced FTP dll with FTPClient class --- PCK-Studio/Classes/Misc/FTPClient.cs | 283 ++++++++++++++++++++++ PCK-Studio/Forms/Utilities/installPS3.cs | 8 +- PCK-Studio/Forms/Utilities/installVita.cs | 8 +- PCK-Studio/Forms/Utilities/installWiiU.cs | 15 +- PCK-Studio/PckStudio.csproj | 4 +- filetransferprotocollib.dll | Bin 7680 -> 0 bytes 6 files changed, 300 insertions(+), 18 deletions(-) create mode 100644 PCK-Studio/Classes/Misc/FTPClient.cs delete mode 100644 filetransferprotocollib.dll diff --git a/PCK-Studio/Classes/Misc/FTPClient.cs b/PCK-Studio/Classes/Misc/FTPClient.cs new file mode 100644 index 00000000..099f9029 --- /dev/null +++ b/PCK-Studio/Classes/Misc/FTPClient.cs @@ -0,0 +1,283 @@ +using System; +using System.IO; +using System.Net; + +namespace PckStudio.Classes.Misc +{ + public class FTPClient : IDisposable + { + private const int bufferSize = 2048; + + private Uri hostUri; + private NetworkCredential credentials; + + private FtpWebRequest request = null; + private FtpWebResponse response = null; + private Stream _stream = null; + + + public FTPClient(string host, string username) + : this(new Uri(host), username) + { + } + + public FTPClient(Uri uri, string username) + : this(uri, username, string.Empty) + { + } + + public FTPClient(string host, string username, string password) + : this(new Uri(host), username, password) + { + } + + public FTPClient(Uri uri, string username, string password) + { + hostUri = uri; + credentials = new NetworkCredential(username, password); + } + + /// + /// Creates a new FTP Request + /// + /// + /// + /// See + /// + public static FtpWebRequest CreateFTPWebRequest(Uri uri, ICredentials credentials, string method) + { + FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); + request.Credentials = credentials; + request.Method = method; + return request; + } + + public void DownloadFile(string remoteFilepath, string localFile) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, remoteFilepath), credentials, WebRequestMethods.Ftp.DownloadFile); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + remoteFile); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.DownloadFile; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + response = (FtpWebResponse)request.GetResponse(); + _stream = response.GetResponseStream(); + FileStream fileStream = new FileStream(localFile, FileMode.OpenOrCreate); + byte[] buffer = new byte[Convert.ToInt32(GetFileSize(remoteFilepath))]; + int num = _stream.Read(buffer, 0, Convert.ToInt32(GetFileSize(remoteFilepath))); + try + { + while (num > 0) + { + fileStream.Write(buffer, 0, num); + num = _stream.Read(buffer, 0, Convert.ToInt32(GetFileSize(remoteFilepath))); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + + fileStream.Close(); + _stream.Close(); + response.Close(); + request = null; + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public void UploadFile(string localFile, string remoteFile) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, remoteFile), credentials, WebRequestMethods.Ftp.UploadFile); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + remoteFile); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.UploadFile; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + _stream = request.GetRequestStream(); + FileStream fileStream = new FileStream(localFile, FileMode.Open); + byte[] buffer = new byte[fileStream.Length]; + int num = fileStream.Read(buffer, 0, (int)fileStream.Length); + try + { + while (num != 0) + { + _stream.Write(buffer, 0, num); + num = fileStream.Read(buffer, 0, (int)fileStream.Length); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + + fileStream.Close(); + _stream.Close(); + request = null; + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public string[] ListDirectory(string directory) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, directory), credentials, WebRequestMethods.Ftp.ListDirectory); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + directory); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.ListDirectory; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + response = (FtpWebResponse)request.GetResponse(); + _stream = response.GetResponseStream(); + StreamReader streamReader = new StreamReader(_stream); + string text = string.Empty; + try + { + while (streamReader.Peek() != -1) + { + text += streamReader.ReadLine() + "|"; + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + + streamReader.Close(); + _stream.Close(); + response.Close(); + request = null; + + try + { + return text.Split("|".ToCharArray()); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + return Array.Empty(); + } + + public void Rename(string name, string newName) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, name), credentials, WebRequestMethods.Ftp.Rename); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + name); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.Rename; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + request.RenameTo = newName; + response = (FtpWebResponse)request.GetResponse(); + response.Close(); + request = null; + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public void DeleteFile(string filename) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, filename), credentials, WebRequestMethods.Ftp.DeleteFile); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + filename); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.DeleteFile; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + response = (FtpWebResponse)request.GetResponse(); + response.Close(); + + request = null; + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public void CreateDirectory(string name) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, name), credentials, WebRequestMethods.Ftp.MakeDirectory); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + name); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.MakeDirectory; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + response = (FtpWebResponse)request.GetResponse(); + response.Close(); + + request = null; + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public long GetFileSize(string filepath) + { + FtpWebRequest ftpWebRequest = CreateFTPWebRequest(new Uri(hostUri, filepath), credentials, WebRequestMethods.Ftp.GetFileSize); + //FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(host + "/" + fileName); + //ftpWebRequest.Credentials = credentials; + //ftpWebRequest.Method = WebRequestMethods.Ftp.GetFileSize; + + ftpWebRequest.UseBinary = true; + + FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse(); + long contentLength = response.ContentLength; + response.Close(); + return contentLength; + } + + public void Dispose() + { + _stream.Dispose(); + response.Dispose(); + request = null; + response = null; + _stream = null; + } + } +} \ No newline at end of file diff --git a/PCK-Studio/Forms/Utilities/installPS3.cs b/PCK-Studio/Forms/Utilities/installPS3.cs index 6b45c461..e8d306d5 100644 --- a/PCK-Studio/Forms/Utilities/installPS3.cs +++ b/PCK-Studio/Forms/Utilities/installPS3.cs @@ -1,5 +1,4 @@ -using FileTransferProtocolLib; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; @@ -7,6 +6,7 @@ using System.IO; using System.Net; using System.Windows.Forms; using MetroFramework.Forms; +using PckStudio.Classes.Misc; namespace PckStudio.Forms { @@ -292,7 +292,7 @@ namespace PckStudio.Forms if (openPCK.ShowDialog() == DialogResult.OK) { - FTP client = new FTP("ftp://" + textBoxHost.Text, "", ""); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } @@ -341,7 +341,7 @@ namespace PckStudio.Forms if (listViewPCKS.SelectedItems.Count != 0) { buttonMode("loading"); - FTP client = new FTP("ftp://" + textBoxHost.Text, "", ""); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } diff --git a/PCK-Studio/Forms/Utilities/installVita.cs b/PCK-Studio/Forms/Utilities/installVita.cs index 1dc0502b..28c90622 100644 --- a/PCK-Studio/Forms/Utilities/installVita.cs +++ b/PCK-Studio/Forms/Utilities/installVita.cs @@ -1,5 +1,4 @@ -using FileTransferProtocolLib; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; @@ -7,6 +6,7 @@ using System.IO; using System.Net; using System.Windows.Forms; using MetroFramework.Forms; +using PckStudio.Classes.Misc; namespace PckStudio.Forms { @@ -292,7 +292,7 @@ namespace PckStudio.Forms if (openPCK.ShowDialog() == DialogResult.OK) { - FTP client = new FTP("ftp://" + textBoxHost.Text, "", ""); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } @@ -341,7 +341,7 @@ namespace PckStudio.Forms if (listViewPCKS.SelectedItems.Count != 0) { buttonMode("loading"); - FTP client = new FTP("ftp://" + textBoxHost.Text, "", ""); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } diff --git a/PCK-Studio/Forms/Utilities/installWiiU.cs b/PCK-Studio/Forms/Utilities/installWiiU.cs index c64e91c1..7e02d173 100644 --- a/PCK-Studio/Forms/Utilities/installWiiU.cs +++ b/PCK-Studio/Forms/Utilities/installWiiU.cs @@ -1,5 +1,4 @@ -using FileTransferProtocolLib; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; @@ -7,10 +6,12 @@ using System.IO; using System.IO.Compression; using System.Net; using System.Windows.Forms; +using System.Diagnostics; + using PckStudio.Classes.FileTypes; using PckStudio.Classes.IO.PCK; using PckStudio.Classes.IO.ARC; -using System.Diagnostics; +using PckStudio.Classes.Misc; namespace PckStudio.Forms { @@ -346,7 +347,7 @@ namespace PckStudio.Forms if (openPCK.ShowDialog() == DialogResult.OK) { - FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443"); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); if(TextBoxPackImage.Text != "") { @@ -411,7 +412,7 @@ namespace PckStudio.Forms if (listViewPCKS.SelectedItems.Count != 0) { buttonMode("loading"); - FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443"); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); if (TextBoxPackImage.Text != "") { @@ -436,7 +437,7 @@ namespace PckStudio.Forms private void GetARCFromConsole() { - FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443"); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); client.DownloadFile(dlcPath + "../../Common/Media/MediaWiiU.arc", Program.AppData + "MediaWiiU.arc"); archive = ARCFileReader.Read(new MemoryStream(File.ReadAllBytes(Program.AppData + "MediaWiiU.arc"))); } @@ -451,7 +452,7 @@ namespace PckStudio.Forms private void SendARCToConsole() { - FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443"); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); MemoryStream ms = new MemoryStream(); ARCFileWriter.Write(ms, archive); File.WriteAllBytes(Program.AppData + "MediaWiiU.arc", ms.ToArray()); diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index 541d1abe..cd8eec19 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -105,9 +105,6 @@ Properties\app.manifest - - ..\filetransferprotocollib.dll - @@ -161,6 +158,7 @@ + diff --git a/filetransferprotocollib.dll b/filetransferprotocollib.dll deleted file mode 100644 index c2d9a867870af748d87a274b7ad0e7a7e2137074..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 7680 zcmeHMeQaCR6+h2@b{wZMb&{r(68b_4q%CoiCQ!mUmNZG*ET1H9x}vV>vwh<{uzlV4 zp4*1fEY$=8Bm_v?KQIkCHcc>%jR`c^m$v+BBw3Lax-@$WKH%o_F3MdKNvm9tGbWEMPdY_Yj@pKrC;^h}MB;K<@J8WA8>AMDU>8LZAld=#KNT8X|~ zL!>G0dk25>HJpKoI=X<@A%VKj;9#7oVzf0%6h?9uu!OEJz)n4|;0L7bSZ;HBfZ`az zKgjv!S;z*+`&;WUueBb)#T?CBhU)aOj<62PO_SmQb zS09W98*NyV|5B8NHZB!Xd#GNAj;PLOPVX=P+PCmP6%Q}TS0nO*lY`pOqux}$&`~v> zLGUKQC0ck6wc76I2w&9 z&SAwls_>aIMmk4B__l%}y&L>SeJ5a>evDqGZe7r? zA%83Vk<$8cY9?1d0XV16L-S|!yXb9tK))B9hx9?nGklJoz^rEauKqApYv<_^ifR`i z8P)!ve+7_bo}xeNj|0A?KZ$Hygg@O%?h?CxI^(f=_2?a zQ=B0MC#g}3QIdK9Z=(@FhdvK@n!X75S^BDWKRrXw(+i|8lqwJ@RgK>~yz@#;EBc$J+h)$05%#z(U73`bVJi_!WiqrcKEBpCWu2t7Ggg8o=NwmL zX)JN8NV+sD9hVA@uybZsP~LQ$lhRHVg<_+^rG0MxkVs63;{^fH88=@-O9M_`<{aUT z6mf5j*x(r9EE8sy2a8yuFaygct=k2~lCHF=U!Kfmq?uyJ=wQB#GVP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