Exemple #1
0
        protected override bool DoDisconnect(bool bDrain)
        {
            TheCommCore.MyHttpService.UnregisterHttpInterceptorB4(GetUrlTrimmed());

            if (myWatchDogTimer != null)
            {
                var wd = myWatchDogTimer;
                myWatchDogTimer = null;
                if (wd != null)
                {
                    try
                    {
                        wd.Change(Timeout.Infinite, Timeout.Infinite);
                        wd.Dispose();
                    }
                    catch { }
                }
            }
            var client = _client;

            if (client != null)
            {
                _client = null;
            }
            return(true);
        }
Exemple #2
0
 private void GetIP(TheStorageMirror <TheVisitorLogData> .StoreResponse pRec)
 {
     lock (mVisitorList.MyLock)
     {
         TheVisitorLogData tLog = null;
         if (pRec == null || pRec.HasErrors || pRec.MyRecords.Count == 0)
         {
             if (pRec != null)
             {
                 mVisitorList.TryGetValue(pRec.SQLFilter, out tLog);
                 if (tLog != null)
                 {
                     TheREST.GetRESTAsync(new Uri($"http://api.ipstack.com/{tLog.ip}?access_key={APIKey}"), 0, sinkProcessLocation, tLog);
                     mVisitorList.RemoveNoCare(pRec.SQLFilter);
                 }
             }
         }
         else
         {
             tLog = pRec.MyRecords[0];
             tLog.Visits++;
             tLog.LastVisit = DateTimeOffset.Now;
             if (tLog.latitude == 0 && tLog.longitude == 0)
             {
                 TheREST.GetRESTAsync(new Uri($"http://api.ipstack.com/{tLog.ip}?access_key={APIKey}"), 0, sinkProcessLocation, tLog);
             }
             else
             {
                 MyVisitorLogStore.UpdateItem(MyLocation);
             }
         }
     }
 }
Exemple #3
0
        public void RegisterDeviceWithServer(Uri pTargetServer, Action <string, TheUPnPDeviceInfo> pCallback)
        {
            TheREST tRest   = new TheREST();
            var     builder = new UriBuilder(pTargetServer)
            {
                Path = "DEVICEREG.JSON"
            };

            tRest.PostRESTAsync(builder.Uri, sinkRegResult, TheCommonUtils.SerializeObjectToJSONString(MyDeviceUPnPInfo), pCallback, sinkRegResult);
        }
        /// <summary>
        /// Returns the external IP of this hosting hardware by making a reverse lookup via checkip.dyndns.org
        /// </summary>
        /// <param name="pCallback">Will be called when the host was resolved</param>
        /// <param name="pCookie">A cookie to track this call with</param>
        public static void GetExternalIp(Action <IPAddress, object> pCallback, object pCookie)
        {
            string whatIsMyIp = "http://checkip.dyndns.org/";
            ExIP   tExIP      = new ExIP
            {
                Callback = pCallback,
                Cookie   = pCookie
            };

            TheREST.GetRESTAsync(new Uri(whatIsMyIp), 0, sinkExternalIP, tExIP);
        }
Exemple #5
0
        protected override bool DoConnect()
        {
            HoldOffOnSenderLoop = true; // Not doing push gateway yet

            TheCommCore.MyHttpService.RegisterHttpInterceptorB4(GetUrlTrimmed(), onPrometheusHttpRequest);

            if (KPILogIntervalInMinutes > 0)
            {
                myWatchDogTimer = new Timer(OnWatchDogTimer, null, KPILogIntervalInMinutes * 60 * 1000, KPILogIntervalInMinutes * 60 * 1000);
            }

            _client = new TheREST();
            return(true);
        }
Exemple #6
0
        /// <summary>
        /// Gets a token, header or just cookies from Target Website
        /// </summary>
        /// <param name="pTokenUrl">Url Of the token page</param>
        /// <param name="pTokenLocator">null for just cookies; HEADER: to fetch a specific header entry; otherwise a matchpattern for a token inside a page</param>
        /// <returns></returns>
        public bool FetchToken(Uri pTokenUrl, string pTokenLocator = null)
        {
            if (pTokenUrl == null)
            {
                return(false);
            }
            mTokenLocator = pTokenLocator;
            var MyRequestData = new TheRequestData {
                RequestUri = pTokenUrl
            };                                                                // new Uri("https://ptdevices.com/login") };

            TheREST.GetRESTAsync(MyRequestData, sinkLoginFetchSuccess, sinkError);
            return(true);
        }
