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

Last change on this file since 188 was 187, checked in by alloc, 11 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) {
81 line = line.Trim ();
82
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 } catch (ObjectDisposedException) {
114 } catch (IOException) {
115 } catch (ThreadInterruptedException) {
116 }
117 ThreadMaster.Remove (Thread.CurrentThread.Name);
118 } catch (Exception ex) {
119 Log.Out ("Error in TelnetClientReceive: " + ex);
120 }
121
122 }
123
124 private void SendThread ()
125 {
126 try {
127 while (!IsClosed() && stream.CanWrite) {
128 try {
129 string line = toClientQueue.Dequeue ();
130 if (!IsClosed () && stream.CanWrite) {
131 byte[] utfData = Encoding.UTF8.GetBytes (line);
132 stream.Write (utfData, 0, utfData.Length);
133 stream.WriteByte (13);
134 stream.WriteByte (10);
135 }
136 } catch (IOException) {
137 } catch (ThreadInterruptedException) {
138 } catch (Exception e) {
139 Log.Out ("Error writing to client: " + e);
140 }
141 }
142
143 ThreadMaster.Remove (Thread.CurrentThread.Name);
144 } catch (Exception ex) {
145 Log.Out ("Error in TelnetClientSend: " + ex);
146 }
147
148 }
149
150 public void WriteLine (string s)
151 {
152 toClientQueue.Enqueue (s);
153 }
154
155 public void Close ()
156 {
157 if (receiveThread != null) {
158 receiveThread.Interrupt ();
159 }
160 if (sendThread != null) {
161 sendThread.Interrupt ();
162 }
163 if (client != null)
164 client.Close ();
165 client = null;
166 }
167
168 public bool IsClosed ()
169 {
170 if (client != null && !client.Connected) {
171 Log.Out ("Telnet connection interrupted: " + endpoint);
172 Close ();
173 }
174 return (client == null) || (!client.Connected);
175 }
176
177 public bool IsAuthenticated ()
178 {
179 return !authEnabled || authenticated;
180 }
181
182 }
183}
Note: See TracBrowser for help on using the repository browser.