public static void LoadClaims() { try { String claimPath = Path.Combine(StarryboundServer.SavePath, "claims.json"); StarryboundServer.logInfo("Loading players claims from file " + claimPath); if (File.Exists(claimPath)) { String serializedData = String.Empty; using (StreamReader rdr = new StreamReader(claimPath)) { serializedData = rdr.ReadLine(); } if (!String.IsNullOrEmpty(serializedData)) { _playerToSystem = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <String, List <Extensions.WorldCoordinate> > >(serializedData); } foreach (String plyUUID in _playerToSystem.Keys) { foreach (Extensions.WorldCoordinate coord in _playerToSystem[plyUUID]) { _SystemToPlayer.Add(coord, plyUUID); } } } } catch (Exception e) { StarryboundServer.logException("Unable to load claims from claims.json: {0}", e.Message); } }
public static bool addNewBan(string username, string uuid, string ipaddress, int timeBanned, string admin, int expiry, string reason) { string[] args = new string[8]; args[0] = nextBanID.ToString(); args[1] = username; args[2] = uuid; args[3] = ipaddress; args[4] = timeBanned.ToString(); args[5] = admin; args[6] = expiry.ToString(); args[7] = reason; try { Ban ban = new Ban(nextBanID, username, uuid, ipaddress, timeBanned, admin, expiry, reason); allBans.Add(nextBanID, ban); Write(Path.Combine(StarryboundServer.SavePath, "bans.json")); nextBanID++; } catch (Exception e) { StarryboundServer.logException("Unable to write ban to banned-players.txt: " + e.Message); return(false); } return(true); }
public static bool ReleaseClaim(PlayerData player, Extensions.WorldCoordinate loc) { try { stakeClaimMutex.WaitOne(); if (_SystemToPlayer.ContainsKey(loc)) { if (_SystemToPlayer[loc] == player.name) { _SystemToPlayer.Remove(loc); _playerToSystem[player.name].Remove(loc); SaveClaims(); stakeClaimMutex.ReleaseMutex(); return(true); } } } catch (Exception e) { StarryboundServer.logException("Could not release claim to location {0} for player {1}", loc, player.uuid); stakeClaimMutex.ReleaseMutex(); return(false); } return(false); }
public static void SaveClaims() { try { using (StreamWriter wtr = new StreamWriter(Path.Combine(StarryboundServer.SavePath, "claims.json"))) wtr.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(_playerToSystem)); }catch (Exception e) { StarryboundServer.logException("Unable to write claims to claims.json: {0}", e.Message); } return; }
public static ServerFile Read(Stream stream) { try { DataContractJsonSerializer str = new DataContractJsonSerializer(StarryboundServer.serverConfig.GetType()); return(str.ReadObject(stream) as ServerFile); } catch (Exception) { StarryboundServer.logException("Starbound server config is unreadable - Re-creating config with default values"); return(new ServerFile()); } }
public static ConfigFile Read(Stream stream) { try { using (var sr = new StreamReader(stream)) { return(JsonConvert.DeserializeObject <ConfigFile>(sr.ReadToEnd())); } } catch (Exception) { StarryboundServer.logException("Starrybound config is unreadable - Re-creating config with default values"); return(new ConfigFile()); } }
public static List <Ban> Read(Stream stream) { try { using (var sr = new StreamReader(stream)) { return(JsonConvert.DeserializeObject <List <Ban> >(sr.ReadToEnd())); } } catch (Exception) { StarryboundServer.logException("Server ban file is not readable - Bans WILL NOT operate until this issue is fixed."); writeBans = false; return(new List <Ban>()); } }
private void doDisconnect(string logMessage) { if (this.state == ClientState.Disposing) { return; } this.state = ClientState.Disposing; StarryboundServer.logInfo("[" + playerData.client + "] " + logMessage); try { if (this.playerData.name != null) { Client target = StarryboundServer.getClient(this.playerData.name); if (target != null) { Users.SaveUser(this.playerData); StarryboundServer.removeClient(this); if (this.kickTargetTimestamp == 0) { StarryboundServer.sendGlobalMessage(this.playerData.name + " has left the server."); } } } } catch (Exception e) { StarryboundServer.logException("Failed to remove client from clients: " + e.ToString()); } try { this.sendServerPacket(Packet.ClientDisconnect, new byte[1]); } catch (Exception) { } try { this.cSocket.Close(); this.sSocket.Close(); } catch (Exception) { } try { this.ClientForwarder.Abort(); this.ServerForwarder.Abort(); } catch (Exception) { } }
public void runTcp() { try { IPAddress localAdd = IPAddress.Parse(StarryboundServer.config.proxyIP); tcpSocket = new TcpListener(localAdd, StarryboundServer.config.proxyPort); tcpSocket.Start(); StarryboundServer.logInfo("Proxy server has been started on " + localAdd.ToString() + ":" + StarryboundServer.config.proxyPort); StarryboundServer.changeState(ServerState.ListenerReady, "ListenerThread::runTcp"); try { while (true) { TcpClient clientSocket = tcpSocket.AcceptTcpClient(); clientSocket.ReceiveTimeout = StarryboundServer.config.clientSocketTimeout * 1000; clientSocket.SendTimeout = StarryboundServer.config.internalSocketTimeout * 1000; new Thread(new ThreadStart(new Client(clientSocket).run)).Start(); } } catch (ThreadAbortException) { StarryboundServer.changeState(ServerState.Crashed, "ListenerThread::runTcp", "Thread has been aborted"); } catch (Exception e) { if ((int)StarryboundServer.serverState > 3) { return; } StarryboundServer.logException("ListenerThread Exception: " + e.ToString()); StarryboundServer.changeState(ServerState.Crashed, "ListenerThread::runTcp", e.ToString()); } tcpSocket.Stop(); StarryboundServer.logFatal("ListenerThread has failed - No new connections will be possible."); } catch (ThreadAbortException) { } catch (SocketException e) { StarryboundServer.logFatal("TcpListener has failed to start: " + e.Message); StarryboundServer.serverState = ServerState.Crashed; } }
public void run() { try { this.cIn = new BinaryReader(this.cSocket.GetStream()); this.cOut = new BinaryWriter(this.cSocket.GetStream()); IPEndPoint ipep = (IPEndPoint)this.cSocket.Client.RemoteEndPoint; IPAddress ipa = ipep.Address; this.playerData.ip = ipep.Address.ToString(); StarryboundServer.logInfo("[" + playerData.client + "] Accepting new connection."); if ((int)StarryboundServer.serverState < 3) { MemoryStream packet = new MemoryStream(); BinaryWriter packetWrite = new BinaryWriter(packet); packetWrite.WriteBE(StarryboundServer.ProtocolVersion); this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray()); rejectPreConnected("Connection Failed: The server is not ready yet."); return; } else if ((int)StarryboundServer.serverState > 3) { MemoryStream packet = new MemoryStream(); BinaryWriter packetWrite = new BinaryWriter(packet); packetWrite.WriteBE(StarryboundServer.ProtocolVersion); this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray()); rejectPreConnected("Connection Failed: The server is shutting down."); return; } else if (StarryboundServer.restartTime != 0) { MemoryStream packet = new MemoryStream(); BinaryWriter packetWrite = new BinaryWriter(packet); packetWrite.WriteBE(StarryboundServer.ProtocolVersion); this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray()); rejectPreConnected("Connection Failed: The server is restarting."); return; } sSocket = new TcpClient(); sSocket.ReceiveTimeout = StarryboundServer.config.internalSocketTimeout * 1000; sSocket.SendTimeout = StarryboundServer.config.internalSocketTimeout * 1000; IAsyncResult result = sSocket.BeginConnect((StarryboundServer.config.proxyIP.Equals("0.0.0.0") ? IPAddress.Loopback : IPAddress.Parse(StarryboundServer.config.proxyIP)), StarryboundServer.config.serverPort, null, null); bool success = result.AsyncWaitHandle.WaitOne(3000, true); if (!success || !sSocket.Connected) { StarryboundServer.failedConnections++; if (StarryboundServer.failedConnections >= StarryboundServer.config.maxFailedConnections) { StarryboundServer.logFatal(StarryboundServer.failedConnections + " clients failed to connect in a row. Restarting..."); StarryboundServer.serverState = ServerState.Crashed; } MemoryStream packet = new MemoryStream(); BinaryWriter packetWrite = new BinaryWriter(packet); packetWrite.WriteBE(StarryboundServer.ProtocolVersion); this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray()); rejectPreConnected("Connection Failed: Unable to connect to the parent server."); return; } this.sIn = new BinaryReader(this.sSocket.GetStream()); this.sOut = new BinaryWriter(this.sSocket.GetStream()); this.connectedTime = Utils.getTimestamp(); // Forwarding for data from SERVER (sIn) to CLIENT (cOut) this.ServerForwarder = new Thread(new ThreadStart(new ForwardThread(this, this.sIn, this.cOut, Direction.Server).run)); ServerForwarder.Start(); // Forwarding for data from CLIENT (cIn) to SERVER (sOut) this.ClientForwarder = new Thread(new ThreadStart(new ForwardThread(this, this.cIn, this.sOut, Direction.Client).run)); ClientForwarder.Start(); StarryboundServer.failedConnections = 0; } catch (Exception e) { StarryboundServer.logException("ClientThread Exception: " + e.ToString()); StarryboundServer.failedConnections++; MemoryStream packet = new MemoryStream(); BinaryWriter packetWrite = new BinaryWriter(packet); packetWrite.WriteBE(StarryboundServer.ProtocolVersion); this.sendClientPacket(Packet.ProtocolVersion, packet.ToArray()); rejectPreConnected("Connection Failed: A internal server error occurred (1)"); } }