private static bool validIp(string ip, bool silent) { string hostname = null; try { hostname = Dns.GetHostEntry(ip).HostName; } catch (Exception ex) { DebugHelper.Exception(ex); } foreach (var line in Settings.Default.ConnectionCheckIpHostList.Split('\n')) { var test = line.Replace(" ", string.Empty).Trim(); if (test.Length < 1) { continue; } var allowed = test.StartsWith("@"); if (allowed) { test = test.Substring(1, test.Length - 1); } if (Settings.Default.ConnectionCheckIpCheck) { // Check Ip range var m = new Regex(@"([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})-([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})").Match(test); if (m.Success) { var lowerip = IPAddress.Parse(m.Groups[1].Value); var higherip = IPAddress.Parse(m.Groups[2].Value); var inrange = new IPAddressRange(lowerip, higherip).IsInRange(IPAddress.Parse(ip)); if (inrange) { if (allowed) { DebugHelper.Write(string.Format("Valid Connection: IP {0} in range -> {1}-{2}", ip, lowerip, higherip)); return(true); } DebugHelper.Write(string.Format("Invalid Connection: IP {0} in range -> {1}-{2}", ip, lowerip, higherip)); return(false); } continue; } // Check single IP m = new Regex(@"([0-9*]{1,3}\.[0-9*]{1,3}\.[0-9*]{1,3}\.[0-9*]{1,3})").Match(test); if (m.Success) { test = m.Groups[1].Value; if (General.WildcardMatch(test, ip)) { if (allowed) { DebugHelper.Write(string.Format("Valid Connection: IP match {0} -> {1}", ip, test)); return(true); } DebugHelper.Write(string.Format("Invalid Connection: IP match {0} -> {1}", ip, test)); return(false); } continue; } } if (hostname == null) { continue; } if (General.WildcardMatch(test.ToLower(), hostname.ToLower())) { if (allowed) { DebugHelper.Write(string.Format("Valid Connection: Host match {0} -> {1}", hostname, test)); return(true); } DebugHelper.Write(string.Format("Invalid Connection: Host match {0} -> {1}", hostname, test)); return(false); } } return(true); }
private static bool validIp(string ip, bool silent) { string hostname = null; try { hostname = Dns.GetHostEntry(ip).HostName; } catch (Exception ex) { DebugHelper.Exception(ex); } foreach (var line in Settings.Default.ConnectionCheckIpHostList.Split('\n')) { var test = line.Replace(" ", string.Empty).Trim(); if (test.Length < 1) continue; var allowed = test.StartsWith("@"); if (allowed) test = test.Substring(1, test.Length-1); if (Settings.Default.ConnectionCheckIpCheck) { // Check Ip range var m = new Regex(@"([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})-([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})").Match(test); if (m.Success) { var lowerip = IPAddress.Parse(m.Groups[1].Value); var higherip = IPAddress.Parse(m.Groups[2].Value); var inrange = new IPAddressRange(lowerip, higherip).IsInRange(IPAddress.Parse(ip)); if (inrange) { if (allowed) { DebugHelper.Write(string.Format("Valid Connection: IP {0} in range -> {1}-{2}", ip, lowerip, higherip)); return true; } DebugHelper.Write(string.Format("Invalid Connection: IP {0} in range -> {1}-{2}", ip, lowerip, higherip)); return false; } continue; } // Check single IP m = new Regex(@"([0-9*]{1,3}\.[0-9*]{1,3}\.[0-9*]{1,3}\.[0-9*]{1,3})").Match(test); if (m.Success) { test = m.Groups[1].Value; if (General.WildcardMatch(test, ip)) { if (allowed) { DebugHelper.Write(string.Format("Valid Connection: IP match {0} -> {1}", ip, test)); return true; } DebugHelper.Write(string.Format("Invalid Connection: IP match {0} -> {1}", ip, test)); return false; } continue; } } if (hostname == null) continue; if (General.WildcardMatch(test.ToLower(), hostname.ToLower())) { if (allowed) { DebugHelper.Write(string.Format("Valid Connection: Host match {0} -> {1}", hostname, test)); return true; } DebugHelper.Write(string.Format("Invalid Connection: Host match {0} -> {1}", hostname, test)); return false; } } return true; }
private static bool validIp(string ip, bool silent) { // Always return true when IP Check is disabled if (!Settings.Default.ConnectionCheckIpCheck) return true; foreach (var line in Settings.Default.ConnectionCheckIpHostList.Split('\n')) { var test = line.Replace(" ", string.Empty); var m = new Regex(@"([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})-([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})").Match(test); if (m.Success) { var lowerip = IPAddress.Parse(m.Groups[1].Value); var higherip = IPAddress.Parse(m.Groups[2].Value); var inrange = new IPAddressRange(lowerip, higherip).IsInRange(IPAddress.Parse(ip)); if (inrange) { Logger.Instance.WriteGlobal("ValidConnection: IP {0} in range -> {1}-{2}", ip, lowerip,higherip); return false; } continue; } m = new Regex(@"([0-9*]{1,3}\.[0-9*]{1,3}\.[0-9*]{1,3}\.[0-9*]{1,3})").Match(test); if (!m.Success) continue; test = m.Groups[1].Value; if (General.WildcardMatch(test, ip)) { Logger.Instance.WriteGlobal("ValidConnection: IP match {0} -> {1}", ip,test); return false; } } return true; }