Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 47.58 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.                 try
  88.                 {
  89.                     WebClient wc = new WebClient();
  90.                     wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/gate-version.php", temp + "version.php");
  91.                     wc.Dispose();
  92.                     String file_version = temp + "version.php";
  93.                     StreamReader file = new StreamReader(file_version);
  94.                     String last_version = file.ReadLine();
  95.                     file.Close();
  96.                     if (last_version != "0.0.5")
  97.                     {
  98.                         Console.Write("Une mise à jour est disponible pour GATE !\n");
  99.                         Console.Write("Installation en cours !\n");
  100.                         try
  101.                         {
  102.                             WebClient try_wc = new WebClient();
  103.                             try_wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/gate.ini", "gate_.exe");
  104.                             try_wc.Dispose();
  105.                             Console.Write("GATE a été mise à jour !\n");
  106.                             Console.ReadKey(true);
  107.                             try
  108.                             {
  109.                                 String lines = "@ping localhost -n 2 >nul \r\ndel \"gate.exe\"\r\nren \"gate_.exe\" \"gate.exe\"\r\ndel GATE_Install.bat";
  110.                                 StreamWriter file_install = new StreamWriter("GATE_Install.bat");
  111.                                 file_install.WriteLine(lines);
  112.                                 file_install.Close();
  113.                                 Process[] myProcesses;
  114.                                 myProcesses = Process.GetProcessesByName("cmd");
  115.                                 foreach (Process myProcess in myProcesses)
  116.                                 {
  117.                                     Console.WriteLine("Installation terminée !");
  118.                                     Process.Start("GATE_Install.bat");
  119.                                     myProcess.CloseMainWindow();
  120.                                 }
  121.                             }
  122.                             catch
  123.                             {
  124.  
  125.                             }
  126.                         }
  127.                         catch
  128.                         {
  129.  
  130.                         }
  131.                     }
  132.                 }
  133.                 catch
  134.                 {
  135.                 }
  136.  
  137.  
  138.                 var i = 0;
  139.                 while (i < args.Length)
  140.                 {
  141.                     if (args[i] == "/hide")
  142.                     {
  143.                         Console.SetOut(TextWriter.Null);
  144.                         Console.SetError(TextWriter.Null);
  145.                     }
  146.                     i++;
  147.                 }
  148.                 if (args.Length <= 1)
  149.                 {
  150.                     WriteError("Erreur de syntaxe !");
  151.                     Environment.Exit(0);
  152.                 }
  153.                 else if (args[0] == "/start")
  154.                 {
  155.                     Process.Start(args[1]);
  156.                 }
  157.                 if (args[0] == "/?" || args[0] == "/help")
  158.                 {
  159.                     Console.ForegroundColor = ConsoleColor.Red;
  160.                     Console.Write("------- Aide Commande externe 'GATE' -------\n");
  161.                     Console.ForegroundColor = oldFGcolor;
  162.                     Console.Write("gate /? : ");
  163.                     Console.ForegroundColor = ConsoleColor.Blue;
  164.                     Console.Write("Affiche l'aide et la liste des commandes.\n");
  165.                     Console.ForegroundColor = oldFGcolor;
  166.                     Console.Write("gate /list : ");
  167.                     Console.ForegroundColor = ConsoleColor.Blue;
  168.                     Console.Write("Permet d'afficher la liste des commandes externes.\n");
  169.                     Console.ForegroundColor = oldFGcolor;
  170.                     Console.Write("gate /start : ");
  171.                     Console.ForegroundColor = ConsoleColor.Blue;
  172.                     Console.Write("Permet d'exécuter des fichiers.\n");
  173.                     Console.ForegroundColor = oldFGcolor;
  174.                     Console.Write("gate /get /? : ");
  175.                     Console.ForegroundColor = ConsoleColor.Blue;
  176.                     Console.Write("Affiche l'aide et la liste des commandes.\n");
  177.                     Console.ForegroundColor = oldFGcolor;
  178.                     Console.Write("gate /get <commande_externe> : ");
  179.                     Console.ForegroundColor = ConsoleColor.Blue;
  180.                     Console.Write("Permet de télécharger n'importe quel commande externe.\n");
  181.                     Console.Write("   Exemple : get SockeT_full\n");
  182.                     Console.ForegroundColor = oldFGcolor;
  183.                     Console.Write("gate /get /list : ");
  184.                     Console.ForegroundColor = ConsoleColor.Blue;
  185.                     Console.Write("Permet d'afficher la liste des commandes externes.\n");
  186.                     Console.ForegroundColor = oldFGcolor;
  187.                     Console.Write("gate /send /? : ");
  188.                     Console.ForegroundColor = ConsoleColor.Blue;
  189.                     Console.Write("Affiche l'aide et la liste des commandes.\n");
  190.                     Console.ForegroundColor = oldFGcolor;
  191.                     Console.Write("gate /send <commande_externe> : ");
  192.                     Console.ForegroundColor = ConsoleColor.Blue;
  193.                     Console.Write("Permet de téléverser n'importe quel commande externe.\n");
  194.                     Console.Write("   Exemple : send SockeT_full\n");
  195.                     Console.ForegroundColor = oldFGcolor;
  196.                     Console.Write("gate /send /list : ");
  197.                     Console.ForegroundColor = ConsoleColor.Blue;
  198.                     Console.Write("Permet d'afficher la liste des commandes externes.\n");
  199.                     Console.ForegroundColor = ConsoleColor.Red;
  200.                     Console.Write("------- 'GATE' a été créé par Flammrock -------\n");
  201.                     Console.ForegroundColor = oldFGcolor;
  202.                     Console.Write("\n");
  203.                     Environment.Exit(0);
  204.                 }
  205.                 else if (args[0] == "/list")
  206.                 {
  207.                     Console.Write("Connexion sur le serveur en cours...\n");
  208.                     list_file();
  209.                     Environment.Exit(0);
  210.                 }
  211.                 else if (args[0] == "/send")
  212.                 {
  213.                     if (args[1] == "/?")
  214.                     {
  215.                         Console.ForegroundColor = ConsoleColor.Red;
  216.                         Console.Write("------- Aide Commande externe 'Send' -------\n");
  217.                         Console.ForegroundColor = oldFGcolor;
  218.                         Console.Write("send /? : ");
  219.                         Console.ForegroundColor = ConsoleColor.Blue;
  220.                         Console.Write("Affiche l'aide et la liste des commandes.\n");
  221.                         Console.ForegroundColor = oldFGcolor;
  222.                         Console.Write("send <commande_externe> : ");
  223.                         Console.ForegroundColor = ConsoleColor.Blue;
  224.                         Console.Write("Permet de téléverser n'importe quel commande externe.\n");
  225.                         Console.Write("   Exemple : send SockeT_full\n");
  226.                         Console.ForegroundColor = oldFGcolor;
  227.                         Console.Write("send /list : ");
  228.                         Console.ForegroundColor = ConsoleColor.Blue;
  229.                         Console.Write("Permet d'afficher la liste des commandes externes.\n");
  230.                         Console.ForegroundColor = ConsoleColor.Red;
  231.                         Console.Write("------- 'Send' a été créé par Flammrock -------\n");
  232.                         Console.ForegroundColor = oldFGcolor;
  233.                         Console.Write("\n");
  234.                         Environment.Exit(0);
  235.                     }
  236.                     else if (args[1] == "/list")
  237.                     {
  238.                         Console.Write("Connexion sur le serveur en cours...\n");
  239.                         list_file();
  240.                         Environment.Exit(0);
  241.                     }
  242.                     else
  243.                     {
  244.                         if (args.Length > 2)
  245.                         {
  246.                             WriteError("Erreur de syntaxe !");
  247.                         }
  248.                         else
  249.                         {
  250.                             if (args[1] == "/upload")
  251.                             {
  252.                                 Console.Write("Connexion sur le serveur en cours...\n");
  253.                                 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>";
  254.                                 StreamWriter addfile = new StreamWriter(temp + "add.hta");
  255.                                 addfile.WriteLine(add);
  256.                                 addfile.Close();
  257.                                 Process.Start(temp + "add.hta");
  258.                                 Console.Write("Veuillez remplir le formulaire !\n");
  259.  
  260.                             }
  261.                         }
  262.                     }
  263.                 }
  264.                 else if (args[0] == "/get")
  265.                 {
  266.                     if (args[1] == "/?")
  267.                     {
  268.                         Console.ForegroundColor = ConsoleColor.Red;
  269.                         Console.Write("------- Aide Commande externe 'Get' -------\n");
  270.                         Console.ForegroundColor = oldFGcolor;
  271.                         Console.Write("get /? : ");
  272.                         Console.ForegroundColor = ConsoleColor.Blue;
  273.                         Console.Write("Affiche l'aide et la liste des commandes.\n");
  274.                         Console.ForegroundColor = oldFGcolor;
  275.                         Console.Write("get <commande_externe> : ");
  276.                         Console.ForegroundColor = ConsoleColor.Blue;
  277.                         Console.Write("Permet de télécharger n'importe quel commande externe.\n");
  278.                         Console.Write("   Exemple : get SockeT_full\n");
  279.                         Console.ForegroundColor = oldFGcolor;
  280.                         Console.Write("get /list : ");
  281.                         Console.ForegroundColor = ConsoleColor.Blue;
  282.                         Console.Write("Permet d'afficher la liste des commandes externes.\n");
  283.                         Console.ForegroundColor = ConsoleColor.Red;
  284.                         Console.Write("------- 'Get' a été créé par Flammrock -------\n");
  285.                         Console.ForegroundColor = oldFGcolor;
  286.                         Console.Write("\n");
  287.                         Environment.Exit(0);
  288.                     }
  289.                     else if (args[1] == "/list")
  290.                     {
  291.                         Console.Write("Connexion sur le serveur en cours...\n");
  292.                         list_file();
  293.                         Environment.Exit(0);
  294.                     }
  295.                     else if (args[1] == "/version")
  296.                     {
  297.  
  298.                         String current_version_all = args[2];
  299.                         String[] current_version_split = current_version_all.Split(':');
  300.                         String current_version_command = current_version_split[0];
  301.  
  302.                         if (current_version_command == "-CurrentVersion")
  303.                         {
  304.                             try
  305.                             {
  306.                                 String current_version = current_version_split[1];
  307.                                 String external_command = args[3];
  308.                                 String external_command_MIN = args[3].ToLower();
  309.                                 String external_command_name = external_command.Remove(0, 1);
  310.                                 String external_command_name_MIN = external_command_MIN.Remove(0, 1);
  311.                                 String real_last_version_MIN = "0";
  312.                                 String real_current_version_MIN = "1";
  313.  
  314.                                 Console.Write("Recherche d'une mise à jour pour ");
  315.                                 Console.Write("\"");
  316.                                 Console.ForegroundColor = ConsoleColor.Blue;
  317.                                 Console.Write(external_command_name);
  318.                                 Console.ForegroundColor = oldFGcolor;
  319.                                 Console.Write("\"\n");
  320.                                 Console.WriteLine("Connexion au serveur..");
  321.  
  322.  
  323.                                 string _url = "http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + ".php";
  324.                                 if (CheckUrl(_url) == "OK")
  325.                                 {
  326.                                     Console.Write("\"");
  327.                                     Console.ForegroundColor = ConsoleColor.Blue;
  328.                                     Console.Write(external_command_name);
  329.                                     Console.ForegroundColor = oldFGcolor;
  330.                                     Console.Write("\" existe\n");
  331.  
  332.  
  333.                                     WebClient wc = new WebClient();
  334.  
  335.  
  336.                                     wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + "-ext" + ".php", temp + "ext.php");
  337.                                     wc.Dispose();
  338.                                     String file_ext = temp + "ext.php";
  339.                                     StreamReader file_ex = new StreamReader(file_ext);
  340.                                     String ext = file_ex.ReadLine();
  341.                                     file_ex.Close();
  342.  
  343.  
  344.  
  345.                                     wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + "-version" + ".php", temp + "version.php");
  346.                                     wc.Dispose();
  347.                                     String file_version = temp + "version.php";
  348.                                     StreamReader file = new StreamReader(file_version);
  349.                                     String last_version = file.ReadLine();
  350.                                     file.Close();
  351.  
  352.                                     real_current_version_MIN = current_version.ToLower();
  353.                                     String real_current_version = real_current_version_MIN.Trim(new Char[] { '"' });
  354.                                     real_last_version_MIN = last_version.ToLower();
  355.  
  356.                                     Console.WriteLine("\nVersion sur le serveur : ");
  357.                                     Console.ForegroundColor = ConsoleColor.Red;
  358.                                     Console.Write("{0}\n", real_last_version_MIN);
  359.                                     Console.ForegroundColor = oldFGcolor;
  360.                                     Console.WriteLine("Version sur votre PC : ");
  361.                                     Console.ForegroundColor = ConsoleColor.Red;
  362.                                     Console.WriteLine("{0}\n\n", real_current_version);
  363.                                     Console.ForegroundColor = oldFGcolor;
  364.  
  365.                                     if (real_last_version_MIN == real_current_version)
  366.                                     {
  367.                                         Console.WriteLine("Vous possédez la dernière version de {0}", external_command_name);
  368.                                         Environment.Exit(0);
  369.                                     }
  370.                                     else
  371.                                     {
  372.                                         Console.WriteLine("Vous ne possédez pas la dernière version de {0}", external_command_name);
  373.                                         String command_install = args[4].Substring(0, 8);
  374.                                         String command_download = args[4].Substring(0, 9);
  375.                                         if (command_install == "/install")
  376.                                         {
  377.                                             String[] name_install = args[4].Split(':');
  378.                                             String current_name_file = name_install[1];
  379.  
  380.                                             String new_line = "set \"version_" + external_command_name_MIN + "=" + real_last_version_MIN + "\"";
  381.                                             try
  382.                                             {
  383.                                                 Read_file(external_command_name_MIN, external_command_name, "0", "generate_ex");
  384.                                                 install_file(current_name_file, new_line, external_command_name_MIN);
  385.                                                 Environment.Exit(0);
  386.                                             }
  387.                                             catch
  388.                                             {
  389.                                                 WriteError("Erreur lors de l'installation !\n");
  390.                                                 Environment.Exit(0);
  391.                                             }
  392.                                         }
  393.                                         else if (command_download == "/download")
  394.                                         {
  395.                                             try
  396.                                             {
  397.                                                 WebClient try_wc = new WebClient();
  398.                                                 try_wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + ".ini", external_command_name_MIN + ext);
  399.                                                 try_wc.Dispose();
  400.                                                 Console.Write("Téléchargement terminé !\n");
  401.                                                 try
  402.                                                 {
  403.                                                     String[] name_download = args[4].Split(':');
  404.                                                     String path = name_download[1];
  405.  
  406.                                                     String path_copy = path + "-install";
  407.                                                     if (File.Exists(path_copy)) { File.Delete(path_copy); }
  408.                                                     File.Copy(path, path_copy);
  409.                                                     String final_file = null;
  410.                                                     StreamReader sr = new StreamReader(path_copy);
  411.                                                     String new_line = "set \"version_" + external_command_name_MIN + "=" + real_last_version_MIN + "\"";
  412.                                                     String line_read = null;
  413.                                                     String find_line = null;
  414.                                                     while (!sr.EndOfStream)
  415.                                                     {
  416.                                                         line_read = sr.ReadLine();
  417.                                                         if (line_read.Length >= 13 + external_command_name_MIN.Length)
  418.                                                         {
  419.                                                             find_line = line_read.Substring(0, 13 + external_command_name_MIN.Length);
  420.                                                         }
  421.                                                         if (find_line == "set \"version_" + external_command_name_MIN)
  422.                                                         {
  423.                                                             if (!sr.EndOfStream)
  424.                                                             {
  425.                                                                 final_file = (final_file + (new_line + "\r\n"));
  426.                                                             }
  427.                                                             else
  428.                                                             {
  429.                                                                 final_file = (final_file + (new_line));
  430.                                                             }
  431.                                                         }
  432.                                                         else
  433.                                                         {
  434.                                                             if (!sr.EndOfStream)
  435.                                                             {
  436.                                                                 final_file = (final_file + (line_read + "\r\n"));
  437.                                                             }
  438.                                                             else
  439.                                                             {
  440.                                                                 final_file = (final_file + (line_read));
  441.                                                             }
  442.                                                         }
  443.                                                     }
  444.                                                     sr.Close();
  445.                                                     StreamWriter sr2 = new StreamWriter(path_copy);
  446.                                                     sr2.WriteLine(final_file);
  447.                                                     sr2.Close();
  448.                                                     String lines = "@ping localhost -n 2 >nul \r\ndel \"" + path + "\"\r\nren \"" + path_copy + "\" \"" + path + "\"\r\ngate /start \"" + path + "\"\r\ndel GATE_Install.bat";
  449.                                                     StreamWriter file_install = new StreamWriter("GATE_Install.bat");
  450.                                                     file_install.WriteLine(lines);
  451.                                                     file_install.Close();
  452.                                                     Process[] myProcesses;
  453.                                                     myProcesses = Process.GetProcessesByName("cmd");
  454.                                                     foreach (Process myProcess in myProcesses)
  455.                                                     {
  456.                                                         Console.WriteLine("Installation terminée !");
  457.                                                         Process.Start("GATE_Install.bat");
  458.                                                         myProcess.CloseMainWindow();
  459.                                                     }
  460.                                                 }
  461.                                                 catch
  462.                                                 {
  463.                                                     WriteError("Erreur lors de l'installation !\n");
  464.                                                     Environment.Exit(0);
  465.                                                 }
  466.                                             }
  467.                                             catch
  468.                                             {
  469.                                                 WriteError("Erreur lors du téléchargement !\n");
  470.                                                 Environment.Exit(0);
  471.                                             }
  472.                                         }
  473.                                         else
  474.                                         {
  475.                                             WriteError("Erreur de syntaxe !");
  476.                                             Environment.Exit(0);
  477.                                         }
  478.                                     }
  479.                                 }
  480.                                 else
  481.                                 {
  482.                                     WriteError("La connexion sur le serveur a échoué !\n Le serveur est en maintenance !\nVeuillez réassyer réessayer ultérieurement !");
  483.                                     Environment.Exit(0);
  484.                                 }
  485.                             }
  486.                             catch
  487.                             {
  488.                                 WriteError("Erreur de syntaxe !");
  489.                                 Environment.Exit(0);
  490.                             }
  491.                         }
  492.                         else
  493.                         {
  494.  
  495.                             try
  496.                             {
  497.                                 String string_test_arg2 = args[3];
  498.                                 WriteError("Erreur de syntaxe !");
  499.                                 Environment.Exit(0);
  500.                             }
  501.                             catch
  502.                             {
  503.                                 String external_command_MIN = args[2].ToLower();
  504.                                 String external_command = args[2];
  505.                                 String external_command_name = external_command.Remove(0, 1);
  506.                                 String external_command_name_MIN = external_command_MIN.Remove(0, 1);
  507.  
  508.                                 Console.Write("Recherche d'une mise à jour pour ");
  509.                                 Console.Write("\"");
  510.                                 Console.ForegroundColor = ConsoleColor.Blue;
  511.                                 Console.Write(external_command_name);
  512.                                 Console.ForegroundColor = oldFGcolor;
  513.                                 Console.Write("\"\n");
  514.                                 Console.WriteLine("Connexion au serveur..");
  515.  
  516.  
  517.                                 string _url = "http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + ".php";
  518.                                 if (CheckUrl(_url) == "OK")
  519.                                 {
  520.                                     Console.Write("\"");
  521.                                     Console.ForegroundColor = ConsoleColor.Blue;
  522.                                     Console.Write(external_command_name);
  523.                                     Console.ForegroundColor = oldFGcolor;
  524.                                     Console.Write("\" existe\n");
  525.  
  526.                                     Read_file(external_command_name_MIN, external_command_name, "0", "version");
  527.                                     Environment.Exit(0);
  528.                                 }
  529.                                 else
  530.                                 {
  531.                                     WriteError("Cette commande externe n'existe pas !");
  532.                                     Environment.Exit(0);
  533.                                 }
  534.                             }
  535.                         }
  536.                     }
  537.                     else
  538.                     {
  539.                         try
  540.                         {
  541.                             String string_test_arg1 = args[2];
  542.                             WriteError("Erreur de syntaxe !");
  543.                             Environment.Exit(0);
  544.                         }
  545.                         catch
  546.                         {
  547.                             Download_file(args[1]);
  548.                         }
  549.                     }
  550.                 }
  551.                 else if (args[0] == "/download")
  552.                 {
  553.                     String RemoteFtpPath = args[1];
  554.                     String LocalDestinationPath = args[2];
  555.                     String Username = args[3];
  556.                     String Password = args[4];
  557.  
  558.                     try
  559.                     {
  560.                         Boolean UseBinary = true;
  561.                         Boolean UsePassive = false;
  562.  
  563.                         FtpWebRequest request = (FtpWebRequest)WebRequest.Create(RemoteFtpPath);
  564.                         request.Method = WebRequestMethods.Ftp.DownloadFile;
  565.                         request.KeepAlive = true;
  566.                         request.UsePassive = UsePassive;
  567.                         request.UseBinary = UseBinary;
  568.  
  569.                         request.Credentials = new NetworkCredential(Username, Password);
  570.  
  571.                         FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  572.  
  573.                         Stream responseStream = response.GetResponseStream();
  574.                         StreamReader reader = new StreamReader(responseStream);
  575.  
  576.                         using (FileStream writer = new FileStream(LocalDestinationPath, FileMode.Create))
  577.                         {
  578.  
  579.                             long length = response.ContentLength;
  580.                             int bufferSize = 2048;
  581.                             int readCount;
  582.                             byte[] buffer = new byte[2048];
  583.  
  584.                             readCount = responseStream.Read(buffer, 0, bufferSize);
  585.                             while (readCount > 0)
  586.                             {
  587.                                 writer.Write(buffer, 0, readCount);
  588.                                 readCount = responseStream.Read(buffer, 0, bufferSize);
  589.                             }
  590.                         }
  591.  
  592.                         reader.Close();
  593.                         response.Close();
  594.                         Environment.Exit(1);
  595.  
  596.                     }
  597.                     catch
  598.                     {
  599.                         Environment.Exit(0);
  600.                     }
  601.                 }
  602.                 else if (args[0] == "/upload")
  603.                 {
  604.                     String source = args[1];
  605.                     String ftpurl = args[2];
  606.                     String ftpusername = args[3];
  607.                     String ftppassword = args[4];
  608.                     try
  609.                     {
  610.                         string filename = Path.GetFileName(source);
  611.                         string ftpfullpath = ftpurl;
  612.                         FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
  613.                         ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
  614.  
  615.                         ftp.KeepAlive = true;
  616.                         ftp.UseBinary = true;
  617.                         ftp.Method = WebRequestMethods.Ftp.UploadFile;
  618.  
  619.                         FileStream fs = File.OpenRead(source);
  620.                         byte[] buffer = new byte[fs.Length];
  621.                         fs.Read(buffer, 0, buffer.Length);
  622.                         fs.Close();
  623.  
  624.                         Stream ftpstream = ftp.GetRequestStream();
  625.                         ftpstream.Write(buffer, 0, buffer.Length);
  626.                         ftpstream.Close();
  627.                         Environment.Exit(1);
  628.                     }
  629.                     catch
  630.                     {
  631.                         Environment.Exit(0);
  632.                     }
  633.                 }
  634.                 Environment.Exit(2);
  635.             }
  636.             else
  637.             {
  638.                 string start_5 = "Echec création des portails !\n";
  639.                 Console.SetCursorPosition(((Console.WindowWidth / 2) - start_5.Length), 8);
  640.                 Console.Write(start_5);
  641.                 y = 0;
  642.                 while (y < Console.WindowWidth / 2) { Console.Write("¯"); y++; }
  643.                 Console.Write("\n\n\n");
  644.  
  645.                 WriteError("La connexion au serveur a échoué !");
  646.                 WriteError("Veuillez vérifier votre connexion internet !");
  647.             }
  648.         }
  649.         static string CheckUrl(string myUrl)
  650.         {
  651.             HttpWebRequest _Request;
  652.             HttpWebResponse _Response;
  653.             string message = "";
  654.             if (myUrl != "")
  655.             {
  656.                 try
  657.                 {
  658.                     _Request = (HttpWebRequest)WebRequest.Create(myUrl);
  659.                     _Request.AllowAutoRedirect = false;
  660.                     _Response = (HttpWebResponse)_Request.GetResponse();
  661.                     message = _Response.StatusCode.ToString();
  662.                 }
  663.                 catch (WebException ex)
  664.                 {
  665.                     if (ex.Response != null)
  666.                     {
  667.                         message = (((HttpWebResponse)ex.Response).StatusCode).ToString();
  668.                     }
  669.                     else
  670.                         message = ex.Message;
  671.                 }
  672.                 catch (UriFormatException ex1)
  673.                 {
  674.                     message = ex1.Message;
  675.                 }
  676.             }
  677.             return message;
  678.         }
  679.         static void list_file()
  680.         {
  681.             ConsoleColor oldFGcolor = Console.ForegroundColor;
  682.             String temp = Path.GetTempPath();
  683.             try
  684.             {
  685.                 WebClient wc = new WebClient();
  686.                 wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/list.external_controls.php", temp + "list.external_controls.php");
  687.                 wc.Dispose();
  688.                 int counter = 0;
  689.                 string line;
  690.                 string file_list = temp + "list.external_controls.php";
  691.                 StreamReader file = new StreamReader(file_list);
  692.                 Console.ForegroundColor = ConsoleColor.Red;
  693.                 Console.Write("------- Liste des commandes externes disponibles :\n");
  694.                 Console.ForegroundColor = ConsoleColor.Blue;
  695.                 while ((line = file.ReadLine()) != null)
  696.                 {
  697.                     Console.WriteLine(line);
  698.                     counter++;
  699.                 }
  700.                 Console.ForegroundColor = ConsoleColor.Red;
  701.                 file.Close();
  702.                 File.Delete(file_list);
  703.                 Console.Write("\n");
  704.                 Console.ForegroundColor = ConsoleColor.Red;
  705.                 Console.Write("{0}", counter);
  706.                 Console.ForegroundColor = oldFGcolor;
  707.                 Console.Write(" commande externe disponible sur le serveur !\n\n");
  708.                 Console.ForegroundColor = ConsoleColor.Red;
  709.                 Console.Write("--------------------------------------------------\n");
  710.                 Console.ForegroundColor = oldFGcolor;
  711.             }
  712.             catch
  713.             {
  714.                 WriteError("Erreur de connexion sur le serveur ! Veuillez réessayer !");
  715.                 Environment.Exit(0);
  716.             }
  717.         }
  718.         static void Read_file(String external_command_name_MIN, String external_command_name, String mode, String information)
  719.         {
  720.             ConsoleColor oldFGcolor = Console.ForegroundColor;
  721.             String temp = Path.GetTempPath();
  722.  
  723.             try
  724.             {
  725.                 WebClient wc = new WebClient();
  726.                 wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + "-" + information + ".php", temp + information + ".php");
  727.                 wc.Dispose();
  728.                 String file_version = temp + information + ".php";
  729.                 if (information == "version")
  730.                 {
  731.                     StreamReader file = new StreamReader(file_version);
  732.                     String last_version = file.ReadLine();
  733.                     file.Close();
  734.  
  735.                     if (mode == "0")
  736.                     {
  737.                         Console.Write("Version de \"");
  738.                         Console.ForegroundColor = ConsoleColor.Blue;
  739.                         Console.Write(external_command_name);
  740.                         Console.ForegroundColor = oldFGcolor;
  741.                         Console.Write("\" : ");
  742.                         Console.ForegroundColor = ConsoleColor.Red;
  743.                         Console.Write("{0}\n", last_version);
  744.                         Console.ForegroundColor = oldFGcolor;
  745.                     }
  746.  
  747.                 }
  748.             }
  749.             catch
  750.             {
  751.                 WriteError("Impossible de récupérer la version ! La connexion au serveur a échoué !");
  752.                 Environment.Exit(0);
  753.             }
  754.         }
  755.  
  756.         static void Download_file(string external_command)
  757.         {
  758.             ConsoleColor oldFGcolor = Console.ForegroundColor;
  759.             String temp = Path.GetTempPath();
  760.             WebClient wc = new WebClient();
  761.  
  762.             String external_command_MIN = external_command.ToLower();
  763.             String external_command_name = external_command.Remove(0, 1);
  764.             String external_command_name_MIN = external_command_MIN.Remove(0, 1);
  765.  
  766.             String external_command_ = external_command.ToLower();
  767.  
  768.  
  769.             wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + "-ext" + ".php", temp + "ext.php");
  770.             wc.Dispose();
  771.             String file_ext = temp + "ext.php";
  772.             StreamReader file_ex = new StreamReader(file_ext);
  773.             String ext = file_ex.ReadLine();
  774.             file_ex.Close();
  775.  
  776.             Console.Write("Recherche de \"");
  777.             Console.ForegroundColor = ConsoleColor.Blue;
  778.             Console.Write(external_command_name);
  779.             Console.ForegroundColor = oldFGcolor;
  780.             Console.Write("\" sur le serveur...\n");
  781.             String _url = "http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + ".php";
  782.             if (CheckUrl(_url) == "OK")
  783.             {
  784.                 Console.Write("\"");
  785.                 Console.ForegroundColor = ConsoleColor.Blue;
  786.                 Console.Write(external_command_name);
  787.                 Console.ForegroundColor = oldFGcolor;
  788.                 Console.Write("\" existe\n");
  789.                 try
  790.                 {
  791.                     wc.DownloadFile("http://batch-external-controls.site88.net/external_controls/" + external_command_name_MIN + ".ini", external_command_name_MIN + ext);
  792.                     wc.Dispose();
  793.                     Console.Write("Téléchargement terminé !");
  794.                     Environment.Exit(0);
  795.                 }
  796.                 catch
  797.                 {
  798.                     WriteError("Erreur lors du téléchargement !");
  799.                     Environment.Exit(0);
  800.                 }
  801.             }
  802.             else
  803.             {
  804.                 WriteError("Cette commande externe n'existe pas !");
  805.                 Console.ReadKey();
  806.  
  807.             }
  808.  
  809.         }
  810.  
  811.         static void WriteError(string msg, params object[] objs)
  812.         {
  813.             var oldFGcolor = Console.ForegroundColor;
  814.             Console.Write("[");
  815.             Console.ForegroundColor = ConsoleColor.Red;
  816.             Console.Write("ERROR");
  817.             Console.ForegroundColor = oldFGcolor;
  818.             Console.Write("] : ");
  819.             Console.WriteLine(msg, objs);
  820.         }
  821.         static void WriteInfo(string msg, params object[] objs)
  822.         {
  823.             var oldFGcolor = Console.ForegroundColor;
  824.             Console.Write("[");
  825.             Console.ForegroundColor = ConsoleColor.Green;
  826.             Console.Write("INFO");
  827.             Console.ForegroundColor = oldFGcolor;
  828.             Console.Write("] : ");
  829.             Console.WriteLine(msg, objs);
  830.         }
  831.         public static bool WebRequestTest()
  832.         {
  833.             string url = "http://www.google.com";
  834.             try
  835.             {
  836.                 System.Net.WebRequest myRequest = System.Net.WebRequest.Create(url);
  837.                 System.Net.WebResponse myResponse = myRequest.GetResponse();
  838.             }
  839.             catch (System.Net.WebException)
  840.             {
  841.                 return false;
  842.             }
  843.             return true;
  844.         }
  845.         static void install_file(string path, string new_line, string external_control)
  846.         {
  847.             Console.WriteLine("Installation en cours..");
  848.             try
  849.             {
  850.                 String temp = Path.GetTempPath();
  851.                 String path_copy = path+"-install";
  852.                 if (File.Exists(path_copy)) { File.Delete(path_copy); }
  853.                 File.Copy(path, path_copy);
  854.                 String final_file = null;
  855.                 StreamReader sr = new StreamReader(path_copy);
  856.                 String line_read = null;
  857.                 String find_line = null;
  858.                 String line_read_ = null;
  859.                 bool install_generate_code = false;
  860.                 while (!sr.EndOfStream)
  861.                 {
  862.                     line_read = sr.ReadLine();
  863.                     if (line_read.Length >= 13 + external_control.Length)
  864.                     {
  865.                         find_line = line_read.Substring(0, 13 + external_control.Length);
  866.                     }
  867.                     if (line_read == ":End_GATE_install_" + external_control)
  868.                     {
  869.                         install_generate_code = false;
  870.                     }
  871.                     else
  872.                     {
  873.                         if (line_read == ":GATE_install_" + external_control)
  874.                         {
  875.                             install_generate_code = true;
  876.                             final_file = (final_file + (line_read + "\r\n"));
  877.                             StreamReader file_ = new StreamReader(temp + "generate_ex.php");
  878.                             while (!file_.EndOfStream)
  879.                             {
  880.                                 line_read_ = file_.ReadLine();
  881.                                 final_file = (final_file + (line_read_ + "\r\n"));
  882.                             }
  883.                             file_.Close();
  884.                             final_file = (final_file + (":End_GATE_install_" + external_control));
  885.                         }
  886.                         else
  887.                         {
  888.                             if (!install_generate_code)
  889.                             {
  890.                                 if (find_line == "set \"version_" + external_control)
  891.                                 {
  892.                                     if (!sr.EndOfStream)
  893.                                     {
  894.                                         final_file = (final_file + (new_line + "\r\n"));
  895.                                     }
  896.                                     else
  897.                                     {
  898.                                         final_file = (final_file + (new_line));
  899.                                     }
  900.                                 }
  901.                                 else
  902.                                 {
  903.                                     if (!sr.EndOfStream)
  904.                                     {
  905.                                         final_file = (final_file + (line_read + "\r\n"));
  906.                                     }
  907.                                     else
  908.                                     {
  909.                                         final_file = (final_file + (line_read));
  910.                                     }
  911.                                 }
  912.                             }
  913.                         }
  914.                     }
  915.                 }
  916.                 sr.Close();
  917.                 StreamWriter sr2 = new StreamWriter(path_copy);
  918.                 sr2.WriteLine(final_file);
  919.                 sr2.Close();
  920.                 String lines = "@ping localhost -n 2 >nul \r\ndel \"" + path + "\"\r\nren \"" + path_copy + "\" \"" + path + "\"\r\ngate /start \"" + path + "\"\r\ndel GATE_Install.bat";
  921.                 StreamWriter file_install = new StreamWriter("GATE_Install.bat");
  922.                 file_install.WriteLine(lines);
  923.                 file_install.Close();
  924.                 Process[] myProcesses;
  925.                 myProcesses = Process.GetProcessesByName("cmd");
  926.                 foreach (Process myProcess in myProcesses)
  927.                 {
  928.                     Console.WriteLine("Installation terminée !");
  929.                     Process.Start("GATE_Install.bat");
  930.                     myProcess.CloseMainWindow();
  931.                 }
  932.             }
  933.             catch (Exception e)
  934.             {
  935.                 WriteError("Echec de l'installation de la nouvelle version, veuillez vérifier la syntaxe de la commande.");
  936.                 Console.WriteLine(e);
  937.                 Environment.Exit(0);
  938.             }
  939.         }
  940.     }
  941. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement