1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Net;
|
---|
5 | using System.Net.Sockets;
|
---|
6 | using System.Text;
|
---|
7 | using System.Threading;
|
---|
8 |
|
---|
9 | namespace AllocsFixes.NetConnections.Servers.Telnet
|
---|
10 | {
|
---|
11 | public class TelnetConnection : IConnection
|
---|
12 | {
|
---|
13 | BlockingQueue<string> toClientQueue = new BlockingQueue<string> ();
|
---|
14 | private Thread receiveThread = null;
|
---|
15 | private Thread sendThread = null;
|
---|
16 | private bool authenticated = false;
|
---|
17 | private bool authEnabled;
|
---|
18 | private TcpClient client;
|
---|
19 | private NetworkStream stream;
|
---|
20 | private EndPoint endpoint;
|
---|
21 |
|
---|
22 | public TelnetConnection (TcpClient client, bool authEnabled)
|
---|
23 | {
|
---|
24 | this.authEnabled = authEnabled;
|
---|
25 | this.client = client;
|
---|
26 | this.stream = client.GetStream ();
|
---|
27 | this.endpoint = client.Client.RemoteEndPoint;
|
---|
28 |
|
---|
29 | Log.Out ("Telnet connection from: " + endpoint);
|
---|
30 |
|
---|
31 | receiveThread = ThreadMaster.Create ("TelnetClientReceive", new ThreadStart (ReceiveThread));
|
---|
32 | receiveThread.Start ();
|
---|
33 | sendThread = ThreadMaster.Create ("TelnetClientSend", new ThreadStart (SendThread));
|
---|
34 | sendThread.Start ();
|
---|
35 |
|
---|
36 | if (authEnabled) {
|
---|
37 | WriteLine ("Please enter password:");
|
---|
38 | } else {
|
---|
39 | LoginMessage ();
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | private void LoginMessage ()
|
---|
44 | {
|
---|
45 | WriteLine ("*** Connected with 7DTD server.");
|
---|
46 | WriteLine ("*** Dedicated server only build");
|
---|
47 | WriteLine ("*** Allocs server fixes loaded");
|
---|
48 | WriteLine (string.Empty);
|
---|
49 | WriteLine ("Server IP: " +
|
---|
50 | ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any")
|
---|
51 | );
|
---|
52 | WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort));
|
---|
53 | WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount));
|
---|
54 | WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode));
|
---|
55 | WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld));
|
---|
56 | WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName));
|
---|
57 | WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty));
|
---|
58 | WriteLine (string.Empty);
|
---|
59 | WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session.");
|
---|
60 | WriteLine (string.Empty);
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void ReceiveThread ()
|
---|
64 | {
|
---|
65 | try {
|
---|
66 | StreamReader reader = new StreamReader (stream);
|
---|
67 | try {
|
---|
68 | while (!IsClosed()) {
|
---|
69 | string line = reader.ReadLine ();
|
---|
70 | if (line != null) {
|
---|
71 | line = line.Trim ();
|
---|
72 |
|
---|
73 | if (!IsAuthenticated ()) {
|
---|
74 | if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) {
|
---|
75 | authenticated = true;
|
---|
76 | WriteLine ("Logon successful.");
|
---|
77 | WriteLine (string.Empty);
|
---|
78 | WriteLine (string.Empty);
|
---|
79 | WriteLine (string.Empty);
|
---|
80 | LoginMessage ();
|
---|
81 | } else {
|
---|
82 | WriteLine ("Password incorrect, please enter password:");
|
---|
83 | }
|
---|
84 | } else {
|
---|
85 | if (line.ToLower ().Equals ("exit")) {
|
---|
86 | Log.Out ("Telnet connection closed by client: " + endpoint);
|
---|
87 | Close ();
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | Log.Out ("Telnet executed \"" + line + "\" from: " + endpoint);
|
---|
91 | ConsoleOutputSeparator.QueueNetCommand (line, this);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | } catch (ObjectDisposedException) {
|
---|
96 | } catch (IOException) {
|
---|
97 | } catch (ThreadInterruptedException) {
|
---|
98 | }
|
---|
99 | ThreadMaster.Remove (Thread.CurrentThread.Name);
|
---|
100 | } catch (Exception ex) {
|
---|
101 | Log.Out ("Error in TelnetClientReceive: " + ex);
|
---|
102 | }
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void SendThread ()
|
---|
107 | {
|
---|
108 | try {
|
---|
109 | while (!IsClosed() && stream.CanWrite) {
|
---|
110 | try {
|
---|
111 | string line = toClientQueue.Dequeue ();
|
---|
112 | if (!IsClosed () && stream.CanWrite) {
|
---|
113 | byte[] utfData = Encoding.UTF8.GetBytes (line);
|
---|
114 | stream.Write (utfData, 0, utfData.Length);
|
---|
115 | stream.WriteByte (13);
|
---|
116 | stream.WriteByte (10);
|
---|
117 | }
|
---|
118 | } catch (IOException) {
|
---|
119 | } catch (ThreadInterruptedException) {
|
---|
120 | } catch (Exception e) {
|
---|
121 | Log.Out ("Error writing to client: " + e);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | ThreadMaster.Remove (Thread.CurrentThread.Name);
|
---|
126 | } catch (Exception ex) {
|
---|
127 | Log.Out ("Error in TelnetClientSend: " + ex);
|
---|
128 | }
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void WriteLine (string s)
|
---|
133 | {
|
---|
134 | toClientQueue.Enqueue (s);
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void Close ()
|
---|
138 | {
|
---|
139 | if (receiveThread != null) {
|
---|
140 | receiveThread.Interrupt ();
|
---|
141 | }
|
---|
142 | if (sendThread != null) {
|
---|
143 | sendThread.Interrupt ();
|
---|
144 | }
|
---|
145 | if (client != null)
|
---|
146 | client.Close ();
|
---|
147 | client = null;
|
---|
148 | }
|
---|
149 |
|
---|
150 | public bool IsClosed ()
|
---|
151 | {
|
---|
152 | if (client != null && !client.Connected) {
|
---|
153 | Log.Out ("Telnet connection interrupted: " + endpoint);
|
---|
154 | Close ();
|
---|
155 | }
|
---|
156 | return (client == null) || (!client.Connected);
|
---|
157 | }
|
---|
158 |
|
---|
159 | public bool IsAuthenticated ()
|
---|
160 | {
|
---|
161 | return !authEnabled || authenticated;
|
---|
162 | }
|
---|
163 |
|
---|
164 | }
|
---|
165 | }
|
---|