[73] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.IO;
|
---|
| 4 | using System.Net;
|
---|
| 5 | using System.Net.Sockets;
|
---|
[74] | 6 | using System.Reflection;
|
---|
[73] | 7 | using System.Threading;
|
---|
| 8 |
|
---|
| 9 | public class AllocsNetTelnetServer
|
---|
| 10 | {
|
---|
[75] | 11 | private static ConsoleSdtd console = null;
|
---|
[74] | 12 | private static Thread telnetThread = null;
|
---|
| 13 | private static TcpListener listener = null;
|
---|
| 14 | private static bool closed = false;
|
---|
| 15 | private static bool authEnabled = false;
|
---|
[93] | 16 | private static List<AllocsTelnetConnection> connections = new List<AllocsTelnetConnection> ();
|
---|
[74] | 17 |
|
---|
[73] | 18 | public static void init (int port)
|
---|
| 19 | {
|
---|
[75] | 20 | Log.Out ("[7dtd-server-fixes by Alloc] Version: " + Assembly.GetExecutingAssembly ().GetName ().Version);
|
---|
[74] | 21 | authEnabled = GamePrefs.GetString (EnumGamePrefs.TelnetPassword).Length != 0;
|
---|
| 22 | if (authEnabled)
|
---|
| 23 | listener = new TcpListener (IPAddress.Any, port);
|
---|
| 24 | else
|
---|
| 25 | listener = new TcpListener (IPAddress.Loopback, port);
|
---|
[73] | 26 | telnetThread = ThreadMaster.Create ("thread Allocs TelnetListenThread", new ThreadStart (telnetListenThread));
|
---|
| 27 | telnetThread.Start ();
|
---|
[74] | 28 | Log.Out ("Started Allocs NetTelnetServer thread on " + port);
|
---|
[73] | 29 | }
|
---|
| 30 |
|
---|
| 31 | private static void telnetListenThread ()
|
---|
| 32 | {
|
---|
[74] | 33 | try {
|
---|
| 34 | Log.Out ("Started thread_Allocs_TelnetListenThread()");
|
---|
| 35 | listener.Start ();
|
---|
| 36 | while (!closed) {
|
---|
| 37 | Thread.Sleep (10);
|
---|
| 38 | if (listener.Pending ()) {
|
---|
[75] | 39 | AllocsTelnetConnection c = new AllocsTelnetConnection (listener.AcceptTcpClient (), authEnabled);
|
---|
[93] | 40 | connections.Add (c);
|
---|
[74] | 41 | Log.Out ("Telnet connection from: " + c.GetEndPoint ());
|
---|
| 42 | if (authEnabled) {
|
---|
| 43 | c.WriteLine ("Please enter password:");
|
---|
| 44 | } else {
|
---|
| 45 | LoginMessage (c);
|
---|
[73] | 46 | }
|
---|
[74] | 47 | }
|
---|
[73] | 48 |
|
---|
[93] | 49 | foreach (AllocsTelnetConnection c in connections) {
|
---|
[74] | 50 | if (c.IsClosed ()) {
|
---|
| 51 | c.Close ();
|
---|
[93] | 52 | connections.Remove (c);
|
---|
[74] | 53 | break;
|
---|
| 54 | }
|
---|
| 55 | if (c.Read ()) {
|
---|
| 56 | string line = lineCorrecter (c.GetLine ());
|
---|
| 57 | if (!c.IsAuthenticated ()) {
|
---|
| 58 | if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) {
|
---|
| 59 | c.SetAuthenticated ();
|
---|
| 60 | c.WriteLine ("Logon successful.");
|
---|
| 61 | c.WriteLine (string.Empty);
|
---|
| 62 | c.WriteLine (string.Empty);
|
---|
| 63 | c.WriteLine (string.Empty);
|
---|
| 64 | LoginMessage (c);
|
---|
| 65 | } else {
|
---|
| 66 | c.WriteLine ("Password incorrect, please enter password:");
|
---|
| 67 | }
|
---|
| 68 | } else {
|
---|
| 69 | if (line.ToLower ().Equals ("exit")) {
|
---|
| 70 | Log.Out ("Telnet connection closed by client: " + c.GetEndPoint ());
|
---|
| 71 | c.Close ();
|
---|
[93] | 72 | connections.Remove (c);
|
---|
[74] | 73 | break;
|
---|
| 74 | }
|
---|
| 75 | Log.Out ("Telnet executed \"" + line + "\" from: " + c.GetEndPoint ());
|
---|
| 76 | console.md000f (line);
|
---|
[73] | 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
[74] | 81 | Log.Out ("Exited thread_Allocs_TelnetListenThread()");
|
---|
| 82 | ThreadMaster.Remove (Thread.CurrentThread.Name);
|
---|
| 83 | } catch (Exception ex) {
|
---|
| 84 | Log.Out ("Error in Allocs telnetListenThread: " + ex.Message);
|
---|
| 85 | Log.Out ("Stack Trace: " + ex.StackTrace);
|
---|
[73] | 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[75] | 89 | private static void LoginMessage (AllocsTelnetConnection c)
|
---|
[73] | 90 | {
|
---|
[74] | 91 | c.WriteLine ("*** Connected with 7DTD server.");
|
---|
| 92 | c.WriteLine ("*** Server version: Alpha 8.7 (b29) Compatibility Version: Alpha 8.7");
|
---|
[91] | 93 | c.WriteLine ("*** Allocs server fixes loaded");
|
---|
[74] | 94 | c.WriteLine (string.Empty);
|
---|
| 95 | c.WriteLine ("Server IP: " +
|
---|
| 96 | ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any")
|
---|
| 97 | );
|
---|
| 98 | c.WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort));
|
---|
| 99 | c.WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount));
|
---|
| 100 | c.WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode));
|
---|
| 101 | c.WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld));
|
---|
| 102 | c.WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName));
|
---|
| 103 | c.WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty));
|
---|
| 104 | c.WriteLine (string.Empty);
|
---|
| 105 | c.WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session.");
|
---|
| 106 | c.WriteLine (string.Empty);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | private static string lineCorrecter (string line)
|
---|
| 110 | {
|
---|
| 111 | string res = "";
|
---|
| 112 | for (int i = 0; i < line.Length; i++) {
|
---|
| 113 | if (line [i] >= ' ' && line [i] != '\'' && line [i] <= '~') {
|
---|
| 114 | res += line [i];
|
---|
[73] | 115 | }
|
---|
| 116 | }
|
---|
[74] | 117 | return res.Trim ();
|
---|
[73] | 118 | }
|
---|
| 119 |
|
---|
| 120 | public static void Disconnect ()
|
---|
| 121 | {
|
---|
| 122 | closed = true;
|
---|
| 123 | if (listener != null) {
|
---|
| 124 | listener.Stop ();
|
---|
| 125 | }
|
---|
[93] | 126 | foreach (AllocsTelnetConnection c in connections) {
|
---|
[74] | 127 | c.Close ();
|
---|
[73] | 128 | }
|
---|
| 129 | Thread.Sleep (100);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[75] | 132 | public static void SetConsole (ConsoleSdtd console)
|
---|
[73] | 133 | {
|
---|
[75] | 134 | Log.Out("Telnet SetConsole called");
|
---|
[73] | 135 | AllocsNetTelnetServer.console = console;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[74] | 138 | private static void RemoveClosedConnections ()
|
---|
| 139 | {
|
---|
[93] | 140 | foreach (AllocsTelnetConnection c in connections) {
|
---|
[74] | 141 | if (c.IsClosed ()) {
|
---|
| 142 | c.Close ();
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[73] | 147 | public static void WriteToClient (string line)
|
---|
| 148 | {
|
---|
| 149 | if (line == null) {
|
---|
| 150 | return;
|
---|
| 151 | }
|
---|
[74] | 152 | RemoveClosedConnections ();
|
---|
[93] | 153 | foreach (AllocsTelnetConnection c in connections) {
|
---|
[75] | 154 | if (c.IsAuthenticated ())
|
---|
[74] | 155 | c.WriteLine (line);
|
---|
[73] | 156 | }
|
---|
| 157 | }
|
---|
| 158 | }
|
---|