Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 45.18 KB | None | 0 0
  1. /* external_controls by Flammrock, used for download all external controls here : http://batch-external-controls.site88.net/list.php  */
  2. /* Under MIT/X11 licence. */
  3.  
  4. /*
  5.    The MIT License (MIT)
  6.  
  7.    Copyright (c) 2016 Flammrock
  8.  
  9.    Permission is hereby granted, free of charge, to any person obtaining a copy
  10.    of this software and associated documentation files (the "Software"), to deal
  11.    in the Software without restriction, including without limitation the rights
  12.    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13.    copies of the Software, and to permit persons to whom the Software is
  14.    furnished to do so, subject to the following conditions:
  15.  
  16.    The above copyright notice and this permission notice shall be included in all
  17.    copies or substantial portions of the Software.
  18.  
  19.    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20.    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22.    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23.    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24.    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25.    SOFTWARE.
  26.  
  27. */
  28.  
  29. using System;
  30. using System.Net;
  31. using System.IO;
  32. using System.Diagnostics;
  33.  
  34. namespace gate
  35. {
  36.     class Program
  37.     {
  38.         static void Main(string[] args)
  39.         {
  40.             ConsoleColor oldFGcolor = Console.ForegroundColor;
  41.             String temp = Path.GetTempPath();
  42.  
  43.             string start_1_a = "\"";
  44.             string start_1_b = "GATE";
  45.             string start_1_c = "\" a été créé par ";
  46.             string start_1_d = "Flammrock";
  47.             string start_2_a = "Copyright (c) 2016 ";
  48.             string start_2_b = "Flammrock";
  49.  
  50.             string start_3 = "Création des portails en cours...";
  51.             string start_4 = "Veuillez patienter...\n";
  52.  
  53.             Console.SetCursorPosition(0, 2);
  54.             var y = 0;
  55.             while (y < Console.WindowWidth / 2) { Console.Write("_"); y++; }
  56.             Console.SetCursorPosition(((Console.WindowWidth / 2) - (start_1_a.Length + start_1_b.Length + start_1_c.Length + start_1_d.Length)), 3);
  57.             Console.ForegroundColor = oldFGcolor;
  58.             Console.Write(start_1_a);
  59.             Console.ForegroundColor = ConsoleColor.Red;
  60.             Console.Write(start_1_b);
  61.             Console.ForegroundColor = oldFGcolor;
  62.             Console.Write(start_1_c);
  63.             Console.ForegroundColor = ConsoleColor.Red;
  64.             Console.Write(start_1_d);
  65.             Console.SetCursorPosition(((Console.WindowWidth / 2) - (start_2_a.Length + start_2_b.Length)), 4);
  66.             Console.ForegroundColor = oldFGcolor;
  67.             Console.Write(start_2_a);
  68.             Console.ForegroundColor = ConsoleColor.Red;
  69.             Console.Write(start_2_b);
  70.             Console.ForegroundColor = oldFGcolor;
  71.  
  72.             Console.SetCursorPosition(((Console.WindowWidth / 2) - start_3.Length), 6);
  73.             Console.Write(start_3);
  74.             Console.SetCursorPosition(((Console.WindowWidth / 2) - start_4.Length)+1, 7);
  75.             Console.Write(start_4);
  76.            
  77.  
  78.             if (WebRequestTest())
  79.             {
  80.                 string start_5 = "Portails créés avec succès !\n";
  81.                 Console.SetCursorPosition(((Console.WindowWidth / 2) - start_5.Length), 8);
  82.                 Console.Write(start_5);
  83.                 y = 0;
  84.                 while (y < Console.WindowWidth / 2) { Console.Write("¯"); y++; }
  85.                 Console.Write("\n\n\n");
  86.  
  87.                 var i = 0;
  88.                 while (i < args.Length)
  89.                 {
  90.                     if (args[i] == "/hide")
  91.                     {
  92.                         Console.SetOut(TextWriter.Null);
  93.                         Console.SetError(TextWriter.Null);
  94.                     }
  95.                     i++;
  96.                 }
  97.                 if (args.Length <= 1)
  98.                 {
  99.                     WriteError("Erreur de syntaxe !");
  100.                     Environment.Exit(0);
  101.                 }
  102.                 else if (args[0] == "/start")
  103.                 {
  104.                     Process.Start(args[1]);
  105.                 }
  106.                 if (args[0] == "/?" || args[0] == "/help")
  107.                 {
  108.                     Console.ForegroundColor = ConsoleColor.Red;
  109.                     Console.Write("------- Aide Commande externe 'GATE' -------\n");
  110.                     Console.ForegroundColor = oldFGcolor;
  111.                     Console.Write("gate /? : ");
  112.                     Console.ForegroundColor = ConsoleColor.Blue;
  113.                     Console.Write("Affiche l'aide et la liste des commandes.\n");
  114.                     Console.ForegroundColor = oldFGcolor;
  115.                     Console.Write("gate /list : ");
  116.                     Console.ForegroundColor = ConsoleColor.Blue;
  117.                     Console.Write("Permet d'afficher la liste des commandes externes.\n");
  118.                     Console.ForegroundColor = oldFGcolor;
  119.                     Console.Write("gate /start : ");
  120.                     Console.ForegroundColor = ConsoleColor.Blue;
  121.                     Console.Write("Permet d'exécuter des fichiers.\n");
  122.                     Console.ForegroundColor = oldFGcolor;
  123.                     Console.Write("gate /get /? : ");
  124.                     Console.ForegroundColor = ConsoleColor.Blue;
  125.                     Console.Write("Affiche l'aide et la liste des commandes.\n");
  126.                     Console.ForegroundColor = oldFGcolor;
  127.                     Console.Write("gate /get <commande_externe> : ");
  128.                     Console.ForegroundColor = ConsoleColor.Blue;
  129.                     Console.Write("Permet de télécharger n'importe quel commande externe.\n");
  130.                     Console.Write("   Exemple : get SockeT_full\n");
  131.                     Console.ForegroundColor = oldFGcolor;
  132.                     Console.Write("gate /get /list : ");
  133.                     Console.ForegroundColor = ConsoleColor.Blue;
  134.                     Console.Write("Permet d'afficher la liste des commandes externes.\n");
  135.                     Console.ForegroundColor = oldFGcolor;
  136.                     Console.Write("gate /send /? : ");
  137.                     Console.ForegroundColor = ConsoleColor.Blue;
  138.                     Console.Write("Affiche l'aide et la liste des commandes.\n");
  139.                     Console.ForegroundColor = oldFGcolor;
  140.                     Console.Write("gate /send <commande_externe> : ");
  141.                     Console.ForegroundColor = ConsoleColor.Blue;
  142.                     Console.Write("Permet de téléverser n'importe quel commande externe.\n");
  143.                     Console.Write("   Exemple : send SockeT_full\n");
  144.                     Console.ForegroundColor = oldFGcolor;
  145.                     Console.Write("gate /send /list : ");
  146.                     Console.ForegroundColor = ConsoleColor.Blue;
  147.                     Console.Write("Permet d'afficher la liste des commandes externes.\n");
  148.                     Console.ForegroundColor = ConsoleColor.Red;
  149.                     Console.Write("------- 'GATE' a été créé par Flammrock -------\n");
  150.                     Console.ForegroundColor = oldFGcolor;
  151.                     Console.Write("\n");
  152.                     Environment.Exit(0);
  153.                 }
  154.                 else if (args[0] == "/list")
  155.                 {
  156.                     Console.Write("Connexion sur le serveur en cours...\n");
  157.                     list_file();
  158.                     Environment.Exit(0);
  159.                 }
  160.                 else if (args[0] == "/send")
  161.                 {
  162.                     if (args[1] == "/?")
  163.                     {
  164.                         Console.ForegroundColor = ConsoleColor.Red;
  165.                         Console.Write("------- Aide Commande externe 'Send' -------\n");
  166.                         Console.ForegroundColor = oldFGcolor;
  167.                         Console.Write("send /? : ");
  168.                         Console.ForegroundColor = ConsoleColor.Blue;
  169.                         Console.Write("Affiche l'aide et la liste des commandes.\n");
  170.                         Console.ForegroundColor = oldFGcolor;
  171.                         Console.Write("send <commande_externe> : ");
  172.                         Console.ForegroundColor = ConsoleColor.Blue;
  173.                         Console.Write("Permet de téléverser n'importe quel commande externe.\n");
  174.                         Console.Write("   Exemple : send SockeT_full\n");
  175.                         Console.ForegroundColor = oldFGcolor;
  176.                         Console.Write("send /list : ");
  177.                         Console.ForegroundColor = ConsoleColor.Blue;
  178.                         Console.Write("Permet d'afficher la liste des commandes externes.\n");
  179.                         Console.ForegroundColor = ConsoleColor.Red;
  180.                         Console.Write("------- 'Send' a été créé par Flammrock -------\n");
  181.                         Console.ForegroundColor = oldFGcolor;
  182.                         Console.Write("\n");
  183.                         Environment.Exit(0);
  184.                     }
  185.                     else if (args[1] == "/list")
  186.                     {
  187.                         Console.Write("Connexion sur le serveur en cours...\n");
  188.                         list_file();
  189.                         Environment.Exit(0);
  190.                     }
  191.                     else
  192.                     {
  193.                         if (args.Length > 2)
  194.                         {
  195.                             WriteError("Erreur de syntaxe !");
  196.                         }
  197.                         else
  198.                         {
  199.                             if (args[1] == "/upload")
  200.                             {
  201.                                 Console.Write("Connexion sur le serveur en cours...\n");
  202.                                 string add = "<html><head><title>Ajout d'une commande externe en cours !</title><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"><script>window.resizeTo(800,800);var largeur=screen.availWidth;var hauteur=screen.availHeight;var demiL=((largeur-800)/2); var demiH=((hauteur-800)/2); window.moveTo(demiL,demiH);</script></head><body><iframe src=\"http://batch-external-controls.site88.net/add_c.html\" style=\"position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;\"></body></html>";
  203.                                 StreamWriter addfile = new StreamWriter(temp + "add.hta");
  204.                                 addfile.WriteLine(add);
  205.                                 addfile.Close();
  206.                                 Process.Start(temp + "add.hta");
  207.                                 Console.Write("Veuillez remplir le formulaire !\n");
  208.  
  209.                             }
  210.                         }
  211.                     }
  212.                 }
  213.                 else if (args[0] == "/get")
  214.                 {
  215.                     if (args[1] == "/?")
  216.                     {
  217.                         Console.ForegroundColor = ConsoleColor.Red;
  218.                         Console.Write("------- Aide Commande externe 'Get' -------\n");
  219.                         Console.ForegroundColor = oldFGcolor;
  220.                         Console.Write("get /? : ");
  221.                         Console.ForegroundColor = ConsoleColor.Blue;
  222.                         Console.Write("Affiche l'aide et la liste des commandes.\n");
  223.                         Console.ForegroundColor = oldFGcolor;
  224.                         Console.Write("get <commande_externe> : ");
  225.                         Console.ForegroundColor = ConsoleColor.Blue;
  226.                         Console.Write("Permet de télécharger n'importe quel commande externe.\n");
  227.                         Console.Write("   Exemple : get SockeT_full\n");
  228.                         Console.ForegroundColor = oldFGcolor;
  229.                         Console.Write("get /list : ");
  230.                         Console.ForegroundColor = ConsoleColor.Blue;
  231.                         Console.Write("Permet d'afficher la liste des commandes externes.\n");
  232.                         Console.ForegroundColor = ConsoleColor.Red;
  233.                         Console.Write("------- 'Get' a été créé par Flammrock -------\n");
  234.                         Console.ForegroundColor = oldFGcolor;
  235.                         Console.Write("\n");
  236.                         Environment.Exit(0);
  237.                     }
  238.                     else if (args[1] == "/list")
  239.                     {
  240.                         Console.Write("Connexion sur le serveur en cours...\n");
  241.                         list_file();
  242.                         Environment.Exit(0);
  243.                     }
  244.                     else if (args[1] == "/version")
  245.                     {
  246.  
  247.                         String current_version_all = args[2];
  248.                         String[] current_version_split = current_version_all.Split(':');
  249.                         String current_version_command = current_version_split[0];
  250.  
  251.                         if (current_version_command == "-CurrentVersion")
  252.                         {
  253.                             try
  254.                             {
  255.                                 String current_version = current_version_split[1];
  256.                                 String external_command = args[3];
  257.                                 String external_command_MIN = args[3].ToLower();
  258.                                 String external_command_name = external_command.Remove(0, 1);
  259.                                 String external_command_name_MIN = external_command_MIN.Remove(0, 1);
  260.                                 String real_last_version_MIN = "0";
  261.                                 String real_current_version_MIN = "1";
  262.  
  263.                                 Console.Write("Recherche d'une mise à jour pour ");
  264.                                 Console.Write("\"");
  265.                                 Console.ForegroundColor = ConsoleColor.Blue;
  266.                                 Console.Write(external_command_name);
  267.                                 Console.ForegroundColor = oldFGcolor;
  268.                                 Console.Write("\"\n");
  269.                                 Console.WriteLine("Connexion au serveur..");
  270.  
  271.  
  272.                                 string _url = "http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + ".php";
  273.                                 if (CheckUrl(_url) == "OK")
  274.                                 {
  275.                                     Console.Write("\"");
  276.                                     Console.ForegroundColor = ConsoleColor.Blue;
  277.                                     Console.Write(external_command_name);
  278.                                     Console.ForegroundColor = oldFGcolor;
  279.                                     Console.Write("\" existe\n");
  280.  
  281.  
  282.                                     WebClient wc = new WebClient();
  283.  
  284.  
  285.                                     wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + "-ext" + ".php", temp + "ext.php");
  286.                                     wc.Dispose();
  287.                                     String file_ext = temp + "ext.php";
  288.                                     StreamReader file_ex = new StreamReader(file_ext);
  289.                                     String ext = file_ex.ReadLine();
  290.                                     file_ex.Close();
  291.  
  292.  
  293.  
  294.                                     wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + "-version" + ".php", temp + "version.php");
  295.                                     wc.Dispose();
  296.                                     String file_version = temp + "version.php";
  297.                                     StreamReader file = new StreamReader(file_version);
  298.                                     String last_version = file.ReadLine();
  299.                                     file.Close();
  300.  
  301.                                     real_current_version_MIN = current_version.ToLower();
  302.                                     String real_current_version = real_current_version_MIN.Trim(new Char[] { '"' });
  303.                                     real_last_version_MIN = last_version.ToLower();
  304.  
  305.                                     Console.WriteLine("\nVersion sur le serveur : ");
  306.                                     Console.ForegroundColor = ConsoleColor.Red;
  307.                                     Console.Write("{0}\n", real_last_version_MIN);
  308.                                     Console.ForegroundColor = oldFGcolor;
  309.                                     Console.WriteLine("Version sur votre PC : ");
  310.                                     Console.ForegroundColor = ConsoleColor.Red;
  311.                                     Console.WriteLine("{0}\n\n", real_current_version);
  312.                                     Console.ForegroundColor = oldFGcolor;
  313.  
  314.                                     if (real_last_version_MIN == real_current_version)
  315.                                     {
  316.                                         Console.WriteLine("Vous possédez la dernière version de {0}", external_command_name);
  317.                                         Environment.Exit(0);
  318.                                     }
  319.                                     else
  320.                                     {
  321.                                         Console.WriteLine("Vous ne possédez pas la dernière version de {0}", external_command_name);
  322.                                         String command_install = args[4].Substring(0, 8);
  323.                                         String command_download = args[4].Substring(0, 9);
  324.                                         if (command_install == "/install")
  325.                                         {
  326.                                             String[] name_install = args[4].Split(':');
  327.                                             String current_name_file = name_install[1];
  328.  
  329.                                             String new_line = "set \"version_" + external_command_name_MIN + "=" + real_last_version_MIN + "\"";
  330.                                             try
  331.                                             {
  332.                                                 Read_file(external_command_name_MIN, external_command_name, "0", "generate_ex");
  333.                                                 install_file(current_name_file, new_line, external_command_name_MIN);
  334.                                                 Environment.Exit(0);
  335.                                             }
  336.                                             catch
  337.                                             {
  338.                                                 WriteError("Erreur lors de l'installation !\n");
  339.                                                 Environment.Exit(0);
  340.                                             }
  341.                                         }
  342.                                         else if (command_download == "/download")
  343.                                         {
  344.                                             try
  345.                                             {
  346.                                                 WebClient try_wc = new WebClient();
  347.                                                 try_wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + ".ini", external_command_name_MIN + ext);
  348.                                                 try_wc.Dispose();
  349.                                                 Console.Write("Téléchargement terminé !\n");
  350.                                                 try
  351.                                                 {
  352.                                                     String[] name_download = args[4].Split(':');
  353.                                                     String path = name_download[1];
  354.  
  355.                                                     String path_copy = path + "-install";
  356.                                                     if (File.Exists(path_copy)) { File.Delete(path_copy); }
  357.                                                     File.Copy(path, path_copy);
  358.                                                     String final_file = null;
  359.                                                     StreamReader sr = new StreamReader(path_copy);
  360.                                                     String new_line = "set \"version_" + external_command_name_MIN + "=" + real_last_version_MIN + "\"";
  361.                                                     String line_read = null;
  362.                                                     String find_line = null;
  363.                                                     while (!sr.EndOfStream)
  364.                                                     {
  365.                                                         line_read = sr.ReadLine();
  366.                                                         if (line_read.Length >= 13 + external_command_name_MIN.Length)
  367.                                                         {
  368.                                                             find_line = line_read.Substring(0, 13 + external_command_name_MIN.Length);
  369.                                                         }
  370.                                                         if (find_line == "set \"version_" + external_command_name_MIN)
  371.                                                         {
  372.                                                             if (!sr.EndOfStream)
  373.                                                             {
  374.                                                                 final_file = (final_file + (new_line + "\r\n"));
  375.                                                             }
  376.                                                             else
  377.                                                             {
  378.                                                                 final_file = (final_file + (new_line));
  379.                                                             }
  380.                                                         }
  381.                                                         else
  382.                                                         {
  383.                                                             if (!sr.EndOfStream)
  384.                                                             {
  385.                                                                 final_file = (final_file + (line_read + "\r\n"));
  386.                                                             }
  387.                                                             else
  388.                                                             {
  389.                                                                 final_file = (final_file + (line_read));
  390.                                                             }
  391.                                                         }
  392.                                                     }
  393.                                                     sr.Close();
  394.                                                     StreamWriter sr2 = new StreamWriter(path_copy);
  395.                                                     sr2.WriteLine(final_file);
  396.                                                     sr2.Close();
  397.                                                     String lines = "@ping localhost -n 2 >nul \r\ndel \"" + path + "\"\r\nren \"" + path_copy + "\" \"" + path + "\"\r\ngate /start \"" + path + "\"\r\ndel GATE_Install.bat";
  398.                                                     StreamWriter file_install = new StreamWriter("GATE_Install.bat");
  399.                                                     file_install.WriteLine(lines);
  400.                                                     file_install.Close();
  401.                                                     Process[] myProcesses;
  402.                                                     myProcesses = Process.GetProcessesByName("cmd");
  403.                                                     foreach (Process myProcess in myProcesses)
  404.                                                     {
  405.                                                         Console.WriteLine("Installation terminée !");
  406.                                                         Process.Start("GATE_Install.bat");
  407.                                                         myProcess.CloseMainWindow();
  408.                                                     }
  409.                                                 }
  410.                                                 catch
  411.                                                 {
  412.                                                     WriteError("Erreur lors de l'installation !\n");
  413.                                                     Environment.Exit(0);
  414.                                                 }
  415.                                             }
  416.                                             catch
  417.                                             {
  418.                                                 WriteError("Erreur lors du téléchargement !\n");
  419.                                                 Environment.Exit(0);
  420.                                             }
  421.                                         }
  422.                                         else
  423.                                         {
  424.                                             WriteError("Erreur de syntaxe !");
  425.                                             Environment.Exit(0);
  426.                                         }
  427.                                     }
  428.                                 }
  429.                                 else
  430.                                 {
  431.                                     WriteError("La connexion sur le serveur a échoué !\n Le serveur est en maintenance !\nVeuillez réassyer réessayer ultérieurement !");
  432.                                     Environment.Exit(0);
  433.                                 }
  434.                             }
  435.                             catch
  436.                             {
  437.                                 WriteError("Erreur de syntaxe !");
  438.                                 Environment.Exit(0);
  439.                             }
  440.                         }
  441.                         else
  442.                         {
  443.  
  444.                             try
  445.                             {
  446.                                 String string_test_arg2 = args[3];
  447.                                 WriteError("Erreur de syntaxe !");
  448.                                 Environment.Exit(0);
  449.                             }
  450.                             catch
  451.                             {
  452.                                 String external_command_MIN = args[2].ToLower();
  453.                                 String external_command = args[2];
  454.                                 String external_command_name = external_command.Remove(0, 1);
  455.                                 String external_command_name_MIN = external_command_MIN.Remove(0, 1);
  456.  
  457.                                 Console.Write("Recherche d'une mise à jour pour ");
  458.                                 Console.Write("\"");
  459.                                 Console.ForegroundColor = ConsoleColor.Blue;
  460.                                 Console.Write(external_command_name);
  461.                                 Console.ForegroundColor = oldFGcolor;
  462.                                 Console.Write("\"\n");
  463.                                 Console.WriteLine("Connexion au serveur..");
  464.  
  465.  
  466.                                 string _url = "http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + ".php";
  467.                                 if (CheckUrl(_url) == "OK")
  468.                                 {
  469.                                     Console.Write("\"");
  470.                                     Console.ForegroundColor = ConsoleColor.Blue;
  471.                                     Console.Write(external_command_name);
  472.                                     Console.ForegroundColor = oldFGcolor;
  473.                                     Console.Write("\" existe\n");
  474.  
  475.                                     Read_file(external_command_name_MIN, external_command_name, "0", "version");
  476.                                     Environment.Exit(0);
  477.                                 }
  478.                                 else
  479.                                 {
  480.                                     WriteError("Cette commande externe n'existe pas !");
  481.                                     Environment.Exit(0);
  482.                                 }
  483.                             }
  484.                         }
  485.                     }
  486.                     else
  487.                     {
  488.                         try
  489.                         {
  490.                             String string_test_arg1 = args[2];
  491.                             WriteError("Erreur de syntaxe !");
  492.                             Environment.Exit(0);
  493.                         }
  494.                         catch
  495.                         {
  496.                             Download_file(args[1]);
  497.                         }
  498.                     }
  499.                 }
  500.                 else if (args[0] == "/download")
  501.                 {
  502.                     String RemoteFtpPath = args[1];
  503.                     String LocalDestinationPath = args[2];
  504.                     String Username = args[3];
  505.                     String Password = args[4];
  506.  
  507.                     try
  508.                     {
  509.                         Boolean UseBinary = true;
  510.                         Boolean UsePassive = false;
  511.  
  512.                         FtpWebRequest request = (FtpWebRequest)WebRequest.Create(RemoteFtpPath);
  513.                         request.Method = WebRequestMethods.Ftp.DownloadFile;
  514.                         request.KeepAlive = true;
  515.                         request.UsePassive = UsePassive;
  516.                         request.UseBinary = UseBinary;
  517.  
  518.                         request.Credentials = new NetworkCredential(Username, Password);
  519.  
  520.                         FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  521.  
  522.                         Stream responseStream = response.GetResponseStream();
  523.                         StreamReader reader = new StreamReader(responseStream);
  524.  
  525.                         using (FileStream writer = new FileStream(LocalDestinationPath, FileMode.Create))
  526.                         {
  527.  
  528.                             long length = response.ContentLength;
  529.                             int bufferSize = 2048;
  530.                             int readCount;
  531.                             byte[] buffer = new byte[2048];
  532.  
  533.                             readCount = responseStream.Read(buffer, 0, bufferSize);
  534.                             while (readCount > 0)
  535.                             {
  536.                                 writer.Write(buffer, 0, readCount);
  537.                                 readCount = responseStream.Read(buffer, 0, bufferSize);
  538.                             }
  539.                         }
  540.  
  541.                         reader.Close();
  542.                         response.Close();
  543.                         Environment.Exit(1);
  544.  
  545.                     }
  546.                     catch
  547.                     {
  548.                         Environment.Exit(0);
  549.                     }
  550.                 }
  551.                 else if (args[0] == "/upload")
  552.                 {
  553.                     String source = args[1];
  554.                     String ftpurl = args[2];
  555.                     String ftpusername = args[3];
  556.                     String ftppassword = args[4];
  557.                     try
  558.                     {
  559.                         string filename = Path.GetFileName(source);
  560.                         string ftpfullpath = ftpurl;
  561.                         FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
  562.                         ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
  563.  
  564.                         ftp.KeepAlive = true;
  565.                         ftp.UseBinary = true;
  566.                         ftp.Method = WebRequestMethods.Ftp.UploadFile;
  567.  
  568.                         FileStream fs = File.OpenRead(source);
  569.                         byte[] buffer = new byte[fs.Length];
  570.                         fs.Read(buffer, 0, buffer.Length);
  571.                         fs.Close();
  572.  
  573.                         Stream ftpstream = ftp.GetRequestStream();
  574.                         ftpstream.Write(buffer, 0, buffer.Length);
  575.                         ftpstream.Close();
  576.                         Environment.Exit(1);
  577.                     }
  578.                     catch
  579.                     {
  580.                         Environment.Exit(0);
  581.                     }
  582.                 }
  583.                 Environment.Exit(2);
  584.             }
  585.             else
  586.             {
  587.                 string start_5 = "Echec création des portails !\n";
  588.                 Console.SetCursorPosition(((Console.WindowWidth / 2) - start_5.Length), 8);
  589.                 Console.Write(start_5);
  590.                 y = 0;
  591.                 while (y < Console.WindowWidth / 2) { Console.Write("¯"); y++; }
  592.                 Console.Write("\n\n\n");
  593.  
  594.                 WriteError("La connexion au serveur a échoué !");
  595.                 WriteError("Veuillez vérifier votre connexion internet !");
  596.             }
  597.         }
  598.         static string CheckUrl(string myUrl)
  599.         {
  600.             HttpWebRequest _Request;
  601.             HttpWebResponse _Response;
  602.             string message = "";
  603.             if (myUrl != "")
  604.             {
  605.                 try
  606.                 {
  607.                     _Request = (HttpWebRequest)WebRequest.Create(myUrl);
  608.                     _Request.AllowAutoRedirect = false;
  609.                     _Response = (HttpWebResponse)_Request.GetResponse();
  610.                     message = _Response.StatusCode.ToString();
  611.                 }
  612.                 catch (WebException ex)
  613.                 {
  614.                     if (ex.Response != null)
  615.                     {
  616.                         message = (((HttpWebResponse)ex.Response).StatusCode).ToString();
  617.                     }
  618.                     else
  619.                         message = ex.Message;
  620.                 }
  621.                 catch (UriFormatException ex1)
  622.                 {
  623.                     message = ex1.Message;
  624.                 }
  625.             }
  626.             return message;
  627.         }
  628.         static void list_file()
  629.         {
  630.             ConsoleColor oldFGcolor = Console.ForegroundColor;
  631.             String temp = Path.GetTempPath();
  632.             try
  633.             {
  634.                 WebClient wc = new WebClient();
  635.                 wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/list.external_controls.php", temp + "list.external_controls.php");
  636.                 wc.Dispose();
  637.                 int counter = 0;
  638.                 string line;
  639.                 string file_list = temp + "list.external_controls.php";
  640.                 StreamReader file = new StreamReader(file_list);
  641.                 Console.ForegroundColor = ConsoleColor.Red;
  642.                 Console.Write("------- Liste des commandes externes disponibles :\n");
  643.                 Console.ForegroundColor = ConsoleColor.Blue;
  644.                 while ((line = file.ReadLine()) != null)
  645.                 {
  646.                     Console.WriteLine(line);
  647.                     counter++;
  648.                 }
  649.                 Console.ForegroundColor = ConsoleColor.Red;
  650.                 file.Close();
  651.                 File.Delete(file_list);
  652.                 Console.Write("\n");
  653.                 Console.ForegroundColor = ConsoleColor.Red;
  654.                 Console.Write("{0}", counter);
  655.                 Console.ForegroundColor = oldFGcolor;
  656.                 Console.Write(" commande externe disponible sur le serveur !\n\n");
  657.                 Console.ForegroundColor = ConsoleColor.Red;
  658.                 Console.Write("--------------------------------------------------\n");
  659.                 Console.ForegroundColor = oldFGcolor;
  660.             }
  661.             catch
  662.             {
  663.                 WriteError("Erreur de connexion sur le serveur ! Veuillez réessayer !");
  664.                 Environment.Exit(0);
  665.             }
  666.         }
  667.         static void Read_file(String external_command_name_MIN, String external_command_name, String mode, String information)
  668.         {
  669.             ConsoleColor oldFGcolor = Console.ForegroundColor;
  670.             String temp = Path.GetTempPath();
  671.  
  672.             try
  673.             {
  674.                 WebClient wc = new WebClient();
  675.                 wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + "-" + information + ".php", temp + information + ".php");
  676.                 wc.Dispose();
  677.                 String file_version = temp + information + ".php";
  678.                 if (information == "version")
  679.                 {
  680.                     StreamReader file = new StreamReader(file_version);
  681.                     String last_version = file.ReadLine();
  682.                     file.Close();
  683.  
  684.                     if (mode == "0")
  685.                     {
  686.                         Console.Write("Version de \"");
  687.                         Console.ForegroundColor = ConsoleColor.Blue;
  688.                         Console.Write(external_command_name);
  689.                         Console.ForegroundColor = oldFGcolor;
  690.                         Console.Write("\" : ");
  691.                         Console.ForegroundColor = ConsoleColor.Red;
  692.                         Console.Write("{0}\n", last_version);
  693.                         Console.ForegroundColor = oldFGcolor;
  694.                     }
  695.  
  696.                 }
  697.             }
  698.             catch
  699.             {
  700.                 WriteError("Impossible de récupérer la version ! La connexion au serveur a échoué !");
  701.                 Environment.Exit(0);
  702.             }
  703.         }
  704.  
  705.         static void Download_file(string external_command)
  706.         {
  707.             ConsoleColor oldFGcolor = Console.ForegroundColor;
  708.             String temp = Path.GetTempPath();
  709.             WebClient wc = new WebClient();
  710.  
  711.             String external_command_MIN = external_command.ToLower();
  712.             String external_command_name = external_command.Remove(0, 1);
  713.             String external_command_name_MIN = external_command_MIN.Remove(0, 1);
  714.  
  715.             String external_command_ = external_command.ToLower();
  716.  
  717.  
  718.             wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + "-ext" + ".php", temp + "ext.php");
  719.             wc.Dispose();
  720.             String file_ext = temp + "ext.php";
  721.             StreamReader file_ex = new StreamReader(file_ext);
  722.             String ext = file_ex.ReadLine();
  723.             file_ex.Close();
  724.  
  725.             Console.Write("Recherche de \"");
  726.             Console.ForegroundColor = ConsoleColor.Blue;
  727.             Console.Write(external_command_name);
  728.             Console.ForegroundColor = oldFGcolor;
  729.             Console.Write("\" sur le serveur...\n");
  730.             String _url = "http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + ".php";
  731.             if (CheckUrl(_url) == "OK")
  732.             {
  733.                 Console.Write("\"");
  734.                 Console.ForegroundColor = ConsoleColor.Blue;
  735.                 Console.Write(external_command_name);
  736.                 Console.ForegroundColor = oldFGcolor;
  737.                 Console.Write("\" existe\n");
  738.                 try
  739.                 {
  740.                     wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + ".ini", external_command_name_MIN + ext);
  741.                     wc.Dispose();
  742.                     Console.Write("Téléchargement terminé !");
  743.                     Environment.Exit(0);
  744.                 }
  745.                 catch
  746.                 {
  747.                     WriteError("Erreur lors du téléchargement !");
  748.                     Environment.Exit(0);
  749.                 }
  750.             }
  751.             else
  752.             {
  753.                 WriteError("Cette commande externe n'existe pas !");
  754.                 Console.ReadKey();
  755.  
  756.             }
  757.  
  758.         }
  759.  
  760.         static void WriteError(string msg, params object[] objs)
  761.         {
  762.             var oldFGcolor = Console.ForegroundColor;
  763.             Console.Write("[");
  764.             Console.ForegroundColor = ConsoleColor.Red;
  765.             Console.Write("ERROR");
  766.             Console.ForegroundColor = oldFGcolor;
  767.             Console.Write("] : ");
  768.             Console.WriteLine(msg, objs);
  769.         }
  770.         static void WriteInfo(string msg, params object[] objs)
  771.         {
  772.             var oldFGcolor = Console.ForegroundColor;
  773.             Console.Write("[");
  774.             Console.ForegroundColor = ConsoleColor.Green;
  775.             Console.Write("INFO");
  776.             Console.ForegroundColor = oldFGcolor;
  777.             Console.Write("] : ");
  778.             Console.WriteLine(msg, objs);
  779.         }
  780.         public static bool WebRequestTest()
  781.         {
  782.             string url = "http://www.google.com";
  783.             try
  784.             {
  785.                 System.Net.WebRequest myRequest = System.Net.WebRequest.Create(url);
  786.                 System.Net.WebResponse myResponse = myRequest.GetResponse();
  787.             }
  788.             catch (System.Net.WebException)
  789.             {
  790.                 return false;
  791.             }
  792.             return true;
  793.         }
  794.         static void install_file(string path, string new_line, string external_control)
  795.         {
  796.             Console.WriteLine("Installation en cours..");
  797.             try
  798.             {
  799.                 String temp = Path.GetTempPath();
  800.                 String path_copy = path+"-install";
  801.                 if (File.Exists(path_copy)) { File.Delete(path_copy); }
  802.                 File.Copy(path, path_copy);
  803.                 String final_file = null;
  804.                 StreamReader sr = new StreamReader(path_copy);
  805.                 String line_read = null;
  806.                 String find_line = null;
  807.                 String line_read_ = null;
  808.                 bool install_generate_code = false;
  809.                 while (!sr.EndOfStream)
  810.                 {
  811.                     line_read = sr.ReadLine();
  812.                     if (line_read.Length >= 13 + external_control.Length)
  813.                     {
  814.                         find_line = line_read.Substring(0, 13 + external_control.Length);
  815.                     }
  816.                     if (line_read == ":End_GATE_install_" + external_control)
  817.                     {
  818.                         install_generate_code = false;
  819.                     }
  820.                     else
  821.                     {
  822.                         if (line_read == ":GATE_install_" + external_control)
  823.                         {
  824.                             install_generate_code = true;
  825.                             final_file = (final_file + (line_read + "\r\n"));
  826.                             StreamReader file_ = new StreamReader(temp + "generate_ex.php");
  827.                             while (!file_.EndOfStream)
  828.                             {
  829.                                 line_read_ = file_.ReadLine();
  830.                                 final_file = (final_file + (line_read_ + "\r\n"));
  831.                             }
  832.                             file_.Close();
  833.                             final_file = (final_file + (":End_GATE_install_" + external_control));
  834.                         }
  835.                         else
  836.                         {
  837.                             if (!install_generate_code)
  838.                             {
  839.                                 if (find_line == "set \"version_" + external_control)
  840.                                 {
  841.                                     if (!sr.EndOfStream)
  842.                                     {
  843.                                         final_file = (final_file + (new_line + "\r\n"));
  844.                                     }
  845.                                     else
  846.                                     {
  847.                                         final_file = (final_file + (new_line));
  848.                                     }
  849.                                 }
  850.                                 else
  851.                                 {
  852.                                     if (!sr.EndOfStream)
  853.                                     {
  854.                                         final_file = (final_file + (line_read + "\r\n"));
  855.                                     }
  856.                                     else
  857.                                     {
  858.                                         final_file = (final_file + (line_read));
  859.                                     }
  860.                                 }
  861.                             }
  862.                         }
  863.                     }
  864.                 }
  865.                 sr.Close();
  866.                 StreamWriter sr2 = new StreamWriter(path_copy);
  867.                 sr2.WriteLine(final_file);
  868.                 sr2.Close();
  869.                 String lines = "@ping localhost -n 2 >nul \r\ndel \"" + path + "\"\r\nren \"" + path_copy + "\" \"" + path + "\"\r\ngate /start \"" + path + "\"\r\ndel GATE_Install.bat";
  870.                 StreamWriter file_install = new StreamWriter("GATE_Install.bat");
  871.                 file_install.WriteLine(lines);
  872.                 file_install.Close();
  873.                 Process[] myProcesses;
  874.                 myProcesses = Process.GetProcessesByName("cmd");
  875.                 foreach (Process myProcess in myProcesses)
  876.                 {
  877.                     Console.WriteLine("Installation terminée !");
  878.                     Process.Start("GATE_Install.bat");
  879.                     myProcess.CloseMainWindow();
  880.                 }
  881.             }
  882.             catch (Exception e)
  883.             {
  884.                 WriteError("Echec de l'installation de la nouvelle version, veuillez vérifier la syntaxe de la commande.");
  885.                 Console.WriteLine(e);
  886.                 Environment.Exit(0);
  887.             }
  888.         }
  889.     }
  890. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement