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

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

Fixes: Changed to a single solution for both patcher + fixes

File size: 5.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net;
5using System.Net.Sockets;
6using System.Reflection;
7using System.Threading;
8
9public class AllocsNetTelnetServer
10{
11 private static ConsoleSdtd console = null;
12 private static Thread telnetThread = null;
13 private static TcpListener listener = null;
14 private static bool closed = false;
15 private static bool authEnabled = false;
16 private static List<AllocsTelnetConnection> connections = new List<AllocsTelnetConnection> ();
17
18 public static void init (int port)
19 {
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 }
33 }
34
35 private static void telnetListenThread ()
36 {
37 try {
38 Log.Out ("Started thread_Allocs_TelnetListenThread()");
39 listener.Start ();
40 while (!closed) {
41 Thread.Sleep (10);
42 if (listener.Pending ()) {
43 AllocsTelnetConnection c = new AllocsTelnetConnection (listener.AcceptTcpClient (), authEnabled);
44 connections.Add (c);
45 Log.Out ("Telnet connection from: " + c.GetEndPoint ());
46 if (authEnabled) {
47 c.WriteLine ("Please enter password:");
48 } else {
49 LoginMessage (c);
50 }
51 }
52
53 foreach (AllocsTelnetConnection c in connections) {
54 if (c.IsClosed ()) {
55 c.Close ();
56 connections.Remove (c);
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 ();
76 connections.Remove (c);
77 break;
78 }
79 Log.Out ("Telnet executed \"" + line + "\" from: " + c.GetEndPoint ());
80 ConsoleOutputSeparator.QueueTelnetCommand (line, c);
81 }
82 }
83 }
84 }
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);
90 }
91 }
92
93 private static void LoginMessage (AllocsTelnetConnection c)
94 {
95 c.WriteLine ("*** Connected with 7DTD server.");
96 c.WriteLine ("*** Dedicated server only build");
97 c.WriteLine ("*** Allocs server fixes loaded");
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];
119 }
120 }
121 return res.Trim ();
122 }
123
124 public static void Disconnect ()
125 {
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);
137 }
138 }
139
140 public static void SetConsole (ConsoleSdtd console)
141 {
142 Log.Out ("Telnet SetConsole called");
143 AllocsNetTelnetServer.console = console;
144 }
145
146 private static void RemoveClosedConnections ()
147 {
148 try {
149 foreach (AllocsTelnetConnection c in connections) {
150 if (c.IsClosed ()) {
151 c.Close ();
152 }
153 }
154 } catch (Exception e) {
155 Log.Out ("Error in AllocsTelnetServer.RemoveClosedConnections: " + e);
156 }
157 }
158
159 public static void WriteToClient (string line)
160 {
161 if (line == null) {
162 return;
163 }
164 RemoveClosedConnections ();
165 foreach (AllocsTelnetConnection c in connections) {
166 if (c.IsAuthenticated ())
167 c.WriteLine (line);
168 }
169 }
170
171 public static void WriteToClient_Single (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 }
182}
Note: See TracBrowser for help on using the repository browser.