1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Net;
|
---|
4 | using System.Net.Sockets;
|
---|
5 | using System.Threading;
|
---|
6 |
|
---|
7 | namespace AllocsFixes.NetConnections.Servers.Telnet
|
---|
8 | {
|
---|
9 | public class Telnet : IServer
|
---|
10 | {
|
---|
11 | private TcpListener listener = null;
|
---|
12 | private bool authEnabled = false;
|
---|
13 | private List<TelnetConnection> connections = new List<TelnetConnection> ();
|
---|
14 |
|
---|
15 | public Telnet ()
|
---|
16 | {
|
---|
17 | try {
|
---|
18 | if (!GamePrefs.GetBool (EnumGamePrefs.TelnetEnabled)) {
|
---|
19 | return;
|
---|
20 | }
|
---|
21 |
|
---|
22 | int port = GamePrefs.GetInt (EnumGamePrefs.TelnetPort);
|
---|
23 |
|
---|
24 | authEnabled = GamePrefs.GetString (EnumGamePrefs.TelnetPassword).Length != 0;
|
---|
25 | if (authEnabled)
|
---|
26 | listener = new TcpListener (IPAddress.Any, port);
|
---|
27 | else
|
---|
28 | listener = new TcpListener (IPAddress.Loopback, port);
|
---|
29 |
|
---|
30 | listener.Start ();
|
---|
31 | listener.BeginAcceptTcpClient (new AsyncCallback (AcceptClient), null);
|
---|
32 |
|
---|
33 | NetTelnetServer.RegisterServer (this);
|
---|
34 |
|
---|
35 | Log.Out ("Started Telnet on " + port);
|
---|
36 | } catch (Exception e) {
|
---|
37 | Log.Out ("Error in Telnet.ctor: " + e);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void FailedLogins ()
|
---|
42 | {
|
---|
43 | }
|
---|
44 |
|
---|
45 | private void AcceptClient (IAsyncResult asyncResult)
|
---|
46 | {
|
---|
47 | if (listener.Server.IsBound) {
|
---|
48 | TelnetConnection c = new TelnetConnection (this, listener.EndAcceptTcpClient (asyncResult), authEnabled);
|
---|
49 | connections.Add (c);
|
---|
50 | listener.BeginAcceptTcpClient (new AsyncCallback (AcceptClient), null);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void Disconnect ()
|
---|
55 | {
|
---|
56 | try {
|
---|
57 | if (listener != null) {
|
---|
58 | listener.Stop ();
|
---|
59 | listener = null;
|
---|
60 | }
|
---|
61 | foreach (TelnetConnection c in connections) {
|
---|
62 | c.Close ();
|
---|
63 | }
|
---|
64 | } catch (Exception e) {
|
---|
65 | Log.Out ("Error in Telnet.Disconnect: " + e);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void RemoveClosedConnections ()
|
---|
70 | {
|
---|
71 | try {
|
---|
72 | List<TelnetConnection> toRemove = new List<TelnetConnection> ();
|
---|
73 | foreach (TelnetConnection c in connections) {
|
---|
74 | if (c.IsClosed ())
|
---|
75 | toRemove.Add (c);
|
---|
76 | }
|
---|
77 | foreach (TelnetConnection c in toRemove) {
|
---|
78 | connections.Remove (c);
|
---|
79 | }
|
---|
80 | } catch (Exception e) {
|
---|
81 | Log.Out ("Error in Telnet.RemoveClosedConnections: " + e);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void WriteToClient (string line)
|
---|
86 | {
|
---|
87 | if (line == null) {
|
---|
88 | return;
|
---|
89 | }
|
---|
90 | RemoveClosedConnections ();
|
---|
91 | foreach (TelnetConnection c in connections) {
|
---|
92 | if (c.IsAuthenticated ())
|
---|
93 | c.WriteLine (line);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void WriteToClient_Single (string line, IConnection client)
|
---|
98 | {
|
---|
99 | if (line == null) {
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | RemoveClosedConnections ();
|
---|
103 | foreach (TelnetConnection con in connections) {
|
---|
104 | if (con == client) {
|
---|
105 | if (con.IsAuthenticated ())
|
---|
106 | con.WriteLine (line);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|