public static MyWebClient GetHttp(string url, MyWebClient wc) { var response = wc.DownloadString(new Uri(url)); logger.Info(Environment.NewLine + "Scaricata prima pagina: {0}", Settings.BaseURI); logger.Info("ResponseHeaders:" + Environment.NewLine + "{0}", wc.ResponseHeaders); var cc = wc._mContainer.GetCookies(new Uri(url)); foreach (var cookie in cc) { logger.Info("Cookie: {0}", cookie); } logger.Info("Redirect Location: {0}", wc._responseUri); foreach (var param in wc.responseParams) { logger.Info("Fragment: {0} = {1}", param.Key, param.Value); } return wc; }
public static bool MFPManual(MyWebClient wc) { // Se siamo nella pagina giusta procedo alla costruzione della POST finale if (wc.responseParams.ContainsKey("identifycode")) { StringBuilder sb = new StringBuilder( "current=2" + "&step=3" + "&ctrl=" + "&portmap_action=manual_setup" + "&channel=" + wc.responseParams["channel"] + "&account=" + wc.responseParams["account"] + "&username="******"username"] + "&service=" + wc.responseParams["service"] + "&actionid=" + "&status=" + wc.responseParams["status"] + "&segmento=" + wc.responseParams["segmento"] + "&selcode=" + wc.responseParams["selcode"] + "&origin=" + wc.responseParams["origin"] + "&checksum=" + wc.responseParams["checksum"] + "&identifycode=" + wc.responseParams["identifycode"] + "&previous=1" + "&aVarsParam=HTTP/1.1"); logger.Info("http://fastmomi.fastweb.it/app/services/cfg-ngrg/RES-Bought.php?" + sb.ToString()); PostHttp(wc, "http://fastmomi.fastweb.it/app/services/cfg-ngrg/RES-Bought.php?", sb.ToString()); // per ora non scriviamo la risposta //logger.Info(response); //File.WriteAllText("response.html", response); Dictionary <string, string> dic = GetComputersFromHtml(wc); string mymac = dic[GetIpAddress()]; wc.responseParams["pmaction"] = "UPDATE"; //wc.responseParams["ipCpe"]; //gia assegnato wc.responseParams["ID"] = ""; //chissa' che cosa e' wc.responseParams["ExternalPort"] = EmuleAdunanzaGetTcpPort(); wc.responseParams["InternalPort"] = EmuleAdunanzaGetTcpPort(); wc.responseParams["Protocol"] = "TCP"; wc.responseParams["InternalClient"] = GetIpAddress(); wc.responseParams["Description"] = "AdunanzA TCP"; wc.responseParams["Chaddr"] = mymac; StringBuilder sb2 = new StringBuilder( "pmaction=" + wc.responseParams["pmaction"] + "&ipCpe=" + wc.responseParams["ipCpe"] + "&ID=" + "&ExternalPort=" + wc.responseParams["ExternalPort"] + "&InternalPort=" + wc.responseParams["InternalPort"] + "&Protocol=" + wc.responseParams["Protocol"] + "&InternalClient=" + wc.responseParams["InternalClient"] + "&Description=" + wc.responseParams["Description"] + "&Chaddr=" + wc.responseParams["Chaddr"] ); //"pmaction=UPDATE&ipCpe=10.45.43.222&ID=&ExternalPort=7002&InternalPort=7002&Protocol=TCP&InternalClient=192.168.1.129&Description=&Chaddr=4c:ed:de:e8:7e:73"; var response = wc.UploadString("http://fastmomi.fastweb.it/app/services/cfg-ngrg/Ajax_writePortMapping.php", "POST", sb2.ToString()); //logger.Info(response); if (wc._responseUri.ToString().Contains("esito") && wc._responseUri.ToString().Contains("OK")) { logger.Info("Procedura apertura porte Manuale avvenuta con successo!"); return(true); } else { logger.Error("Procedura apertura porte Manuale Fallita."); return(false); } } else { logger.Info("Procedura apertura porte automatica Fallita."); return(false); } }
public static Dictionary<string, string> GetComputersFromHtml(MyWebClient wc) { #if !Release //Read from file for now StreamReader sr = new StreamReader("response.html"); var response = File.ReadAllText("response.html"); // End read from file #endif HtmlNode html; var doc = new HtmlDocument(); doc.LoadHtml(response); html = doc.DocumentNode; var inputs = html.CssSelect("input[type=hidden]#ipCpe"); wc.responseParams["ipCpe"] = inputs.First().Attributes["value"].Value; var optiontags = html.CssSelect("option"); var query = from x in optiontags where x.Attributes["value"].Value.StartsWith("192.168") && x.Attributes["value"].Value != x.Attributes["label"].Value select new { ip = x.Attributes["value"].Value.ToString(), macAddress = x.Attributes["label"].Value }; Dictionary<string, string> computers = new Dictionary<string, string>(); foreach (var pc in query) { computers.Add(pc.ip, pc.macAddress); } return computers; }
public static void PostHttp(MyWebClient wc, string baseUrl, string paramConcat) { try { wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; var response = wc.UploadString(baseUrl, paramConcat); } catch (WebException ex) { logger.Error("Impossibile eseguire la post: " + ex.Message); throw new Exception("Impossibile eseguire la post: " + ex.Message); } Console.ForegroundColor = ConsoleColor.Green; logger.Info("ResponseHeaders:" + Environment.NewLine + "{0}", wc.ResponseHeaders); var cc = wc._mContainer.GetCookies(new Uri("http://www.fastweb.it")); foreach (var cookie in cc) { Console.ForegroundColor = ConsoleColor.Magenta; logger.Info("Cookie: {0}", cookie); } Console.ForegroundColor = ConsoleColor.White; logger.Info("Redirect Location: {0}", wc._responseUri); foreach (var param in wc.responseParams) { Console.ForegroundColor = ConsoleColor.Yellow; logger.Info("Fragment: {0} = {1}", param.Key, param.Value); } }
public static bool MFPManual(MyWebClient wc) { // Se siamo nella pagina giusta procedo alla costruzione della POST finale if (wc.responseParams.ContainsKey("identifycode")) { StringBuilder sb = new StringBuilder( "current=2" + "&step=3" + "&ctrl=" + "&portmap_action=manual_setup" + "&channel=" + wc.responseParams["channel"] + "&account=" + wc.responseParams["account"] + "&username="******"username"] + "&service=" + wc.responseParams["service"] + "&actionid=" + "&status=" + wc.responseParams["status"] + "&segmento=" + wc.responseParams["segmento"] + "&selcode=" + wc.responseParams["selcode"] + "&origin=" + wc.responseParams["origin"] + "&checksum=" + wc.responseParams["checksum"] + "&identifycode=" + wc.responseParams["identifycode"] + "&previous=1" + "&aVarsParam=HTTP/1.1"); logger.Info("http://fastmomi.fastweb.it/app/services/cfg-ngrg/RES-Bought.php?" + sb.ToString()); PostHttp(wc, "http://fastmomi.fastweb.it/app/services/cfg-ngrg/RES-Bought.php?", sb.ToString()); // per ora non scriviamo la risposta //logger.Info(response); //File.WriteAllText("response.html", response); Dictionary<string, string> dic = GetComputersFromHtml(wc); string mymac = dic[GetIpAddress()]; wc.responseParams["pmaction"] = "UPDATE"; //wc.responseParams["ipCpe"]; //gia assegnato wc.responseParams["ID"] = ""; //chissa' che cosa e' wc.responseParams["ExternalPort"] = EmuleAdunanzaGetTcpPort(); wc.responseParams["InternalPort"] = EmuleAdunanzaGetTcpPort(); wc.responseParams["Protocol"] = "TCP"; wc.responseParams["InternalClient"] = GetIpAddress(); wc.responseParams["Description"] = "AdunanzA TCP"; wc.responseParams["Chaddr"] = mymac; StringBuilder sb2 = new StringBuilder( "pmaction=" + wc.responseParams["pmaction"] + "&ipCpe=" + wc.responseParams["ipCpe"] + "&ID=" + "&ExternalPort=" + wc.responseParams["ExternalPort"] + "&InternalPort=" + wc.responseParams["InternalPort"] + "&Protocol=" + wc.responseParams["Protocol"] + "&InternalClient=" + wc.responseParams["InternalClient"] + "&Description=" + wc.responseParams["Description"] + "&Chaddr=" + wc.responseParams["Chaddr"] ); //"pmaction=UPDATE&ipCpe=10.45.43.222&ID=&ExternalPort=7002&InternalPort=7002&Protocol=TCP&InternalClient=192.168.1.129&Description=&Chaddr=4c:ed:de:e8:7e:73"; var response = wc.UploadString("http://fastmomi.fastweb.it/app/services/cfg-ngrg/Ajax_writePortMapping.php", "POST", sb2.ToString()); //logger.Info(response); if (wc._responseUri.ToString().Contains("esito") && wc._responseUri.ToString().Contains("OK")) { logger.Info("Procedura apertura porte Manuale avvenuta con successo!"); return true; } else { logger.Error("Procedura apertura porte Manuale Fallita."); return false; } } else { logger.Info("Procedura apertura porte automatica Fallita."); return false; } }
public static bool MFPConfigurazioneRapida(MyWebClient wc) { // Se siamo nella pagina giusta procedo alla costruzione della POST finale if (wc.responseParams.ContainsKey("identifycode")) { StringBuilder sb = new StringBuilder( "current=2" + "&step=E" + "&ctrl=" + "&portmap_action=automatic_setup" + "&channel=" + wc.responseParams["channel"] + "&account=" + wc.responseParams["account"] + "&username="******"username"] + "&service=" + wc.responseParams["service"] + "&actionid=" + "&status=" + wc.responseParams["status"] + "&segmento=" + wc.responseParams["segmento"] + "&selcode=" + wc.responseParams["selcode"] + "&origin=" + wc.responseParams["origin"] + "&checksum=" + wc.responseParams["checksum"] + "&identifycode=" + wc.responseParams["identifycode"] + "&previous=1" + "&aVarsParam=HTTP/1.1"); logger.Info("http://fastmomi.fastweb.it/app/services/cfg-ngrg/RES-Bought.php?" + sb.ToString()); Utils.PostHttp(wc, "http://fastmomi.fastweb.it/app/services/cfg-ngrg/RES-Bought.php?", sb.ToString()); if (wc._responseUri.ToString().Contains("esito") && wc._responseUri.ToString().Contains("OK")) { logger.Info("Procedura apertura porte automatica avvenuta con successo!"); return true; } else { logger.Info("Procedura apertura porte automatica Fallita."); return false; } } else { logger.Info("Procedura apertura porte automatica Fallita."); return false; } }
private void frmMain_Load(object sender, EventArgs e) { logger = LogManager.GetLogger("ArieteMainFormUI"); logger.Info("MainFormUI created."); cookieContainer = new CookieContainer(); wc = new MyWebClient(); NatUtility.DeviceFound += DeviceFound; NatUtility.DeviceLost += DeviceLost; // Pulizia e creazione regole sul firewall per Ariete if (Utils.FirewallNetshIsRule()) { Utils.FirewallNetshDeleteRule(); } Utils.FirewallNetshExe(); if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) { ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment; this.Text += " v" + ad.CurrentVersion.ToString(); } else this.Text += " Debug"; }