source: binary-improvements/7dtd-server-fixes/src/AllocsNetTelnetServer.cs@ 73

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

First version of binary fixer

File size: 5.3 KB
RevLine 
[73]1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net;
5using System.Net.Sockets;
6using System.Threading;
7
8public class AllocsNetTelnetServer
9{
10 private static console;
11 private static Thread telnetThread;
12 private static Dictionary<EndPoint, bool> endpointsAuthed;
13 private static TcpListener listener;
14 private static NetworkStream stream;
15 private static bool closed;
16 private static string lineRead = string.Empty;
17
18 public static void init (int port)
19 {
20 listener = new TcpListener (IPAddress.Any, port);
21 telnetThread = ThreadMaster.Create ("thread Allocs TelnetListenThread", new ThreadStart (telnetListenThread));
22 telnetThread.Start ();
23 endpointsAuthed = new Dictionary<EndPoint, bool> ();
24 Console.Out.WriteLine ("Started Allocs NetTelnetServer thread on " + port);
25// Log.Out ("Started Allocs NetTelnetServer thread on " + port);
26 }
27
28 private static void telnetListenThread ()
29 {
30 Console.Out.WriteLine ("Started thread_Allocs_TelnetListenThread()");
31 //Log.Out ("Started thread_Allocs_TelnetListenThread()");
32 listener.Start ();
33 while (!closed) {
34 Thread.Sleep (10);
35 TcpClient tcpClient = listener.AcceptTcpClient ();
36 if (tcpClient != null) {
37 try {
38 Console.Out.WriteLine ("Someone connected to telnet!");
39 endpointsAuthed.Add (tcpClient.Client.RemoteEndPoint, false);
40 stream = tcpClient.GetStream ();
41 StreamReader streamReader = new StreamReader (stream);
42 WriteToClient ("Please enter password:");
43 while (!closed && !endpointsAuthed[tcpClient.Client.RemoteEndPoint]) {
44 lineRead = streamReader.ReadLine ();
45 string text = string.Empty;
46 lineCorrecter (ref text);
47 text = text.Trim ();
48 if (text.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) {
49 endpointsAuthed [tcpClient.Client.RemoteEndPoint] = true;
50 WriteToClient ("Logon successful.");
51 WriteToClient (string.Empty);
52 WriteToClient (string.Empty);
53 WriteToClient (string.Empty);
54 } else {
55 WriteToClient ("Password incorrect, please enter password:");
56 }
57 }
58 WriteToClient ("*** Connected with 7DTD server.");
59 WriteToClient ("*** Server version: Alpha 8.7 (b29) Compatibility Version: Alpha 8.7");
60 WriteToClient (string.Empty);
61 WriteToClient ("Server IP: " +
62 ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any")
63 );
64 WriteToClient ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort));
65 WriteToClient ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount));
66 WriteToClient ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode));
67 WriteToClient ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld));
68 WriteToClient ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName));
69 WriteToClient ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty));
70 WriteToClient (string.Empty);
71 WriteToClient ("Press 'help' to get a list of all commands. Press 'exit' to end session.");
72 WriteToClient (string.Empty);
73 while (!closed) {
74 lineRead = streamReader.ReadLine ();
75 string text2 = string.Empty;
76 lineCorrecter (ref text2);
77 text2 = text2.Trim ();
78 if (text2.Equals ("exit")) {
79 endpointsAuthed [tcpClient.Client.RemoteEndPoint] = false;
80 break;
81 }
82
83 if (text2.Length != 0) {
84 Console.WriteLine ("Telnet executed \"" + text2 + "\" by " + tcpClient.Client.RemoteEndPoint);
85// console.᝚ᙂ (text2);
86 lineRead = string.Empty;
87 }
88 Thread.Sleep (50);
89 }
90 if (stream != null) {
91 endpointsAuthed [tcpClient.Client.RemoteEndPoint] = false;
92 stream.Close ();
93 stream = null;
94 }
95 } catch (Exception ex) {
96 Console.Out.WriteLine ("Error in Allocs telnetListenThread: " + ex.Message);
97 //Log.Out ("Error in Allocs telnetListenThread: " + ex.Message);
98 Console.Out.WriteLine ("Stack Trace: " + ex.StackTrace);
99 //Log.Out ("Stack Trace: " + ex.StackTrace);
100 }
101 if (stream != null) {
102 endpointsAuthed [tcpClient.Client.RemoteEndPoint] = false;
103 stream.Close ();
104 stream = null;
105 }
106 }
107 }
108 Console.Out.WriteLine ("Exited thread_Allocs_TelnetListenThread()");
109 //Log.Out ("Exited thread_Allocs_TelnetListenThread()");
110 ThreadMaster.Remove (Thread.CurrentThread.Name);
111 }
112
113 private static void lineCorrecter (ref string line)
114 {
115 if (lineRead == null) {
116 return;
117 }
118 for (int i = 0; i < lineRead.Length; i++) {
119 if ((lineRead [i] == '\b' || lineRead [i] == '~') && line.Length > 0) {
120 line = line.Substring (0, line.Length);
121 }
122 if (lineRead [i] >= ' ' && lineRead [i] != '\'' && lineRead [i] <= '~') {
123 line += lineRead [i];
124 }
125 }
126 }
127
128 public static void Disconnect ()
129 {
130 closed = true;
131 if (listener != null) {
132 listener.Stop ();
133 }
134 if (stream != null) {
135 stream.Close ();
136 }
137 Thread.Sleep (100);
138 }
139
140 public static void SetConsole ( console)
141 {
142 AllocsNetTelnetServer.console = console;
143 }
144
145 public static void WriteToClient (string line)
146 {
147 if (line == null) {
148 return;
149 }
150 if (stream != null) {
151 for (int i = 0; i < line.Length; i++) {
152 stream.WriteByte ((byte)line [i]);
153 }
154 stream.WriteByte (13);
155 stream.WriteByte (10);
156 }
157 }
158}
Note: See TracBrowser for help on using the repository browser.