Ejemplo n.º 1
0
        public async Task <string> LogUser([FromBody] PageLogRequest pageData)
        {
            try
            {
                var innerAddress = new Uri(pageData.Address).PathAndQuery;
                var host         = await _hostCache.GetHostByIdAsync(pageData.HostId);

                if (host == null || host.Id == 0)
                {
                    return(CommonStrings.NoHost);
                }
                #region SecurityCheck
                if (_configurations.SecurityCheck)
                {
                    var origin = Request.Headers[CommonStrings.Origin].ToString();
                    if (!string.IsNullOrEmpty(origin))
                    {
                        var aut = new Uri(origin).Host;
                        aut = aut.ToLower();
                        var topdomain = string.Empty;
                        if (aut.IndexOf(CommonStrings.Dot) == aut.LastIndexOf(CommonStrings.Dot))
                        {
                            topdomain = aut;
                        }
                        else
                        {
                            topdomain = aut.Substring(aut.IndexOf(CommonStrings.Dot) + 1);
                        }
                        if (host.Host != topdomain)
                        {
                            return(CommonStrings.NoHostAccess);
                        }
                    }
                }
                #endregion

                if (pageData != null && (await _crowlerCache.IsCrowler(pageData.UserId)))
                {
                    return("");
                }
                await _totalVisitUpdater.UpdateTotalVisit(pageData.HostId);

                await _hostScriptChecker.UpdatePageValidation(pageData.HostId);

                await _kafkaLogger.SendMessage(new DruidData()
                {
                    CategoryName = "",
                    Date         = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:sszzz"),
                    HostName     = host.Host,
                    ImageAddress = "",
                    Price        = 0,
                    ProductName  = "",
                    PageAddress  = innerAddress,
                    ProductId    = "",
                    StatType     = StatTypes.PageView,
                    UserId       = pageData.UserId.ToString(),
                    HostId       = pageData.HostId.ToString(),
                });
            }
            catch (Exception ex)
            {
                await _errorLogger.LogError("Operation:PageLogger =>" + "User=>" + pageData.UserId + ":" + ex.Message);
            }
            return("");
        }
Ejemplo n.º 2
0
        public async Task <string> UpdateProducts([FromBody] ProductLog logData)
        {
            try
            {
                var fiProducts = logData.ProductData.OrderByDescending(c => c.ProductId).Take(20);
                var host       = await _hostCache.GetHostByIdAsync(logData.HostId).ConfigureAwait(false);

                if (host == null || host.Id == 0)
                {
                    return(CommonStrings.NoHost);
                }
                #region SecurityCheck
                if (_configurations.SecurityCheck)
                {
                    var origin = Request.Headers[CommonStrings.Origin].ToString();
                    if (!string.IsNullOrEmpty(origin))
                    {
                        var aut = new Uri(origin).Host;
                        aut = aut.ToLower();
                        var topdomain = string.Empty;
                        if (aut.IndexOf(CommonStrings.Dot) == aut.LastIndexOf(CommonStrings.Dot))
                        {
                            topdomain = aut;
                        }
                        else
                        {
                            topdomain = aut.Substring(aut.IndexOf(CommonStrings.Dot) + 1);
                        }
                        if (host.Host != topdomain)
                        {
                            return(CommonStrings.NoHostAccess);
                        }
                    }
                }
                #endregion
                if (logData != null && (await _crowlerCache.IsCrowler(logData.UserId)))
                {
                    return(string.Empty);
                }
                List <Task> tasks = new List <Task>
                {
                    _totalVisitUpdater.UpdateTotalVisit(logData.HostId)
                };
                await _hostScriptChecker.UpdateProductValidation(logData.HostId);

                var categoriesforlog = logData.ProductData.Where(c => !string.IsNullOrEmpty(c.Category)).GroupBy(c => c.Category).Select(v => new KeyValuePair <string, int>(v.Key, v.Count()));
                foreach (var category in categoriesforlog)
                {
                    tasks.Add(_categoryLogger.LogCategory(host.Id, category.Key, category.Value));
                }
                foreach (var item in fiProducts)
                {
                    var updateedProduct = new HostProduct()
                    {
                        Description  = item.Description,
                        ImageAddress = item.ImageAddress,
                        IsAvailable  = item.Available,
                        Price        = item.Price,
                        Url          = item.PageAddress,
                        Id           = item.ProductId,
                        ProductName  = item.Name,
                        CategoryName = item.Category,
                        UpdateDate   = DateTime.Now
                    };
                    tasks.Add(_productCache.UpdateProduct(logData.HostId, updateedProduct));
                }

                #region Log Into Kafka
                foreach (var item in logData.ProductData)
                {
                    await _kafkaLogger.SendMessage(new DruidData()
                    {
                        CategoryName = item.Category,
                        Date         = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:sszzz"),
                        HostName     = host.Host,
                        ImageAddress = item.ImageAddress,
                        Price        = item.Price,
                        ProductName  = item.Name,
                        PageAddress  = item.PageAddress,
                        ProductId    = item.ProductId,
                        StatType     = StatTypes.ProductView,
                        UserId       = logData.UserId.ToString(),
                        HostId       = logData.HostId.ToString(),
                    });
                }
                #endregion

                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await _errorLogger.LogError("Operation:ProductUpdate =>" + ex.Message);
            }
            return(string.Empty);
        }
