Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
        static public bool ConnectWithConfirmationLocal(BaseProxyServer item, string homeIP, out bool createdNew, JobLog jobLog)
        {
            bool result = false;

            jobLog.Debug("Start VpnServerConnectWithFullTestLocal for item " + item.JProxyServerId);
            createdNew = false;
            int closeAttemptCount = 0;

            while (NetConnUtils.IsActiveConnectionPresent())
            {
                if (closeAttemptCount > 10)
                {
                    throw new Exception("Unable to close previous active connection");
                }
                jobLog.Debug("Found previous active connection. Going to close it");
                NetConnUtils.CloseAllActiveConnections(false);
                Thread.Sleep(2 * 1000);//!!!
                closeAttemptCount++;
            }
            ///////test
            if (homeIP != null)
            {
                string externalIP = NetConnUtils.GetMyExternalIP();
                if (externalIP == null)
                {
                    throw new Exception("Ip of default connection is null");
                }
                jobLog.Info("Default ExternalIP: " + externalIP);
                if (!homeIP.Equals(externalIP))
                {
                    throw new Exception("Ip address of default connection not equals home ip address.");
                }
            }
            //////////
            if (NetConnUtils.IsConnectionEntryExist(item) == false)
            {
                if (item.IsProtocolAvailable(ProxyProtocolTypeEnum.PPTP))
                {
                    NetConnUtils.CreateConnectionEntry(item, ProxyProtocolTypeEnum.PPTP);
                    createdNew = true;
                }
                else if (item.IsProtocolAvailable(ProxyProtocolTypeEnum.L2TP))
                {
                    NetConnUtils.CreateConnectionEntry(item, ProxyProtocolTypeEnum.L2TP);
                    createdNew = true;
                }
                else
                {
                    throw new ArgumentException(VpnSelectorLibRes.Non_PPTP_no_L2TP_protocols_available_for_this_vpn_entry);
                }
            }
            try
            {
                NetConnUtils.OpenConnectLocal(item, false); //sync

                Thread.Sleep(2 * 1000);                     //!!!
                //for (int i = 0; i < 60; i++)//~ 1 min
                //{
                if (NetConnUtils.IsConnected(item))
                {
                    ConfirmIPAddress(item, homeIP, jobLog);
                    result               = true;
                    item.SuccessCount    = item.SuccessCount + 1;
                    item.LastSuccessDate = DateTimeOffset.Now;
                    Dm.Instance.SaveObject(item);
                }
                else
                {
                    jobLog.Info("Not connected for item " + item.JProxyServerId);
                }
                //Thread.Sleep(1 * 1000);
                //}
                //todo error event
            }
            finally {
                //change label
                if (OnNetworkChekComplatedEvent != null)
                {
                    NetworkChekComplatedEventArgs e = new NetworkChekComplatedEventArgs();
                    OnNetworkChekComplatedEvent(null, e);
                }
            }
            return(result);
        }