Exemple #7
0
        private void sinkRegisterHost(IPAddress pIP, object pData)
        {
            TheVisitorLogData tData = pData as TheVisitorLogData;

            if (tData != null)
            {
                if (pIP == null)
                {
                    pIP = nsCDEngine.Discovery.TheNetworkInfo.GetIPAddress(false);
                }
                tData.ip = pIP.ToString();
                TheBaseAssets.MySYSLOG.WriteToLog(511, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("VisitorLog", "GetExternalIp returned: " + pIP.ToString(), eMsgLevel.l4_Message));
                TheREST.GetRESTAsync(new Uri($"http://api.ipstack.com/{pIP}?access_key={APIKey}"), 0, sinkProcessLocation, tData);
            }
        }
Exemple #8
0
 private void StoreIsUp(StoreEventArgs ID)
 {
     if (!mIsHostLogged)
     {
         mIsHostLogged = true;
         TheVisitorLogData tLog = new TheVisitorLogData
         {
             LastVisit   = DateTimeOffset.Now,
             Description = TheBaseAssets.MyServiceHostInfo.MyStationName
         };
         IPAddress myaddr = nsCDEngine.Discovery.TheNetworkInfo.GetIPAddress(true);
         if (myaddr == null)
         {
             nsCDEngine.Discovery.TheNetworkInfo.GetExternalIp(sinkRegisterHost, tLog);
         }
         else
         {
             TheREST.GetRESTAsync(new Uri($"http://api.ipstack.com/{myaddr}?access_key={APIKey}"), 0, sinkProcessLocation, tLog);
         }
     }
 }
        private void ReadHttpPage(TheRequestData pRequest, Guid MyAppGuid, string tMagixc, Action <TheRequestData> pSinkProcessResponse) //TheRelayAppInfo MyApp,
        {
            TheRelayAppInfo MyApp = TheThingRegistry.GetThingObjectByMID(MyBaseEngine.GetEngineName(), MyAppGuid) as TheRelayAppInfo;    // MyRelayApps.MyMirrorCache.GetEntryByFunc(s => s.cdeMID.Equals(MyAppGuid));

            if (MyApp == null)
            {
                if (pRequest.cdeN.Equals(TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID))
                {
                    TSM tTSM = new TSM(MyBaseEngine.GetEngineName(), "WEBRELAY_REQUEST")
                    {
                        PLB = pRequest.PostData
                    };
                    pRequest.PostData       = null;
                    pRequest.ResponseBuffer = null;
                    if (!string.IsNullOrEmpty(pRequest.CookieString))
                    {
                        pRequest.CookieString += ";";
                    }
                    else
                    {
                        pRequest.CookieString = "";
                    }
                    pRequest.CookieString += tMagixc;
                    if (string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.RootDir))
                    {
                        pRequest.RequestUriString = pRequest.RequestUri.ToString();
                    }
                    else
                    {
                        pRequest.RequestUriString = pRequest.RequestUri.Scheme + "://" + pRequest.RequestUri.Host + ":" + pRequest.RequestUri.Port + pRequest.cdeRealPage;
                        if (!string.IsNullOrEmpty(pRequest.RequestUri.Query))
                        {
                            pRequest.RequestUriString += "?" + pRequest.RequestUri.Query;
                        }
                    }
                    TheBaseAssets.MySYSLOG.WriteToLog(400, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM(MyBaseEngine.GetEngineName(), string.Format("Requesting Page:{0}", pRequest.RequestUriString), eMsgLevel.l6_Debug));

                    tTSM.PLS = TheCommonUtils.SerializeObjectToJSONString(pRequest);
                    tTSM.SID = pRequest.SessionState.GetSID();
                    TheCommCore.PublishCentral(tTSM); //  .PublishToNode(MyApp.HostUrl, pRequest.SessionState.SScopeID, tTSM);
                }
                return;
            }
            if (TheCommonUtils.IsUrlLocalhost(MyApp.GetBaseThing().Address))
            {
                TheCommonUtils.GetAnyFile(pRequest);
                if (tMagixc != null)
                {
                    pRequest.CookieString = tMagixc;
                }
                pSinkProcessResponse(pRequest);
            }
            else
            {
                int    relPathIndex = pRequest.RequestUri.AbsolutePath.IndexOf('/', 1);
                string relPath      = relPathIndex == -1 ? "" : pRequest.RequestUri.AbsolutePath.Substring(relPathIndex + 1) + pRequest.RequestUri.Query;
                var    tUri         = pRequest.RequestUri.AbsolutePath.StartsWith("/CDEWRA") ? new Uri(MyApp.GetBaseThing().Address + MyApp.HomePage + relPath) : new Uri(MyApp.GetBaseThing().Address.TrimEnd('/') + pRequest.RequestUri.AbsolutePath + pRequest.RequestUri.Query);
                if (!string.IsNullOrEmpty(tMagixc))
                {
                    if (!string.IsNullOrEmpty(pRequest.CookieString))
                    {
                        pRequest.CookieString += ";";
                    }
                    else
                    {
                        pRequest.CookieString = "";
                    }
                    pRequest.CookieString += tMagixc;
                }
                pRequest.RequestUri       = tUri;
                pRequest.ErrorDescription = "";
                pRequest.RequestCookies   = pRequest.SessionState.StateCookies;
                if (!string.IsNullOrEmpty(MyApp.SUID) && !string.IsNullOrEmpty(MyApp.SPWD))
                {
                    pRequest.UID = MyApp.SUID;
                    pRequest.PWD = MyApp.SPWD;
                }
                if (MyApp.IsHTTP10)
                {
                    pRequest.HttpVersion = 1.0;
                }
                if (string.IsNullOrEmpty(MyApp.CloudUrl))
                {
                    MyApp.CloudUrl = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false);
                }
                pRequest.Header = null;
                if (pRequest.PostData != null && pRequest.PostData.Length > 0)      //MONO Relay might not work right!
                {
                    TheREST MyRest = new TheREST();
                    MyRest.PostRESTAsync(pRequest, pSinkProcessResponse, pSinkProcessResponse);
                }
                else
                {
                    TheREST.GetRESTAsync(pRequest, pSinkProcessResponse, pSinkProcessResponse);
                }
                TheBaseAssets.MySYSLOG.WriteToLog(400, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(MyBaseEngine.GetEngineName(), string.Format("Requesting Page:{0} Sent", pRequest.RequestUri), eMsgLevel.l3_ImportantMessage));
            }
        }
