Advertisement
SalemEGY

Conquer Country Ban System

Jun 24th, 2024 (edited)
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.79 KB | Source Code | 0 0
  1. /**
  2. step 1
  3. open Game Server then add this lines in Program.cs
  4. **/
  5. public static CountryLookup Lookup = new CountryLookup("http://github.com/SalemEGY/CountryIPLookUp/raw/main/LookUp_BySalem.dat"); //dont change this link or you will get errors
  6. public static List<string> BlockedCountry = new List<string>() { "Egypt", "United States" }; // add countryName as string to block
  7. ---------------------
  8. /**
  9. step 2 add new class file CountryLookup.cs
  10. and copy this class to your new class CountryLookup
  11. **/
  12. using System;
  13. using System.IO;
  14. using System.Net;
  15. using System.Net.Http;
  16.  
  17. namespace CoServer //change the name space whith your project namespace <-----------
  18. {
  19.     public class CountryLookup
  20.     {
  21.         private byte[] ipData;
  22.         private static long COUNTRY_BEGIN = 16776960L;
  23.  
  24.         private static string[] countryCode = new string[253]
  25.         {
  26.         "--", "AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM",
  27.         "AN", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ", "BA",
  28.         "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BM", "BN",
  29.         "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC",
  30.         "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO",
  31.         "CR", "CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM",
  32.         "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI",
  33.         "FJ", "FK", "FM", "FO", "FR", "FX", "GA", "GB", "GD", "GE",
  34.         "GF", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS",
  35.         "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU",
  36.         "ID", "IE", "IL", "IN", "IO", "IQ", "IR", "IS", "IT", "JM",
  37.         "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR",
  38.         "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS",
  39.         "LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK",
  40.         "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU",
  41.         "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG",
  42.         "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE",
  43.         "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT",
  44.         "PW", "PY", "QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC",
  45.         "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN",
  46.         "SO", "SR", "ST", "SV", "SY", "SZ", "TC", "TD", "TF", "TG",
  47.         "TH", "TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV",
  48.         "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC",
  49.         "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "RS",
  50.         "ZA", "ZM", "ME", "ZW", "A1", "A2", "O1", "AX", "GG", "IM",
  51.         "JE", "BL", "MF"
  52.         };
  53.  
  54.         private static string[] countryName = new string[253]
  55.         {
  56.         "N/A", "Asia/Pacific Region", "Europe", "Andorra", "United Arab Emirates", "Afghanistan", "Antigua and Barbuda", "Anguilla", "Albania", "Armenia",
  57.         "Netherlands Antilles", "Angola", "Antarctica", "Argentina", "American Samoa", "Austria", "Australia", "Aruba", "Azerbaijan", "Bosnia and Herzegovina",
  58.         "Barbados", "Bangladesh", "Belgium", "Burkina Faso", "Bulgaria", "Bahrain", "Burundi", "Benin", "Bermuda", "Brunei Darussalam",
  59.         "Bolivia", "Brazil", "Bahamas", "Bhutan", "Bouvet Island", "Botswana", "Belarus", "Belize", "Canada", "Cocos (Keeling) Islands",
  60.         "Congo, The Democratic Republic of the", "Central African Republic", "Congo", "Switzerland", "Cote D'Ivoire", "Cook Islands", "Chile", "Cameroon", "China", "Colombia",
  61.         "Costa Rica", "Cuba", "Cape Verde", "Christmas Island", "Cyprus", "Czech Republic", "Germany", "Djibouti", "Denmark", "Dominica",
  62.         "Dominican Republic", "Algeria", "Ecuador", "Estonia", "Egypt", "Western Sahara", "Eritrea", "Spain", "Ethiopia", "Finland",
  63.         "Fiji", "Falkland Islands (Malvinas)", "Micronesia, Federated States of", "Faroe Islands", "France", "France, Metropolitan", "Gabon", "United Kingdom", "Grenada", "Georgia",
  64.         "French Guiana", "Ghana", "Gibraltar", "Greenland", "Gambia", "Guinea", "Guadeloupe", "Equatorial Guinea", "Greece", "South Georgia and the South Sandwich Islands",
  65.         "Guatemala", "Guam", "Guinea-Bissau", "Guyana", "Hong Kong", "Heard Island and McDonald Islands", "Honduras", "Croatia", "Haiti", "Hungary",
  66.         "Indonesia", "Ireland", "Israel", "India", "British Indian Ocean Territory", "Iraq", "Iran, Islamic Republic of", "Iceland", "Italy", "Jamaica",
  67.         "Jordan", "Japan", "Kenya", "Kyrgyzstan", "Cambodia", "Kiribati", "Comoros", "Saint Kitts and Nevis", "Korea, Democratic People's Republic of", "Korea, Republic of",
  68.         "Kuwait", "Cayman Islands", "Kazakstan", "Lao People's Democratic Republic", "Lebanon", "Saint Lucia", "Liechtenstein", "Sri Lanka", "Liberia", "Lesotho",
  69.         "Lithuania", "Luxembourg", "Latvia", "Libyan Arab Jamahiriya", "Morocco", "Monaco", "Moldova, Republic of", "Madagascar", "Marshall Islands", "Macedonia",
  70.         "Mali", "Myanmar", "Mongolia", "Macau", "Northern Mariana Islands", "Martinique", "Mauritania", "Montserrat", "Malta", "Mauritius",
  71.         "Maldives", "Malawi", "Mexico", "Malaysia", "Mozambique", "Namibia", "New Caledonia", "Niger", "Norfolk Island", "Nigeria",
  72.         "Nicaragua", "Netherlands", "Norway", "Nepal", "Nauru", "Niue", "New Zealand", "Oman", "Panama", "Peru",
  73.         "French Polynesia", "Papua New Guinea", "Philippines", "Pakistan", "Poland", "Saint Pierre and Miquelon", "Pitcairn Islands", "Puerto Rico", "Palestinian Territory", "Portugal",
  74.         "Palau", "Paraguay", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saudi Arabia", "Solomon Islands", "Seychelles",
  75.         "Sudan", "Sweden", "Singapore", "Saint Helena", "Slovenia", "Svalbard and Jan Mayen", "Slovakia", "Sierra Leone", "San Marino", "Senegal",
  76.         "Somalia", "Suriname", "Sao Tome and Principe", "El Salvador", "Syrian Arab Republic", "Swaziland", "Turks and Caicos Islands", "Chad", "French Southern Territories", "Togo",
  77.         "Thailand", "Tajikistan", "Tokelau", "Turkmenistan", "Tunisia", "Tonga", "Timor-Leste", "Turkey", "Trinidad and Tobago", "Tuvalu",
  78.         "Taiwan", "Tanzania, United Republic of", "Ukraine", "Uganda", "United States Minor Outlying Islands", "United States", "Uruguay", "Uzbekistan", "Holy See (Vatican City State)", "Saint Vincent and the Grenadines",
  79.         "Venezuela", "Virgin Islands, British", "Virgin Islands, U.S.", "Vietnam", "Vanuatu", "Wallis and Futuna", "Samoa", "Yemen", "Mayotte", "Serbia",
  80.         "South Africa", "Zambia", "Montenegro", "Zimbabwe", "Anonymous Proxy", "Satellite Provider", "Other", "Aland Islands", "Guernsey", "Isle of Man",
  81.         "Jersey", "Saint Barthelemy", "Saint Martin"
  82.         };
  83.         public static bool CountryBan(string Ip, out string Messaj)
  84.         {
  85.             string country = Program.Lookup.lookupCountryName(Ip);
  86.             if (Program.BlockedCountry.Contains(country))
  87.             {
  88.                 using (var rec = new ServerSockets.RecycledPacket())
  89.                 {
  90.                     var stream = rec.GetStream();
  91.                     Messaj = "[" + country + "]";
  92.                 }
  93.                 return true;
  94.             }
  95.             else
  96.             {
  97.                 Messaj = null;
  98.                 return false;
  99.             }
  100.         }
  101.         public CountryLookup(string url)
  102.         {
  103.             try
  104.             {
  105.                 using (var client = new HttpClient())
  106.                 {
  107.                     ipData = client.GetByteArrayAsync(url).Result;
  108.                 }
  109.             }
  110.             catch (Exception ex)
  111.             {
  112.                 Console.WriteLine($"Failed to download IP data: {ex.Message}");
  113.                 throw;
  114.             }
  115.         }
  116.  
  117.         public string lookupCountryCode(string str)
  118.         {
  119.             IPAddress addr;
  120.             try
  121.             {
  122.                 addr = IPAddress.Parse(str);
  123.             }
  124.             catch (FormatException)
  125.             {
  126.                 return "--";
  127.             }
  128.             return lookupCountryCode(addr);
  129.         }
  130.  
  131.         private long addrToNum(IPAddress addr)
  132.         {
  133.             long num = 0L;
  134.             byte[] addressBytes = addr.GetAddressBytes();
  135.             for (int i = 0; i < 4; i++)
  136.             {
  137.                 long num2 = addressBytes[i];
  138.                 if (num2 < 0)
  139.                 {
  140.                     num2 += 256;
  141.                 }
  142.                 num += num2 << (3 - i) * 8;
  143.             }
  144.             return num;
  145.         }
  146.  
  147.         public string lookupCountryCode(IPAddress addr)
  148.         {
  149.             return countryCode[(int)seekCountry(0L, addrToNum(addr), 31)];
  150.         }
  151.  
  152.         public string lookupCountryName(string str)
  153.         {
  154.             IPAddress addr;
  155.             try
  156.             {
  157.                 addr = IPAddress.Parse(str);
  158.             }
  159.             catch (FormatException)
  160.             {
  161.                 return "N/A";
  162.             }
  163.             return lookupCountryName(addr);
  164.         }
  165.  
  166.         public string lookupCountryName(IPAddress addr)
  167.         {
  168.             return countryName[(int)seekCountry(0L, addrToNum(addr), 31)];
  169.         }
  170.  
  171.         private long seekCountry(long offset, long ipnum, int depth)
  172.         {
  173.             byte[] array = new byte[6];
  174.             long[] array2 = new long[2];
  175.  
  176.             if (depth < 0)
  177.             {
  178.                 // Console.WriteLine("Error seeking country: Invalid depth.");
  179.                 return 0L;
  180.             }
  181.  
  182.             try
  183.             {
  184.                 // Calculate the position to copy from ipData
  185.                 long position = 6 * offset;
  186.  
  187.                 // Check if position is within bounds of ipData
  188.                 if (position >= ipData.Length)
  189.                 {
  190.                     // Console.WriteLine($"Error seeking country: Offset {offset} out of bounds or ipData length {ipData.Length} insufficient.");
  191.                     return 0L;
  192.                 }
  193.  
  194.                 // Copy 6 bytes from ipData into array
  195.                 Array.Copy(ipData, position, array, 0, 6);
  196.             }
  197.             catch (Exception ex)
  198.             {
  199.                 Console.WriteLine($"Error seeking country: {ex.Message}");
  200.                 return 0L;
  201.             }
  202.  
  203.             // Process array to populate array2
  204.             for (int i = 0; i < 2; i++)
  205.             {
  206.                 array2[i] = 0L;
  207.                 for (int j = 0; j < 3; j++)
  208.                 {
  209.                     int num = array[i * 3 + j];
  210.                     if (num < 0)
  211.                     {
  212.                         num += 256;
  213.                     }
  214.                     array2[i] += num << j * 8;
  215.                 }
  216.             }
  217.  
  218.             // Perform the seek based on ipnum and depth
  219.             if ((ipnum & (1 << depth)) > 0)
  220.             {
  221.                 if (array2[1] >= COUNTRY_BEGIN)
  222.                 {
  223.                     return array2[1] - COUNTRY_BEGIN;
  224.                 }
  225.                 return seekCountry(array2[1], ipnum, depth - 1);
  226.             }
  227.             if (array2[0] >= COUNTRY_BEGIN)
  228.             {
  229.                 return array2[0] - COUNTRY_BEGIN;
  230.             }
  231.             return seekCountry(array2[0], ipnum, depth - 1);
  232.         }
  233.     }
  234. }
  235.  
  236. -------------------------------------------
  237.  /**
  238.  
  239.  step 3 Goto MsgLoginClient.cs
  240.  search for : public unsafe static void LoginHandler in this void we gonna add our check for country
  241.  after this : string BanMessaje;
  242.  **/
  243. if ( CountryLookup.CountryBan(client.Socket.RemoteIp, out BanMessaje))
  244. {
  245.     using (var rec = new ServerSockets.RecycledPacket())
  246.     {
  247.         var stream = rec.GetStream();
  248.  
  249.         string Messaj = "You Country: " + BanMessaje + " Has ben banned !";
  250.  
  251.  
  252.         client.Send(new MsgServer.MsgMessage(Messaj, "ALLUSERS", MsgMessage.MsgColor.red, MsgMessage.ChatMode.Dialog).GetArray(stream));
  253.  
  254.     }
  255.     return;
  256. }
  257.  
  258. --------------------------------------
  259.    
  260. now we have finished and you have Country Ban System
Tags: Country_Ban
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement