Changeset 74 for binary-improvements/7dtd-server-fixes
- Timestamp:
- Jul 15, 2014, 9:10:56 PM (10 years ago)
- Location:
- binary-improvements/7dtd-server-fixes
- Files:
-
- 4 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/7dtd-server-fixes.csproj
r73 r74 32 32 <Reference Include="System" /> 33 33 <Reference Include="Assembly-CSharp"> 34 <HintPath> Assembly-CSharp.dll</HintPath>34 <HintPath>..\..\..\..\..\sdtd\engine\7DaysToDie_Data\Managed\Assembly-CSharp.dll</HintPath> 35 35 </Reference> 36 36 </ItemGroup> -
binary-improvements/7dtd-server-fixes/7dtd-server-fixes.userprefs
r73 r74 1 1 <Properties> 2 2 <MonoDevelop.Ide.Workspace ActiveConfiguration="Release" /> 3 <MonoDevelop.Ide.Workbench ActiveDocument="src/A ssemblyInfo.cs">3 <MonoDevelop.Ide.Workbench ActiveDocument="src/AllocsNetTelnetServer.cs"> 4 4 <Files> 5 5 <File FileName="src/AssemblyInfo.cs" Line="1" Column="1" /> 6 <File FileName="src/AllocsNetTelnetServer.cs" Line="37" Column="5" /> 6 7 </Files> 7 8 </MonoDevelop.Ide.Workbench> -
binary-improvements/7dtd-server-fixes/src/AllocsNetTelnetServer.cs
r73 r74 4 4 using System.Net; 5 5 using System.Net.Sockets; 6 using System.Reflection; 6 7 using System.Threading; 7 8 8 9 public class AllocsNetTelnetServer 9 10 { 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; 11 private class Connection 12 { 13 private bool authenticated = false; 14 private TcpClient client; 15 private NetworkStream stream; 16 private string lineBuffer = string.Empty; 17 private EndPoint endpoint; 18 19 public Connection (TcpClient client) 20 { 21 this.client = client; 22 this.stream = client.GetStream (); 23 this.endpoint = client.Client.RemoteEndPoint; 24 } 25 26 public EndPoint GetEndPoint () 27 { 28 return endpoint; 29 } 30 31 private void WriteByte (byte v) 32 { 33 if (!IsClosed () && stream.CanWrite) { 34 stream.WriteByte (v); 35 } 36 } 37 38 public void WriteLine (string s) 39 { 40 if (!IsClosed () && stream.CanWrite) { 41 for (int i = 0; i < s.Length; i++) { 42 WriteByte ((byte)s [i]); 43 } 44 WriteByte (10); 45 } 46 } 47 48 public void Close () 49 { 50 if (client != null) 51 client.Close (); 52 client = null; 53 } 54 55 public bool IsClosed () 56 { 57 if (client != null && !client.Connected) { 58 Log.Out ("Telnet connection interrupted: " + endpoint); 59 } 60 return (client == null) || (!client.Connected); 61 } 62 63 public bool IsAuthenticated () 64 { 65 return !authEnabled || authenticated; 66 } 67 68 public void SetAuthenticated () 69 { 70 authenticated = true; 71 } 72 73 public bool Read () 74 { 75 while (!IsClosed() && stream.CanRead && stream.DataAvailable) { 76 int b = stream.ReadByte (); 77 if (b == '\r' || b == '\n') { 78 if (lineBuffer.Length > 0) 79 return true; 80 } else { 81 lineBuffer += (char)b; 82 } 83 } 84 return false; 85 } 86 87 public string GetLine () 88 { 89 string res = lineBuffer; 90 lineBuffer = string.Empty; 91 return res; 92 } 93 } 94 95 private static cl004c console = null; 96 private static Thread telnetThread = null; 97 private static TcpListener listener = null; 98 private static bool closed = false; 99 private static bool authEnabled = false; 100 private static List<Connection> conns = new List<Connection> (); 17 101 18 102 public static void init (int port) 19 103 { 20 listener = new TcpListener (IPAddress.Any, port); 104 Log.Out ("[7dtd-server-fixes by Alloc] Version: " + Assembly.GetExecutingAssembly().GetName().Version); 105 authEnabled = GamePrefs.GetString (EnumGamePrefs.TelnetPassword).Length != 0; 106 if (authEnabled) 107 listener = new TcpListener (IPAddress.Any, port); 108 else 109 listener = new TcpListener (IPAddress.Loopback, port); 21 110 telnetThread = ThreadMaster.Create ("thread Allocs TelnetListenThread", new ThreadStart (telnetListenThread)); 22 111 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); 112 Log.Out ("Started Allocs NetTelnetServer thread on " + port); 26 113 } 27 114 28 115 private static void telnetListenThread () 29 116 { 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); 117 try { 118 Log.Out ("Started thread_Allocs_TelnetListenThread()"); 119 listener.Start (); 120 while (!closed) { 121 Thread.Sleep (10); 122 if (listener.Pending ()) { 123 Connection c = new Connection (listener.AcceptTcpClient ()); 124 conns.Add (c); 125 Log.Out ("Telnet connection from: " + c.GetEndPoint ()); 126 if (authEnabled) { 127 c.WriteLine ("Please enter password:"); 128 } else { 129 LoginMessage (c); 130 } 131 } 132 133 foreach (Connection c in conns) { 134 if (c.IsClosed ()) { 135 c.Close (); 136 conns.Remove (c); 137 break; 138 } 139 if (c.Read ()) { 140 string line = lineCorrecter (c.GetLine ()); 141 if (!c.IsAuthenticated ()) { 142 if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) { 143 c.SetAuthenticated (); 144 c.WriteLine ("Logon successful."); 145 c.WriteLine (string.Empty); 146 c.WriteLine (string.Empty); 147 c.WriteLine (string.Empty); 148 LoginMessage (c); 149 } else { 150 c.WriteLine ("Password incorrect, please enter password:"); 151 } 54 152 } else { 55 WriteToClient ("Password incorrect, please enter password:"); 153 if (line.ToLower ().Equals ("exit")) { 154 Log.Out ("Telnet connection closed by client: " + c.GetEndPoint ()); 155 c.Close (); 156 conns.Remove (c); 157 break; 158 } 159 160 Log.Out ("Telnet executed \"" + line + "\" from: " + c.GetEndPoint ()); 161 console.md000f (line); 56 162 } 57 163 } 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 } 164 } 165 } 166 Log.Out ("Exited thread_Allocs_TelnetListenThread()"); 167 ThreadMaster.Remove (Thread.CurrentThread.Name); 168 } catch (Exception ex) { 169 Log.Out ("Error in Allocs telnetListenThread: " + ex.Message); 170 Log.Out ("Stack Trace: " + ex.StackTrace); 171 } 172 } 173 174 private static void LoginMessage (Connection c) 175 { 176 c.WriteLine ("*** Connected with 7DTD server."); 177 c.WriteLine ("*** Server version: Alpha 8.7 (b29) Compatibility Version: Alpha 8.7"); 178 c.WriteLine (string.Empty); 179 c.WriteLine ("Server IP: " + 180 ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any") 181 ); 182 c.WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort)); 183 c.WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount)); 184 c.WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode)); 185 c.WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld)); 186 c.WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName)); 187 c.WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty)); 188 c.WriteLine (string.Empty); 189 c.WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session."); 190 c.WriteLine (string.Empty); 191 } 192 193 private static string lineCorrecter (string line) 194 { 195 string res = ""; 196 for (int i = 0; i < line.Length; i++) { 197 if (line [i] >= ' ' && line [i] != '\'' && line [i] <= '~') { 198 res += line [i]; 199 } 200 } 201 return res.Trim (); 126 202 } 127 203 … … 132 208 listener.Stop (); 133 209 } 134 if (stream != null) {135 stream.Close ();210 foreach (Connection c in conns) { 211 c.Close (); 136 212 } 137 213 Thread.Sleep (100); 138 214 } 139 215 140 public static void SetConsole ( ឦconsole)216 public static void SetConsole (cl004c console) 141 217 { 142 218 AllocsNetTelnetServer.console = console; 219 } 220 221 private static void RemoveClosedConnections () 222 { 223 foreach (Connection c in conns) { 224 if (c.IsClosed ()) { 225 c.Close (); 226 } 227 } 143 228 } 144 229 … … 148 233 return; 149 234 } 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); 235 RemoveClosedConnections (); 236 foreach (Connection c in conns) { 237 if (c.IsAuthenticated()) 238 c.WriteLine (line); 156 239 } 157 240 }
Note:
See TracChangeset
for help on using the changeset viewer.