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

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

Fixes

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