示例#1
0
 /// <summary>
 ///     获取到Host的网络延迟
 /// </summary>
 public void GetHostDelays()
 {
     if (!_ping.Ping(Host.Value))
     {
         // 地址无效
         HostDelays.Value = "invalid addr";
     }
 }
示例#2
0
        public GenericResponse Ping(IGenericRequest request, GenericResponse response)
        {
            var success = "Error";
            var ip      = request.PayLoad.AdditionalInfo["ipAddress"];

            try
            {
                if (ip != null)
                {
                    if (PingHelper.Ping(ip.ToString()).Status == IPStatus.Success)
                    {
                        success = "success";
                    }
                }
            }
            catch (PingException e)
            {
            }

            response.Result.Status = success;
            return(response);
        }
        private bool RemoteVerifyPrdKey(string productKey)
        {
            HttpHelper http        = new HttpHelper();
            var        postdata    = string.Format("CPUSerialNumber={0}&ProductKey={1}", MachineInfo.Instance.GetCPUSerialNumber(), productKey);
            string     responseStr = "";

            if (PingHelper.Ping())
            {
                // 验证有效性
                responseStr = http.PostPage(remoteservice + "/api/device/VerifyProductKey", postdata);
            }

            var verifyKeyReponse = JsonHelper.FromJson <VerifyKeyReponse>(responseStr);

            if (verifyKeyReponse != null && verifyKeyReponse.ExpirationDate.HasValue)
            {
                //验证通过
                var filename = Constants.PRODUCT_KEY;

                var launchInfo = JsonSerializeHelper.DeSerializeJson <LaunchInfo>(filename);

                launchInfo.ExpirationDate = verifyKeyReponse.ExpirationDate.Value.Date;
                launchInfo.EncryptedText  = EncryptionService.EncryptText(launchInfo.ExpirationDate.ToShortDateString());
                launchInfo.ProductKey     = productKey;
                File.Delete(filename);
                launchInfo.SerializeJson(filename);

                WindowsRegistry.SetRegistryValue(Constants.EXPIRATION_DATE, launchInfo.ExpirationDate.ToShortDateString());
                WindowsRegistry.SetRegistryValue(Constants.ENCRYPTED_TEXT, launchInfo.EncryptedText);
                WindowsRegistry.SetRegistryValue(Constants.PRODUCT_KEY, productKey);

                var datediff = launchInfo.ExpirationDate.Subtract(DateTime.Now).Days;
                return(datediff >= 0);
            }
            else
            {
                return(false);
            }
        }
        public void Launch()
        {
            new Thread(() =>
            {
                var filename = Constants.PRODUCT_KEY;

                var launchInfo = JsonSerializeHelper.DeSerializeJson <LaunchInfo>(filename);


                if (launchInfo == null)
                {
                    //使用试用账号
                    launchInfo = new LaunchInfo
                    {
                        ComputerName    = MachineInfo.Instance.GetComputerName(),
                        CPUSerialNumber = MachineInfo.Instance.GetCPUSerialNumber(),
                        MACAddress      = MachineInfo.Instance.GetMacAddress(),
                        Name            = PharmacyServiceConfig.Config.CurrentStore.Name,
                        ProductKey      = Constants.DEFAULT_SECURITY_KEY,
                        ExpirationDate  = DateTime.Now.AddDays(30).Date,
                        SystemType      = MachineInfo.Instance.GetSystemType()
                    };
                    launchInfo.EncryptedText = EncryptionService.EncryptText(launchInfo.ExpirationDate.ToShortDateString());


                    WindowsRegistry.CreateRegistry();

                    if (WindowsRegistry.IsRegeditKeyExist(Constants.EXPIRATION_DATE))
                    {
                        CallBack(Constants.PRODUCT_KEY_NOT_AVAILABLE);
                    }

                    CreateRegistry(launchInfo);
                }
                launchInfo.Name = PharmacyServiceConfig.Config.CurrentStore.Name;


                HttpHelper http = new HttpHelper();
                var postdata    = string.Format("ComputerName={0}&CPUSerialNumber={1}&MACAddress={2}&Name={3}&ProductKey={4}&SystemType={5}&ExpirationDate={6}", launchInfo.ComputerName, launchInfo.CPUSerialNumber, launchInfo.MACAddress, launchInfo.Name, launchInfo.ProductKey, launchInfo.SystemType, launchInfo.ExpirationDate);
                try
                {
                    if (PingHelper.Ping())
                    {
                        Log.Error("外部网络正常");
                        // 发送客户端设备信息
                        var strPHtml = http.PostPage(remoteservice + "/api/device/launch", postdata);
                    }
                    else
                    {
                        Log.Error("外部网络不通");
                    }
                }
                catch (Exception)
                {
                }
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                launchInfo.SerializeJson(filename);

                while (true)
                {
                    launchInfo = JsonSerializeHelper.DeSerializeJson <LaunchInfo>(filename);

                    if (launchInfo == null)
                    {
                        CallBack(Constants.PRODUCT_KEY_NOT_AVAILABLE);
                    }
                    else
                    {
                        var networkIsNotAvailable = PingHelper.Ping();

                        if (networkIsNotAvailable)
                        {
                            try
                            {
                                if (RemoteVerifyPrdKey(launchInfo.ProductKey))
                                {
                                    CallBack(Constants.PRODUCT_KEY_AVAILABLE);
                                }
                                else
                                {
                                    CallBack(Constants.PRODUCT_KEY_NOT_AVAILABLE);
                                }
                            }
                            catch (WebException)
                            {
                                networkIsNotAvailable = false;
                            }
                        }

                        if (!networkIsNotAvailable)
                        {
                            if (OfflineVerify(launchInfo))
                            {
                                CallBack(Constants.PRODUCT_KEY_AVAILABLE);
                            }
                            else
                            {
                                CallBack(Constants.PRODUCT_KEY_NOT_AVAILABLE);
                            }
                        }
                    }

                    Thread.Sleep(new TimeSpan(0, int.Parse(verifyInterval), 0));
                }
            }).Start();
        }