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> conns = 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 | conns.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 conns) {
|
---|
50 | if (c.IsClosed ()) {
|
---|
51 | c.Close ();
|
---|
52 | conns.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 | conns.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 (string.Empty);
|
---|
94 | c.WriteLine ("Server IP: " +
|
---|
95 | ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any")
|
---|
96 | );
|
---|
97 | c.WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort));
|
---|
98 | c.WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount));
|
---|
99 | c.WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode));
|
---|
100 | c.WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld));
|
---|
101 | c.WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName));
|
---|
102 | c.WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty));
|
---|
103 | c.WriteLine (string.Empty);
|
---|
104 | c.WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session.");
|
---|
105 | c.WriteLine (string.Empty);
|
---|
106 | }
|
---|
107 |
|
---|
108 | private static string lineCorrecter (string line)
|
---|
109 | {
|
---|
110 | string res = "";
|
---|
111 | for (int i = 0; i < line.Length; i++) {
|
---|
112 | if (line [i] >= ' ' && line [i] != '\'' && line [i] <= '~') {
|
---|
113 | res += line [i];
|
---|
114 | }
|
---|
115 | }
|
---|
116 | return res.Trim ();
|
---|
117 | }
|
---|
118 |
|
---|
119 | public static void Disconnect ()
|
---|
120 | {
|
---|
121 | closed = true;
|
---|
122 | if (listener != null) {
|
---|
123 | listener.Stop ();
|
---|
124 | }
|
---|
125 | foreach (AllocsTelnetConnection c in conns) {
|
---|
126 | c.Close ();
|
---|
127 | }
|
---|
128 | Thread.Sleep (100);
|
---|
129 | }
|
---|
130 |
|
---|
131 | public static void SetConsole (ConsoleSdtd console)
|
---|
132 | {
|
---|
133 | Log.Out("Telnet SetConsole called");
|
---|
134 | AllocsNetTelnetServer.console = console;
|
---|
135 | console.AddCommand(new GetGamePrefs(console));
|
---|
136 | console.AddCommand(new GetTime(console));
|
---|
137 | console.AddCommand(new ListPlayersExtended(console));
|
---|
138 | console.AddCommand(new SayToPlayer(console));
|
---|
139 | }
|
---|
140 |
|
---|
141 | private static void RemoveClosedConnections ()
|
---|
142 | {
|
---|
143 | foreach (AllocsTelnetConnection c in conns) {
|
---|
144 | if (c.IsClosed ()) {
|
---|
145 | c.Close ();
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | public static void WriteToClient (string line)
|
---|
151 | {
|
---|
152 | if (line == null) {
|
---|
153 | return;
|
---|
154 | }
|
---|
155 | RemoveClosedConnections ();
|
---|
156 | foreach (AllocsTelnetConnection c in conns) {
|
---|
157 | if (c.IsAuthenticated ())
|
---|
158 | c.WriteLine (line);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|