diff --git a/tytd-setup/Program.cs b/tytd-setup/Program.cs new file mode 100644 index 0000000..5a77b52 --- /dev/null +++ b/tytd-setup/Program.cs @@ -0,0 +1,304 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; +using ICSharpCode.SharpZipLib.Core; +using ICSharpCode.SharpZipLib.Zip; + +namespace tytdsetup +{ + class MainClass + { + public static void Main(string[] args) + { + bool isLinux + + = Environment.OSVersion.Platform == PlatformID.Unix; + + begin: + Console.Clear(); + Console.WriteLine("TYTD Setup"); + Console.WriteLine(); + string locationToStoreItIn = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"TYTDServer"); + if (isLinux) + { + Console.WriteLine("Pick a path without spaces"); + } + Console.WriteLine("If you leave it empty it will be stored in \"" + locationToStoreItIn + "\""); + Console.Write("Location: "); + { + string location = Console.ReadLine(); + if (!string.IsNullOrWhiteSpace(location)) + { + locationToStoreItIn = location; + } + } + Console.WriteLine(); + string workingDirForApp = "working"; + Console.WriteLine("This directory is for downloading into: "); + Console.WriteLine("If you leave it empty the name will be \"" + workingDirForApp + "\""); + Console.Write("Working Directory Name: "); + { + string location = Console.ReadLine(); + if (!string.IsNullOrWhiteSpace(location) && location != "tytd-web-master") + { + workingDirForApp = location; + } + } + Console.WriteLine(); + string binaryName= "server"; + Console.WriteLine("If you leave it empty the name will be \"" + binaryName + "\""); + Console.Write("Server Binaries Directory Name: "); + { + string location = Console.ReadLine(); + if (!string.IsNullOrWhiteSpace(location) && location != "tytd-server-master") + { + binaryName= location; + } + } + Console.WriteLine(); + + Console.WriteLine("If you leave it empty the port number will be 3250"); + Console.Write("Port Number: "); + + string portStr= Console.ReadLine(); + System.Net.ServicePointManager.ServerCertificateValidationCallback += certificateValidationCallback; + int portNumber; + + if(!int.TryParse(portStr,out portNumber)) + { + portNumber = 3250; + } + Console.WriteLine(); + + Console.Write("Do you want to keep zip files (Y/n)> "); + + string read = Console.ReadLine(); + + bool keepZipFiles = read != "N" && read != "n" && read != "No" && read != "no" && read != "nO" && read != "NO"; + + Console.WriteLine(); + Console.WriteLine("Your configuration is: "); + Console.WriteLine("Directory For Everything: " + locationToStoreItIn); + Console.WriteLine("Server Binaries Directory Name: " + binaryName); + Console.WriteLine("Working Directory Name: " + workingDirForApp); + + Console.WriteLine("Port Number: " + portNumber.ToString()); + Console.WriteLine("Keep Zip Files: " + (keepZipFiles ? "Yes" : "No")); + Console.WriteLine("Is This configuration what you want:"); + Console.WriteLine("(y)es"); + Console.WriteLine("(n)o"); + Console.WriteLine("(c)ancel install"); + retype: Console.Write("> "); + + string resp = Console.ReadLine(); + if (string.IsNullOrWhiteSpace(resp)) + { + goto retype; + } + resp = resp.ToLower().Replace(" ", ""); + switch (resp) + { + case "y": + case "yes": + break; + case "n": + case "no": + goto begin; + case "c": + case "cancelinstall": + return; + default: + Console.WriteLine("You must select one"); + + goto retype; + } + + using (var wc = new System.Net.WebClient()) + { + string name = "tytd-server-master.zip"; + wc.DownloadProgressChanged += (sender, e) => + { + string progressStr = "\r"+(name + " " + e.ProgressPercentage + "% ").PadRight(27); + Console.Write(progressStr); + }; + if (!File.Exists("tytd-server-master.zip")) + { + wc.DownloadFile("https://gitlab.tesses.cf/tesses50/tytd-server/-/archive/master/tytd-server-master.zip", "tytd-server-master.zip"); + } + name = "tytd-web-master.zip"; + if (!File.Exists("tytd-web-master.zip")) { + wc.DownloadFile("https://gitlab.tesses.cf/tesses50/tytd-web/-/archive/master/tytd-web-master.zip", "tytd-web-master.zip"); + } + name = "tytd-config.zip"; + if (!File.Exists("tytd-config.zip")) { + wc.DownloadFile("https://downloads.tesses.cf/apps/tytd/tytd-config.zip", "tytd-config.zip"); + } + Console.WriteLine(); + } + Directory.CreateDirectory(Path.Combine(locationToStoreItIn, workingDirForApp)); + + ExtractTo("tytd-web-master.zip", locationToStoreItIn); + ExtractTo("tytd-server-master.zip", locationToStoreItIn); + + ExtractTo("tytd-config.zip", Path.Combine(locationToStoreItIn, workingDirForApp)); + /* + ls TYTDServer/ + working + server + */ + + Directory.Move(Path.Combine(locationToStoreItIn,"tytd-server-master", "bin", "Release"),Path.Combine(locationToStoreItIn,binaryName)); + Directory.Move(Path.Combine(locationToStoreItIn,"tytd-web-master"), Path.Combine(locationToStoreItIn,workingDirForApp,"WebSite")); + Directory.Delete(Path.Combine(locationToStoreItIn,"tytd-server-master"),true); + if(!keepZipFiles) + { + File.Delete( "tytd-server-master.zip"); + File.Delete("tytd-server-master.zip"); + } + + if (isLinux) + { + string runPath = Path.Combine(locationToStoreItIn, ".run"); + Directory.CreateDirectory(runPath); + Console.WriteLine("if you want this permanently in PATH put this in your ~/.bashrc"); + Console.WriteLine("export PATH=\""+runPath+":$PATH\""); + + string tytd_server = Path.Combine(runPath, "tytd-server"); + File.WriteAllText(tytd_server, "cd \"" + Path.Combine(locationToStoreItIn,workingDirForApp) + "\"\nmono \"" + Path.Combine(locationToStoreItIn,binaryName,"youtube-downloader.exe") + "\" http://+:" + portNumber.ToString() + "/\n",System.Text.Encoding.UTF8); + Process.Start("chmod","755 \"" + tytd_server +"\""); + + } else + { + string tytd_batch_desktop = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),"TYTD Server.bat"); + string tytd_batch_start = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "TYTD Server", "TYTD Server.bat"); + + string cmd = "@echo off\r\ncd \"" + Path.Combine(locationToStoreItIn, workingDirForApp) + "\"" + Path.Combine(locationToStoreItIn, binaryName, "youtube-downloader.exe") + "\" http://+:" + portNumber.ToString() + "/\r\n"; + + Directory.CreateDirectory(Path.GetDirectoryName(tytd_batch_start)); + File.WriteAllText(tytd_batch_desktop, cmd, System.Text.Encoding.ASCII); + File.WriteAllText(tytd_batch_start, cmd, System.Text.Encoding.ASCII); + } + + Console.WriteLine("Done"); + } + /// + /// Extracts the content from a .zip file inside an specific folder. + /// + /// + /// + /// + public static void ExtractZipContent(string FileZipPath, string password, string OutputFolder) + { + ZipFile file = null; + try + { + FileStream fs = File.OpenRead(FileZipPath); + file = new ZipFile(fs); + + if (!String.IsNullOrEmpty(password)) + { + // AES encrypted entries are handled automatically + file.Password = password; + } + + foreach (ZipEntry zipEntry in file) + { + if (!zipEntry.IsFile) + { + // Ignore directories + continue; + } + + String entryFileName = zipEntry.Name; + // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName); + // Optionally match entrynames against a selection list here to skip as desired. + // The unpacked length is available in the zipEntry.Size property. + + // 4K is optimum + byte[] buffer = new byte[4096]; + Stream zipStream = file.GetInputStream(zipEntry); + + // Manipulate the output filename here as desired. + String fullZipToPath = Path.Combine(OutputFolder, entryFileName); + string directoryName = Path.GetDirectoryName(fullZipToPath); + + if (directoryName.Length > 0) + { + Directory.CreateDirectory(directoryName); + } + + // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size + // of the file, but does not waste memory. + // The "using" will close the stream even if an exception occurs. + using (FileStream streamWriter = File.Create(fullZipToPath)) + { + StreamUtils.Copy(zipStream, streamWriter, buffer); + } + } + } + finally + { + if (file != null) + { + file.IsStreamOwner = true; // Makes close also shut the underlying stream + file.Close(); // Ensure we release resources + } + } + } + static void ExtractTo(string src,string rootDir) + { + ExtractZipContent(src, null, rootDir); + + } + static bool accept_cert=false; + private static bool certificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) + { + if(sslPolicyErrors == SslPolicyErrors.None) + { + return true; + } + if (accept_cert) + { + return true; + } + Console.WriteLine("Certificate failure for gitlab.tesses.cf"); + Console.WriteLine("Cert Hash: " + certificate.GetCertHashString()); + + Console.WriteLine("Type any of the following to dismiss:"); + Console.WriteLine("Trust: (y)es"); + Console.WriteLine("Trust every request: (a)lways"); + Console.WriteLine("Dont Trust (will cause Exception aka program will crash): (n)o"); + retype: Console.Write("> "); + + string resp = Console.ReadLine(); + if(string.IsNullOrWhiteSpace(resp)) + { + goto retype; + } + resp = resp.ToLower().Replace(" ", ""); + if (resp == "y" || resp == "yes") + { + return true; + }else if(resp == "n" || resp == "no" ) + { + return false; + } + else if(resp == "a" || resp == "always") + { + accept_cert = true; + return true; + } + else + { + Console.WriteLine("You must select one"); + + goto retype; + } + + + + } + } +}