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

Last change on this file since 225 was 224, checked in by alloc, 10 years ago

A11 preps

File size: 5.6 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 telnet;
15 private bool authenticated = false;
16 private readonly bool authEnabled;
17 private readonly TcpClient client;
18 private readonly EndPoint endpoint;
19 private readonly int endpointAddressHash;
20 private bool closed = false;
21
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
28 public TelnetConnection (Telnet owner, TcpClient client, bool authEnabled)
29 {
30 this.telnet = owner;
31 this.authEnabled = authEnabled;
32 this.client = client;
33 this.endpoint = client.Client.RemoteEndPoint;
34
35 if (endpoint is IPEndPoint) {
36 endpointAddressHash = ((IPEndPoint)endpoint).Address.GetHashCode ();
37 } else {
38 endpointAddressHash = endpoint.GetHashCode ();
39 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ());
40 }
41
42 Log.Out ("Telnet connection from: " + endpoint);
43
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);
46
47 if (authEnabled) {
48 toClientQueue.Enqueue ("Please enter password:");
49 } else {
50 LoginMessage ();
51 }
52 }
53
54 private void LoginMessage ()
55 {
56 toClientQueue.Enqueue ("*** Connected with 7DTD server.");
57 toClientQueue.Enqueue ("*** Server version: " + GamePrefs.GetString(EnumGamePrefs.GameVersion));
58 toClientQueue.Enqueue ("*** Dedicated server only build");
59 toClientQueue.Enqueue ("*** Allocs server fixes loaded");
60 toClientQueue.Enqueue (string.Empty);
61 toClientQueue.Enqueue ("Server IP: " +
62 ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any")
63 );
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);
73 }
74
75 private void ReceiveThread (ThreadManager.ThreadInfo _tInfo)
76 {
77 try {
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:");
93 } else {
94 toClientQueue.Enqueue ("Too many failed login attempts!");
95 Thread.Sleep (100);
96 Close (true);
97 break;
98 }
99 }
100 } else {
101 if (line.ToLower ().Equals ("exit")) {
102 break;
103 }
104 Log.Out ("Telnet executed \"" + line + "\" from: " + endpoint);
105 ConsoleOutputSeparator.QueueNetCommand (line, this);
106 }
107 }
108 }
109 } catch (Exception) {
110 }
111
112 if (!closed)
113 Close ();
114 }
115
116 private void SendThread (ThreadManager.ThreadInfo _tInfo)
117 {
118 try {
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 {
125 byte[] utfData = Encoding.UTF8.GetBytes (line);
126 client.GetStream ().Write (utfData, 0, utfData.Length);
127 client.GetStream ().WriteByte (13);
128 client.GetStream ().WriteByte (10);
129 }
130 }
131 }
132 } catch (Exception) {
133 }
134
135 if (!closed)
136 Close ();
137 }
138
139 public void Close (bool kickedForLogins = false)
140 {
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 }
153 }
154
155 public void SendLine (string line)
156 {
157 if (!closed && IsAuthenticated)
158 toClientQueue.Enqueue (line);
159 else
160 toClientQueue.Enqueue (null);
161 }
162
163 public void SendLog (string text, string trace, UnityEngine.LogType type)
164 {
165 throw new System.NotImplementedException ();
166 }
167
168 public string GetDescription ()
169 {
170 return "Telnet from " + endpoint;
171 }
172 }
173}
Note: See TracBrowser for help on using the repository browser.