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

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

fixes

File size: 3.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Net;
4using System.Net.Sockets;
5using System.Threading;
6
7namespace AllocsFixes.NetConnections.Servers.Telnet
8{
9 public class Telnet : IServer
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
34 private TcpListener listener = null;
35 private bool authEnabled = false;
36 private List<TelnetConnection> connections = new List<TelnetConnection> ();
37 private Dictionary<int, LoginAttempts> loginAttemptsPerIP = new Dictionary<int, LoginAttempts> ();
38
39 public Telnet ()
40 {
41 try {
42 if (!GamePrefs.GetBool (EnumGamePrefs.TelnetEnabled)) {
43 return;
44 }
45
46 int port = GamePrefs.GetInt (EnumGamePrefs.TelnetPort);
47
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);
53
54 listener.Start ();
55 listener.BeginAcceptTcpClient (new AsyncCallback (AcceptClient), null);
56
57 NetTelnetServer.RegisterServer (this);
58
59 Log.Out ("Started Telnet on " + port);
60 } catch (Exception e) {
61 Log.Out ("Error in Telnet.ctor: " + e);
62 }
63 }
64
65 private void AcceptClient (IAsyncResult asyncResult)
66 {
67 if (listener.Server.IsBound) {
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 {
75 addressHash = endpoint.GetHashCode ();
76 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ());
77 }
78
79 lock (loginAttemptsPerIP) {
80 LoginAttempts la = null;
81 if (loginAttemptsPerIP.ContainsKey (addressHash))
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);
89 lock (connections) {
90 connections.Add (con);
91 }
92 } else {
93 client.Close ();
94 Log.Out ("Telnet connection not accepted for too many login attempts: " + endpoint);
95 }
96 }
97 listener.BeginAcceptTcpClient (new AsyncCallback (AcceptClient), null);
98 }
99 }
100
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
116 public void Disconnect ()
117 {
118 try {
119 if (listener != null) {
120 listener.Stop ();
121 listener = null;
122 }
123 List<TelnetConnection> cur = new List<TelnetConnection> (connections);
124 foreach (TelnetConnection c in cur) {
125 c.Close ();
126 }
127 } catch (Exception e) {
128 Log.Out ("Error in Telnet.Disconnect: " + e);
129 }
130 }
131
132 public void SendLine (string line)
133 {
134 if (line == null) {
135 return;
136 }
137 lock (connections) {
138 foreach (TelnetConnection c in connections) {
139 c.SendLine (line);
140 }
141 }
142 }
143
144 public void SendLog (string text, string trace, UnityEngine.LogType type)
145 {
146 throw new System.NotImplementedException ();
147 }
148
149 }
150}
151
Note: See TracBrowser for help on using the repository browser.