Ejemplo n.º 3
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "text/html";
                var requestHost = context.Request.Headers["Referer"];
                if (string.IsNullOrEmpty(requestHost))
                {
                    await context.Response.WriteAsync("No Iframe! Don't Cheat!");

                    return;
                }
                string host      = new Uri(requestHost).Authority.ToString();
                var    topdomain = "";
                if (host.IndexOf(".") == host.LastIndexOf("."))
                {
                    topdomain = host;
                }
                else
                {
                    topdomain = host.Substring(host.IndexOf(".") + 1);
                }
                var hostData = await _hostCache.GetHostAsync(topdomain);

                if (hostData == null)
                {
                    return;
                }

                string userCookie = context.Request.Cookies[_configuration.CookieName];
                //try
                //{
                //    var logAgent= _redisCache.GetDatabase(DataAccess.RedisDatabases.CacheData).StringGet("LogAgent");
                //    if (logAgent.HasValue)
                //    {
                //    var userAgent = context.Request.Headers["User-Agent"].ToString();
                //    var agentstring = string.IsNullOrEmpty(userAgent.ToString()) ? "unknown" : userAgent.ToString();
                //    var agentDb = _redisCache.GetDatabase(DataAccess.RedisDatabases.Logs);
                //    await agentDb.HashIncrementAsync("AgentCounter",agentstring);
                //    }

                //}
                //catch
                //{

                //}

                long userId = 0;
                if (userCookie == null)
                {
                    userId = await _userIdSetter.GetNewUserIdAsync();

                    var co = new CookieOptions()
                    {
                        Expires  = DateTime.Now.AddYears(5),
                        SameSite = SameSiteMode.None
                    };
                    if (_configuration.CookieDomain != ".")
                    {
                        co.Domain = _configuration.CookieDomain;
                    }
                    context.Response.Cookies.Append(_configuration.CookieName, userId.ToString(), co);
                }
                else
                {
                    var resultofparseCookie = long.TryParse(userCookie, out userId);
                    if (!resultofparseCookie)
                    {
                        return;
                    }
                }
                if (await _crowlerCache.IsCrowler(userId))
                {
                    return;
                }
                var rawhtml  = "<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><title></title></head><body>partner_iframes<script>var sendingData = {};sendingData.message = \"GetCookie\";sendingData.UserId = user_identity;sendingData.FinalizePage = 'finalize_page';sendingData.HostId = host_identity;sendingData.AddToCart = 'add_to_cart';parent.postMessage(JSON.stringify(sendingData), \"*\");</script></body></html>";
                var html     = rawhtml.Replace("user_identity", userId.ToString()).Replace("host_identity", hostData.Id.ToString()).Replace("add_to_cart", hostData.AddToCardId).Replace("finalize_page", hostData.FinalizePage);
                var partners = await _partnerCache.ListAsync();

                var pixelHtml = "";
                foreach (var item in partners)
                {
                    if (!string.IsNullOrEmpty(item.CookieSyncAddress))
                    {
                        var partnerAddress = item.CookieSyncAddress?.Replace("@StickyId", userId.ToString());
                        pixelHtml += "<iframe src=\"" + partnerAddress + "\"></iframe>";
                    }
                }
                html = html.Replace("partner_iframes", pixelHtml);
                await context.Response.WriteAsync(html);
            }
            catch (Exception ex)
            {
                await _errorLogger.LogError(ex.Message);
            }
        }
