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

Last change on this file since 189 was 189, checked in by alloc, 12 years ago

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.Text;
7using System.Threading;
8
9namespace AllocsFixes.NetConnections.Servers.Telnet
10{
11 public class TelnetConnection : IConnection
12 {
13 private readonly BlockingQueue<string> toClientQueue = new BlockingQueue<string> ();
14 private readonly Telnet owner;
15 private readonly Thread receiveThread = null;
16 private readonly Thread sendThread = null;
17 private bool authenticated = false;
18 private readonly bool authEnabled;
19 private TcpClient client;
20 private readonly NetworkStream stream;
21 private readonly EndPoint endpoint;
22 private readonly int endpointAddressHash;
23
24 public TelnetConnection (Telnet owner, TcpClient client, bool authEnabled)
25 {
26 this.owner = owner;
27 this.authEnabled = authEnabled;
28 this.client = client;
29 this.stream = client.GetStream ();
30 this.endpoint = client.Client.RemoteEndPoint;
31
32 Log.Out ("Telnet connection from: " + endpoint);
33
34 receiveThread = ThreadMaster.Create ("TelnetClientReceive", new ThreadStart (ReceiveThread));
35 receiveThread.Start ();
36 sendThread = ThreadMaster.Create ("TelnetClientSend", new ThreadStart (SendThread));
37 sendThread.Start ();
38
39 if (endpoint is IPEndPoint) {
40 endpointAddressHash = ((IPEndPoint)endpoint).Address.GetHashCode ();
41 //Log.Out ("Hash: " + endpointAddressHash);
42 } else {
43 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ());
44 }
45
46 if (authEnabled) {
47 WriteLine ("Please enter password:");
48 } else {
49 LoginMessage ();
50 }
51 }
52
53 private void LoginMessage ()
54 {
55 WriteLine ("*** Connected with 7DTD server.");
56 WriteLine ("*** Dedicated server only build");
57 WriteLine ("*** Allocs server fixes loaded");
58 WriteLine (string.Empty);
59 WriteLine ("Server IP: " +
60 ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any")
61 );
62 WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort));
63 WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount));
64 WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode));
65 WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld));
66 WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName));
67 WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty));
68 WriteLine (string.Empty);
69 WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session.");
70 WriteLine (string.Empty);
71 }
72
73 private void ReceiveThread ()
74 {
75 try {
76 StreamReader reader = new StreamReader (stream);
77 try {
78 while (!IsClosed()) {
79 string line = reader.ReadLine ();
80 if (line != null && line.Length > 0) {
81 line = line.Trim ();
82 if (line.Length > 0) {
83 if (!IsAuthenticated ()) {
84 if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) {
85 authenticated = true;
86 WriteLine ("Logon successful.");
87 WriteLine (string.Empty);
88 WriteLine (string.Empty);
89 WriteLine (string.Empty);
90 LoginMessage ();
91 } else {
92 if (owner.RegisterFailedLogin (endpointAddressHash)) {
93 WriteLine ("Password incorrect, please enter password:");
94 } else {
95 WriteLine ("Too many failed login attempts!");
96 Thread.Sleep (100);
97 Close ();
98 Log.Out ("Telnet connection closed for too many login attempts: " + endpoint);
99 break;
100 }
101 }
102 } else {
103 if (line.ToLower ().Equals ("exit")) {
104 Log.Out ("Telnet connection closed by client: " + endpoint);
105 Close ();
106 break;
107 }
108 Log.Out ("Telnet executed \"" + line + "\" from: " + endpoint);
109 ConsoleOutputSeparator.QueueNetCommand (line, this);
110 }
111 }
112 }
113 }
114 } catch (ObjectDisposedException) {
115 } catch (IOException) {
116 } catch (ThreadInterruptedException) {
117 }
118 ThreadMaster.Remove (Thread.CurrentThread.Name);
119 } catch (Exception ex) {
120 Log.Out ("Error in TelnetClientReceive: " + ex);
121 }
122
123 }
124
125 private void SendThread ()
126 {
127 try {
128 while (!IsClosed() && stream.CanWrite) {
129 try {
130 string line = toClientQueue.Dequeue ();
131 if (!IsClosed () && stream.CanWrite) {
132 byte[] utfData = Encoding.UTF8.GetBytes (line);
133 stream.Write (utfData, 0, utfData.Length);
134 stream.WriteByte (13);
135 stream.WriteByte (10);
136 }
137 } catch (IOException) {
138 } catch (ThreadInterruptedException) {
139 } catch (Exception e) {
140 Log.Out ("Error writing to client: " + e);
141 }
142 }
143
144 ThreadMaster.Remove (Thread.CurrentThread.Name);
145 } catch (Exception ex) {
146 Log.Out ("Error in TelnetClientSend: " + ex);
147 }
148
149 }
150
151 public void WriteLine (string s)
152 {
153 toClientQueue.Enqueue (s);
154 }
155
156 public void Close ()
157 {
158 if (receiveThread != null) {
159 receiveThread.Interrupt ();
160 }
161 if (sendThread != null) {
162 sendThread.Interrupt ();
163 }
164 if (client != null)
165 client.Close ();
166 client = null;
167 }
168
169 public bool IsClosed ()
170 {
171 if (client != null && !client.Connected) {
172 Log.Out ("Telnet connection interrupted: " + endpoint);
173 Close ();
174 }
175 return (client == null) || (!client.Connected);
176 }
177
178 public bool IsAuthenticated ()
179 {
180 return !authEnabled || authenticated;
181 }
182
183 }
184}
Note: See TracBrowser for help on using the repository browser.