public async Task <IActionResult> Edit(int id, [Bind("ID,IPAddress,countryName,countryCode,regionName,cityName,zipCode,timeZone,longitude,latitude")] IPinfo iPinfo)
        {
            if (id != iPinfo.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(iPinfo);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!IPinfoExists(iPinfo.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(iPinfo));
        }
        public async Task <IActionResult> Create([Bind("ID,IPAddress,countryName,countryCode,regionName,cityName,zipCode,timeZone,longitude,latitude")] IPinfo iPinfo)
        {
            if (ModelState.IsValid)
            {
                _context.Add(iPinfo);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(iPinfo));
        }
        public async Task <JsonResult> GetIPinfo()
        {
            // Look for cache key.
            if (!_cache.TryGetValue(IPinfoCacheKey, out IPinfo ipInfo))
            {
                // Key not in cache, so get data.
                var pinfo = await iIPAddressService.GetIPAddressLocation();

                if (pinfo.owner == null)
                {
                    pinfo.owner = "no value";
                }

                ipInfo = new IPinfo()
                {
                    ip_address     = pinfo.ip_address,
                    country        = pinfo.country,
                    country_code   = pinfo.country_code,
                    continent      = pinfo.continent,
                    continent_code = pinfo.continent_code,
                    city           = pinfo.city,
                    county         = pinfo.county,
                    region         = pinfo.region,
                    region_code    = pinfo.region_code,
                    timezone       = pinfo.timezone,
                    owner          = pinfo.owner,
                    longitude      = pinfo.longitude,
                    latitude       = pinfo.latitude,
                    currency       = pinfo.currency,
                    languages      = pinfo.languages
                };

                // Set cache options.
                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        // Keep in cache for this time, reset time if accessed.
                                        .SetSlidingExpiration(TimeSpan.FromMinutes(60));

                // Save data in cache.
                _cache.Set(IPinfoCacheKey, ipInfo, cacheEntryOptions);
            }

            return(Json(new { data = ipInfo }));
        }
        // GET: /IPAddress/Create
        public IActionResult ValidateInput()
        {
            IPinfo newIPinfo = new IPinfo();

            return(View(newIPinfo));
        }
Beispiel #5
0
        public async Task <bool> GetData(string path)
        {
            if (File.Exists(path))
            {
                lines = File.ReadAllLines(path);
                List <string> allowed = new List <string>();
                for (int i = 0; i < lines.Length; i++)
                {
                    if (!(lines[i].Contains("media") || lines[i].Contains("scripts") || lines[i].Contains("css") ||
                          lines[i].Contains("/js/") || lines[i].Contains("assets") ||
                          lines[i].Contains("png") || lines[i].Contains("jpg") ||
                          lines[i].Contains("php") || lines[i].Contains("/administrator/") ||
                          lines[i].Contains("ico")

                          ))
                    // if (lines[i].Contains("search"))
                    {
                        if (!(lines[i].Contains(" - admin ") || lines[i].Contains("/administrator ")))
                        {
                            allowed.Add(lines[i]);
                        }
                    }
                }



                List <string> mainTables = new List <string>();
                List <string> ip         = new List <string>();
                List <string> files      = new List <string>();

                IPinfo    infoIp    = new IPinfo();
                FilesInfo filesInfo = new FilesInfo();


                List <byte[]>   ip_list       = new List <byte[]>();
                List <string>   dateTimeStr   = new List <string>();
                List <DateTime> dateTimes     = new List <DateTime>();
                List <string>   urls          = new List <string>();
                List <string>   request_types = new List <string>();
                List <string>   compNames     = new List <string>();
                List <int>      results       = new List <int>();
                List <long>     dataVolumes   = new List <long>();
                List <string>   names         = new List <string>();

                List <IPinfo> _IpInfos = await appDbContext.IpInfo.ToListAsync();

                List <FilesInfo> _filesInfo = await appDbContext.FilesInfos.ToListAsync();


                //  await Task.Run(async () =>

                List <Task> tasks = new List <Task> {
                    Task.Run(() =>
                    {
                        Parallel.For(0, allowed.Count, async(i) =>
                        {
                            Logger.LogInformation($"{DateTime.Now.ToString()} loop for processing data is started");
                            var _ip = GetIp(allowed[i]);
                            ip_list.Add(GetIp(allowed[i]));
                            var _datetimestr = await GetDateTimesString(allowed[i]);
                            dateTimeStr.Add(await GetDateTimesString(allowed[i]));
                            var date_time = await _GetDateTimes(_datetimestr);
                            dateTimes.Add(await _GetDateTimes(_datetimestr));
                            var t = await GetRequests(allowed[i]);
                            string request;
                            string url;
                            ParseRequest(t, out request, out url);
                            request_types.Add(request);
                            urls.Add(url);
                            string compName = string.Empty;
                            if (_IpInfos.Count > i)
                            {
                                if (_IpInfos[i] == null)
                                {
                                    try{
                                        compName = await GetCompaniesName(_ip);
                                        compNames.Add(compName);
                                    }
                                    catch (HttpRequestException w)
                                    {
                                        Logger.LogError($"{w.Message}");
                                        compNames.Add(compName);
                                    }
                                }
                                else
                                {
                                    compName = _IpInfos[i].CompanyName;
                                    compNames.Add(compName);
                                }
                            }
                            else
                            {
                                try
                                {
                                    compName = await GetCompaniesName(_ip);
                                }
                                catch (HttpRequestException ex)
                                {
                                    Logger.LogError($"{ex.Message}");
                                }
                                finally
                                {
                                    compNames.Add(compName);
                                }
                            }
                            int _res;
                            long volume;
                            string name = string.Empty;

                            GetResultAndDataVolume(allowed[i], out _res, out volume);
                            if (_filesInfo.Count > i)
                            {
                                var check_name = _filesInfo[i];
                                if (check_name == null)
                                {
                                    if (_res == 200)
                                    {
                                        name = await GetNames(url);
                                        names.Add(name);
                                    }
                                    else
                                    {
                                        names.Add(String.Empty);
                                    }
                                }
                                else
                                {
                                    name = check_name.Name;
                                    names.Add(check_name.Name);
                                }
                            }


                            results.Add(_res);
                            dataVolumes.Add(volume);


                            //var _f = await appDbContext.FilesInfos.FirstOrDefaultAsync(t => t.Path == url);
                            //var _c = await appDbContext.IpInfo.FirstOrDefaultAsync(t => t.IPAddress == _ip);
                            //var _m = await appDbContext.MainTable.FirstOrDefaultAsync(t => t.RequestType == request && t.RequestResult == _res
                            //     && t.DateTimeLog == _datetimestr && t.FilesInfo.Name == name && t.FilesInfo.Path == url && t.FilesInfo.DataVolume == volume
                            //     && t._IPinfo.IPAddress == _ip && t._IPinfo.CompanyName == compName
                            //);
                            //if (_m == null)
                            //{
                            //    if (name == string.Empty)
                            //    {
                            //        if (compName == string.Empty)
                            //        {
                            //            await appDbContext.MainTable.AddAsync(new MainTable()
                            //            {
                            //                DateTimeLog = _datetimestr,
                            //                DateTime = date_time,
                            //                DataVolume = volume,
                            //                RequestResult = _res,
                            //                RequestType = request,
                            //                //FilesInfo = _f ?? new FilesInfo()
                            //                //{ DataVolume = volume, Name = name, Path = url },
                            //                //_IPinfo = _c ?? new IPinfo() { CompanyName = compName, IPAddress = _ip }

                            //            });
                            //        }
                            //        else
                            //        {
                            //            await appDbContext.MainTable.AddAsync(new MainTable()
                            //            {
                            //                DateTimeLog = _datetimestr,
                            //                DateTime = date_time,
                            //                DataVolume = volume,
                            //                RequestResult = _res,
                            //                RequestType = request,
                            //                //FilesInfo = _f ?? new FilesInfo()
                            //                //{ DataVolume = volume, Name = name, Path = url },
                            //                _IPinfo = _c ?? new IPinfo() { CompanyName = compName, IPAddress = _ip }

                            //            });
                            //        }
                            //    }
                            //    else if (compName == string.Empty)
                            //    {
                            //        if (name == string.Empty)
                            //        {
                            //            await appDbContext.MainTable.AddAsync(new MainTable()
                            //            {
                            //                DateTimeLog = _datetimestr,
                            //                DateTime = date_time,
                            //                DataVolume = volume,
                            //                RequestResult = _res,
                            //                RequestType = request,
                            //                //FilesInfo = _f ?? new FilesInfo()
                            //                //{ DataVolume = volume, Name = name, Path = url },
                            //                //_IPinfo = _c ?? new IPinfo() { CompanyName = compName, IPAddress = _ip }

                            //            });
                            //        }
                            //        else
                            //        {
                            //            await appDbContext.MainTable.AddAsync(new MainTable()
                            //            {
                            //                DateTimeLog = _datetimestr,
                            //                DateTime = date_time,
                            //                DataVolume = volume,
                            //                RequestResult = _res,
                            //                RequestType = request,
                            //                FilesInfo = _f ?? new FilesInfo()
                            //                { DataVolume = volume, Name = name, Path = url },
                            //                // _IPinfo = _c ?? new IPinfo() { CompanyName = compName, IPAddress = _ip }

                            //            });
                            //        }

                            //    }
                            //}
                        });
                    })
                };


                //}).ContinueWith(async(s) => {
                if (tasks[0].IsCompleted)
                {
                    await Task.Run(async() =>
                    {
                        for (int i = 0; i < names.Count; i++)
                        {
                            var url          = urls[i];
                            var _ip          = ip_list[i];
                            var request      = request_types[i];
                            var _res         = results[i];
                            var _datetimestr = dateTimeStr[i];
                            var volume       = dataVolumes[i];
                            var name         = names[i];
                            var compName     = compNames[i];
                            var date_time    = dateTimes[i];
                            var _f           = await appDbContext.FilesInfos.FirstOrDefaultAsync(t => t.Path == url);
                            var _c           = await appDbContext.IpInfo.FirstOrDefaultAsync(t => t.IPAddress == _ip);
                            var _m           = await appDbContext.MainTable.FirstOrDefaultAsync(t => t.RequestType == request && t.RequestResult == _res &&
                                                                                                t.DateTimeLog == _datetimestr && t.FilesInfo.Name == name && t.FilesInfo.Path == url && t.FilesInfo.DataVolume == volume &&
                                                                                                t._IPinfo.IPAddress == _ip && t._IPinfo.CompanyName == compName
                                                                                                );
                            if (_m == null)
                            {
                                if (name == string.Empty)
                                {
                                    if (compName == string.Empty)
                                    {
                                        await appDbContext.MainTable.AddAsync(new MainTable()
                                        {
                                            DateTimeLog   = _datetimestr,
                                            DateTime      = date_time,
                                            DataVolume    = volume,
                                            RequestResult = _res,
                                            RequestType   = request,
                                            //FilesInfo = _f ?? new FilesInfo()
                                            //{ DataVolume = volume, Name = name, Path = url },
                                            //_IPinfo = _c ?? new IPinfo() { CompanyName = compName, IPAddress = _ip }
                                        });
                                    }
                                    else
                                    {
                                        await appDbContext.MainTable.AddAsync(new MainTable()
                                        {
                                            DateTimeLog   = _datetimestr,
                                            DateTime      = date_time,
                                            DataVolume    = volume,
                                            RequestResult = _res,
                                            RequestType   = request,
                                            //FilesInfo = _f ?? new FilesInfo()
                                            //{ DataVolume = volume, Name = name, Path = url },
                                            _IPinfo = _c ?? new IPinfo()
                                            {
                                                CompanyName = compName, IPAddress = _ip
                                            }
                                        });
                                    }
                                }
                                else if (compName == string.Empty)
                                {
                                    if (name == string.Empty)
                                    {
                                        await appDbContext.MainTable.AddAsync(new MainTable()
                                        {
                                            DateTimeLog   = _datetimestr,
                                            DateTime      = date_time,
                                            DataVolume    = volume,
                                            RequestResult = _res,
                                            RequestType   = request,
                                            //FilesInfo = _f ?? new FilesInfo()
                                            //{ DataVolume = volume, Name = name, Path = url },
                                            //_IPinfo = _c ?? new IPinfo() { CompanyName = compName, IPAddress = _ip }
                                        });
                                    }
                                    else
                                    {
                                        await appDbContext.MainTable.AddAsync(new MainTable()
                                        {
                                            DateTimeLog   = _datetimestr,
                                            DateTime      = date_time,
                                            DataVolume    = volume,
                                            RequestResult = _res,
                                            RequestType   = request,
                                            FilesInfo     = _f ?? new FilesInfo()
                                            {
                                                DataVolume = volume, Name = name, Path = url
                                            },
                                            // _IPinfo = _c ?? new IPinfo() { CompanyName = compName, IPAddress = _ip }
                                        });
                                    }
                                }
                            }
                        }
                    });
                }


                await appDbContext.SaveChangesAsync();

                Logger.LogInformation($"{DateTime.Now.ToString()} Data saved to db");

                #region old loop for save,
                //for (int i = 0; i < allowed.Count; i++)
                //{
                //    //if(compNames.Count==ip_list.Count && urls.Count==compNames.Count && urls.Count==names.Count && dataVolumes.Count==urls.Count)
                //    infoIp.CompanyName = compNames[i];
                //    infoIp.IPAddress = ip_list[i];
                //    if (names[i] != string.Empty)
                //    {
                //        filesInfo.Path = urls[i];
                //        filesInfo.Name = names[i];
                //        filesInfo.DataVolume = dataVolumes[i];
                //    }
                //    if (!ip.Contains(JsonConvert.SerializeObject(infoIp)))
                //    {
                //        infoIp = new IPinfo() { CompanyName = compNames[i], IPAddress = ip_list[i] };
                //        ip.Add(JsonConvert.SerializeObject(infoIp));
                //    }
                //    if (!files.Contains(JsonConvert.SerializeObject(filesInfo)))
                //    {
                //        filesInfo = new FilesInfo() { Name = names[i], Path = urls[i], DataVolume = dataVolumes[i] };
                //    }

                //    if ((appDbContext.IpInfo.Where(q => q.IPAddress == infoIp.IPAddress).Count() == 0) && appDbContext.FilesInfos.Where(q => q.Path == filesInfo.Path).Count() == 0)
                //    {


                //        MainTable mainTable = new MainTable()
                //        {
                //            DataVolume = dataVolumes[i],
                //            DateTime = dateTimes[i],
                //            DateTimeLog = dateTimeStr[i],
                //            FilesInfo = filesInfo,
                //            _IPinfo = infoIp,
                //            RequestType = request_types[i],
                //            RequestResult = results[i]
                //        };
                //        if (!mainTables.Contains(JsonConvert.SerializeObject(mainTable)))
                //        {
                //            mainTables.Add(JsonConvert.SerializeObject(mainTable));
                //            await appDbContext.MainTable.AddAsync(mainTable);

                //        }
                //        if (!files.Contains(JsonConvert.SerializeObject(filesInfo)))
                //        {
                //            files.Add(JsonConvert.SerializeObject(filesInfo));
                //            await appDbContext.FilesInfos.AddAsync(filesInfo);
                //        }
                //        if (!ip.Contains(JsonConvert.SerializeObject(infoIp)))
                //        {
                //            ip.Add(JsonConvert.SerializeObject(infoIp));
                //            await appDbContext.IpInfo.AddAsync(infoIp);
                //        }
                //    }
                //}
                #endregion

                await appDbContext.SaveChangesAsync();
            }


            return(await Task.Run(() => false));
        }