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
RevLine 
[75]1using System;
[182]2using System.Collections.Generic;
3using System.IO;
[75]4using System.Net;
5using System.Net.Sockets;
[142]6using System.Text;
[182]7using System.Threading;
[75]8
[132]9namespace AllocsFixes.NetConnections.Servers.Telnet
[130]10{
[132]11 public class TelnetConnection : IConnection
[75]12 {
[184]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;
[132]17 private bool authenticated = false;
[184]18 private readonly bool authEnabled;
[132]19 private TcpClient client;
[184]20 private readonly NetworkStream stream;
21 private readonly EndPoint endpoint;
22 private readonly int endpointAddressHash;
[75]23
[184]24 public TelnetConnection (Telnet owner, TcpClient client, bool authEnabled)
[132]25 {
[184]26 this.owner = owner;
[132]27 this.authEnabled = authEnabled;
28 this.client = client;
29 this.stream = client.GetStream ();
30 this.endpoint = client.Client.RemoteEndPoint;
[182]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
[184]39 if (endpoint is IPEndPoint) {
40 endpointAddressHash = ((IPEndPoint)endpoint).Address.GetHashCode();
[185]41 //Log.Out ("Hash: " + endpointAddressHash);
[184]42 } else {
43 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType().ToString());
44 }
45
[182]46 if (authEnabled) {
47 WriteLine ("Please enter password:");
48 } else {
49 LoginMessage ();
50 }
[132]51 }
[75]52
[182]53 private void LoginMessage ()
[132]54 {
[182]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);
[75]71 }
72
[182]73 private void ReceiveThread ()
[132]74 {
[182]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 {
[187]92 if (owner.RegisterFailedLogin(endpointAddressHash)) {
[184]93 WriteLine ("Password incorrect, please enter password:");
94 } else {
95 WriteLine ("Too many failed login attempts!");
[185]96 Thread.Sleep(100);
[184]97 Close ();
[185]98 Log.Out ("Telnet connection closed for too many login attempts: " + endpoint);
[184]99 break;
100 }
[182]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 }
[183]113 } catch (ObjectDisposedException) {
[182]114 } catch (IOException) {
115 } catch (ThreadInterruptedException) {
116 }
117 ThreadMaster.Remove (Thread.CurrentThread.Name);
118 } catch (Exception ex) {
119 Log.Out ("Error in TelnetClientReceive: " + ex);
[132]120 }
[182]121
[132]122 }
123
[182]124 private void SendThread ()
[132]125 {
126 try {
[182]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 }
[91]141 }
[182]142
143 ThreadMaster.Remove (Thread.CurrentThread.Name);
144 } catch (Exception ex) {
145 Log.Out ("Error in TelnetClientSend: " + ex);
[75]146 }
[182]147
[75]148 }
149
[182]150 public void WriteLine (string s)
151 {
152 toClientQueue.Enqueue (s);
153 }
154
[132]155 public void Close ()
156 {
[182]157 if (receiveThread != null) {
158 receiveThread.Interrupt ();
159 }
160 if (sendThread != null) {
161 sendThread.Interrupt ();
162 }
[132]163 if (client != null)
164 client.Close ();
165 client = null;
166 }
[75]167
[132]168 public bool IsClosed ()
169 {
170 if (client != null && !client.Connected) {
171 Log.Out ("Telnet connection interrupted: " + endpoint);
[182]172 Close ();
[132]173 }
174 return (client == null) || (!client.Connected);
[75]175 }
176
[132]177 public bool IsAuthenticated ()
178 {
179 return !authEnabled || authenticated;
180 }
[75]181
182 }
183}
Note: See TracBrowser for help on using the repository browser.