Changeset 190 for binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs
- Timestamp:
- Sep 13, 2014, 12:55:47 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs
r189 r190 12 12 { 13 13 private readonly BlockingQueue<string> toClientQueue = new BlockingQueue<string> (); 14 private readonly Telnet owner;14 private readonly Telnet telnet; 15 15 private readonly Thread receiveThread = null; 16 16 private readonly Thread sendThread = null; 17 17 private bool authenticated = false; 18 18 private readonly bool authEnabled; 19 private TcpClient client; 20 private readonly NetworkStream stream; 19 private readonly TcpClient client; 21 20 private readonly EndPoint endpoint; 22 21 private readonly int endpointAddressHash; 22 private bool closed = false; 23 24 public bool IsClosed { get { return closed; } } 25 26 public bool IsAuthenticated { get { return !authEnabled || authenticated; } } 27 28 public int EndPointHash { get { return endpointAddressHash; } } 23 29 24 30 public TelnetConnection (Telnet owner, TcpClient client, bool authEnabled) 25 31 { 26 this. owner= owner;32 this.telnet = owner; 27 33 this.authEnabled = authEnabled; 28 34 this.client = client; 29 this.stream = client.GetStream ();30 35 this.endpoint = client.Client.RemoteEndPoint; 36 37 if (endpoint is IPEndPoint) { 38 endpointAddressHash = ((IPEndPoint)endpoint).Address.GetHashCode (); 39 } else { 40 endpointAddressHash = endpoint.GetHashCode (); 41 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ()); 42 } 31 43 32 44 Log.Out ("Telnet connection from: " + endpoint); 33 45 34 receiveThread = ThreadMaster.Create ("TelnetClientReceive ", new ThreadStart (ReceiveThread));46 receiveThread = ThreadMaster.Create ("TelnetClientReceive_" + endpoint.ToString (), new ThreadStart (ReceiveThread)); 35 47 receiveThread.Start (); 36 sendThread = ThreadMaster.Create ("TelnetClientSend" , new ThreadStart (SendThread));48 sendThread = ThreadMaster.Create ("TelnetClientSend" + endpoint.ToString (), new ThreadStart (SendThread)); 37 49 sendThread.Start (); 38 50 39 if (endpoint is IPEndPoint) {40 endpointAddressHash = ((IPEndPoint)endpoint).Address.GetHashCode ();41 //Log.Out ("Hash: " + endpointAddressHash);42 } else {43 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ());44 }45 46 51 if (authEnabled) { 47 WriteLine ("Please enter password:");52 toClientQueue.Enqueue ("Please enter password:"); 48 53 } else { 49 54 LoginMessage (); … … 53 58 private void LoginMessage () 54 59 { 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 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: " + 60 65 ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any") 61 66 ); 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);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); 71 76 } 72 77 … … 74 79 { 75 80 try { 76 StreamReader reader = new StreamReader (stream); 77 try { 78 while (!IsClosed()) { 79 string line = reader.ReadLine (); 80 if (line != null && line.Length > 0) { 81 line = line.Trim (); 82 if (line.Length > 0) { 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 { 92 if (owner.RegisterFailedLogin (endpointAddressHash)) { 93 WriteLine ("Password incorrect, please enter password:"); 94 } else { 95 WriteLine ("Too many failed login attempts!"); 96 Thread.Sleep (100); 97 Close (); 98 Log.Out ("Telnet connection closed for too many login attempts: " + endpoint); 99 break; 100 } 101 } 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:"); 102 96 } 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); 97 toClientQueue.Enqueue ("Too many failed login attempts!"); 98 Thread.Sleep (100); 99 Close (true); 100 break; 110 101 } 111 102 } 103 } else { 104 if (line.ToLower ().Equals ("exit")) { 105 break; 106 } 107 Log.Out ("Telnet executed \"" + line + "\" from: " + endpoint); 108 ConsoleOutputSeparator.QueueNetCommand (line, this); 112 109 } 113 110 } 114 } catch (ObjectDisposedException) {115 } catch (IOException) {116 } catch (ThreadInterruptedException) {117 111 } 118 ThreadMaster.Remove (Thread.CurrentThread.Name); 119 } catch (Exception ex) { 120 Log.Out ("Error in TelnetClientReceive: " + ex); 112 } catch (Exception) { 121 113 } 122 114 115 if (!closed) 116 Close (); 117 ThreadMaster.Remove (Thread.CurrentThread.Name); 123 118 } 124 119 … … 126 121 { 127 122 try { 128 while (!IsClosed() && stream.CanWrite) { 129 try { 130 string line = toClientQueue.Dequeue (); 131 if (!IsClosed () && stream.CanWrite) { 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 { 132 129 byte[] utfData = Encoding.UTF8.GetBytes (line); 133 stream.Write (utfData, 0, utfData.Length);134 stream.WriteByte (13);135 stream.WriteByte (10);130 client.GetStream ().Write (utfData, 0, utfData.Length); 131 client.GetStream ().WriteByte (13); 132 client.GetStream ().WriteByte (10); 136 133 } 137 } catch (IOException) {138 } catch (ThreadInterruptedException) {139 } catch (Exception e) {140 Log.Out ("Error writing to client: " + e);141 134 } 142 135 } 143 144 ThreadMaster.Remove (Thread.CurrentThread.Name); 145 } catch (Exception ex) { 146 Log.Out ("Error in TelnetClientSend: " + ex); 136 } catch (Exception) { 147 137 } 148 138 139 if (!closed) 140 Close (); 141 ThreadMaster.Remove (Thread.CurrentThread.Name); 149 142 } 150 143 151 public void WriteLine (string s)144 public void Close (bool kickedForLogins = false) 152 145 { 153 toClientQueue.Enqueue (s); 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 } 154 158 } 155 159 156 public void Close ()160 public void SendLine (string line) 157 161 { 158 if (receiveThread != null) { 159 receiveThread.Interrupt (); 160 } 161 if (sendThread != null) { 162 sendThread.Interrupt (); 163 } 164 if (client != null) 165 client.Close (); 166 client = null; 162 if (!closed && IsAuthenticated) 163 toClientQueue.Enqueue (line); 164 else 165 toClientQueue.Enqueue (null); 167 166 } 168 167 169 public bool IsClosed ()168 public void SendLog (string text, string trace, UnityEngine.LogType type) 170 169 { 171 if (client != null && !client.Connected) { 172 Log.Out ("Telnet connection interrupted: " + endpoint); 173 Close (); 174 } 175 return (client == null) || (!client.Connected); 170 throw new System.NotImplementedException (); 176 171 } 177 172 178 public bool IsAuthenticated()173 public string GetDescription () 179 174 { 180 return !authEnabled || authenticated;175 return "Telnet from " + endpoint; 181 176 } 182 183 177 } 184 178 }
Note:
See TracChangeset
for help on using the changeset viewer.