public void UpdateInterfaceStyle() { // AppleInterfaceStyle is user-level settings. // Setting the 'Dark mode' in preferences, don't change the interface style of the ROOT user, and AirVPN client run as root. // We detect the settings when this software relaunch itself, and here we update accordly the settings of the current (ROOT) user. string defaultsPath = Core.Platform.Instance.LocateExecutable("defaults"); if (defaultsPath != "") { // If 'white', return error in StdErr and empty in StdOut. SystemShell s = new SystemShell(); s.Path = defaultsPath; s.Arguments.Add("read"); s.Arguments.Add("-g"); s.Arguments.Add("AppleInterfaceStyle"); s.Run(); string rootColorMode = s.StdOut.Trim().ToLowerInvariant(); if (rootColorMode == "") { rootColorMode = "light"; } string argsColorMode = Engine.Instance.Storage.Get("gui.osx.style"); if (rootColorMode != argsColorMode) { if (argsColorMode == "dark") { Core.SystemShell.Shell(defaultsPath, new string[] { "write", "-g", "AppleInterfaceStyle", "Dark" }); } else { Core.SystemShell.Shell(defaultsPath, new string[] { "remove", "-g", "AppleInterfaceStyle" }); } } } }
public override bool OnCheckEnvironmentSession() { if (Engine.Instance.Storage.GetLower("ipv6.mode") == "disable") { string keyname = "net.ipv6.conf.all.disable_ipv6"; string ipV6 = ""; string sysctlPath = LocateExecutable("sysctl"); if (sysctlPath != "") { ipV6 = SystemShell.Shell1(sysctlPath, keyname).Replace(keyname, "").Trim().Trim(new char[] { '=', ' ', '\n', '\r' }); // 2.10.1 } if (ipV6 == "0") { if (Engine.Instance.OnAskYesNo(Messages.IpV6Warning)) { Engine.Instance.Storage.Set("ipv6.mode", "none"); } else { return(false); } } else if (ipV6 == "1") { // Already disabled } else { Engine.Instance.Logs.Log(LogType.Verbose, Messages.IpV6WarningUnableToDetect); } } return(true); }
public override void OnUpdateIps() { if (m_activated == false) { return; } IpAddresses ipsFirewalled = GetAllIps(true); // ClodoTemp: can be 'false', but pinger don't work string ipList = ""; foreach (IpAddress ip in ipsFirewalled.IPs) { if (ipList != "") { ipList += ","; } ipList += ip.ToCIDR(); } if (ipList != m_lastestIpList) { if (m_lastestIpList != "") { SystemShell.ShellCmd("netsh advfirewall firewall set rule name=\"Eddie - Out - AllowAirIPS\" dir=out new action=allow remoteip=\"" + ipList + "\""); } else { SystemShell.ShellCmd("netsh advfirewall firewall add rule name=\"Eddie - Out - AllowAirIPS\" dir=out action=allow remoteip=\"" + ipList + "\""); } m_lastestIpList = ipList; } }
public override bool OnInit() { base.OnInit(); m_version = SystemShell.Shell1(LocateExecutable("uname"), "-a"); m_architecture = NormalizeArchitecture(SystemShell.Shell1(LocateExecutable("uname"), "-m").Trim()); try { bool result = (NativeMethods.Init() == 0); if (result == false) { throw new Exception("fail"); } NativeMethods.Signal((int)NativeMethods.Signum.SIGHUP, SignalCallback); NativeMethods.Signal((int)NativeMethods.Signum.SIGINT, SignalCallback); NativeMethods.Signal((int)NativeMethods.Signum.SIGTERM, SignalCallback); NativeMethods.Signal((int)NativeMethods.Signum.SIGUSR1, SignalCallback); NativeMethods.Signal((int)NativeMethods.Signum.SIGUSR2, SignalCallback); } catch { Console.WriteLine("Unable to initialize native library. Maybe a CPU architecture issue."); return(false); } return(true); }
public override IpAddresses DetectDNS() { IpAddresses list = new IpAddresses(); // Method1: Don't return DHCP DNS string networksetupPath = LocateExecutable("networksetup"); if (networksetupPath != "") { string[] interfaces = GetInterfaces(); foreach (string i in interfaces) { string i2 = i.Trim(); string current = SystemShell.Shell(networksetupPath, new string[] { "-getdnsservers", SystemShell.EscapeInsideQuote(i2) }); foreach (string line in current.Split('\n')) { string field = line.Trim(); list.Add(field); } } } // Method2 - More info about DHCP DNS string scutilPath = LocateExecutable("scutil"); if (scutilPath != "") { string scutilOut = SystemShell.Shell1(scutilPath, "--dns"); List <List <string> > result = UtilsString.RegExMatchMulti(scutilOut.Replace(" ", ""), "nameserver\\[[0-9]+\\]:([0-9:\\.]+)"); foreach (List <string> match in result) { foreach (string field in match) { list.Add(field); } } } // Method3 - Compatibility if (FileExists("/etc/resolv.conf")) { string o = FileContentsReadText("/etc/resolv.conf"); foreach (string line in o.Split('\n')) { if (line.Trim().StartsWith("#")) { continue; } if (line.Trim().StartsWith("nameserver")) { string field = line.Substring(11).Trim(); list.Add(field); } } } return(list); }
public override void Init() { base.Init(); m_iptablesVersion = SystemShell.Shell1(Platform.Instance.LocateExecutable("iptables"), "--version"); m_iptablesVersion = m_iptablesVersion.Replace("iptables v", ""); }
protected override void OpenDirectoryInFileManagerEx(string path) { // TOFIX Don't work well on all distro string args = " - " + m_logname + " -c 'xdg-open \"" + SystemShell.EscapePath(path) + "\"'"; // IJTF2 // TOCHECK Shell("su", args, false); }
public override bool OnIpV6Restore() { foreach (IpV6ModeEntry entry in m_listIpV6Mode) { if (entry.Mode == "Off") { ShellCmd("networksetup -setv6off \"" + SystemShell.EscapeInsideQuote(entry.Interface) + "\""); } else if (entry.Mode == "Automatic") { ShellCmd("networksetup -setv6automatic \"" + SystemShell.EscapeInsideQuote(entry.Interface) + "\""); } else if (entry.Mode == "LinkLocal") { ShellCmd("networksetup -setv6LinkLocal \"" + SystemShell.EscapeInsideQuote(entry.Interface) + "\""); } else if (entry.Mode == "Manual") { ShellCmd("networksetup -setv6manual \"" + SystemShell.EscapeInsideQuote(entry.Interface) + "\" " + entry.Address + " " + entry.PrefixLength + " " + entry.Router); // IJTF2 // TOCHECK } Engine.Instance.Logs.Log(LogType.Verbose, MessagesFormatter.Format(Messages.NetworkAdapterIpV6Restored, entry.Interface)); } m_listIpV6Mode.Clear(); Recovery.Save(); base.OnIpV6Restore(); return(true); }
public override void OnUpdateIps() { if (m_activated == false) { return; } IpAddresses ipsWhiteListOutgoing = GetIpsWhiteListOutgoing(true); // ClodoTemp: can be 'false', but pinger don't work string currentIpsWhiteListOutgoing = ""; foreach (IpAddress ip in ipsWhiteListOutgoing.IPs) { if (currentIpsWhiteListOutgoing != "") { currentIpsWhiteListOutgoing += ","; } currentIpsWhiteListOutgoing += ip.ToCIDR(); } if (currentIpsWhiteListOutgoing != m_lastestIpsWhiteListOutgoing) { if (m_lastestIpsWhiteListOutgoing != "") { SystemShell.ShellCmd("netsh advfirewall firewall set rule name=\"Eddie - Out - Allow IPs\" dir=out new action=allow remoteip=\"" + currentIpsWhiteListOutgoing + "\""); } else { SystemShell.ShellCmd("netsh advfirewall firewall add rule name=\"Eddie - Out - Allow IPs\" dir=out action=allow remoteip=\"" + currentIpsWhiteListOutgoing + "\""); } m_lastestIpsWhiteListOutgoing = currentIpsWhiteListOutgoing; } }
public override void OnReport(Report report) { base.OnReport(report); report.Add("ifconfig", (LocateExecutable("ifconfig") != "") ? SystemShell.Shell0(LocateExecutable("ifconfig")) : "'ifconfig' " + Messages.NotFound); report.Add("netstat /rnl", (LocateExecutable("netstat") != "") ? SystemShell.Shell1(LocateExecutable("netstat"), "/rnl") : "'netstat' " + Messages.NotFound); }
public override bool RestartAsRoot() { string path = Platform.Instance.GetExecutablePath(); List <string> args = CommandLine.SystemEnvironment.GetFullArray(); string defaultsPath = Core.Platform.Instance.LocateExecutable("defaults"); if (defaultsPath != "") { // If 'white', return error in StdErr and empty in StdOut. SystemShell s = new SystemShell(); s.Path = defaultsPath; s.Arguments.Add("read"); s.Arguments.Add("-g"); s.Arguments.Add("AppleInterfaceStyle"); s.Run(); string colorMode = s.StdOut.Trim().ToLowerInvariant(); if (colorMode == "dark") { args.Add("gui.osx.style=\"dark\""); } } RootLauncher.LaunchExternalTool(path, args.ToArray()); return(true); }
public override IpAddresses ResolveDNS(string host) { IpAddresses result = new IpAddresses(); string getentPath = LocateExecutable("getent"); if (getentPath != "") { // Note: CNAME record are automatically followed. SystemShell s = new SystemShell(); s.Path = getentPath; s.Arguments.Add("ahosts"); s.Arguments.Add(SystemShell.EscapeHost(host)); s.NoDebugLog = true; if (s.Run()) { string o = s.Output; o = o.CleanSpace(); foreach (string line in o.Split('\n')) { string[] fields = line.Split(' '); if (fields.Length < 2) { continue; } if (fields[1].Trim() != "STREAM") { continue; } result.Add(fields[0].Trim()); } } } return(result); }
public override bool OnIpV6Restore() { foreach (IpV6ModeEntry entry in m_listIpV6Mode) { if (entry.Mode == "Off") { SystemShell.Shell("/usr/sbin/networksetup", new string[] { "-setv6off", SystemShell.EscapeInsideQuote(entry.Interface) }); } else if (entry.Mode == "Automatic") { SystemShell.Shell("/usr/sbin/networksetup", new string[] { "-setv6automatic", SystemShell.EscapeInsideQuote(entry.Interface) }); } else if (entry.Mode == "LinkLocal") { SystemShell.Shell("/usr/sbin/networksetup", new string[] { "-setv6LinkLocal", SystemShell.EscapeInsideQuote(entry.Interface) }); } else if (entry.Mode == "Manual") { SystemShell.Shell("/usr/sbin/networksetup", new string[] { "-setv6manual", SystemShell.EscapeInsideQuote(entry.Interface), entry.Address, entry.PrefixLength, entry.Router }); } Engine.Instance.Logs.Log(LogType.Verbose, MessagesFormatter.Format(Messages.NetworkAdapterIpV6Restored, entry.Interface)); } m_listIpV6Mode.Clear(); Recovery.Save(); base.OnIpV6Restore(); return(true); }
public override void Deactivation() { base.Deactivation(); // Restore system rules SystemShell s = new SystemShell(); s.Path = m_pfctlPath; s.Arguments.Add("-v"); s.Arguments.Add("-f"); s.Arguments.Add(SystemShell.EscapePath("/etc/pf.conf")); s.Run(); if (m_filePfConf != null) { m_filePfConf.Close(); m_filePfConf = null; } if (m_prevActive) { } else { SystemShell.Shell1(m_pfctlPath, "-d"); } }
public override string GetExecutablePathEx() { // We use this because querying .Net Assembly (what the base class do) doesn't work within Mkbundle. // TOFIX: Linux and OS X version are different, merge. Probably OS X it's more a clean approach. // Removed in 2.11 to avoid dependencies with libMonoPosixHelper.so // Useless, still required, but at least it's an external requirement. /* * string output = ""; * StringBuilder builder = new StringBuilder(8192); * if (Syscall.readlink("/proc/self/exe", builder) >= 0) * output = builder.ToString(); */ int pid = Process.GetCurrentProcess().Id; string output = SystemShell.Shell1(LocateExecutable("readlink"), "/proc/" + pid.ToString() + "/exe"); if ((output != "") && (new FileInfo(output).Name.ToLowerInvariant().StartsWith("mono", StringComparison.InvariantCulture))) { // Exception: Assembly directly load by Mono output = base.GetExecutablePathEx(); } return(output); }
public override void FlushDNS() { base.FlushDNS(); // 10.5 - 10.6 string dscacheutilPath = LocateExecutable("dscacheutil"); if (dscacheutilPath != "") { SystemShell.Shell1(dscacheutilPath, "-flushcache"); } // 10.7 - 10.8 - 10.9 - 10.10.4 - 10.11 - Sierra 10.12.0 string killallPath = LocateExecutable("killall"); if (killallPath != "") { SystemShell.Shell2(killallPath, "-HUP", "mDNSResponder"); } // 10.10.0 - 10.10.3 string discoveryutilPath = LocateExecutable("discoveryutil"); if (discoveryutilPath != "") { SystemShell.Shell1(discoveryutilPath, "udnsflushcaches"); SystemShell.Shell1(discoveryutilPath, "mdnsflushcache"); } }
public override bool FileImmutableGet(string path) { // We don't find a better direct method in Mono/Posix without adding ioctl references // The list of flags can be different between Linux distro (for example 16 on Debian, 19 on Manjaro) if (FileExists(path) == false) { return(false); } string result = ShellCmd("lsattr \"" + SystemShell.EscapePath(path) + "\"", true); // noDebugLog=true to avoid log recursion. /* // < 2.11.11 * if (result.IndexOf(' ') != 16) * return false; * if (result[4] == 'i') * return true; */ if (result.StartsWith("lsattr: ")) // Generic error { return(false); } if (result.IndexOf(' ') != -1) { result = result.Substring(0, result.IndexOf(' ')); } return(result.IndexOf("-i-") != -1); }
public override bool OnDnsSwitchDo(IpAddresses dns) { string mode = Engine.Instance.Storage.GetLower("dns.mode"); if (mode == "auto") { string[] interfaces = GetInterfaces(); foreach (string i in interfaces) { string i2 = i.Trim(); string currentStr = SystemShell.Shell("/usr/sbin/networksetup", new string[] { "-getdnsservers", SystemShell.EscapeInsideQuote(i2) }); // v2 IpAddresses current = new IpAddresses(); foreach (string line in currentStr.Split('\n')) { string ip = line.Trim(); if (IpAddress.IsIP(ip)) { current.Add(ip); } } if (dns.Equals(current) == false) { DnsSwitchEntry e = new DnsSwitchEntry(); e.Name = i2; e.Dns = current.Addresses; m_listDnsSwitch.Add(e); SystemShell s = new SystemShell(); s.Path = LocateExecutable("networksetup"); s.Arguments.Add("-setdnsservers"); s.Arguments.Add(SystemShell.EscapeInsideQuote(i2)); if (dns.IPs.Count == 0) { s.Arguments.Add("empty"); } else { foreach (IpAddress ip in dns.IPs) { s.Arguments.Add(ip.Address); } } s.Run(); Engine.Instance.Logs.Log(LogType.Verbose, MessagesFormatter.Format(Messages.NetworkAdapterDnsDone, i2, ((current.Count == 0) ? "Automatic" : current.Addresses), dns.Addresses)); } } Recovery.Save(); } base.OnDnsSwitchDo(dns); return(true); }
public override bool IsAdmin() { // With root privileges by RootLauncher.cs, Environment.UserName still return the normal username, 'whoami' return 'root'. string u = SystemShell.Shell("/usr/bin/whoami", new string[] { }).ToLowerInvariant().Trim(); //return true; // Uncomment for debugging return(u == "root"); }
public override void FileImmutableSet(string path, bool value) { if (FileExists(path)) { string flag = (value ? "+i" : "-i"); ShellCmd("chattr " + flag + " \"" + SystemShell.EscapePath(path) + "\""); } }
public override void OnReport(Report report) { base.OnReport(report); report.Add("ip addr show", (LocateExecutable("ip") != "") ? SystemShell.Shell2(LocateExecutable("ip"), "addr", "show") : "'ip' " + LanguageManager.GetText("NotFound")); report.Add("ip link show", (LocateExecutable("ip") != "") ? SystemShell.Shell2(LocateExecutable("ip"), "link", "show") : "'ip' " + LanguageManager.GetText("NotFound")); report.Add("ip -4 route show", (LocateExecutable("ip") != "") ? SystemShell.Shell3(LocateExecutable("ip"), "-4", "route", "show") : "'ip' " + LanguageManager.GetText("NotFound")); report.Add("ip -6 route show", (LocateExecutable("ip") != "") ? SystemShell.Shell3(LocateExecutable("ip"), "-6", "route", "show") : "'ip' " + LanguageManager.GetText("NotFound")); }
public override void OpenFolder(string path) { SystemShell s = new SystemShell(); s.Path = LocateExecutable("xdg-open"); s.Arguments.Add(SystemShell.EscapePath(path)); s.WaitEnd = false; s.Run(); }
public override void EnsureExecutablePermissions(string path) { if ((path == "") || (Platform.Instance.FileExists(path) == false)) { return; } ShellCmd("chmod +x \"" + SystemShell.EscapePath(path) + "\""); }
public override void OpenUrl(string url) { SystemShell s = new SystemShell(); s.Path = LocateExecutable("xdg-open"); s.Arguments.Add(url); s.WaitEnd = false; s.Run(); }
public string DoIptablesShell(string exe, string args, bool fatal) { lock (this) { // 2.14.0 /* * SystemShell s = new SystemShell(); * s.Path = Platform.Instance.LocateExecutable(exe); * if (UtilsCore.CompareVersions(m_iptablesVersion, "1.4.21") >= 0) * { * // 2.13.6 - The version 1.4.21 is generic Debian8. I don't find in official * // changelogs https://www.netfilter.org/projects/iptables/downloads.html * // the correct version. For sure don't exists in 1.4.14 of Debian7. * args = "--wait " + args; * } * if (args != "") * s.Arguments.Add(args); // Exception: all arguments as one, it works. * if (fatal) * s.ExceptionIfFail = true; * s.Run(); * return s.StdOut; */ // 2.14.1: Previous version use --wait if iptables >1.4.21. But there are issues about distro, even in the latest Debian unstable (2018). int nTry = 10; string lastestOutput = ""; for (int iTry = 0; iTry < 10; iTry++) { SystemShell s = new SystemShell(); s.Path = Platform.Instance.LocateExecutable(exe); if (args != "") { s.Arguments.Add(args); // Exception: all arguments as one, it works. } if ((fatal) && (iTry == nTry - 1)) { s.ExceptionIfFail = true; } s.Run(); if (s.StdErr.ToLowerInvariant().Contains("temporarily unavailable")) // Older Debian (iptables without --wait) { System.Threading.Thread.Sleep(500); continue; } if (s.StdErr.ToLowerInvariant().Contains("xtables lock")) // Newest Debian (iptables with --wait but not automatic) { System.Threading.Thread.Sleep(500); continue; } lastestOutput = s.StdOut; return(lastestOutput); } return(lastestOutput); } }
public override void OnReport(Report report) { base.OnReport(report); report.Add("UID", Conversions.ToString(m_uid)); report.Add("LogName", m_logname); report.Add("ip addr show", (LocateExecutable("ip") != "") ? SystemShell.Shell2(LocateExecutable("ip"), "addr", "show") : "'ip' " + Messages.NotFound); report.Add("ip link show", (LocateExecutable("ip") != "") ? SystemShell.Shell2(LocateExecutable("ip"), "link", "show") : "'ip' " + Messages.NotFound); report.Add("ip route show", (LocateExecutable("ip") != "") ? SystemShell.Shell2(LocateExecutable("ip"), "route", "show") : "'ip' " + Messages.NotFound); }
public override List <RouteEntry> RouteList() { List <RouteEntry> entryList = new List <RouteEntry>(); // TOFIX: "route" not available on recent Linux systems. // Need to be adapted to "ip". But it's used only for "Remove default gateway" feature, useless for a lots of reason, deprecated soon. string routePath = LocateExecutable("route"); if (routePath == "") { Engine.Instance.Logs.Log(LogType.Error, "'route' " + Messages.NotFound); } else { string result = SystemShell.Shell2(routePath, "-n", "-ee"); string[] lines = result.Split('\n'); foreach (string line in lines) { string[] fields = Utils.StringCleanSpace(line).Split(' '); if (fields.Length == 11) { RouteEntry e = new RouteEntry(); e.Address = fields[0]; e.Gateway = fields[1]; e.Mask = fields[2]; e.Flags = fields[3].ToUpperInvariant(); e.Metrics = fields[4]; // Ref, Use ignored e.Interface = fields[7]; e.Mss = fields[8]; e.Window = fields[9]; e.Irtt = fields[10]; if (e.Address.Valid == false) { continue; } if (e.Gateway.Valid == false) { continue; } if (e.Mask.Valid == false) { continue; } entryList.Add(e); } } } return(entryList); }
public override bool OnDnsSwitchDo(string dns) { string mode = Engine.Instance.Storage.GetLower("dns.mode"); if (mode == "auto") { string[] interfaces = GetInterfaces(); foreach (string i in interfaces) { string i2 = i.Trim(); string current = ShellCmd("networksetup -getdnsservers \"" + SystemShell.EscapeInsideQuote(i2) + "\""); // v2 List <string> ips = new List <string>(); foreach (string line in current.Split('\n')) { string ip = line.Trim(); if (IpAddress.IsIP(ip)) { ips.Add(ip); } } if (ips.Count != 0) { current = String.Join(",", ips.ToArray()); } else { current = ""; } if (current != dns) { // Switch Engine.Instance.Logs.Log(LogType.Verbose, MessagesFormatter.Format(Messages.NetworkAdapterDnsDone, i2, ((current == "") ? "Automatic" : current), dns)); DnsSwitchEntry e = new DnsSwitchEntry(); e.Name = i2; e.Dns = current; m_listDnsSwitch.Add(e); string dns2 = dns.Replace(",", "\" \""); ShellCmd("networksetup -setdnsservers \"" + SystemShell.EscapeInsideQuote(i2) + "\" \"" + dns2 + "\""); // IJTF2 eh? } } Recovery.Save(); } base.OnDnsSwitchDo(dns); return(true); }
public override void OnJsonNetworkInfo(Json jNetworkInfo) { // Step1: Set IPv6 support to true by default. // From base virtual, always 'false'. Missing Mono implementation? // After for interfaces listed by 'networksetup -listallhardwareports' we detect specific support. foreach (Json jNetworkInterface in jNetworkInfo["interfaces"].Json.GetArray()) { jNetworkInterface["support_ipv6"].Value = true; } // Step2: Query 'networksetup -listallhardwareports' to obtain a more accurate device friendly names. string networksetupPath = LocateExecutable("networksetup"); if (networksetupPath != "") { string nsOutput = SystemShell.Shell1(networksetupPath, "-listallhardwareports"); string lastName = ""; foreach (string line in nsOutput.Split('\n')) { if (line.StartsWith("Hardware Port: ", StringComparison.InvariantCulture)) { lastName = line.Substring(15).Trim(); } if (line.StartsWith("Device:", StringComparison.InvariantCulture)) { string deviceId = line.Substring(7).Trim(); foreach (Json jNetworkInterface in jNetworkInfo["interfaces"].Json.GetArray()) { if (jNetworkInterface["id"].Value as string == deviceId) { // Set friendly name jNetworkInterface["friendly"].Value = lastName; // Detect IPv6 support string getInfo = SystemShell.Shell(LocateExecutable("networksetup"), new string[] { "-getinfo", SystemShell.EscapeInsideQuote(lastName) }); string mode = getInfo.RegExMatchOne("^IPv6: (.*?)$").Trim(); if (mode == "Off") { jNetworkInterface["support_ipv6"].Value = false; } else { jNetworkInterface["support_ipv6"].Value = true; } break; } } } } } }
public override void DeallowProgram(string path, string name, string guid) { base.DeallowProgram(path, name, guid); if (path == Software.GetTool("curl").Path) { return; } SystemShell.ShellCmd("netsh advfirewall firewall delete rule name=\"Eddie - Out - AllowProgram - " + guid + "\""); }