[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;
|
---|
[75] | 16 | private static List<AllocsTelnetConnection> conns = 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);
|
---|
[74] | 40 | conns.Add (c);
|
---|
| 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 |
|
---|
[75] | 49 | foreach (AllocsTelnetConnection c in conns) {
|
---|
[74] | 50 | if (c.IsClosed ()) {
|
---|
| 51 | c.Close ();
|
---|
| 52 | conns.Remove (c);
|
---|
| 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 ();
|
---|
| 72 | conns.Remove (c);
|
---|
| 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");
|
---|
| 93 | c.WriteLine (string.Empty);
|
---|
| 94 | c.WriteLine ("Server IP: " +
|
---|
| 95 | ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any")
|
---|
| 96 | );
|
---|
| 97 | c.WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort));
|
---|
| 98 | c.WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount));
|
---|
| 99 | c.WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode));
|
---|
| 100 | c.WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld));
|
---|
| 101 | c.WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName));
|
---|
| 102 | c.WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty));
|
---|
| 103 | c.WriteLine (string.Empty);
|
---|
| 104 | c.WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session.");
|
---|
| 105 | c.WriteLine (string.Empty);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private static string lineCorrecter (string line)
|
---|
| 109 | {
|
---|
| 110 | string res = "";
|
---|
| 111 | for (int i = 0; i < line.Length; i++) {
|
---|
| 112 | if (line [i] >= ' ' && line [i] != '\'' && line [i] <= '~') {
|
---|
| 113 | res += line [i];
|
---|
[73] | 114 | }
|
---|
| 115 | }
|
---|
[74] | 116 | return res.Trim ();
|
---|
[73] | 117 | }
|
---|
| 118 |
|
---|
| 119 | public static void Disconnect ()
|
---|
| 120 | {
|
---|
| 121 | closed = true;
|
---|
| 122 | if (listener != null) {
|
---|
| 123 | listener.Stop ();
|
---|
| 124 | }
|
---|
[75] | 125 | foreach (AllocsTelnetConnection c in conns) {
|
---|
[74] | 126 | c.Close ();
|
---|
[73] | 127 | }
|
---|
| 128 | Thread.Sleep (100);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[75] | 131 | public static void SetConsole (ConsoleSdtd console)
|
---|
[73] | 132 | {
|
---|
[75] | 133 | Log.Out("Telnet SetConsole called");
|
---|
[73] | 134 | AllocsNetTelnetServer.console = console;
|
---|
[81] | 135 | console.AddCommand(new GetGamePrefs(console));
|
---|
[75] | 136 | console.AddCommand(new GetTime(console));
|
---|
| 137 | console.AddCommand(new ListPlayersExtended(console));
|
---|
[73] | 138 | }
|
---|
| 139 |
|
---|
[74] | 140 | private static void RemoveClosedConnections ()
|
---|
| 141 | {
|
---|
[75] | 142 | foreach (AllocsTelnetConnection c in conns) {
|
---|
[74] | 143 | if (c.IsClosed ()) {
|
---|
| 144 | c.Close ();
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[73] | 149 | public static void WriteToClient (string line)
|
---|
| 150 | {
|
---|
| 151 | if (line == null) {
|
---|
| 152 | return;
|
---|
| 153 | }
|
---|
[74] | 154 | RemoveClosedConnections ();
|
---|
[75] | 155 | foreach (AllocsTelnetConnection c in conns) {
|
---|
| 156 | if (c.IsAuthenticated ())
|
---|
[74] | 157 | c.WriteLine (line);
|
---|
[73] | 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|