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