public static void SetupConfig() { try { if (File.Exists(ConfigPath)) { StarryboundServer.serverConfig = ServerFile.Read(ConfigPath); } StarryboundServer.serverConfig.gamePort = StarryboundServer.config.serverPort; StarryboundServer.privatePassword = Utils.GenerateSecureSalt(); StarryboundServer.serverConfig.serverPasswords = new string[] { StarryboundServer.privatePassword }; StarryboundServer.serverConfig.maxPlayers = StarryboundServer.config.maxClients + 10; StarryboundServer.serverConfig.bind = StarryboundServer.config.proxyIP; if (StarryboundServer.serverConfig.useDefaultWorldCoordinate) { string[] spawnPlanet = StarryboundServer.serverConfig.defaultWorldCoordinate.Split(':'); if (spawnPlanet.Length == 5) { StarryboundServer.spawnPlanet = new WorldCoordinate(spawnPlanet[0], Convert.ToInt32(spawnPlanet[1]), Convert.ToInt32(spawnPlanet[2]), Convert.ToInt32(spawnPlanet[3]), Convert.ToInt32(spawnPlanet[4]), 0); } else { StarryboundServer.spawnPlanet = new WorldCoordinate(spawnPlanet[0], Convert.ToInt32(spawnPlanet[1]), Convert.ToInt32(spawnPlanet[2]), Convert.ToInt32(spawnPlanet[3]), Convert.ToInt32(spawnPlanet[4]), Convert.ToInt32(spawnPlanet[5])); } } StarryboundServer.serverConfig.Write(ConfigPath); } catch (Exception e) { StarryboundServer.logFatal("Failed to parse starbound.config: " + e.ToString()); Thread.Sleep(5000); Environment.Exit(8); } }
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 static BootstrapFile Read(Stream stream) { try { using (var sr = new StreamReader(stream)) { return(JsonConvert.DeserializeObject <BootstrapFile>(sr.ReadToEnd())); } } catch (Exception) { StarryboundServer.logFatal("bootstrap.config file is unreadable. The server start cannot continue."); Thread.Sleep(5000); Environment.Exit(6); } return(null); }
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)"); } }
void parseOutput(string consoleLine) { try { foreach (string line in filterConsole) { if (consoleLine.Contains(line)) { return; } } if (consoleLine.StartsWith("Error: ") || consoleLine.StartsWith("AssetException:")) { this.parseError = true; } else if ((consoleLine.StartsWith("Warn:") || consoleLine.StartsWith("Info:") || consoleLine.StartsWith("Debug:")) && this.parseError) { logStarboundError(" "); this.parseError = false; } else if (String.IsNullOrWhiteSpace(consoleLine) && this.parseError) { logStarboundError(" "); this.parseError = false; return; } if (consoleLine.StartsWith("Warn: Perf: ")) { string[] perf = consoleLine.Remove(0, 12).Split(' '); string function = perf[0]; float millis = Convert.ToSingle(perf[2]); if (millis > 5000) { StarryboundServer.logWarn("Parent Server [" + function + "] lagged for " + (millis / 1000) + " seconds"); } return; } else if (consoleLine.Contains("Info: Server version")) { string[] versionString = consoleLine.Split('\''); string versionName = versionString[1]; int protocolVersion = int.Parse(versionString[3]); StarryboundServer.starboundVersion.Protocol = protocolVersion; StarryboundServer.starboundVersion.Name = versionName; if (protocolVersion != StarryboundServer.ProtocolVersion) { StarryboundServer.logFatal("Detected protcol version [" + protocolVersion + "] != [" + StarryboundServer.ProtocolVersion + "] to expected protocol version!"); Thread.Sleep(5000); Environment.Exit(4); } StarryboundServer.logInfo("Parent Server Version: [" + versionName + "] Protocol: [" + protocolVersion + "]"); return; } else if (consoleLine.Contains("TcpServer will close, listener thread caught exception")) { StarryboundServer.logFatal("Parent Server TcpServer listener thread caught exception, Forcing a restart."); StarryboundServer.changeState(ServerState.Crashed, "ServerThread::parseOutput", "Starbound Tcp listener has crashed"); } else if (consoleLine.Contains("TcpServer listening on: ")) { StarryboundServer.changeState(ServerState.StarboundReady, "ServerThread::parseOutput"); ServerConfig.RemovePrivateConfig(); } else if (consoleLine.StartsWith("Info: Kicking client ")) { string[] kick = consoleLine.Remove(0, 21).Split(' '); string user = kick[0]; string id = kick[1]; string ip = kick[2]; StarryboundServer.logWarn("Parent Server disconnected " + user + " " + ip + " for inactivity."); return; } if (!this.parseError) { Console.WriteLine("[STAR] " + consoleLine); } else { logStarboundError(consoleLine); } } catch (Exception) { } }