Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- step 1
- open Game Server then add this lines in Program.cs
- **/
- 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
- public static List<string> BlockedCountry = new List<string>() { "Egypt", "United States" }; // add countryName as string to block
- ---------------------
- /**
- step 2 add new class file CountryLookup.cs
- and copy this class to your new class CountryLookup
- **/
- using System;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- namespace CoServer //change the name space whith your project namespace <-----------
- {
- public class CountryLookup
- {
- private byte[] ipData;
- private static long COUNTRY_BEGIN = 16776960L;
- private static string[] countryCode = new string[253]
- {
- "--", "AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM",
- "AN", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ", "BA",
- "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BM", "BN",
- "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC",
- "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO",
- "CR", "CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM",
- "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI",
- "FJ", "FK", "FM", "FO", "FR", "FX", "GA", "GB", "GD", "GE",
- "GF", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS",
- "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU",
- "ID", "IE", "IL", "IN", "IO", "IQ", "IR", "IS", "IT", "JM",
- "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR",
- "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS",
- "LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK",
- "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU",
- "MV", "MW", "MX", "MY", "MZ", "NA", "NC", "NE", "NF", "NG",
- "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE",
- "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT",
- "PW", "PY", "QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC",
- "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN",
- "SO", "SR", "ST", "SV", "SY", "SZ", "TC", "TD", "TF", "TG",
- "TH", "TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV",
- "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC",
- "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "RS",
- "ZA", "ZM", "ME", "ZW", "A1", "A2", "O1", "AX", "GG", "IM",
- "JE", "BL", "MF"
- };
- private static string[] countryName = new string[253]
- {
- "N/A", "Asia/Pacific Region", "Europe", "Andorra", "United Arab Emirates", "Afghanistan", "Antigua and Barbuda", "Anguilla", "Albania", "Armenia",
- "Netherlands Antilles", "Angola", "Antarctica", "Argentina", "American Samoa", "Austria", "Australia", "Aruba", "Azerbaijan", "Bosnia and Herzegovina",
- "Barbados", "Bangladesh", "Belgium", "Burkina Faso", "Bulgaria", "Bahrain", "Burundi", "Benin", "Bermuda", "Brunei Darussalam",
- "Bolivia", "Brazil", "Bahamas", "Bhutan", "Bouvet Island", "Botswana", "Belarus", "Belize", "Canada", "Cocos (Keeling) Islands",
- "Congo, The Democratic Republic of the", "Central African Republic", "Congo", "Switzerland", "Cote D'Ivoire", "Cook Islands", "Chile", "Cameroon", "China", "Colombia",
- "Costa Rica", "Cuba", "Cape Verde", "Christmas Island", "Cyprus", "Czech Republic", "Germany", "Djibouti", "Denmark", "Dominica",
- "Dominican Republic", "Algeria", "Ecuador", "Estonia", "Egypt", "Western Sahara", "Eritrea", "Spain", "Ethiopia", "Finland",
- "Fiji", "Falkland Islands (Malvinas)", "Micronesia, Federated States of", "Faroe Islands", "France", "France, Metropolitan", "Gabon", "United Kingdom", "Grenada", "Georgia",
- "French Guiana", "Ghana", "Gibraltar", "Greenland", "Gambia", "Guinea", "Guadeloupe", "Equatorial Guinea", "Greece", "South Georgia and the South Sandwich Islands",
- "Guatemala", "Guam", "Guinea-Bissau", "Guyana", "Hong Kong", "Heard Island and McDonald Islands", "Honduras", "Croatia", "Haiti", "Hungary",
- "Indonesia", "Ireland", "Israel", "India", "British Indian Ocean Territory", "Iraq", "Iran, Islamic Republic of", "Iceland", "Italy", "Jamaica",
- "Jordan", "Japan", "Kenya", "Kyrgyzstan", "Cambodia", "Kiribati", "Comoros", "Saint Kitts and Nevis", "Korea, Democratic People's Republic of", "Korea, Republic of",
- "Kuwait", "Cayman Islands", "Kazakstan", "Lao People's Democratic Republic", "Lebanon", "Saint Lucia", "Liechtenstein", "Sri Lanka", "Liberia", "Lesotho",
- "Lithuania", "Luxembourg", "Latvia", "Libyan Arab Jamahiriya", "Morocco", "Monaco", "Moldova, Republic of", "Madagascar", "Marshall Islands", "Macedonia",
- "Mali", "Myanmar", "Mongolia", "Macau", "Northern Mariana Islands", "Martinique", "Mauritania", "Montserrat", "Malta", "Mauritius",
- "Maldives", "Malawi", "Mexico", "Malaysia", "Mozambique", "Namibia", "New Caledonia", "Niger", "Norfolk Island", "Nigeria",
- "Nicaragua", "Netherlands", "Norway", "Nepal", "Nauru", "Niue", "New Zealand", "Oman", "Panama", "Peru",
- "French Polynesia", "Papua New Guinea", "Philippines", "Pakistan", "Poland", "Saint Pierre and Miquelon", "Pitcairn Islands", "Puerto Rico", "Palestinian Territory", "Portugal",
- "Palau", "Paraguay", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saudi Arabia", "Solomon Islands", "Seychelles",
- "Sudan", "Sweden", "Singapore", "Saint Helena", "Slovenia", "Svalbard and Jan Mayen", "Slovakia", "Sierra Leone", "San Marino", "Senegal",
- "Somalia", "Suriname", "Sao Tome and Principe", "El Salvador", "Syrian Arab Republic", "Swaziland", "Turks and Caicos Islands", "Chad", "French Southern Territories", "Togo",
- "Thailand", "Tajikistan", "Tokelau", "Turkmenistan", "Tunisia", "Tonga", "Timor-Leste", "Turkey", "Trinidad and Tobago", "Tuvalu",
- "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",
- "Venezuela", "Virgin Islands, British", "Virgin Islands, U.S.", "Vietnam", "Vanuatu", "Wallis and Futuna", "Samoa", "Yemen", "Mayotte", "Serbia",
- "South Africa", "Zambia", "Montenegro", "Zimbabwe", "Anonymous Proxy", "Satellite Provider", "Other", "Aland Islands", "Guernsey", "Isle of Man",
- "Jersey", "Saint Barthelemy", "Saint Martin"
- };
- public static bool CountryBan(string Ip, out string Messaj)
- {
- string country = Program.Lookup.lookupCountryName(Ip);
- if (Program.BlockedCountry.Contains(country))
- {
- using (var rec = new ServerSockets.RecycledPacket())
- {
- var stream = rec.GetStream();
- Messaj = "[" + country + "]";
- }
- return true;
- }
- else
- {
- Messaj = null;
- return false;
- }
- }
- public CountryLookup(string url)
- {
- try
- {
- using (var client = new HttpClient())
- {
- ipData = client.GetByteArrayAsync(url).Result;
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Failed to download IP data: {ex.Message}");
- throw;
- }
- }
- public string lookupCountryCode(string str)
- {
- IPAddress addr;
- try
- {
- addr = IPAddress.Parse(str);
- }
- catch (FormatException)
- {
- return "--";
- }
- return lookupCountryCode(addr);
- }
- private long addrToNum(IPAddress addr)
- {
- long num = 0L;
- byte[] addressBytes = addr.GetAddressBytes();
- for (int i = 0; i < 4; i++)
- {
- long num2 = addressBytes[i];
- if (num2 < 0)
- {
- num2 += 256;
- }
- num += num2 << (3 - i) * 8;
- }
- return num;
- }
- public string lookupCountryCode(IPAddress addr)
- {
- return countryCode[(int)seekCountry(0L, addrToNum(addr), 31)];
- }
- public string lookupCountryName(string str)
- {
- IPAddress addr;
- try
- {
- addr = IPAddress.Parse(str);
- }
- catch (FormatException)
- {
- return "N/A";
- }
- return lookupCountryName(addr);
- }
- public string lookupCountryName(IPAddress addr)
- {
- return countryName[(int)seekCountry(0L, addrToNum(addr), 31)];
- }
- private long seekCountry(long offset, long ipnum, int depth)
- {
- byte[] array = new byte[6];
- long[] array2 = new long[2];
- if (depth < 0)
- {
- // Console.WriteLine("Error seeking country: Invalid depth.");
- return 0L;
- }
- try
- {
- // Calculate the position to copy from ipData
- long position = 6 * offset;
- // Check if position is within bounds of ipData
- if (position >= ipData.Length)
- {
- // Console.WriteLine($"Error seeking country: Offset {offset} out of bounds or ipData length {ipData.Length} insufficient.");
- return 0L;
- }
- // Copy 6 bytes from ipData into array
- Array.Copy(ipData, position, array, 0, 6);
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Error seeking country: {ex.Message}");
- return 0L;
- }
- // Process array to populate array2
- for (int i = 0; i < 2; i++)
- {
- array2[i] = 0L;
- for (int j = 0; j < 3; j++)
- {
- int num = array[i * 3 + j];
- if (num < 0)
- {
- num += 256;
- }
- array2[i] += num << j * 8;
- }
- }
- // Perform the seek based on ipnum and depth
- if ((ipnum & (1 << depth)) > 0)
- {
- if (array2[1] >= COUNTRY_BEGIN)
- {
- return array2[1] - COUNTRY_BEGIN;
- }
- return seekCountry(array2[1], ipnum, depth - 1);
- }
- if (array2[0] >= COUNTRY_BEGIN)
- {
- return array2[0] - COUNTRY_BEGIN;
- }
- return seekCountry(array2[0], ipnum, depth - 1);
- }
- }
- }
- -------------------------------------------
- /**
- step 3 Goto MsgLoginClient.cs
- search for : public unsafe static void LoginHandler in this void we gonna add our check for country
- after this : string BanMessaje;
- **/
- if ( CountryLookup.CountryBan(client.Socket.RemoteIp, out BanMessaje))
- {
- using (var rec = new ServerSockets.RecycledPacket())
- {
- var stream = rec.GetStream();
- string Messaj = "You Country: " + BanMessaje + " Has ben banned !";
- client.Send(new MsgServer.MsgMessage(Messaj, "ALLUSERS", MsgMessage.MsgColor.red, MsgMessage.ChatMode.Dialog).GetArray(stream));
- }
- return;
- }
- --------------------------------------
- now we have finished and you have Country Ban System
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement