| 1 | using System;
|
|---|
| 2 | using System.Net;
|
|---|
| 3 | using System.Net.Sockets;
|
|---|
| 4 |
|
|---|
| 5 | namespace AllocsFixes.NetConnections.Servers.Telnet
|
|---|
| 6 | {
|
|---|
| 7 | public class TelnetConnection : IConnection
|
|---|
| 8 | {
|
|---|
| 9 | private bool authenticated = false;
|
|---|
| 10 | private bool authEnabled;
|
|---|
| 11 | private TcpClient client;
|
|---|
| 12 | private NetworkStream stream;
|
|---|
| 13 | private string lineBuffer = string.Empty;
|
|---|
| 14 | private EndPoint endpoint;
|
|---|
| 15 |
|
|---|
| 16 | public TelnetConnection (TcpClient client, bool authEnabled)
|
|---|
| 17 | {
|
|---|
| 18 | this.authEnabled = authEnabled;
|
|---|
| 19 | this.client = client;
|
|---|
| 20 | this.stream = client.GetStream ();
|
|---|
| 21 | this.endpoint = client.Client.RemoteEndPoint;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | public EndPoint GetEndPoint ()
|
|---|
| 25 | {
|
|---|
| 26 | return endpoint;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | private void WriteByte (byte v)
|
|---|
| 30 | {
|
|---|
| 31 | if (!IsClosed () && stream.CanWrite) {
|
|---|
| 32 | stream.WriteByte (v);
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | public void WriteLine (string s)
|
|---|
| 37 | {
|
|---|
| 38 | try {
|
|---|
| 39 | if (!IsClosed () && stream.CanWrite) {
|
|---|
| 40 | for (int i = 0; i < s.Length; i++) {
|
|---|
| 41 | WriteByte ((byte)s [i]);
|
|---|
| 42 | }
|
|---|
| 43 | WriteByte (13);
|
|---|
| 44 | WriteByte (10);
|
|---|
| 45 | }
|
|---|
| 46 | } catch (Exception e) {
|
|---|
| 47 | Log.Out ("Error writing to client: " + e);
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | public void Close ()
|
|---|
| 52 | {
|
|---|
| 53 | if (client != null)
|
|---|
| 54 | client.Close ();
|
|---|
| 55 | client = null;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | public bool IsClosed ()
|
|---|
| 59 | {
|
|---|
| 60 | if (client != null && !client.Connected) {
|
|---|
| 61 | Log.Out ("Telnet connection interrupted: " + endpoint);
|
|---|
| 62 | }
|
|---|
| 63 | return (client == null) || (!client.Connected);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | public bool IsAuthenticated ()
|
|---|
| 67 | {
|
|---|
| 68 | return !authEnabled || authenticated;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | public void SetAuthenticated ()
|
|---|
| 72 | {
|
|---|
| 73 | authenticated = true;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | public bool Read ()
|
|---|
| 77 | {
|
|---|
| 78 | while (!IsClosed() && stream.CanRead && stream.DataAvailable) {
|
|---|
| 79 | int b = stream.ReadByte ();
|
|---|
| 80 | if (b == '\r' || b == '\n') {
|
|---|
| 81 | if (lineBuffer.Length > 0)
|
|---|
| 82 | return true;
|
|---|
| 83 | } else {
|
|---|
| 84 | lineBuffer += (char)b;
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | return false;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | public string GetLine ()
|
|---|
| 91 | {
|
|---|
| 92 | string res = lineBuffer;
|
|---|
| 93 | lineBuffer = string.Empty;
|
|---|
| 94 | return res;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|