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

Last change on this file since 194 was 190, checked in by alloc, 10 years ago

fixes

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