Advertisement
SirSpamModz

uri handler

Sep 22nd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Scraper
  9. {
  10.     public enum Protocol : byte
  11.     {
  12.         UNKNOWN,
  13.         HTTP,
  14.         HTTPS,
  15.         FTP,
  16.         FILE,
  17.         SFTP,
  18.     };
  19.  
  20.     public struct Url
  21.     {
  22.         public Protocol protocol;
  23.         public string domain;
  24.         public string path;
  25.         public UInt16 port;
  26.  
  27.         public bool valid;
  28.     }
  29.  
  30.     public struct ProtocolStringMatch
  31.     {
  32.         public Protocol protocol;
  33.         public string protocol_string;
  34.     }
  35.  
  36.     class UrlHandler
  37.     {
  38.         private static List<ProtocolStringMatch> protocols = new List<ProtocolStringMatch>();
  39.         private static List<Url> urls = new List<Url>(Int32.MaxValue);
  40.         private static List<string> domainBlacklist = new List<string>();
  41.  
  42.         public static void PushProtocol(Protocol protocol, string match)
  43.         {
  44.             protocols.Add(new ProtocolStringMatch()
  45.             {
  46.                 protocol = protocol,
  47.                 protocol_string = match
  48.             });
  49.         }
  50.  
  51.         public static Protocol GetUrlProtocol(string url)
  52.         {
  53.             foreach (ProtocolStringMatch psm in protocols)
  54.             {
  55.                 if (url.StartsWith(psm.protocol_string))
  56.                 {
  57.                     return psm.protocol;
  58.                 }
  59.             }
  60.  
  61.             return Protocol.UNKNOWN;
  62.         }
  63.  
  64.         public static string GetProtocolMatch(Protocol protocol)
  65.         {
  66.             foreach (ProtocolStringMatch psm in protocols)
  67.             {
  68.                 if (protocol == psm.protocol)
  69.                 {
  70.                     return psm.protocol_string;
  71.                 }
  72.             }
  73.  
  74.             return null;
  75.         }
  76.  
  77.         public static void PushBlacklistDomain(string domain)
  78.         {
  79.             domainBlacklist.Add(domain);
  80.         }
  81.  
  82.         public static void PushUrl(Url url)
  83.         {
  84.             foreach (string domain in domainBlacklist)
  85.             {
  86.                 if(url.domain == domain)
  87.                 {
  88.                     return;
  89.                 }
  90.             }
  91.  
  92.             urls.Add(url);
  93.         }
  94.  
  95.         public static Url ParseUrl(string currentConnection, string new_path)
  96.         {
  97.             Url ret = default(Url);
  98.  
  99.             // Let's just stick with 80, we can get ports from the url later.
  100.             ret.port = 80;
  101.  
  102.             if (new_path[0] == '?')
  103.             {
  104.                 //
  105.                 // Parsing a file like "?ajax=1&download=all"
  106.                 //
  107.  
  108.                 // TODO: Parse query redirect
  109.                 ret.valid = false;
  110.             }
  111.             else if (new_path[0] == '#')
  112.             {
  113.                 //
  114.                 // Parsing url as such "#lol"
  115.                 //
  116.  
  117.                 // This is only useful with human interaction. So let's skip it.
  118.                 ret.valid = false;
  119.             }
  120.             else if (new_path[0] == '/')
  121.             {
  122.                 //
  123.                 // Parsing a path like "/path/index.php?lol"
  124.                 //
  125.  
  126.                 // Getting the protocol
  127.                 Protocol prot = GetUrlProtocol(currentConnection);
  128.  
  129.                 // If the protocol doesn't equal to unknown, continue.
  130.                 if(prot != Protocol.UNKNOWN)
  131.                 {
  132.                     // Setting the return protocol
  133.                     ret.protocol = prot;
  134.  
  135.                     // Setting the doamin, subtracting the protocol.
  136.                     ret.domain = currentConnection.Substring(GetProtocolMatch(prot).Length);
  137.  
  138.                     // Getting the path position in the url
  139.                     int pathPosition = ret.domain.IndexOf('/');
  140.  
  141.                     // Checking it the path index was found
  142.                     if(pathPosition != -1)
  143.                     {
  144.                         // Path was found, so let's remote the path from the domain.
  145.                         ret.domain = ret.domain.Substring(0, pathPosition);
  146.  
  147.                         // Setting the path, as the path.
  148.                         ret.path = new_path;
  149.                     }
  150.  
  151.                     // So far the domain is valid
  152.                     ret.valid = true;
  153.                 }
  154.                 else
  155.                 {
  156.                     ret.valid = false;
  157.                 }
  158.             }
  159.             else if ((ret.protocol = GetUrlProtocol(new_path)) != Protocol.UNKNOWN)
  160.             {
  161.                 //
  162.                 // Parsing path like "http://google.com/drinkbleach?dankmeme"
  163.                 //
  164.  
  165.                 // Getting the start of the domain.
  166.                 ret.domain = currentConnection.Substring(GetProtocolMatch(ret.protocol).Length);
  167.  
  168.                 // Getting the path position in the url
  169.                 int pathPosition = ret.domain.IndexOf('/');
  170.  
  171.                 // Checking it the path index was found
  172.                 if (pathPosition != -1)
  173.                 {
  174.                     ret.path = ret.domain.Substring(pathPosition);
  175.  
  176.                     // Path was found, so let's remote the path from the domain.
  177.                     ret.domain = ret.domain.Substring(0, pathPosition);
  178.                 }
  179.  
  180.                 // So far the domain is valid
  181.                 ret.valid = true;
  182.             }
  183.             else
  184.             {
  185.                 //
  186.                 // Parsing a domain like "google.com/blahclhfgdfg?fghghjhj"
  187.                 //
  188.  
  189.                 // Getting the start of the domain.
  190.                 ret.domain = new_path;
  191.  
  192.                 // Getting the path position in the url
  193.                 int pathPosition = ret.domain.IndexOf('/');
  194.  
  195.                 // Checking it the path index was found
  196.                 if (pathPosition != -1)
  197.                 {
  198.                     ret.path = ret.domain.Substring(pathPosition);
  199.  
  200.                     // Path was found, so let's remote the path from the domain.
  201.                     ret.domain = ret.domain.Substring(0, pathPosition);
  202.                 }
  203.  
  204.                 // So far the domain is valid
  205.                 ret.valid = true;
  206.             }
  207.  
  208.             if (!IsValidDomain(ret.domain))
  209.             {
  210.                 // Unable to resolve the domain, so it's invalid.
  211.                 ret.valid = false;
  212.             }
  213.  
  214.             return ret;
  215.         }
  216.  
  217.         public static bool IsValidDomain(this string domainName)
  218.         {
  219.             IPAddress[] ips = Dns.GetHostAddresses(domainName);
  220.  
  221.             if (ips.Length == 0)
  222.                 return false;
  223.             return true;
  224.         }
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement