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