Terminal remote control!

This commit is contained in:
Michael 2017-01-21 16:23:10 -05:00
parent 5c9629c4c6
commit a531cefa00
11 changed files with 254 additions and 141 deletions

View file

@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShiftOS.Objects
{
public abstract class Exploit
{
public void BeginExploit(string remote_user, bool isMud)
{
var ctx = new ExploitContext();
SendToMUD(remote_user, "hack_getcontext");
MessageReceived += (u, c, j) =>
{
};
ThisContext = ctx;
}
public ExploitContext ThisContext { get; internal set; }
public virtual void SendToMUD(string target_user, string command, string json = "")
{
ThisContext.IsMUDHack = false;
if (command == "hack_getcontext")
{
MessageReceived?.Invoke(target_user, "context_info", ExploitContext.CreateRandom());
}
}
public event MUDMessageEventHandler MessageReceived;
public abstract void OnRun(ExploitContext ctx);
}
}

View file

@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShiftOS.Objects
{
public class ExploitContext
{
public static string CreateRandom()
{
//We can't use JSON.NET. We must construct the JSON ourselves.
StringBuilder jBuilder = new StringBuilder();
jBuilder.AppendLine("{");
jBuilder.Append("\tIsMUDHack: \"false\",");
jBuilder.AppendLine("}");
return jBuilder.ToString();
}
/// <summary>
/// Gets or sets whether or not this exploit context belongs to a MUD hack session.
/// </summary>
public bool IsMUDHack { get; set; }
/// <summary>
/// Gets or sets the target username for this exploit context. Used for talking with the MUD about it.
/// </summary>
public string TargetUsername { get; set; }
/// <summary>
/// Gets or sets the target's locks.
/// </summary>
public List<Lock> TargetLocks { get; set; }
}
}

View file

@ -1,44 +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;
namespace ShiftOS.Objects
{
public delegate void MUDMessageEventHandler(string target_user, string command, string json);
public abstract class Lock
{
public abstract bool Unlocked { get; }
public abstract void Unlock();
}
}

View file

@ -55,9 +55,6 @@
<ItemGroup>
<Compile Include="ClientSave.cs" />
<Compile Include="DiscourseUser.cs" />
<Compile Include="Exploit.cs" />
<Compile Include="ExploitContext.cs" />
<Compile Include="Hack.cs" />
<Compile Include="Job.cs" />
<Compile Include="Legion.cs" />
<Compile Include="Objects.cs" />

View file

