private void FormRemoveoLoad(object sender, EventArgs e) { if (Config.BlockOptions.All(o => !File.Exists(o.IpFileName) && !File.Exists(o.DomainsFileName)) && !File.Exists(Config.CustomOption.IpFileName) && !File.Exists(Config.CustomOption.DomainsFileName)) { var answer = MessageBox.Show("No hosts file found. Download hosts file ?", "Hosts not found", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (answer == DialogResult.OK) { foreach (var option in Config.BlockOptions) { var remoteFileSize = Common.GetRemoteFileSize(option.IpUrl); var localFileSize = File.Exists(option.IpFileName) ? new FileInfo(option.IpFileName).Length : 0; if (remoteFileSize != localFileSize) { var frm = new FormDownload(option.IpUrl, option.IpFileName); frm.ShowDialog(this); } remoteFileSize = Common.GetRemoteFileSize(option.DomainsUrl); localFileSize = File.Exists(option.DomainsFileName) ? new FileInfo(option.DomainsFileName).Length : 0; if (remoteFileSize != localFileSize) { var frm = new FormDownload(option.DomainsUrl, option.DomainsFileName); frm.ShowDialog(this); } } } else { BeginInvoke((MethodInvoker)(() => Close())); return; } } if (!Firewall.Initialize(out var errorMessage)) { Common.Error(errorMessage); BeginInvoke((MethodInvoker)(() => Close())); return; } if (!Firewall.IsFirewallEnabled(out errorMessage)) { Common.Error(errorMessage); BeginInvoke((MethodInvoker)(() => Close())); return; } if (Config.Activated /*&& Firewall.IsRuleEnabled("removeo")*/) { SetStatus("Removeo activated", Color.Blue); } else { Config.Activated = false; buttonDeactivate.Enabled = false; SetStatus("Removeo deactivated", Color.Red); } }
private bool DeactivateFirewallRules() { try { Firewall.RemoveRules("Removeo"); HostsFile.RemoveDomainsFromHosts(Config.SystemHostsFile); } catch (Exception e) { lastError = e.Message; return(false); } return(true); }
private bool ActivateFirewallRules() { try { Firewall.RemoveRules("Removeo"); HostsFile.RemoveDomainsFromHosts(Config.SystemHostsFile); foreach (var option in new List <Config.BlockOption>(Config.BlockOptions.Where(opt => opt.Enabled)) { Config.CustomOption }) { if (File.Exists(option.IpFileName)) { var ipv4 = Common.ReadUniqueIpv4(option.IpFileName); // add ip rules if (ipv4.Count > 0) { foreach (var chunk in Common.Split(ipv4, 1000)) { var address = string.Join(",", chunk); Firewall.AddRule("Removeo", address); } } } if (File.Exists(option.DomainsFileName)) { var domains = File.ReadAllLines(option.DomainsFileName).Distinct().ToList(); // add domains to hosts file if (domains.Count > 0) { HostsFile.AppendDomainsToHosts(Config.SystemHostsFile, domains); } } } } catch (Exception e) { lastError = e.Message; return(false); } return(true); }