Ejemplo n.º 4
0
        public async Task Invoke(HttpContext context)
        {
            var requestHost = context.Request.Headers["Referer"];

            if (string.IsNullOrEmpty(requestHost))
            {
                await context.Response.WriteAsync("No Iframe!");

                return;
            }
            var errorLocation = "";

            try
            {
                var partnerHashCode = context.Request.Query["p"].ToString();
                var partnerUserId   = context.Request.Query["pu"].ToString();
                if (string.IsNullOrEmpty(partnerHashCode) || string.IsNullOrEmpty(partnerUserId))
                {
                    await context.Response.WriteAsync("<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><title></title></head><body><h1></h1></body></html>");

                    return;
                }

                var partnerData = await _partnerCache.FindPartner(partnerHashCode);

                string isNewlySet = context.Request.Cookies["stny"];
                if (isNewlySet != null)
                {
                    return;
                }

                if (partnerData == null)
                {
                    return;
                }
                context.Response.ContentType = "text/html";
                long userId = 0;
                //partner has no cookie from sticky and we need to set cookie if it does not exist and then save it to cookie sync table.
                string userCookie = context.Request.Cookies[_configuration.CookieName];
                if (userCookie == null)
                {
                    userId = await _userIdSetter.GetNewUserIdAsync();

                    var co = new CookieOptions()
                    {
                        Expires  = DateTime.Now.AddYears(5),
                        SameSite = SameSiteMode.None
                    };
                    if (_configuration.CookieDomain != ".")
                    {
                        co.Domain = _configuration.CookieDomain;
                    }
                    context.Response.Cookies.Append(_configuration.CookieName, userId.ToString(), co);
                    context.Response.Cookies.Append("stny", "1", new CookieOptions()
                    {
                        Domain = _configuration.CookieDomain, SameSite = SameSiteMode.None, Expires = DateTime.Now.AddMinutes(10)
                    });
                }
                else
                {
                    context.Response.Cookies.Append("stny", "1", new CookieOptions()
                    {
                        Domain = _configuration.CookieDomain, SameSite = SameSiteMode.None, Expires = DateTime.Now.AddMinutes(10)
                    });
                    var resultofparseCookie = long.TryParse(userCookie, out userId);
                    if (!resultofparseCookie)
                    {
                        return;
                    }
                }
                if (await _crowlerCache.IsCrowler(userId))
                {
                    return;
                }
                var            partnerId = partnerData.Id;
                IMongoDatabase _database = _mongoClient.GetDatabase("TrackEm");
                IMongoCollection <PartnersCookieMatch> CookieMatches = _database.GetCollection <PartnersCookieMatch>(name: "Partner_" + partnerId + "_CookieMatch", settings: new MongoCollectionSettings()
                {
                    AssignIdOnInsert = true
                });
                var filter_Builder = Builders <PartnersCookieMatch> .Filter;
                var filter         = filter_Builder.Eq(c => c.Id, partnerUserId);

                var updates = new List <UpdateDefinition <PartnersCookieMatch> >
                {
                    Builders <PartnersCookieMatch> .Update.Set(m => m.StickyId, userId),
                    Builders <PartnersCookieMatch> .Update.SetOnInsert(m => m.Id, partnerUserId)
                };
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                await CookieMatches.UpdateOneAsync(filter, Builders <PartnersCookieMatch> .Update.Combine(updates), new UpdateOptions()
                {
                    IsUpsert = true
                }).ConfigureAwait(continueOnCapturedContext: false);

#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

                var rawhtml = "<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><title></title></head><body>Done!</body></html>";
                await context.Response.WriteAsync(rawhtml);
            }
            catch (Exception ex)
            {
                await context.Response.WriteAsync("<script>console.log('" + ex.Message + "on=>" + errorLocation + "')</script>");
            }
        }