static public JIPAddressInfo GetIPAddressInfo(string ipAddress) { WebClient webclient = new WebClient(); string ipServerTestAddress = "http://ip-api.com/json/";//Our system will automatically ban any IP addresses doing over 150 requests per minute. To unban your IP click here. //alt http://ipinfodb.com/ip_location_api.php req login and password //alt http://www.telize.com/ not free if (!string.IsNullOrEmpty(ipAddress)) { ipServerTestAddress = ipServerTestAddress + ipAddress; } //else test my ip try { string ipInfoStr = webclient.DownloadString(ipServerTestAddress); dynamic ipInfo = JsonConvert.DeserializeObject(ipInfoStr); if (ipInfo.status == "success") { JIPAddressInfo ip = new JIPAddressInfo(); ip.As = ipInfo["as"]; //ip.As = ipInfo.as; ip.City = ipInfo.city; ip.Country = ipInfo.country; ip.CountryCode = ipInfo.countryCode; ip.Ip = ipInfo.query; ip.Isp = ipInfo.isp; ip.Lat = ipInfo.lat; ip.Lon = ipInfo.lon; ip.Mobile = ipInfo.mobile; ip.Org = ipInfo.org; ip.Proxy = ipInfo.proxy; ip.Region = ipInfo.region; ip.RegionName = ipInfo.regionName; ip.Reverse = ipInfo.reverse; ip.Timezone = ipInfo.timezone; ip.Zip = ipInfo.zip; return(ip); } else if (ipInfo.status == "fail") { Console.WriteLine("Fail response from IP info server: " + ipInfo.message); } else { Console.WriteLine("Unknown response from IP info server"); } } catch (Exception e) { ProcessWebException(e); } return(null); }
static public bool ConnectWithConfirmation(BaseProxyServer item, string homeIP, JobLog jobLog) { IsSynchMode = true; string oldExternalIP = NetConnUtils.MyExternalIP; JIPAddressInfo oldExtIPAddressInfo = NetConnUtils.MyExtIPAddressInfo; bool createdNew = false; bool res = false; try { res = ConnectWithConfirmationLocal(item, homeIP, out createdNew, jobLog); } finally{ DisconnectWithConfirmation(item, homeIP, createdNew, jobLog); NetConnUtils.MyExternalIP = oldExternalIP; NetConnUtils.MyExtIPAddressInfo = oldExtIPAddressInfo; IsSynchMode = false; } return(res); }
static public void ConfirmIPAddress(BaseProxyServer item, string homeIP, JobLog jobLog) { string externalIP = NetConnUtils.GetMyExternalIP(); if (externalIP == null) { throw new Exception("Ip of vpn connection is null"); } jobLog.Info("ExternalIP: " + externalIP); if (homeIP != null) { if (homeIP.Equals(externalIP)) { throw new Exception("Ip address of vpn connection not changed. It equals home ip address."); } } JIPAddressInfo extIPAddressInfo = NetConnUtils.GetIPAddressInfo(externalIP); if (extIPAddressInfo == null) { throw new Exception("IPAddressInfo of vpn connection is null"); } jobLog.Info("New IP address Info: " + Log.PropertyList(extIPAddressInfo)); if (item != null) { if (item.JCountry != null) { if (!string.IsNullOrEmpty(extIPAddressInfo.CountryCode)) { if (extIPAddressInfo.CountryCode.ToLower().Equals(item.JCountry.JCountryId) == false) { //throw new Exception jobLog.Warn("Country code of vpn connection ip address (" + extIPAddressInfo.CountryCode.ToLower() + ") not equals to contry code of VPN server (" + item.JCountry.JCountryId + ")"); JCountry newContry = Dm.Instance.Find <JCountry>(extIPAddressInfo.CountryCode.ToLower()); if (newContry != null) { if (item.JCountryDeclared == null) { item.JCountryDeclared = item.JCountry; } item.JCountry = newContry; Dm.Instance.SaveObject(item); } else { throw new Exception("Country code of vpn connection ip address (" + extIPAddressInfo.CountryCode.ToLower() + ") not a valid country code"); } } } else { throw new Exception("Country code of vpn connection is empty"); } if (!string.IsNullOrEmpty(extIPAddressInfo.City)) { if (item.Town != null) { if (extIPAddressInfo.City.ToLower().Equals(item.Town.ToLower()) == false) { jobLog.Warn("City vpn connection ip address (" + extIPAddressInfo.City + ") not equals to town of VPN server (" + item.Town + "). New City value was set"); if (item.TownDeclared == null) { item.TownDeclared = item.Town; } item.Town = extIPAddressInfo.City; Dm.Instance.SaveObject(item); } } else { item.Town = extIPAddressInfo.City; Dm.Instance.SaveObject(item); } } } else { //todo } jobLog.Info("Test OK for item " + item.JProxyServerId); } //ok //todo NetConnUtils.MyExternalIP = externalIP; NetConnUtils.MyExtIPAddressInfo = extIPAddressInfo; }