- Timestamp:
- Sep 10, 2014, 8:09:28 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/Telnet.cs
r184 r187 9 9 public class Telnet : IServer 10 10 { 11 private const int MAX_LOGIN_ATTEMPTS = 10; 12 private const int BLOCK_TIME_SECONDS = 10; 13 14 private class LoginAttempts 15 { 16 private int count = 0; 17 private DateTime lastAttempt = new DateTime (0); 18 19 public bool LogAttempt () 20 { 21 lastAttempt = DateTime.Now; 22 count++; 23 return count < MAX_LOGIN_ATTEMPTS; 24 } 25 26 public bool IsBanned () 27 { 28 if ((DateTime.Now - lastAttempt).TotalSeconds > BLOCK_TIME_SECONDS) 29 count = 0; 30 return count >= MAX_LOGIN_ATTEMPTS; 31 } 32 } 33 11 34 private TcpListener listener = null; 12 35 private bool authEnabled = false; 13 36 private List<TelnetConnection> connections = new List<TelnetConnection> (); 37 private Dictionary<int, LoginAttempts> loginAttemptsPerIP = new Dictionary<int, LoginAttempts> (); 14 38 15 39 public Telnet () … … 39 63 } 40 64 41 public void FailedLogins ()65 public bool RegisterFailedLogin (int addressHash) 42 66 { 67 lock (loginAttemptsPerIP) { 68 LoginAttempts la = loginAttemptsPerIP [addressHash]; 69 return la.LogAttempt (); 70 } 43 71 } 44 72 … … 46 74 { 47 75 if (listener.Server.IsBound) { 48 TelnetConnection c = new TelnetConnection (this, listener.EndAcceptTcpClient (asyncResult), authEnabled); 49 connections.Add (c); 76 TcpClient client = listener.EndAcceptTcpClient (asyncResult); 77 78 EndPoint endpoint = client.Client.RemoteEndPoint; 79 int addressHash = -1; 80 if (endpoint is IPEndPoint) { 81 addressHash = ((IPEndPoint)endpoint).Address.GetHashCode (); 82 //Log.Out ("Hash: " + endpointAddressHash); 83 } else { 84 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ()); 85 } 86 87 lock (loginAttemptsPerIP) { 88 LoginAttempts la = null; 89 if (loginAttemptsPerIP.ContainsKey(addressHash)) 90 la = loginAttemptsPerIP [addressHash]; 91 if (la == null) { 92 la = new LoginAttempts (); 93 loginAttemptsPerIP [addressHash] = la; 94 } 95 if (!la.IsBanned ()) { 96 TelnetConnection con = new TelnetConnection (this, client, authEnabled); 97 connections.Add (con); 98 } else { 99 client.Close (); 100 Log.Out ("Telnet connection not accepted for too many login attempts: " + endpoint); 101 } 102 } 50 103 listener.BeginAcceptTcpClient (new AsyncCallback (AcceptClient), null); 51 104 }
Note:
See TracChangeset
for help on using the changeset viewer.