@ -245,6 +245,99 @@ Contents:
switch (msg.Name)
{
case "usr_getcp":
break;
case "usr_takecp":
if (args["username"] != null && args["password"] != null && args["amount"] != null && args["yourusername"] != null)
{
string userName = args["username"] as string;
string passw = args["password"] as string;
int amount = (int)args["amount"];
if (Directory.Exists("saves"))
{
foreach (var saveFile in Directory.GetFiles("saves"))
{
var saveFileContents = JsonConvert.DeserializeObject<Save>(File.ReadAllText(saveFile));
if (saveFileContents.Username == userName && saveFileContents.Password == passw)
{
saveFileContents.Codepoints += amount;
File.WriteAllText(saveFile, JsonConvert.SerializeObject(saveFileContents, Formatting.Indented));
server.DispatchAll(new NetObject("stop_being_drunk_michael", new ServerMessage
{
Name = "update_your_cp",
GUID = "server",
Contents = $@"{{
username: ""{userName}"",
amount: -{amount}
}}"
}));
server.DispatchTo(new Guid(msg.GUID), new NetObject("argh", new ServerMessage
{
Name = "update_your_cp",
GUID = "server",
Contents = $@"{{
username: ""{args["yourusername"]}"",
amount: {amount}
}}"
}));
return;
}
}
}
}
server.DispatchTo(new Guid(msg.GUID), new NetObject("no", new ServerMessage
{
Name = "user_cp_not_found",
GUID = "Server",
}));
break;
case "trm_handshake_accept":
if(args["guid"] != null)
{
server.DispatchTo(new Guid(msg.GUID), new NetObject("hold_it", new ServerMessage
{
Name = "trm_handshake_guid",
GUID = args["guid"] as string
}));
}
break;
case "trm_handshake_request":
if(args["username"] != null && args["password"] != null && args["sysname"] != null)
{
server.DispatchAll(new NetObject("hold_my_hand", new ServerMessage
{
Name = "handshake_from",
GUID = msg.GUID,
Contents = JsonConvert.SerializeObject(args)
}));
}
break;
case "write":
if(args["guid"] != null && args["text"] != null)
{
server.DispatchTo(new Guid(args["guid"] as string), new NetObject("pleaseWrite", new ServerMessage
{
Name = "pleasewrite",
GUID = "server",
Contents = args["text"] as string
}));
}
break;
case "trm_invcmd":
if(args["guid"] != null && args["cmdstr"] != null)
{
string cmd = args["cmdstr"] as string;
string cGuid = args["guid"] as string;
server.DispatchTo(new Guid(cGuid), new NetObject("trminvoke", new ServerMessage
{
Name = "trm_invokecommand",
GUID = "server",
Contents = cmd
}));
}
break;
case "usr_givecp":
if (args["username"] != null && args["amount"] != null)
{
@ -400,7 +493,7 @@ Contents:
}));
break;
case "mud_checkuserexists":
case "mud_checkuserexists":
if (args["username"] != null && args["password"] != null)
{
foreach (var savefile in Directory.GetFiles("saves"))

View file

@ -42,6 +42,7 @@ using System.Text.RegularExpressions;
using System.Collections;
using static ShiftOS.Engine.SkinEngine;
using ShiftOS.Engine;
using ShiftOS.Objects;
namespace ShiftOS.WinForms.Applications
{
@ -55,6 +56,10 @@ namespace ShiftOS.WinForms.Applications
public static string latestCommmand = "";
public static bool IsInRemoteSystem = false;
public static string RemoteGuid = "";
[Obsolete("This is used for compatibility with old parts of the backend. Please use TerminalBackend instead.")]
public static bool PrefixEnabled
{
@ -110,6 +115,7 @@ namespace ShiftOS.WinForms.Applications
[Obsolete("This is used for compatibility with old parts of the backend. Please use TerminalBackend instead.")]
public static void InvokeCommand(string text)
{
TerminalBackend.InvokeCommand(text);
}
@ -245,19 +251,29 @@ namespace ShiftOS.WinForms.Applications
var text3 = "";
var text4 = Regex.Replace(text2, @"\t|\n|\r", "");
if (TerminalBackend.PrefixEnabled)
if (IsInRemoteSystem == true)
{
text3 = text4.Remove(0, $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ".Length);
ServerManager.SendMessage("trm_invcmd", $@"{{
guid = ""{RemoteGuid}"",
command: ""{text4}""
}}");
}
TerminalBackend.LastCommand = text3;
TextSent?.Invoke(text4);
if (TerminalBackend.InStory == false)
else
{
TerminalBackend.InvokeCommand(text3);
}
if (TerminalBackend.PrefixEnabled)
{
Console.Write($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ");
if (TerminalBackend.PrefixEnabled)
{
text3 = text4.Remove(0, $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ".Length);
}
TerminalBackend.LastCommand = text3;
TextSent?.Invoke(text4);
if (TerminalBackend.InStory == false)
{
TerminalBackend.InvokeCommand(text3);
}
if (TerminalBackend.PrefixEnabled)
{
Console.Write($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ");
}
}
}
catch
@ -337,12 +353,22 @@ namespace ShiftOS.WinForms.Applications
private void Terminal_Load(object sender, EventArgs e)
{
ServerManager.MessageReceived += (msg) =>
{
if(msg.Name == "trm_handshake_guid")
{
IsInRemoteSystem = true;
RemoteGuid = msg.GUID;
}
};
}
private void Terminal_FormClosing(object sender, FormClosingEventArgs e)
{
ti.Stop();
IsInRemoteSystem = false;
RemoteGuid = "";
}
public void OnLoad()

View file

@ -32,12 +32,36 @@ using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using Newtonsoft.Json;
/// <summary>
/// Coherence commands.
/// </summary>
namespace ShiftOS.WinForms
{
[Namespace("trm")]
public static class TerminalExtensions
{
[Command("setpass", true)]
[RequiresArgument("pass")]
public static bool setPass(Dictionary<string, object> args)
{
SaveSystem.CurrentSave.Password = args["pass"] as string;
return true;
}
[Command("remote", "username:,sysname:,pass:", "Allows you to control a remote system on the multi-user domain given a username, password and system name.")]
[RequiresArgument("username")]
[RequiresArgument("sysname")]
[RequiresArgument("pass")]
public static bool RemoteControl(Dictionary<string, object> args)
{
ServerManager.SendMessage("trm_handshake_request", JsonConvert.SerializeObject(args));
return true;
}
}
[Namespace("coherence")]
[RequiresUpgrade("kernel_coherence")]
public static class CoherenceCommands

View file

@ -302,6 +302,12 @@
Description: "Adds an app launcher entry for the MUD chat application.",
Dependencies: "mud_fundamentals;app_launcher"
},
{
Name: "Another test upgrade",
Cost: 1,
Description: "Read the ID or Name",
Dependencies: "test_upgrade"
},
{
Name: "Draggable windows",
Cost: 400,

View file

@ -49,7 +49,7 @@ namespace ShiftOS.Engine
{JsonConvert.SerializeObject(client, Formatting.Indented)}");
}
private static Guid thisGuid { get; set; }
public static Guid thisGuid { get; private set; }
private static NetObjectClient client { get; set; }
public static void Disconnect()

View file

@ -240,6 +240,61 @@ namespace ShiftOS.Engine
return false;
}
static TerminalBackend()
{
ServerMessageReceived onMessageReceived = (msg) =>
{
if (msg.Name == "trm_invokecommand")
{
string text3 = "";
string text4 = msg.Contents;
if (TerminalBackend.PrefixEnabled)
{
text3 = text4.Remove(0, $"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ".Length);
}
IsForwardingConsoleWrites = true;
if (TerminalBackend.InStory == false)
{
TerminalBackend.InvokeCommand(text3);
}
if (TerminalBackend.PrefixEnabled)
{
Console.Write($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ");
}
IsForwardingConsoleWrites = false;
}
else if(msg.Name == "pleasewrite")
{
Console.Write(msg.Contents);
}
else if(msg.Name == "handshake_from")
{
var a = JsonConvert.DeserializeObject<Dictionary<string, object>>(msg.Contents);
string uName = a["username"] as string;
string pass = a["password"] as string;
string sys = a["sysname"] as string;
string guid = msg.GUID;
if(SaveSystem.CurrentSave.Username == uName && SaveSystem.CurrentSave.Password == pass && CurrentSave.SystemName == sys)
{
ForwardGUID = guid;
ServerManager.SendMessage("trm_handshake_accept", $@"{{
guid: ""{ServerManager.thisGuid}""
}}");
IsForwardingConsoleWrites = true;
InvokeCommand("sos.status");
Console.Write($"{SaveSystem.CurrentSave.Username}@{SaveSystem.CurrentSave.SystemName}:~$ ");
IsForwardingConsoleWrites = false;
}
}
};
ServerManager.MessageReceived += onMessageReceived;
}
public static bool IsForwardingConsoleWrites { get; private set; }
public static string ForwardGUID { get; private set; }
}
}

View file

@ -65,20 +65,41 @@ namespace ShiftOS.Engine
public override void Write(char value)
{
Desktop.InvokeOnWorkerThread(new Action(() =>
if (TerminalBackend.IsForwardingConsoleWrites)
{
UnderlyingControl.Write(value.ToString());
select();
}));
ServerManager.SendMessage("write", $@"{{
guid: ""{TerminalBackend.ForwardGUID}"",
text: ""{value}""
}}");
}
else
{
Desktop.InvokeOnWorkerThread(new Action(() =>
{
UnderlyingControl.Write(value.ToString());
select();
}));
}
}
public override void WriteLine(string value)
{
Desktop.InvokeOnWorkerThread(new Action(() =>
if (TerminalBackend.IsForwardingConsoleWrites)
{
ServerManager.SendMessage("write", $@"{{
guid: ""{TerminalBackend.ForwardGUID}"",
text: ""{value + Environment.NewLine}""
}}");
}
else
{
Desktop.InvokeOnWorkerThread(new Action(() =>
{
UnderlyingControl.WriteLine(value);
select();
}));
}
}
public void SetLastText()
@ -87,11 +108,22 @@ namespace ShiftOS.Engine
public override void Write(string value)
{
Desktop.InvokeOnWorkerThread(new Action(() =>
if (TerminalBackend.IsForwardingConsoleWrites)
{
ServerManager.SendMessage("write", $@"{{
guid: ""{TerminalBackend.ForwardGUID}"",
text: ""{value}""
}}");
}
else
{
Desktop.InvokeOnWorkerThread(new Action(() =>
{
UnderlyingControl.Write(value.ToString());
select();
}));
}
}