[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");
|
---|
[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 | }
|
---|
[75] | 126 | foreach (AllocsTelnetConnection c in conns) {
|
---|
[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;
|
---|
[81] | 136 | console.AddCommand(new GetGamePrefs(console));
|
---|
[75] | 137 | console.AddCommand(new GetTime(console));
|
---|
| 138 | console.AddCommand(new ListPlayersExtended(console));
|
---|
[83] | 139 | console.AddCommand(new SayToPlayer(console));
|
---|
[84] | 140 | console.AddCommand(new SetTimeReal(console));
|
---|
[73] | 141 | }
|
---|
| 142 |
|
---|
[74] | 143 | private static void RemoveClosedConnections ()
|
---|
| 144 | {
|
---|
[75] | 145 | foreach (AllocsTelnetConnection c in conns) {
|
---|
[74] | 146 | if (c.IsClosed ()) {
|
---|
| 147 | c.Close ();
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[73] | 152 | public static void WriteToClient (string line)
|
---|
| 153 | {
|
---|
| 154 | if (line == null) {
|
---|
| 155 | return;
|
---|
| 156 | }
|
---|
[74] | 157 | RemoveClosedConnections ();
|
---|
[75] | 158 | foreach (AllocsTelnetConnection c in conns) {
|
---|
| 159 | if (c.IsAuthenticated ())
|
---|
[74] | 160 | c.WriteLine (line);
|
---|
[73] | 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|