public GeoIPCountryBo GetGeoIpCountry(long ipNumber) { using (var db = ApiDataContext.Create()) { GeoIPCountry ipLookup = db.GeoIPCountries.FirstOrDefault(x => ipNumber >= x.IPFrom && ipNumber <= x.IPTo); return(ObjectMapper.Map <GeoIPCountry, GeoIPCountryBo>(ipLookup)); } }
internal static void Initialize() { //_blocked ; //_allowed ; ReadCountries(); if (geoIPCountry == null) { geoIPCountry = new GeoIPCountry(CountryDataFile); } }
/// <summary> /// Initializese the get data handlers, loads the config and sets up the GeoIP /// </summary> public override void Initialize() { ServerApi.Hooks.NetGetData.Register(this, GetData); GetDataHandlers = new GetDataHandlers(this); var geoippath = "Dimensions-GeoIP.dat"; string path = Path.Combine(TShock.SavePath, "Dimensions.json"); if (!File.Exists(path)) { Config.WriteTemplates(path); } Config = Config.Read(path); if (Config.EnableGeoIP && File.Exists(geoippath)) { Geo = new GeoIPCountry(geoippath); } }
public override object onReceive() { BinaryReader packetData = (BinaryReader)this.stream; bool success = packetData.ReadBoolean(); uint clientID = packetData.ReadVarUInt32(); string rejectReason = packetData.ReadStarString(); Client target = StarryboundServer.getClient(clientID); if (target != null) { target.forceDisconnect(direction, "The parent server reclaimed this clientId"); StarryboundServer.logError("[" + this.client.playerData.name + "] " + direction + ": The parent server reclaimed this clientId (" + clientID + ")"); return(true); } this.client.playerData.id = clientID; PlayerData player = this.client.playerData; if (!success) { this.client.rejectPreConnected("Connection Failed: Rejected by parent server: " + rejectReason); return(true); } StarryboundServer.addClient(this.client); string geoip_prefix = ""; if (StarryboundServer.config.enableGeoIP && StarryboundServer.Geo != null) { var code = StarryboundServer.Geo.TryGetCountryCode(IPAddress.Parse(player.ip)); var geo_loc = code == null ? "N/A" : GeoIPCountry.GetCountryNameByCode(code); geoip_prefix = String.Format("({0})", geo_loc); } StarryboundServer.sendGlobalMessage(String.Format("{0}{1} has joined the server!", player.name, geoip_prefix)); this.client.state = ClientState.Connected; StarryboundServer.logInfo(String.Format("[{0}][{1}] joined with UUID [{2}]{3}", this.client.playerData.client, this.client.playerData.ip, player.uuid, geoip_prefix != "" ? String.Format(" from {0}", geoip_prefix) : "")); return(true); }
private void OnServerConnect(ConnectEventArgs args) { if (TShock.ShuttingDown) { NetMessage.SendData(2, args.Who, -1, NetworkText.FromLiteral("Server is shutting down...")); args.Handled = true; return; } var player = new TSPlayer(args.Who); Utils.CacheIP?.SetValue(player, _forward[args.Who].IP); if (TShock.Utils.ActivePlayers() + 1 > TShock.Config.MaxSlots + TShock.Config.ReservedSlots) { player.Disconnect(TShock.Config.ServerFullNoReservedReason); args.Handled = true; return; } if (!FileTools.OnWhitelist(player.IP)) { player.Disconnect(TShock.Config.WhitelistKickReason); args.Handled = true; return; } if (TShock.Geo != null) { var code = TShock.Geo.TryGetCountryCode(IPAddress.Parse(player.IP)); player.Country = code == null ? "N/A" : GeoIPCountry.GetCountryNameByCode(code); if (code == "A1" && TShock.Config.KickProxyUsers) { player.Disconnect("Proxies are not allowed."); args.Handled = true; return; } } TShock.Players[args.Who] = player; }
/// <summary> /// Handles ip information on a new client /// </summary> /// <param name="remoteAddress">The IP address of the user</param> /// <param name="player">The player bound to the client with this IP</param> /// <returns>Whether or not the update was made successfully</returns> private bool HandleIpInformation(string remoteAddress, TSPlayer player) { typeof(TSPlayer).GetField("CacheIP", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic) .SetValue(player, remoteAddress); // This needs to be handled as Geo check for proxy in TShock runs before the IP is updated to the correct one if (Dimensions.Geo != null) { var code = Dimensions.Geo.TryGetCountryCode(System.Net.IPAddress.Parse(remoteAddress)); player.Country = code == null ? "N/A" : GeoIPCountry.GetCountryNameByCode(code); if (code == "A1") { if (TShock.Config.KickProxyUsers) { TShock.Utils.ForceKick(player, "Proxies are not allowed.", true, true); return(false); } } } return(true); }
static void Main(string[] args) { #if DEBUG config.logLevel = LogType.Debug; #endif startTime = Utils.getTimestamp(); changeState(ServerState.Starting, "StarryboundServer::Main"); Console.Title = "Loading... Starrybound Server (" + VersionNum + ") (" + ProtocolVersion + ")"; try { int processId = Convert.ToInt32(File.ReadAllText("starbound_server.pid")); Process proc = Process.GetProcessById(processId); proc.Kill(); File.Delete("starbound_server.pid"); } catch (Exception) { } monitorThread = new Thread(new ThreadStart(crashMonitor)); monitorThread.Start(); if (IsMono) { Environment.CurrentDirectory = Path.GetDirectoryName(typeof(StarryboundServer).Assembly.Location); } AppDomain.CurrentDomain.ProcessExit += new EventHandler(ProcessExit); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException); if (!IsMono) { NativeMethods.SetConsoleCtrlHandler(new NativeMethods.HandlerRoutine(ConsoleCtrlCheck), true); } BootstrapConfig.SetupConfig(); writeLog("", LogType.FileOnly); writeLog("-- Log Start: " + DateTime.Now + " --", LogType.FileOnly); logInfo("##############################################"); logInfo("#### Avilance Ltd. Starrybound Server ####"); logInfo("#### Copyright (c) Avilance Ltd. 2013 ####"); logInfo("#### Licensed under the GPLv3 ####"); logInfo("##############################################"); logInfo("Version: " + VersionNum + " (" + ProtocolVersion + ")"); logInfo("Loading Starrybound Server..."); Config.SetupConfig(); ServerConfig.SetupConfig(); Groups.SetupGroups(); Users.SetupUsers(); #if !DEBUG if (config.logLevel == LogType.Debug) { logWarn("The logLevel in your config is currently set to DEBUG. This **WILL** flood your console and log file, if you do not want this please edit your config logLevel to INFO"); logWarn("Launch will proceed in 5 seconds."); System.Threading.Thread.Sleep(5000); } #endif #if !NOSERVER if (config.proxyPort == config.serverPort) { logFatal("You cannot have the serverPort and proxyPort on the same port!"); Thread.Sleep(5000); Environment.Exit(3); } #endif var geoippath = Path.Combine(SavePath, "GeoIP.dat"); if (config.enableGeoIP && File.Exists(geoippath)) { Geo = new GeoIPCountry(geoippath); } foreach (string sector in config.sectors) { byte[] sectorBytes = Encoding.UTF8.GetBytes(sector); byte[] buffer = new byte[sectorBytes.Length + 1]; buffer[0] = (byte)sectorBytes.Length; Buffer.BlockCopy(sectorBytes, 0, buffer, 1, sectorBytes.Length); sectors.Add(sectorBytes); } Bans.ProcessBans(); Claims.LoadClaims(); logInfo("Starrybound Server initialization complete."); listener = new ListenerThread(); listenerThread = new Thread(new ThreadStart(listener.runTcp)); listenerThread.Start(); udpThread = new Thread(new ThreadStart(listener.runUdp)); udpThread.Start(); while (serverState != ServerState.ListenerReady) { } if ((int)serverState > 3) { return; } Console.Title = "Starting... Starrybound Server (" + VersionNum + ") (" + ProtocolVersion + ")"; #if !NOSERVER logInfo("Starting parent Starbound server - This may take a few moments..."); sbServer = new ServerThread(); sbServerThread = new Thread(new ThreadStart(sbServer.run)); sbServerThread.Start(); while (serverState != ServerState.StarboundReady) { } if ((int)serverState > 3) { return; } #endif logInfo("Parent Starbound server is ready. Starrybound Server now accepting connections."); changeState(ServerState.Running, "StarryboundServer::Main"); }