private string GenerateUrlID(URLContext urlContext, URL url) { Console.WriteLine($"Generating ID for website: {url.BaseURL} for User (IP): {url.ExternalIP} at {DateTime.Now}"); string id = Guid.NewGuid().ToString().Substring(0, 5); //Ensures that the shortened id is unique, if it has to retry 5 times, another char is allowed to be added int duplicateCount = 0; int duplicateModifier = 1; while (!IsIdValid(id) || urlContext.UrlSet.Where(x => x.ShortenedIdentifier == id).Count() > 0) { if (duplicateCount != 0 && duplicateCount % 5 == 0) { id = Guid.NewGuid().ToString().Substring(0, 5 + duplicateModifier); duplicateModifier++; } else { id = Guid.NewGuid().ToString().Substring(0, 5); } duplicateCount++; } return(id); }
private void BeginRequest(object sender, EventArgs eventArgs) { HttpApplication httpApplication = (HttpApplication) sender; HttpContext context = (HttpContext) httpApplication.Context; HttpRequest httpRequest = context.Request; HttpResponse httpResponse = context.Response; string filepath = httpRequest.FilePath; if (filepath.Contains("/pages")) { // No special Processing } else if (filepath.Contains("/Trace.axd") || filepath.Contains("/CacheManager.axd")) { // No special Processing } else if (filepath.Contains("/resources")) { // No special Processing } else { // Rewrite the URL URLContext urlContext = new URLContext(); URLContext.StoreURLContext(urlContext); urlContext.Scheme = httpRequest.Url.Scheme; urlContext.Host = httpRequest.Url.Host; ProcessParameters(urlContext, httpRequest); ProcessFilePath(urlContext, filepath); string newPath = URLBuilder.BuildInternalURL(urlContext); context.RewritePath(newPath, false); } }
public async Task UpdateUrlUsers(URLContext urlContext, HttpRequest request, URL url) { string userIpAddress = request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); User retrievedUser; //Get user object for this url and this IP address - if it doesn't exist, create it try { retrievedUser = urlContext.UrlUsersSet.Where(x => x.UrlId == url.Id).Select(x => x.User).Where(x => x.IpAddress == request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString()).Single(); } catch (InvalidOperationException) { User newUser = CreateUser(urlContext, userIpAddress, url, false); await urlContext.SaveChangesAsync(); return; } //If retrieved user exists Update it retrievedUser.LastUsedTime = DateTime.Now; retrievedUser.UseCount += 1; await urlContext.SaveChangesAsync(); }
static long Seek(IntPtr pUrlcontext, long pos, int whence) { URLContext urlcontext = PtrToStructure <URLContext>(pUrlcontext); Stream s; if (streams.TryGetValue(urlcontext.privdata.ToString(), out s)) { // Get SeekOrigin matching given whence SeekOrigin so = SeekOrigin.Current; switch (whence) { case 0: so = SeekOrigin.Begin; break; case 1: so = SeekOrigin.Current; break; case 3: so = SeekOrigin.End; break; case 0x10000: return(s.Length); } // Seek return((int)s.Seek(pos, so)); } return(0); }
/// <summary> /// Seeks to the right position in the memorystream. /// </summary> /// <param name="pUrlcontext">Settings for the custom Defraser protol.</param> /// <param name="pos">The seek offset from 'whence'.</param> /// <param name="whence">The position from which the seek starts.</param> /// <returns>The new (seeked) position in the file/Data.</returns> private static long ProtocolSeek(IntPtr pUrlcontext, long pos, int whence) { URLContext context = PtrToStructure <URLContext>(pUrlcontext); int index = Int32.Parse(context.filename.Substring(_protocol.name.Length + 3)); MemoryStream videoStream = _videoMemoryStreams[index]; SeekOrigin so = SeekOrigin.Current; switch (whence) { case 0: so = SeekOrigin.Begin; break; case 1: so = SeekOrigin.Current; break; case 3: so = SeekOrigin.End; break; case 0x10000: return(videoStream.Length); } return(videoStream.Seek(pos, so)); }
/// <summary> /// /// </summary> /// <param name="pUrlcontext">Settings for the custom Defraser protol.</param> /// <returns>Status, 0 when no errors occurred.</returns> private static int ProtocolClose(IntPtr pUrlcontext) { URLContext context = PtrToStructure <URLContext>(pUrlcontext); int index = Int32.Parse(context.filename.Substring(DefraserProtocolPrefix.Length)); _videoMemoryStreams[index].Close(); return(0); }
public IEnumerable <URL> GetUserUrls(URLContext urlContext, Microsoft.AspNetCore.Http.HttpContext contextHttp) { var userURLs = urlContext.UrlUsersSet .Where(x => x.User.IpAddress == contextHttp.Connection.RemoteIpAddress.MapToIPv4().ToString()) .Where(x => x.User.HasAdminPrivileges == true) .Select(x => x.Url); //Ensures the same user is accessing the page by validating their IP and admin privileges return(userURLs); }
public async Task RemoveURL(URLContext urlContext, string shortenedID) { URL removeURL = urlContext.UrlSet.Single(x => x.ShortenedIdentifier == shortenedID); List <int> removeUserIds = urlContext.UrlUsersSet.Where(x => x.UrlId == removeURL.Id).Select(x => x.UserId).ToList(); urlContext.UrlSet.Remove(removeURL); await urlContext.SaveChangesAsync(); await RemoveUrlUsers(urlContext, removeUserIds); //Remove every user associated with the URL }
public static int url_open(out URLContext h, string filename, int flags) { IntPtr ptr; int ret = url_open(out ptr, filename, flags); h = *(URLContext *)ptr.ToPointer(); av_free(ptr); return(ret); }
public string GetLastCountryAccessed(URLContext urlContext, URL url) { IEnumerable <User> users = urlContext.UrlUsersSet.Where(x => x.UrlId == url.Id).Select(x => x.User).OrderByDescending(x => x.LastUsedTime); if (users != null && users.Count() != 0) { return(users.First().CountryCode); } return(null); }
private void ProcessParameters(URLContext URLContext, HttpRequest httpRequest) { if (httpRequest.QueryString["topic"] != null && httpRequest.QueryString["topic"].Length > 0) { URLContext.Topic = httpRequest.QueryString["topic"]; } if (httpRequest.QueryString["page"] != null && httpRequest.QueryString["page"].Length > 0) { URLContext.Page = httpRequest.QueryString["page"]; } }
private async Task RemoveUrlUsers(URLContext urlContext, List <int> removeUserIds) { List <User> removeUsers = new List <User>(); foreach (var i in removeUserIds) { User currentUser = urlContext.UserSet.Single(x => x.Id == i); urlContext.UserSet.Remove(currentUser); } await urlContext.SaveChangesAsync(); }
public string ReturnBaseUrl(URLContext urlContext, string shortId) { try { string baseUrl = urlContext.UrlSet.Single(x => x.ShortenedIdentifier == shortId).BaseURL; return(baseUrl); } catch (InvalidOperationException) { return("404"); } }
public int GetUrlTotalUses(URLContext urlContext, URL url) { IEnumerable <User> urlUsers = urlContext.UrlUsersSet.Where(x => x.UrlId == url.Id).Select(x => x.User); int count = 0; foreach (var i in urlUsers) { count += i.UseCount; } return(count); }
static int Close(IntPtr pUrlcontext) { URLContext urlcontext = PtrToStructure <URLContext>(pUrlcontext); Stream s; if (streams.TryGetValue(urlcontext.privdata.ToString(), out s)) { s.Close(); streams.Remove(urlcontext.privdata.ToString()); return(0); } return(1); }
/// <summary> /// Reads the required header and frame Data from a MemoryStream to a buffer for Defraser. /// </summary> /// <param name="pUrlcontext">Settings for the custom Defraser protol.</param> /// <param name="buffer">Buffer for FFmpeg which contains the Data read from the MemoryStream.</param> /// <param name="size">The size of the buffer to read to.</param> /// <returns>Size of the Data read and put into the buffer.</returns> private static int ProtocolRead(IntPtr pUrlcontext, IntPtr buffer, int size) { URLContext context = PtrToStructure <URLContext>(pUrlcontext); // Get video packet id from filename int index = Int32.Parse(context.filename.Substring(DefraserProtocolPrefix.Length)); MemoryStream videoStream = _videoMemoryStreams[index]; byte[] temp = new byte[size]; int count = videoStream.Read(temp, 0, size); Marshal.Copy(temp, 0, buffer, count); return(count); }
private async Task AppendUrlToDB(URLContext urlContext, URL urlToAdd, HttpRequest request) //Called from AddUrl method { urlToAdd.ShortenedIdentifier = GenerateUrlID(urlContext, urlToAdd); Console.WriteLine($"Adding ID: {urlToAdd.ShortenedIdentifier} for website: {urlToAdd.BaseURL} for User (IP): {urlToAdd.ExternalIP} at {DateTime.Now}"); await urlContext.UrlSet.AddAsync(urlToAdd); await urlContext.SaveChangesAsync(); if (request != null) { CreateUser(urlContext, request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(), urlToAdd, true); } }
public ScannerController(URLContext context, ScannerService scannerService, VirusScanService virusScanService) { // Initializing an in-memory database for saving requests and responses for measuring purposes _context = context; _scannerService = scannerService; _virusScanService = virusScanService; if (_context.URLItems.Count() == 0) { _context.URLItems.Add(new URLItem { Url = "http://foobar.com" }); _context.SaveChanges(); } }
private URLContext ProcessFilePath(URLContext urlContext, string filepath) { URLPathTokenizer tokenizer = new URLPathTokenizer(filepath); string token = tokenizer.nextToken(); if (token == null) return urlContext; // parse virtual if ("TheInternetBuzzWebApplication".Equals(token)) { urlContext.Virtual = token; token = tokenizer.nextToken(); if (token == null) return urlContext; } // parse section if (IsValidSection(token)) { urlContext.Section = token; token = tokenizer.nextToken(); if (token == null) return urlContext; // parse topic urlContext.Topic = token.Replace("-"," "); token = tokenizer.nextToken(); if (token == null) return urlContext; // parse page urlContext.Page = token; } // resources else { if ("sitemap.xml".Equals(token)) { urlContext.Resource = "/resources/Sitemap.aspx"; } else { urlContext.Resource = "/resources/" + token; } } return urlContext; }
static int Read(IntPtr pUrlcontext, IntPtr buffer, int size) { URLContext urlcontext = PtrToStructure <URLContext>(pUrlcontext); Stream s; if (streams.TryGetValue(urlcontext.privdata.ToString(), out s)) { // Write bytes to temporary buffer byte[] temp = new byte[size]; int count = s.Read(temp, 0, size); // Write buffer to pointer Marshal.Copy(temp, 0, buffer, count); return(count); } return(0); }
public Dictionary <string, int> GetUrlCountries(URLContext urlContext, URL url) { var countryData = new Dictionary <string, int>(); IEnumerable <User> users = urlContext.UrlUsersSet.Where(x => x.UrlId == url.Id).Select(x => x.User); foreach (var i in users) { if (!countryData.ContainsKey(i.CountryCode)) { countryData.Add(i.CountryCode, i.UseCount); } else { countryData[i.CountryCode] += i.UseCount; } } return(countryData); }
static int Open(IntPtr pUrlcontext, string filename, int flags) { // Get URLContext structure from pointer URLContext urlcontext = PtrToStructure <URLContext>(pUrlcontext); Stream s; if (streams.TryGetValue(filename, out s)) { // Set privdata to stream id urlcontext.privdata = (IntPtr)counter; // Register stream id streams.Remove(filename); streams.Add(counter.ToString(), s); ++counter; // Write new URLContext to pointer Marshal.StructureToPtr(urlcontext, pUrlcontext, false); return(0); } return(1); }
private User CreateUser(URLContext urlContext, string ipAddress, URL parentUrl, bool isAdmin) { User returnUser = new User() { IpAddress = ipAddress, DateInitialised = DateTime.Now, LastUsedTime = DateTime.Now, UseCount = 1, HasAdminPrivileges = isAdmin, CountryCode = GetCountryCode(ipAddress) }; urlContext.UserSet.AddAsync(returnUser); urlContext.SaveChangesAsync(); urlContext.UrlUsersSet.AddAsync(new UrlUsers() { UrlId = parentUrl.Id, User = returnUser }); urlContext.SaveChangesAsync(); return(returnUser); }
public UrlQueryRepository(URLContext context) { _context = context; }
public UrlVisitorsCounterQueryRepository(URLContext context) { _context = context; }
public URLController(URLContext context) { Context = context; }
public static extern int url_close(ref URLContext h);
public static extern long url_filesize(ref URLContext h);
public static extern int url_get_max_packet_size(ref URLContext h);
public static extern void url_get_filename(ref URLContext h, [In, Out] StringBuilder buf, int buf_size);
public URLController(URLService urlService, URLContext urlContext, MailService mailService) { _urlService = urlService; _urlContext = urlContext; _mailService = mailService; }
public async Task AddURL(URLContext urlContext, URL urlToAdd, HttpRequest request) { IEnumerable <URL> urls = urlContext.UrlSet.Where(x => x.BaseURL == urlToAdd.BaseURL); await AppendUrlToDB(urlContext, urlToAdd, request); }
//Returns the URL from a given short ID public URL ReturnUrlModel(URLContext urlContext, string shortId) => urlContext.UrlSet.Single(x => x.ShortenedIdentifier == shortId);