diff options
Diffstat (limited to 'ShiftOS.Server')
| -rw-r--r-- | ShiftOS.Server/App.config | 14 | ||||
| -rw-r--r-- | ShiftOS.Server/ChatBackend.cs | 162 | ||||
| -rw-r--r-- | ShiftOS.Server/Core.cs | 140 | ||||
| -rw-r--r-- | ShiftOS.Server/Program.cs | 64 | ||||
| -rw-r--r-- | ShiftOS.Server/RandomUserGenerator.cs | 287 | ||||
| -rw-r--r-- | ShiftOS.Server/SaveManager.cs | 82 | ||||
| -rw-r--r-- | ShiftOS.Server/ShiftOS.Server.csproj | 70 | ||||
| -rw-r--r-- | ShiftOS.Server/ShiftnetBackend.cs | 44 | ||||
| -rw-r--r-- | ShiftOS.Server/ShopBackend.cs | 19 | ||||
| -rw-r--r-- | ShiftOS.Server/packages.config | 29 |
10 files changed, 599 insertions, 312 deletions
diff --git a/ShiftOS.Server/App.config b/ShiftOS.Server/App.config index cadfa91..a87bf6b 100644 --- a/ShiftOS.Server/App.config +++ b/ShiftOS.Server/App.config @@ -1,18 +1,18 @@ -<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <configuration> <startup> - <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/> + <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /> </startup> <runtime> - <loadFromRemoteSources enabled="true"/> + <loadFromRemoteSources enabled="true" /> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> - <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/> - <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0"/> + <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" /> </dependentAssembly> <dependentAssembly> - <assemblyIdentity name="System.Interactive.Async" publicKeyToken="94bc3704cddfc263" culture="neutral"/> - <bindingRedirect oldVersion="0.0.0.0-3.0.1000.0" newVersion="3.0.1000.0"/> + <assemblyIdentity name="System.Interactive.Async" publicKeyToken="94bc3704cddfc263" culture="neutral" /> + <bindingRedirect oldVersion="0.0.0.0-3.0.1000.0" newVersion="3.0.1000.0" /> </dependentAssembly> </assemblyBinding> </runtime> diff --git a/ShiftOS.Server/ChatBackend.cs b/ShiftOS.Server/ChatBackend.cs deleted file mode 100644 index 242ae16..0000000 --- a/ShiftOS.Server/ChatBackend.cs +++ /dev/null @@ -1,162 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ShiftOS.Objects; -using NetSockets; -using Newtonsoft.Json; -using System.IO; -using static ShiftOS.Server.Program; -using Discord; -using Discord.WebSocket; -using Discord.Net.WebSockets; - -namespace ShiftOS.Server -{ - public static class ChatBackend - { - public static async Task StartDiscordBots() - { - Reinitialized?.Invoke(); - if (!File.Exists("chats.json")) - File.WriteAllText("chats.json", "[]"); - foreach (var chat in JsonConvert.DeserializeObject<List<ShiftOS.Objects.Channel>>(File.ReadAllText("chats.json"))) - { - string chatID = chat.ID; - bool chatKilled = false; - if (chat.IsDiscordProxy == true) - { - DiscordSocketConfig builder = new DiscordSocketConfig(); - builder.AudioMode = Discord.Audio.AudioMode.Disabled; - builder.WebSocketProvider = () => Discord.Net.Providers.WS4Net.WS4NetProvider.Instance(); - var client = new DiscordSocketClient(builder); - await client.LoginAsync(TokenType.Bot, chat.DiscordBotToken); - - await client.ConnectAsync(); - await client.SetGameAsync("ShiftOS"); - await client.SetStatusAsync(UserStatus.Online); - //Get the Discord channel for this chat. - var Chan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)) as ISocketMessageChannel; - //Relay the message to Discord. - await Chan.SendMessageAsync("**Hello! Multi-user domain is online.**"); - - client.MessageReceived += async (s) => - { - if (chatKilled == false) - { - if (s.Channel.Id == Convert.ToUInt64(chat.DiscordChannelID)) - { - if (s.Author.Id != client.CurrentUser.Id) - { - server.DispatchAll(new NetObject("chat_msgreceived", new ServerMessage - { - Name = "chat_msgreceived", - GUID = "server", - Contents = JsonConvert.SerializeObject(new ChatMessage(s.Author.Username, "discord_" + s.Channel.Name, (s as SocketUserMessage).Resolve(0), chatID)) - })); - } - } - } - }; - MessageReceived += (g, msg) => - { - if (chatKilled == false) - { - //Determine if the message was sent to this channel. - if (msg.Channel == chat.ID) - { - //Get the Discord channel for this chat. - var dChan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)) as ISocketMessageChannel; - //Relay the message to Discord. - dChan.SendMessageAsync($"**[{msg.Username}@{msg.SystemName}]** `<mud/{msg.Channel}>` {msg.Message}"); - - } - //Relay it back to all MUD clients. - RelayMessage(g, msg); - } - }; - Reinitialized += () => - { - client.DisconnectAsync(); - - chatKilled = true; - }; - } - else - { - MessageReceived += (g, msg) => - { - if (chatKilled == false) - { - //Just relay it. - RelayMessage(g, msg); - } - }; - Reinitialized += () => { chatKilled = true; }; - } - } - } - - internal static void RelayMessage(string guid, ChatMessage msg) - { - server.DispatchAllExcept(new Guid(guid), new NetObject("chat_msgreceived", new ServerMessage - { - Name = "chat_msgreceived", - GUID = "server", - Contents = JsonConvert.SerializeObject(msg) - })); - - } - - public static event Action<string, ChatMessage> MessageReceived; - public static event empty Reinitialized; - - - public delegate void empty(); - - [MudRequest("chat_getallchannels", null)] - public static void GetAllChannels(string guid, object contents) - { - server.DispatchTo(new Guid(guid), new NetObject("chat_all", new ServerMessage - { - Name = "chat_all", - GUID = "Server", - Contents = (File.Exists("chats.json") == true) ? File.ReadAllText("chats.json") : "[]" - })); - } - - [MudRequest("chat_send", typeof(Dictionary<string, string>))] - public static void ReceiveMessage(string guid, object contents) - { - var msg = contents as Dictionary<string, string>; - MessageReceived?.Invoke(guid, new ChatMessage(msg["Username"], msg["SystemName"], msg["Message"], msg["Channel"])); - - } - } - -} diff --git a/ShiftOS.Server/Core.cs b/ShiftOS.Server/Core.cs index 42a9127..a53a5bc 100644 --- a/ShiftOS.Server/Core.cs +++ b/ShiftOS.Server/Core.cs @@ -32,12 +32,44 @@ using NetSockets; using Newtonsoft.Json; using System.IO; using static ShiftOS.Server.Program; +using ShiftOS.Engine namespace ShiftOS.Server { public static class Core { + [MudRequest("mud_forward", typeof(ServerMessage))] + public static void ForwardMessage(string guid, ServerMessage message) + { + if (message.GUID == "all") + { + Server.Program.server.DispatchAll(new NetObject("forward", new ServerMessage + { + Name = "forward", + GUID = "Server", + Contents = JsonConvert.SerializeObject(message) + })); + } + else + { + try + { + Server.Program.server.DispatchTo(new Guid(message.GUID), new NetObject("forward", new ServerMessage + { + Name = "forward", + GUID = "Server", + Contents = JsonConvert.SerializeObject(message) + })); + } + catch + { + + } + } + } + + [MudRequest("getguid_reply", typeof(string))] public static void GuidBounce(string guid, object contents) { @@ -96,10 +128,11 @@ namespace ShiftOS.Server { Name = "run", GUID = "Server", - Contents = $@"{{ - script:""{File.ReadAllText($"scripts/{user}/{script}.lua").Replace("\"", "\\\"")}"", - args:""{sArgs}"" - }}" + Contents = JsonConvert.SerializeObject(new + { + script = File.ReadAllText($"scripts/{user}/{script}.lua"), + args = sArgs + }) })); } else @@ -115,7 +148,7 @@ namespace ShiftOS.Server { Name = "Error", GUID = "Server", - Contents = JsonConvert.SerializeObject(new MudException("Command parse error")) + Contents = JsonConvert.SerializeObject(new MudException("<script_runner> Script not found or script error detected.")) })); } catch @@ -125,5 +158,102 @@ namespace ShiftOS.Server } } + + [MudRequest("diag_log", typeof(string))] + public static void Diagnostic(string guid, string line) + { + List<string> lines = new List<string>(); + if (File.Exists("diagnostics.log")) + lines = new List<string>(File.ReadAllLines("diagnostics.log")); + + lines.Add(line); + File.WriteAllLines("diagnostics.log", lines.ToArray()); + } + + [MudRequest("getusers", typeof(string))] + public static void GetAllUsers(string guid, string contents) + { + List<string> accs = new List<string>(); + if(contents == "dead") + { + foreach(var sve in Directory.GetFiles("deadsaves")) + { + if (sve.EndsWith(".save")) + { + var save = JsonConvert.DeserializeObject<Save>(File.ReadAllText(sve)); + accs.Add($"{ShiftOS.Engine.SaveSytem.CurrentUser.Username}@{save.SystemName}"); + } + + } + } + server.DispatchTo(new Guid(guid), new NetObject("h4xx0r", new ServerMessage + { + Name = "allusers", + GUID = "server", + Contents = JsonConvert.SerializeObject(accs) + })); + } + + [MudRequest("mud_save_allow_dead", typeof(Save))] + public static void SaveDead(string guid, Save sve) + { + if(File.Exists("saves/" + sve.Username + ".save")) + { + WriteEncFile("saves/" + sve.Username + ".save", JsonConvert.SerializeObject(sve)); + } + else if(File.Exists("deadsaves/" + sve.Username + ".save")) + { + File.WriteAllText("deadsaves/" + sve.Username + ".save", JsonConvert.SerializeObject(sve)); + } + } + + [MudRequest("get_user_data", typeof(Dictionary<string, string>))] + public static void GetUserData(string guid, Dictionary<string, string> contents) + { + string usr = contents["user"]; + string sys = contents["sysname"]; + + foreach(var sve in Directory.GetFiles("deadsaves")) + { + if (sve.EndsWith(".save")) + { + var saveFile = JsonConvert.DeserializeObject<Save>(File.ReadAllText(sve)); + if(saveFile.Username == usr && saveFile.SystemName == sys) + { + server.DispatchTo(new Guid(guid), new NetObject("1337", new ServerMessage + { + Name = "user_data", + GUID = "server", + Contents = JsonConvert.SerializeObject(saveFile) + })); + return; + } + } + } + foreach (var sve in Directory.GetFiles("saves")) + { + if (sve.EndsWith(".save")) + { + var saveFile = JsonConvert.DeserializeObject<Save>(ReadEncFile(sve)); + if (saveFile.Username == usr && saveFile.SystemName == sys) + { + server.DispatchTo(new Guid(guid), new NetObject("1337", new ServerMessage + { + Name = "user_data", + GUID = "server", + Contents = JsonConvert.SerializeObject(saveFile) + })); + return; + } + } + } + + server.DispatchTo(new Guid(guid), new NetObject("n07_50_1337", new ServerMessage + { + Name = "user_data_not_found", + GUID = "server" + })); + + } } } diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index 04d7b2e..c880321 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -86,7 +86,25 @@ namespace ShiftOS.Server /// <param name="args">The command-line arguments.</param> public static void Main(string[] args) { - + Thread.Sleep(2000); + AppDomain.CurrentDomain.UnhandledException += (o, a) => + { + System.Diagnostics.Process.Start("ShiftOS.Server.exe"); + Environment.Exit(0); + }; + UserConfig.Get(); + System.Timers.Timer tmr = new System.Timers.Timer(5000); + tmr.Elapsed += (o, a) => + { + if (server.IsOnline) + { + server.DispatchAll(new NetObject("heartbeat", new ServerMessage + { + Name = "heartbeat", + GUID = "server" + })); + } + }; if (!Directory.Exists("saves")) { Directory.CreateDirectory("saves"); @@ -106,11 +124,13 @@ namespace ShiftOS.Server { Console.WriteLine($"Server started on address {server.Address}, port {server.Port}."); ServerStarted?.Invoke(server.Address.ToString()); - }; + tmr.Start(); + }; server.OnStopped += (o, a) => { Console.WriteLine("WARNING! Server stopped."); + tmr.Stop(); }; server.OnError += (o, a) => @@ -121,8 +141,32 @@ namespace ShiftOS.Server server.OnClientAccepted += (o, a) => { Console.WriteLine("Client connected."); - server.DispatchTo(a.Guid, new NetObject("welcome", new ServerMessage { Name = "Welcome", Contents = a.Guid.ToString(), GUID = "Server" })); - }; + try + { + server.DispatchTo(a.Guid, new NetObject("welcome", new ServerMessage { Name = "Welcome", Contents = a.Guid.ToString(), GUID = "Server" })); + } + catch + { + Console.WriteLine("Oh, you don't have time to finish the handshake? Fine. Get off."); + } + }; + + server.OnClientDisconnected += (o, a) => + { + Console.WriteLine("Client disconnected."); + }; + + server.OnClientRejected += (o, a) => + { + Console.WriteLine("FUCK. Something HORRIBLE JUST HAPPENED."); + }; + + AppDomain.CurrentDomain.UnhandledException += (o, a) => + { + if(server.IsOnline == true) + server.Stop(); + System.Diagnostics.Process.Start("ShiftOS.Server.exe"); + }; server.OnReceived += (o, a) => { @@ -164,8 +208,13 @@ namespace ShiftOS.Server Console.WriteLine("Server stopping."); }; + + /* var task = ChatBackend.StartDiscordBots(); task.Wait(); + */ + + RandomUserGenerator.StartThread(); while (server.IsOnline) { @@ -254,8 +303,6 @@ namespace ShiftOS.Server /// <param name="msg">Message.</param> public static void Interpret(ServerMessage msg) { - Dictionary<string, object> args = null; - try { Console.WriteLine($@"[{DateTime.Now}] Message received from {msg.GUID}: {msg.Name}"); @@ -283,8 +330,7 @@ namespace ShiftOS.Server try { object contents = null; - bool throwOnNull = false; - + if (mAttrib.ExpectedType == typeof(int)) { @@ -312,7 +358,6 @@ namespace ShiftOS.Server } else if (mAttrib.ExpectedType == typeof(bool)) { - throwOnNull = true; if (msg.Contents.ToLower() == "true") { contents = true; @@ -329,7 +374,6 @@ namespace ShiftOS.Server } else if (mAttrib.ExpectedType == null) { - throwOnNull = false; } else if(mAttrib.ExpectedType == typeof(string)) { diff --git a/ShiftOS.Server/RandomUserGenerator.cs b/ShiftOS.Server/RandomUserGenerator.cs new file mode 100644 index 0000000..3a62f9c --- /dev/null +++ b/ShiftOS.Server/RandomUserGenerator.cs @@ -0,0 +1,287 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Threading; +using System.IO; +using ShiftOS.Objects; +using static ShiftOS.Objects.ShiftFS.Utils; + +namespace ShiftOS.Server +{ + public static class RandomUserGenerator + { + const string USERPREFIXES = "culled;purged;anon;fatal;unaccounted;netban;killed;old"; + const string SYSNAMES = "unknown;error;dead;system;mud"; + const string PASSCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_"; + + public static void StartThread() + { + var t = new Thread(() => + { + var rnd = new Random(); + while (Program.server.IsOnline) + { + if (!Directory.Exists("deadsaves")) + { + Directory.CreateDirectory("deadsaves"); + } + + var sve = new Save(); + + int passLength = 0; + int securityGrade = rnd.Next(1, 5); + switch (securityGrade) + { + default: + passLength = 4; + break; + case 2: + passLength = 6; + break; + case 3: + passLength = 10; + break; + case 4: + passLength = 15; + break; + } + + string pass = ""; + char lastChar = '\0'; + for (int i = 0; i < passLength; i++) + { + char c = PASSCHARS[rnd.Next(0, PASSCHARS.Length)]; + while(c == lastChar) + { + c = PASSCHARS[rnd.Next(0, PASSCHARS.Length)]; + } + pass += c; + lastChar = c; //this ensures no repeated sequences. + } + + sve.Password = pass; + + int id_length = rnd.Next(3, 10); + string id = ""; + + lastChar = '\0'; + for (int i = 0; i < id_length; i++) + { + char c = PASSCHARS[rnd.Next(0, PASSCHARS.Length)]; + while (c == lastChar) + { + c = PASSCHARS[rnd.Next(0, PASSCHARS.Length)]; + } + id += c; + lastChar = c; //this ensures no repeated sequences. + } + + string[] names = USERPREFIXES.Split(';'); + string name = names[rnd.Next(0, names.Length)]; + sve.Username = $"{id}_{name}"; + + names = SYSNAMES.Split(';'); + name = names[rnd.Next(0, names.Length)]; + + sve.SystemName = name; + + //Codepoint generation. + int startCP = 0; + int maxAmt = 0; + + switch (securityGrade) + { + default: + startCP = 1000; + maxAmt = 12500; + break; + case 2: + startCP = 25000; + maxAmt = 50000; + break; + case 3: + startCP = 75000; + maxAmt = 150000; + break; + case 4: + startCP = 500000; + maxAmt = 1500000; + break; + } + + sve.Codepoints = rnd.Next(startCP, maxAmt); + + //FS treasure generation. + /* + //create a ramdisk dir + var dir = new ShiftOS.Objects.ShiftFS.Directory(); + //name the directory after the user + dir.Name = sve.Username; + dir.permissions = Objects.ShiftFS.Permissions.All; + //json the object and mount + string json = Newtonsoft.Json.JsonConvert.SerializeObject(dir); + //mount it to the MUD + ShiftOS.Objects.ShiftFS.Utils.Mount(json); + //get the mount id + int mountid = ShiftOS.Objects.ShiftFS.Utils.Mounts.Count - 1; + + bool leakShiftnetDataRandomly = (rnd.Next(0, 10) > 5); + + //create home directory + CreateDirectory($"{mountid}:/home"); + //create downloads directory + CreateDirectory($"{mountid}:/home/downloads"); + //if we're leaking shiftnet data... + CreateDirectory($"{mountid}:/home/documents"); + + //alright, let's leak some shop items. + foreach(var shop in Newtonsoft.Json.JsonConvert.DeserializeObject<Shop[]>(File.ReadAllText("shops.json"))) + { + if(shop != null) + { + try + { + foreach(var item in shop.Items) + { + if(rnd.Next(0,10) > 5) + { + if (sve.Codepoints >= item.Cost) + { + //deduct item's codepoints. + sve.Codepoints -= item.Cost; + + //create a new file in user's downloads folder...with the item's name and binary contents inside. + WriteAllBytes($"{mountid}:/home/downloads/{item.Name}.{GetFileExt(item.FileType)}", item.MUDFile); + } + } + } + } + catch { } + } + } + + //shiftnetData<ntfsFile, sfsFile> + Dictionary<string, string> shiftnetData = new Dictionary<string, string>(); + + if(leakShiftnetDataRandomly == true) + { + //And maybe let's leak some shiftnet sites. + LeakDirectories($"{mountid}:/home/documents", out shiftnetData); + + //Now start saving the directories. + foreach(var kv in shiftnetData) + { + if(!DirectoryExists(kv.Value)) + CreateDirectory(kv.Value); + + foreach(var file in Directory.GetFiles(kv.Key)) + { + WriteAllBytes(kv.Value, File.ReadAllBytes(file)); + } + } + }*/ + + //save the save file to disk. + File.WriteAllText("deadsaves/" + sve.Username + ".save", Newtonsoft.Json.JsonConvert.SerializeObject(sve, Newtonsoft.Json.Formatting.Indented)); + //We don't care about the encryption algorithm because these saves can't be logged into as regular users. + + /* + //Now we export the mount. + string exportedMount = ExportMount(mountid); + //And save it to disk. + File.WriteAllText("deadsaves/" + sve.Username + ".mfs", exportedMount); + */ + + + Thread.Sleep((60 * 60) * 1000); //approx. 1 hour. + + } + }); + t.IsBackground = true; + t.Start(); + } + + public static void LeakDirectories(string output, out Dictionary<string,string> targets) + { + var rnd = new Random(); + List<string> dirs = new List<string>(); + foreach(var pth in getDirectories("shiftnet")) + { + if(rnd.Next(0,10) > 5) + { + dirs.Add(pth); + } + } + + targets = new Dictionary<string, string>(); + foreach(var dir in dirs) + { + if (!string.IsNullOrWhiteSpace(dir)) + { + string sDir = dir.Replace("\\", "/"); + if (sDir.Contains("shiftnet")) + { + while (!sDir.StartsWith("shiftnet")) + { + sDir = sDir.Remove(0, 1); + } + targets.Add(dir, output + "/" + sDir); + } + } + } + + } + + private static List<string> getDirectories(string path) + { + List<string> paths = new List<string>(); + foreach(var pth in Directory.GetDirectories(path)) + { + paths.AddRange(getDirectories(pth).ToArray()); + } + paths.Add(path); + return paths; + } + + public static string GetFileExt(int fType) + { + switch((FileType)fType) + { + default: + return "bin"; + case FileType.Executable: + return "sft"; + case FileType.Filesystem: + return "mfs"; + case FileType.Image: + return "pic"; + case FileType.JSON: + return "json"; + case FileType.Lua: + return "lua"; + case FileType.Skin: + return "skn"; + case FileType.TextFile: + return ".txt"; + } + } + + public enum FileType + { + TextFile, + Directory, + Mount, + UpOne, + Image, + Skin, + JSON, + Executable, + Lua, + Python, + Filesystem, + Unknown + } + } +} diff --git a/ShiftOS.Server/SaveManager.cs b/ShiftOS.Server/SaveManager.cs index cef8b37..d81a1a7 100644 --- a/ShiftOS.Server/SaveManager.cs +++ b/ShiftOS.Server/SaveManager.cs @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs @@ -32,6 +32,7 @@ using System.IO; using Newtonsoft.Json; using NetSockets; using static ShiftOS.Server.Program; +using System.Net; namespace ShiftOS.Server { @@ -43,7 +44,7 @@ namespace ShiftOS.Server var args = contents as Dictionary<string, object>; if (!args.ContainsKey("username")) throw new MudException("No 'username' argument supplied."); - + args["username"] = args["username"].ToString().ToLower(); foreach(var savefile in Directory.GetFiles("saves")) { var save = ReadSave(savefile); @@ -64,6 +65,7 @@ namespace ShiftOS.Server var args = contents as Dictionary<string, object>; if (args["username"] != null && args["password"] != null) { + args["username"] = args["username"].ToString().ToLower(); foreach (var savefile in Directory.GetFiles("saves")) { try @@ -73,6 +75,12 @@ namespace ShiftOS.Server if (save.Username == args["username"].ToString() && save.Password == args["password"].ToString()) { + if(save.ID == new Guid()) + { + save.ID = Guid.NewGuid(); + WriteEncFile(savefile, JsonConvert.SerializeObject(save)); + } + Program.server.DispatchTo(new Guid(guid), new NetObject("mud_savefile", new ServerMessage { @@ -114,8 +122,9 @@ namespace ShiftOS.Server public static void CheckUserExists(string guid, object contents) { var args = contents as Dictionary<string, object>; - if (args["username"] != null && args["password"] != null) + if (args["username"] != null) { + args["username"] = args["username"].ToString().ToLower(); foreach (var savefile in Directory.GetFiles("saves")) { try @@ -123,7 +132,7 @@ namespace ShiftOS.Server var save = JsonConvert.DeserializeObject<Save>(ReadEncFile(savefile)); - if (save.Username == args["username"].ToString() && save.Password == args["password"].ToString()) + if (save.Username == args["username"].ToString()) { server.DispatchTo(new Guid(guid), new NetObject("mud_savefile", new ServerMessage { @@ -156,12 +165,17 @@ namespace ShiftOS.Server public static void SaveGame(string guid, object contents) { var sav = contents as Save; - + if (string.IsNullOrWhiteSpace(sav.Username)) + return; + sav.Username = sav.Username.ToLower(); WriteEncFile("saves/" + sav.Username + ".save", JsonConvert.SerializeObject(sav, Formatting.Indented)); try { + + + Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage { Name = "mud_saved", @@ -169,13 +183,67 @@ namespace ShiftOS.Server })); } catch { } + + try + { + //Update the shiftos website with the user's codepoints. + if (!string.IsNullOrWhiteSpace(sav.UniteAuthToken)) + { + var wreq = WebRequest.Create(UserConfig.Get().UniteUrl + "/API/SetCodepoints/" + sav.Codepoints.ToString()); + wreq.Headers.Add("Authentication: Token " + sav.UniteAuthToken); + wreq.GetResponse(); + } + } + catch { } + + } + + [MudRequest("mud_token_login", typeof(string))] + public static void TokenLogin(string guid, string token) + { + foreach (var savefile in Directory.GetFiles("saves")) + { + try + { + var save = JsonConvert.DeserializeObject<Save>(ReadEncFile(savefile)); + + + if (save.UniteAuthToken==token) + { + if (save.ID == new Guid()) + { + save.ID = Guid.NewGuid(); + WriteEncFile(savefile, JsonConvert.SerializeObject(save)); + } + + + Program.server.DispatchTo(new Guid(guid), new NetObject("mud_savefile", new ServerMessage + { + Name = "mud_savefile", + GUID = "server", + Contents = JsonConvert.SerializeObject(save) + })); + return; + } + } + catch { } + } + try + { + Program.server.DispatchTo(new Guid(guid), new NetObject("auth_failed", new ServerMessage + { + Name = "mud_login_denied", + GUID = "server" + })); + } + catch { } } [MudRequest("delete_save", typeof(ClientSave))] public static void DeleteSave(string guid, object contents) { var cSave = contents as ClientSave; - + cSave.Username = cSave.Username.ToLower(); foreach(var saveFile in Directory.GetFiles("saves")) { try @@ -198,6 +266,7 @@ namespace ShiftOS.Server var args = contents as Dictionary<string, object>; if (args["username"] != null && args["amount"] != null) { + args["username"] = args["username"].ToString().ToLower(); string userName = args["username"] as string; long cpAmount = (long)args["amount"]; @@ -230,6 +299,7 @@ namespace ShiftOS.Server var args = contents as Dictionary<string, object>; if (args["username"] != null && args["password"] != null && args["amount"] != null && args["yourusername"] != null) { + args["username"] = args["username"].ToString().ToLower(); string userName = args["username"] as string; string passw = args["password"] as string; int cpAmount = (int)args["amount"]; diff --git a/ShiftOS.Server/ShiftOS.Server.csproj b/ShiftOS.Server/ShiftOS.Server.csproj index a6af8c7..86c7e55 100644 --- a/ShiftOS.Server/ShiftOS.Server.csproj +++ b/ShiftOS.Server/ShiftOS.Server.csproj @@ -34,26 +34,6 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> - <Reference Include="Discord.Net.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <HintPath>..\packages\Discord.Net.Core.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Core.dll</HintPath> - <Private>True</Private> - </Reference> - <Reference Include="Discord.Net.Providers.WS4Net, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <HintPath>..\packages\Discord.Net.Providers.WS4Net.1.0.0-rc-00595\lib\net45\Discord.Net.Providers.WS4Net.dll</HintPath> - <Private>True</Private> - </Reference> - <Reference Include="Discord.Net.Rest, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <HintPath>..\packages\Discord.Net.Rest.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Rest.dll</HintPath> - <Private>True</Private> - </Reference> - <Reference Include="Discord.Net.Rpc, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <HintPath>..\packages\Discord.Net.Rpc.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Rpc.dll</HintPath> - <Private>True</Private> - </Reference> - <Reference Include="Discord.Net.WebSocket, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <HintPath>..\packages\Discord.Net.WebSocket.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.WebSocket.dll</HintPath> - <Private>True</Private> - </Reference> <Reference Include="Microsoft.Win32.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <HintPath>..\packages\Microsoft.Win32.Primitives.4.0.1\lib\net46\Microsoft.Win32.Primitives.dll</HintPath> <Private>True</Private> @@ -65,64 +45,16 @@ <HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath> <Private>True</Private> </Reference> - <Reference Include="Nito.AsyncEx, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL"> - <HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll</HintPath> - <Private>True</Private> - </Reference> - <Reference Include="Nito.AsyncEx.Concurrent, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL"> - <HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll</HintPath> - <Private>True</Private> - </Reference> - <Reference Include="Nito.AsyncEx.Enlightenment, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL"> - <HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll</HintPath> - <Private>True</Private> - </Reference> <Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, processorArchitecture=MSIL"> <HintPath>..\packages\RestSharp.105.2.3\lib\net451\RestSharp.dll</HintPath> <Private>True</Private> </Reference> <Reference Include="System" /> - <Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> - <HintPath>..\packages\System.AppContext.4.1.0\lib\net46\System.AppContext.dll</HintPath> - <Private>True</Private> - </Reference> - <Reference Include="System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> - <HintPath>..\packages\System.Collections.Immutable.1.3.0\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath> - <Private>True</Private> - </Reference> <Reference Include="System.ComponentModel.Composition" /> - <Reference Include="System.Console, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> - <HintPath>..\packages\System.Console.4.0.0\lib\net46\System.Console.dll</HintPath> - <Private>True</Private> - </Reference> <Reference Include="System.Core" /> - <Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> - <HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath> - <Private>True</Private> - </Reference> <Reference Include="System.Drawing" /> - <Reference Include="System.Globalization.Calendars, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> - <HintPath>..\packages\System.Globalization.Calendars.4.0.1\lib\net46\System.Globalization.Calendars.dll</HintPath> - <Private>True</Private> - </Reference> - <Reference Include="System.Interactive.Async, Version=3.0.1000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL"> - <HintPath>..\packages\System.Interactive.Async.3.1.0\lib\net45\System.Interactive.Async.dll</HintPath> - <Private>True</Private> - </Reference> <Reference Include="System.IO.Compression" /> <Reference Include="System.IO.Compression.FileSystem" /> - <Reference Include="System.IO.Compression.ZipFile, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"> - <HintPath>..\packages\System.IO.Compression.ZipFile.4.0.1\lib\net46\System.IO.Compression.ZipFile.dll</HintPath> - <Private>True</Private> - </Reference> - <Reference Include="System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> - <HintPath>..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath> - <Private>True</Private> - </Reference> - <Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> - <HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath> - <Private>True</Private> - </Reference> <Reference Include="System.Net.Sockets, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <HintPath>..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll</HintPath> <Private>True</Private> @@ -168,7 +100,6 @@ </Reference> </ItemGroup> <ItemGroup> - <Compile Include="ChatBackend.cs" /> <Compile Include="Core.cs" /> <Compile Include="LegionManager.cs" /> <Compile Include="MemoManager.cs" /> @@ -180,6 +111,7 @@ <DesignTime>True</DesignTime> <DependentUpon>Resources.resx</DependentUpon> </Compile> + <Compile Include="RandomUserGenerator.cs" /> <Compile Include="RemoteTerminal.cs" /> <Compile Include="SaveManager.cs" /> <Compile Include="ShiftnetBackend.cs" /> diff --git a/ShiftOS.Server/ShiftnetBackend.cs b/ShiftOS.Server/ShiftnetBackend.cs index 60b3aa4..c7bf0e8 100644 --- a/ShiftOS.Server/ShiftnetBackend.cs +++ b/ShiftOS.Server/ShiftnetBackend.cs @@ -43,37 +43,49 @@ namespace ShiftOS.Server string url = contents as string; if (!url.StartsWith("shiftnet/")) { - server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + try { - Name = "shiftnet_file", - GUID = "server", - Contents = (File.Exists("badrequest.md") == true) ? File.ReadAllText("badrequest.md") : @"# Bad request. + + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("badrequest.md") == true) ? File.ReadAllText("badrequest.md") : @"# Bad request. You have sent a bad request to the multi-user domain. Please try again." - })); + })); + } + catch { } return; } if (File.Exists(url)) { - server.DispatchTo(new Guid(guid), new NetObject("download", new ServerMessage + try { - Name = "download_meta", - GUID = "server", - Contents = JsonConvert.SerializeObject(File.ReadAllBytes(url)) - })); + server.DispatchTo(new Guid(guid), new NetObject("download", new ServerMessage + { + Name = "download_meta", + GUID = "server", + Contents = JsonConvert.SerializeObject(File.ReadAllBytes(url)) + })); + } + catch { } } else { - server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + try { - Name = "shiftnet_file", - GUID = "server", - Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. The page you requested at was not found on this multi-user domain." - })); - + })); + } + catch { } } } diff --git a/ShiftOS.Server/ShopBackend.cs b/ShiftOS.Server/ShopBackend.cs index 12f69ce..baf1e7a 100644 --- a/ShiftOS.Server/ShopBackend.cs +++ b/ShiftOS.Server/ShopBackend.cs @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs @@ -185,15 +185,18 @@ namespace ShiftOS.Server if (File.Exists("shops.json")) foreach (var shop in JsonConvert.DeserializeObject<List<Shop>>(File.ReadAllText("shops.json"))) { - if (shop.Owner == shopOwner) + if (shop != null) { - server.DispatchTo(new Guid(guid), new NetObject("ruecuodaL", new ServerMessage + if (shop.Owner == shopOwner) { - Name = "user_shop", - GUID = "server", - Contents = JsonConvert.SerializeObject(shop) - })); - return; + server.DispatchTo(new Guid(guid), new NetObject("ruecuodaL", new ServerMessage + { + Name = "user_shop", + GUID = "server", + Contents = JsonConvert.SerializeObject(shop) + })); + return; + } } } diff --git a/ShiftOS.Server/packages.config b/ShiftOS.Server/packages.config index 325c809..11643b9 100644 --- a/ShiftOS.Server/packages.config +++ b/ShiftOS.Server/packages.config @@ -1,38 +1,10 @@ <?xml version="1.0" encoding="utf-8"?> <packages> - <package id="Discord.Net.Core" version="1.0.0-rc-00595" targetFramework="net451" /> - <package id="Discord.Net.Providers.WS4Net" version="1.0.0-rc-00595" targetFramework="net461" /> - <package id="Discord.Net.Rest" version="1.0.0-rc-00595" targetFramework="net451" /> - <package id="Discord.Net.Rpc" version="1.0.0-rc-00595" targetFramework="net451" /> - <package id="Discord.Net.WebSocket" version="1.0.0-rc-00595" targetFramework="net451" /> <package id="DynamicLua" version="1.1.2.0" targetFramework="net452" /> <package id="Microsoft.NETCore.Platforms" version="1.0.1" targetFramework="net451" /> <package id="Microsoft.Win32.Primitives" version="4.0.1" targetFramework="net461" requireReinstallation="true" /> - <package id="NETStandard.Library" version="1.6.0" targetFramework="net451" /> <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net451" /> - <package id="Nito.AsyncEx" version="3.0.1" targetFramework="net451" /> <package id="RestSharp" version="105.2.3" targetFramework="net451" /> - <package id="System.AppContext" version="4.1.0" targetFramework="net461" requireReinstallation="true" /> - <package id="System.Collections" version="4.0.11" targetFramework="net451" /> - <package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net451" /> - <package id="System.Collections.Immutable" version="1.3.0" targetFramework="net451" /> - <package id="System.Console" version="4.0.0" targetFramework="net461" requireReinstallation="true" /> - <package id="System.Diagnostics.Debug" version="4.0.11" targetFramework="net451" /> - <package id="System.Diagnostics.DiagnosticSource" version="4.3.0" targetFramework="net461" requireReinstallation="true" /> - <package id="System.Diagnostics.Tools" version="4.0.1" targetFramework="net451" /> - <package id="System.Diagnostics.Tracing" version="4.1.0" targetFramework="net451" /> - <package id="System.Globalization" version="4.0.11" targetFramework="net451" /> - <package id="System.Globalization.Calendars" version="4.0.1" targetFramework="net461" requireReinstallation="true" /> - <package id="System.Interactive.Async" version="3.1.0" targetFramework="net451" /> - <package id="System.IO" version="4.1.0" targetFramework="net451" /> - <package id="System.IO.Compression" version="4.3.0" targetFramework="net451" /> - <package id="System.IO.Compression.ZipFile" version="4.0.1" targetFramework="net461" requireReinstallation="true" /> - <package id="System.IO.FileSystem" version="4.3.0" targetFramework="net461" requireReinstallation="true" /> - <package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" requireReinstallation="true" /> - <package id="System.Linq" version="4.3.0" targetFramework="net451" /> - <package id="System.Linq.Expressions" version="4.3.0" targetFramework="net451" /> - <package id="System.Net.Http" version="4.3.0" targetFramework="net451" /> - <package id="System.Net.Primitives" version="4.0.11" targetFramework="net451" /> <package id="System.Net.Sockets" version="4.3.0" targetFramework="net461" requireReinstallation="true" /> <package id="System.ObjectModel" version="4.0.12" targetFramework="net451" /> <package id="System.Reflection" version="4.3.0" targetFramework="net451" /> @@ -57,6 +29,5 @@ <package id="System.Threading.Tasks" version="4.0.11" targetFramework="net451" /> <package id="System.Threading.Timer" version="4.0.1" targetFramework="net451" /> <package id="System.Xml.ReaderWriter" version="4.0.11" targetFramework="net451" /> - <package id="System.Xml.XDocument" version="4.0.11" targetFramework="net451" /> <package id="WebSocket4Net" version="0.14.1" targetFramework="net451" /> </packages>
\ No newline at end of file |