Exemple #10
0
        public void GetDevices(Uri pDeviceURL = null, string tPost = null, cdeConcurrentDictionary <string, string> addHeader = null)
        {
            if (!IsConnected || !TheBaseAssets.MasterSwitch)
            {
                return;
            }
            if (pDeviceURL != null)
            {
                mDeviceUrl = pDeviceURL;
            }
            if (tPost != null)
            {
                mDevicePost = tPost;
            }
            if (addHeader != null)
            {
                mAddHeader = addHeader;
            }
            if (mDevicePost == null)
            {
                TheRequestData tRequest = new TheRequestData
                {
                    RequestUri     = mDeviceUrl,
                    RequestCookies = MyRequestData?.SessionState?.StateCookies
                };
                if (mAddHeader != null)
                {
                    tRequest.Header = new cdeConcurrentDictionary <string, string>();
                    foreach (string key in mAddHeader.Keys)
                    {
                        tRequest.Header.TryAdd(key, mAddHeader[key]);
                    }
                }
                TheREST.GetRESTAsync(tRequest, ParseDevices, sinkError);
            }
            else
            {
                MyRequestData.RequestUri = mDeviceUrl; // new Uri("https://ptdevices.com/device/all");
                //tPost = $"_token={WebSocketSharp.Ext.UrlEncode(mToken)}";
                MyRequestData.PostData   = TheCommonUtils.CUTF8String2Array(mDevicePost);
                MyRequestData.HttpMethod = "POST";
                MyRequestData.Header     = null;
                if (mAddHeader != null)
                {
                    MyRequestData.Header = new cdeConcurrentDictionary <string, string>();
                    foreach (string key in mAddHeader.Keys)
                    {
                        MyRequestData.Header.TryAdd(key, mAddHeader[key]);
                    }
                }

                MyRequestData.RequestCookies   = MyRequestData?.SessionState?.StateCookies;
                MyRequestData.ResponseMimeType = mPostMimeType; // "application/x-www-form-urlencoded; charset=UTF-8";
                while (MyRest.IsPosting > 1 && IsConnected)
                {
                    TheCommonUtils.SleepOneEye(1000, 100);
                }
                if (IsConnected)
                {
                    MyRest.PostRESTAsync(MyRequestData, ParseDevices, sinkError);
                }
            }
        }