public static bool IsValidIpAddress(IpSupport protocol, string ipString) { if (String.IsNullOrWhiteSpace(ipString)) { return(false); } if (protocol == IpSupport.IPv4) { string[] splitValues = ipString.Split('.'); if (splitValues.Length != 4) { return(false); } byte tempForParsing; return(splitValues.All(r => byte.TryParse(r, out tempForParsing))); } else { var regex = new Regex(@"(?:^|(?<=\s))(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(?=\s|$)"); var match = regex.Match(ipString); return(match.Success); } }
public static string GetPublicIpAddress(IpSupport protocol, HttpClient Client, out string errorMesssage) { string publicIpAddress = null; var maxAttempts = 3; var attempts = 0; errorMesssage = null; var url = protocol == IpSupport.IPv4 ? "http://ipv4bot.whatismyipaddress.com" : "http://ipv6bot.whatismyipaddress.com"; while (publicIpAddress == null && attempts < maxAttempts) { try { attempts++; var response = Client.GetStringAsync(url).Result; var candidatePublicIpAddress = response.Replace("\n", ""); if (!IsValidIpAddress(protocol, candidatePublicIpAddress)) { throw new Exception($"Malformed response, expected IP address: {response}"); } publicIpAddress = candidatePublicIpAddress; } catch (Exception e) { if (attempts >= maxAttempts) { errorMesssage = e.Message; } } } return(publicIpAddress); }
void DisplayPublicIpAddressThreadSafe(IpSupport protocol, string s) { TextBox input = protocol == IpSupport.IPv4 ? txtPublicIpv4 : txtPublicIpv6; input.Invoke((MethodInvoker) delegate { input.Text = s; // Running on the UI thread }); }
public void LogMaintenance() { Logs log = new Logs(); log.logtype = "用户访问"; log.logcontent = "用户访问"; log.logtime = DateTime.Now; log.loguser = ""; log.logip = IpSupport.GetClientIp(); log.logfree = IpSupport.GetAdrByIp(log.logip); db.Logs.Add(log); db.SaveChanges(); }
// Ref: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records public DnsRecordsResponse ListDnsRecords(IpSupport protocol, string zoneIdentifier) { Client.DefaultRequestHeaders .Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); var recordType = protocol == IpSupport.IPv4 ? "A" : "AAAA"; var req = GetRequestMessage(HttpMethod.Get, $"zones/{zoneIdentifier}/dns_records?type={recordType}&page=1&per_page=100&order=name&direction=asc&match=all"); var response = Client.SendAsync(req).Result; var result = response.Content.ReadAsStringAsync().Result; ValidateCloudflareResult(response, result, "list DNS records"); var ret = JsonConvert.DeserializeObject <DnsRecordsResponse>(result); return(ret); }
string GetPublicIpAddress(IpSupport protocol) { string errorMesssage; var publicIpAddress = Utility.GetPublicIpAddress(protocol, httpClient, out errorMesssage); // Abort if we get an error, keeping the current address in settings if (publicIpAddress == null) { AppendStatusTextThreadSafe($"Error getting public {protocol.ToString()}: {errorMesssage}"); return(null); } if ((protocol == IpSupport.IPv4 && txtPublicIpv4.Text != publicIpAddress) || (protocol == IpSupport.IPv6 && txtPublicIpv6.Text != publicIpAddress)) { DisplayPublicIpAddressThreadSafe(protocol, publicIpAddress); } return(publicIpAddress); }
public ActionResult LoginCheck(LoginModel model, string returnUrl) { if (ModelState.IsValid && LoginCk(model.UserName, model.Password)) { Session.Add("username", model.UserName); Session.Add("password", model.Password); Logs log = new Logs(); log.logtype = "控制台登陆"; log.logcontent = "控制台登陆"; log.logtime = DateTime.Now; log.loguser = model.UserName; log.logip = IpSupport.GetClientIp(); db.Logs.Add(log); db.SaveChanges(); return(RedirectToAction("Index", "Console")); } // 如果我们进行到这一步时某个地方出错,则重新显示表单 ModelState.AddModelError("", "提供的用户名或密码不正确。"); return(View("Index", model)); }
private string UpdateContent(IpSupport protocol, string content, string publicIpAddress) { // we need explicit protocol in this method if (protocol == IpSupport.IPv4AndIPv6) { throw new ArgumentOutOfRangeException(); } var newContent = content; if (protocol == IpSupport.IPv4) { newContent = Regex.Replace(newContent, ipv4Regex, publicIpAddress); } else if (protocol == IpSupport.IPv6) { newContent = Regex.Replace(newContent, ipv6Regex, publicIpAddress); } return(newContent); }
// Ref: https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record public DnsUpdateResponse UpdateDns(IpSupport protocol, string zoneIdentifier, string dnsRecordIdentifier, string dnsRecordType, string dnsRecordName, string content, int ttl, bool proxied) { var dnsUpdateRequest = new DnsUpdateRequest() { type = dnsRecordType, name = dnsRecordName, content = content, ttl = ttl, proxied = proxied }; HttpResponseMessage response; HttpRequestMessage req = GetRequestMessage(HttpMethod.Put, $"zones/{zoneIdentifier}/dns_records/{dnsRecordIdentifier}"); req.Content = new StringContent(JsonSerializer.Serialize(dnsUpdateRequest), Encoding.UTF8, "application/json"); response = Client.SendAsync(req).Result; var result = response.Content.ReadAsStringAsync().Result; ValidateCloudflareResult(response, result, $"update {protocol} DNS"); var ret = JsonSerializer.Deserialize <DnsUpdateResponse>(result); return(ret); }
private bool UpdateCloudflareDns(IpSupport protocol) { var publicIpAddress = GetPublicIpAddress(protocol); if (publicIpAddress == null) { AppendStatusTextThreadSafe($"Error detecting public {protocol.ToString()} address"); return(false); } var oldPublicIpAddress = protocol == IpSupport.IPv4 ? settings.PublicIpv4Address : settings.PublicIpv6Address; if (publicIpAddress != oldPublicIpAddress) { if (protocol == IpSupport.IPv4) { settings.PublicIpv4Address = publicIpAddress; } else { settings.PublicIpv6Address = publicIpAddress; } settings.Save(); if (oldPublicIpAddress != null) { AppendStatusTextThreadSafe($"Public {protocol.ToString()} changed from {oldPublicIpAddress} to {publicIpAddress}"); } DisplayPublicIpAddressThreadSafe(protocol, publicIpAddress); // loop through DNS entries and update the ones selected that have a different IP List <Dns.Result> entriesToUpdate = null; try { entriesToUpdate = cfClient.GetAllDnsRecordsByZone().Where(d => settings.SelectedDomains .Any(s => s.ZoneName == d.zone_name && s.DnsName == d.name && d.content != publicIpAddress)).ToList(); } catch (Exception ex) { AppendStatusTextThreadSafe($"Error getting DNS records"); AppendStatusTextThreadSafe(ex.Message); tc.TrackException(ex); } if (entriesToUpdate == null) { return(false); } foreach (var entry in entriesToUpdate) { try { cfClient.UpdateDns(protocol, entry.zone_id, entry.id, entry.name, publicIpAddress, entry.proxied); txtOutput.Invoke((MethodInvoker) delegate { AppendStatusTextThreadSafe($"Updated name [{entry.name}] in zone [{entry.zone_name}] to {publicIpAddress}"); }); } catch (Exception ex) { AppendStatusTextThreadSafe($"Error updating [{entry.name}] in zone [{entry.zone_name}] to {publicIpAddress}"); AppendStatusTextThreadSafe(ex.Message); tc.TrackException(ex); } } return(true); } return(false); }
private bool UpdateCloudflareDns(IpSupport protocol) { var publicIpAddress = GetPublicIpAddress(protocol); if (publicIpAddress == null) { AppendStatusTextThreadSafe($"Error detecting public {protocol} address"); return(false); } var oldPublicIpAddress = protocol == IpSupport.IPv4 ? settings.PublicIpv4Address : settings.PublicIpv6Address; if (publicIpAddress != oldPublicIpAddress) { if (protocol == IpSupport.IPv4) { settings.PublicIpv4Address = publicIpAddress; } else { settings.PublicIpv6Address = publicIpAddress; } settings.Save(); if (oldPublicIpAddress != null) { AppendStatusTextThreadSafe($"Public {protocol} changed from {oldPublicIpAddress} to {publicIpAddress}"); } DisplayPublicIpAddressThreadSafe(protocol, publicIpAddress); var typesToUpdateForThisProtocol = new List <string> { "SPF", "TXT", protocol == IpSupport.IPv4 ? "A" : "AAAA" }; // Get requested entries to update List <Dns.Result> potentialEntriesToUpdate = null; try { var allRecordsByZone = cfClient.GetAllDnsRecordsByZone(); potentialEntriesToUpdate = allRecordsByZone.Where(d => settings.SelectedDomains.Any(s => s.ZoneName == d.zone_name && s.DnsName == d.name && s.Type == d.type) && typesToUpdateForThisProtocol.Contains(d.type)).ToList(); } catch (Exception ex) { AppendStatusTextThreadSafe($"Error getting DNS records"); AppendStatusTextThreadSafe(ex.Message); } // TODO:determine which ones need updating if (potentialEntriesToUpdate == null || !potentialEntriesToUpdate.Any()) { return(false); } foreach (var entry in potentialEntriesToUpdate) { string content; if (entry.type == "SPF" || entry.type == "TXT") { content = UpdateContent(protocol, entry.content, publicIpAddress); } else { content = publicIpAddress; } if (entry.content == content) { continue; } try { cfClient.UpdateDns(protocol, entry.zone_id, entry.id, entry.type, entry.name, content, entry.proxied); txtOutput.Invoke((MethodInvoker) delegate { AppendStatusTextThreadSafe($"Updated {entry.type} record [{entry.name}] in zone [{entry.zone_name}] to {content}"); }); } catch (Exception ex) { AppendStatusTextThreadSafe($"Error updating [{entry.type}] record [{entry.name}] in zone [{entry.zone_name}] to {content}"); AppendStatusTextThreadSafe(ex.Message); } } return(true); } return(false); }
private void DisplayPublicIpAddressThreadSafe(IpSupport protocol, string s) { TextBox input = protocol == IpSupport.IPv4 ? txtPublicIpv4 : txtPublicIpv6; input.Text = s; }