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

Last change on this file since 204 was 190, checked in by alloc, 10 years ago

fixes

File size: 3.7 KB
RevLine 
[73]1using System;
2using System.Collections.Generic;
3using System.Net;
4using System.Net.Sockets;
5using System.Threading;
6
[132]7namespace AllocsFixes.NetConnections.Servers.Telnet
[73]8{
[132]9 public class Telnet : IServer
[130]10 {
[187]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
[182]34 private TcpListener listener = null;
35 private bool authEnabled = false;
36 private List<TelnetConnection> connections = new List<TelnetConnection> ();
[187]37 private Dictionary<int, LoginAttempts> loginAttemptsPerIP = new Dictionary<int, LoginAttempts> ();
[74]38
[182]39 public Telnet ()
[130]40 {
41 try {
[182]42 if (!GamePrefs.GetBool (EnumGamePrefs.TelnetEnabled)) {
43 return;
44 }
45
46 int port = GamePrefs.GetInt (EnumGamePrefs.TelnetPort);
47
[130]48 authEnabled = GamePrefs.GetString (EnumGamePrefs.TelnetPassword).Length != 0;
49 if (authEnabled)
50 listener = new TcpListener (IPAddress.Any, port);
51 else
52 listener = new TcpListener (IPAddress.Loopback, port);
[182]53
[183]54 listener.Start ();
55 listener.BeginAcceptTcpClient (new AsyncCallback (AcceptClient), null);
56
[182]57 NetTelnetServer.RegisterServer (this);
58
59 Log.Out ("Started Telnet on " + port);
[130]60 } catch (Exception e) {
[132]61 Log.Out ("Error in Telnet.ctor: " + e);
[130]62 }
[103]63 }
[73]64
[183]65 private void AcceptClient (IAsyncResult asyncResult)
[130]66 {
[183]67 if (listener.Server.IsBound) {
[187]68 TcpClient client = listener.EndAcceptTcpClient (asyncResult);
69
70 EndPoint endpoint = client.Client.RemoteEndPoint;
71 int addressHash = -1;
72 if (endpoint is IPEndPoint) {
73 addressHash = ((IPEndPoint)endpoint).Address.GetHashCode ();
74 } else {
[190]75 addressHash = endpoint.GetHashCode ();
[187]76 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ());
77 }
78
79 lock (loginAttemptsPerIP) {
80 LoginAttempts la = null;
[190]81 if (loginAttemptsPerIP.ContainsKey (addressHash))
[187]82 la = loginAttemptsPerIP [addressHash];
83 if (la == null) {
84 la = new LoginAttempts ();
85 loginAttemptsPerIP [addressHash] = la;
86 }
87 if (!la.IsBanned ()) {
88 TelnetConnection con = new TelnetConnection (this, client, authEnabled);
[190]89 lock (connections) {
90 connections.Add (con);
91 }
[187]92 } else {
93 client.Close ();
94 Log.Out ("Telnet connection not accepted for too many login attempts: " + endpoint);
95 }
96 }
[183]97 listener.BeginAcceptTcpClient (new AsyncCallback (AcceptClient), null);
[73]98 }
99 }
100
[190]101 public bool RegisterFailedLogin (TelnetConnection con)
102 {
103 lock (loginAttemptsPerIP) {
104 LoginAttempts la = loginAttemptsPerIP [con.EndPointHash];
105 return la.LogAttempt ();
106 }
107 }
108
109 public void ConnectionClosed (TelnetConnection con)
110 {
111 lock (connections) {
112 connections.Remove (con);
113 }
114 }
115
[132]116 public void Disconnect ()
[130]117 {
118 try {
119 if (listener != null) {
120 listener.Stop ();
[182]121 listener = null;
[130]122 }
[190]123 List<TelnetConnection> cur = new List<TelnetConnection> (connections);
124 foreach (TelnetConnection c in cur) {
[130]125 c.Close ();
126 }
127 } catch (Exception e) {
[132]128 Log.Out ("Error in Telnet.Disconnect: " + e);
[103]129 }
[73]130 }
131
[190]132 public void SendLine (string line)
[130]133 {
134 if (line == null) {
135 return;
136 }
[190]137 lock (connections) {
138 foreach (TelnetConnection c in connections) {
139 c.SendLine (line);
140 }
[130]141 }
[73]142 }
[107]143
[190]144 public void SendLog (string text, string trace, UnityEngine.LogType type)
[130]145 {
[190]146 throw new System.NotImplementedException ();
[107]147 }
[132]148
[107]149 }
[73]150}
[132]151
Note: See TracBrowser for help on using the repository browser.