Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CommandTerminal/Commands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using UnityEngine;
using CommandTerminal;

public static class Commands {
[RegisterCommand(Help = "Adds 2 numbers")]
static void CommandAdd(CommandArg[] args) {
int a = args[0].Int;
int b = args[1].Int;

if (Terminal.IssuedError) return; // Error will be handled by Terminal

int result = a + b;
Terminal.Log("{0} + {1} = {2}", a, b, result);
}

[RegisterCommand(Help = "Adds 2 numbers", MinArgCount = 1)]
static void Log(CommandArg[] args) {
string text = "";
foreach(var arg in args) {
text += arg + " ";
}
Debug.Log(text);
}
}

80 changes: 80 additions & 0 deletions CommandTerminal/TerminalRemoteHTTPAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using UnityEngine;
using UnityEngine.Networking;
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Text;

//This is a simple http listener that sends strings as commands to the terminal shell
//It receives 2 keys, password and command. Checks if password is correct before sending command string.
//TODO: parametrize ports and password from gamedata.serversettings
public class TerminalRemoteHTTPAccess : MonoBehaviour
{

private HttpListener listener;
private Thread listenerThread;
public string password = "password";
private bool passwordCorrect = false;

void Start ()
{
listener = new HttpListener ();
listener.Prefixes.Add ("http://localhost:4444/");
listener.Prefixes.Add ("http://127.0.0.1:4444/");
listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
listener.Start ();

listenerThread = new Thread (startListener);
listenerThread.Start ();
Debug.Log ("Remote Terminal HTTP Listener Server Started");
}

private void startListener ()
{
while (true) {
var result = listener.BeginGetContext (ListenerCallback, listener);
result.AsyncWaitHandle.WaitOne ();
}
}

private void ListenerCallback (IAsyncResult result)
{
var context = listener.EndGetContext (result);
// Debug.Log ("Method: " + context.Request.HttpMethod);
// Debug.Log ("LocalUrl: " + context.Request.Url.LocalPath);
passwordCorrect = false;
if (context.Request.QueryString.AllKeys.Length > 0)
foreach (var key in context.Request.QueryString.AllKeys) {
// Debug.Log ("Key: " + key + ", Value: " + context.Request.QueryString.GetValues (key) [0]);
if (key == "password") {
if (password == context.Request.QueryString.GetValues(key)[0]) {
passwordCorrect = true;
} else {
Response(context, "Password incorrect.");
}
}
}

if (context.Request.QueryString.AllKeys.Length > 0 && passwordCorrect)
foreach (var key in context.Request.QueryString.AllKeys) {
// Debug.Log ("Key: " + key + ", Value: " + context.Request.QueryString.GetValues (key) [0]);
if (key == "command") {
CommandTerminal.Terminal.Shell.RunCommand(context.Request.QueryString.GetValues(key)[0]);
Response(context, "Command executed.");
}
}


context.Response.Close ();
}

public void Response(HttpListenerContext context, string message) {
string logContent = message; // your json string here
context.Response.ContentType = "application/json";
byte[] encodedPayload = new UTF8Encoding().GetBytes(logContent);
context.Response.ContentLength64 = encodedPayload.Length;
System.IO.Stream output = context.Response.OutputStream;
output.Write(encodedPayload, 0, encodedPayload.Length);
}
}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ static void FrontCommandAdd(CommandArg[] args) {
```csharp
Terminal.Shell.AddCommand("add", CommandAdd, 2, 2, "Adds 2 numbers");
```

## Remote Commands
We can also send commands remotely, via HTTP.

Attach the TerminalRemoteHTTPAccess.cs component to any gameObject (Be organized, put it on the one that already has the Terminal.cs) and enter playmode.


You can use the browser (e.g. http://localhost:4444/?password=password&command=log%20hello%20world%20) or any other HTTP client to send your commands. All you need is the password and the command.