source: binary-improvements/7dtd-server-fixes/src/AllocsNetTelnetServer.cs@ 108

Last change on this file since 108 was 107, checked in by alloc, 10 years ago

fixes

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