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