source: binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/Telnet.cs@ 158

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

Fixes: Fixed #49

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