mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-27 07:04:57 +01:00
Adds player counting
This commit is contained in:
+314
-311
@@ -1,311 +1,314 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TGServiceInterface;
|
||||
|
||||
namespace TGCommandLine
|
||||
{
|
||||
class DDCommand : RootCommand
|
||||
{
|
||||
public DDCommand()
|
||||
{
|
||||
Keyword = "dd";
|
||||
Children = new Command[] { new DDStartCommand(), new DDStopCommand(), new DDRestartCommand(), new DDStatusCommand(), new DDAutostartCommand(), new DDPortCommand(), new DDSecurityCommand(), new DDWorldAnnounceCommand(), new DDWebclientCommand() };
|
||||
}
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Manage DreamDaemon";
|
||||
}
|
||||
}
|
||||
|
||||
class DDWorldAnnounceCommand : Command
|
||||
{
|
||||
public DDWorldAnnounceCommand()
|
||||
{
|
||||
Keyword = "announce";
|
||||
RequiredParameters = 1;
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Sends a message all players on the server";
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "<message>";
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var res = Server.GetComponent<ITGDreamDaemon>().WorldAnnounce(String.Join(" ", parameters));
|
||||
OutputProc(res ?? "Success!");
|
||||
return res == null ? ExitCode.Normal : ExitCode.ServerError;
|
||||
}
|
||||
}
|
||||
|
||||
class DDStartCommand : Command
|
||||
{
|
||||
public DDStartCommand()
|
||||
{
|
||||
Keyword = "start";
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Starts the server and watchdog";
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var res = Server.GetComponent<ITGDreamDaemon>().Start();
|
||||
OutputProc(res ?? "Success!");
|
||||
return res == null ? ExitCode.Normal : ExitCode.ServerError;
|
||||
}
|
||||
}
|
||||
|
||||
class DDStopCommand : Command
|
||||
{
|
||||
public DDStopCommand()
|
||||
{
|
||||
Keyword = "stop";
|
||||
}
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "[--graceful]";
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Stops the server and watchdog optionally waiting for the current round to end";
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var DD = Server.GetComponent<ITGDreamDaemon>();
|
||||
if (parameters.Count > 0 && parameters[0].ToLower() == "--graceful")
|
||||
{
|
||||
if (DD.DaemonStatus() != TGDreamDaemonStatus.Online)
|
||||
{
|
||||
OutputProc("Error: The game is not currently running!");
|
||||
return ExitCode.ServerError;
|
||||
}
|
||||
DD.RequestStop();
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
var res = DD.Stop();
|
||||
OutputProc(res ?? "Success!");
|
||||
return res == null ? ExitCode.Normal : ExitCode.ServerError;
|
||||
}
|
||||
}
|
||||
class DDRestartCommand : Command
|
||||
{
|
||||
public DDRestartCommand()
|
||||
{
|
||||
Keyword = "restart";
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "[--graceful]";
|
||||
}
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var DD = Server.GetComponent<ITGDreamDaemon>();
|
||||
if (parameters.Count > 0 && parameters[0].ToLower() == "--graceful")
|
||||
{
|
||||
if (DD.DaemonStatus() != TGDreamDaemonStatus.Online)
|
||||
{
|
||||
OutputProc("Error: The game is not currently running!");
|
||||
return ExitCode.ServerError;
|
||||
}
|
||||
DD.RequestRestart();
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
var res = DD.Restart();
|
||||
OutputProc(res ?? "Success!");
|
||||
return res == null ? ExitCode.Normal : ExitCode.ServerError;
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Restarts the server and watchdog optionally waiting for the current round to end";
|
||||
}
|
||||
}
|
||||
class DDStatusCommand : Command
|
||||
{
|
||||
public DDStatusCommand()
|
||||
{
|
||||
Keyword = "status";
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Gets the current status of the watchdog and server";
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var DD = Server.GetComponent<ITGDreamDaemon>();
|
||||
OutputProc(DD.StatusString(true));
|
||||
if (DD.ShutdownInProgress())
|
||||
OutputProc("The server will shutdown once the current round completes.");
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
}
|
||||
|
||||
class DDAutostartCommand : Command
|
||||
{
|
||||
public DDAutostartCommand()
|
||||
{
|
||||
Keyword = "autostart";
|
||||
RequiredParameters = 1;
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var DD = Server.GetComponent<ITGDreamDaemon>();
|
||||
switch (parameters[0].ToLower())
|
||||
{
|
||||
case "on":
|
||||
DD.SetAutostart(true);
|
||||
break;
|
||||
case "off":
|
||||
DD.SetAutostart(false);
|
||||
break;
|
||||
case "check":
|
||||
OutputProc("Autostart is: " + (DD.Autostart() ? "On" : "Off"));
|
||||
break;
|
||||
default:
|
||||
OutputProc("Invalid parameter: " + parameters[0]);
|
||||
return ExitCode.BadCommand;
|
||||
}
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "<on|off|check>";
|
||||
}
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Change or check autostarting of the game server with the service";
|
||||
}
|
||||
}
|
||||
class DDWebclientCommand : Command
|
||||
{
|
||||
public DDWebclientCommand()
|
||||
{
|
||||
Keyword = "webclient";
|
||||
RequiredParameters = 1;
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var DD = Server.GetComponent<ITGDreamDaemon>();
|
||||
switch (parameters[0].ToLower())
|
||||
{
|
||||
case "on":
|
||||
DD.SetWebclient(true);
|
||||
break;
|
||||
case "off":
|
||||
DD.SetWebclient(false);
|
||||
break;
|
||||
case "check":
|
||||
OutputProc("Webclient is: " + (DD.Webclient() ? "Enabled" : "Disabled"));
|
||||
break;
|
||||
default:
|
||||
OutputProc("Invalid parameter: " + parameters[0]);
|
||||
return ExitCode.BadCommand;
|
||||
}
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "<on|off|check>";
|
||||
}
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Change or check if the BYOND webclient is enabled for the game server";
|
||||
}
|
||||
}
|
||||
|
||||
class DDPortCommand : Command
|
||||
{
|
||||
public DDPortCommand()
|
||||
{
|
||||
Keyword = "set-port";
|
||||
RequiredParameters = 1;
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
ushort port;
|
||||
try
|
||||
{
|
||||
port = Convert.ToUInt16(parameters[0]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
OutputProc("Invalid port number!");
|
||||
return ExitCode.BadCommand;
|
||||
}
|
||||
|
||||
Server.GetComponent<ITGDreamDaemon>().SetPort(port);
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "<number>";
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Sets the port DreamDaemon will open the server on. Requires a server restart to apply and queues a graceful one up";
|
||||
}
|
||||
}
|
||||
|
||||
class DDSecurityCommand : Command
|
||||
{
|
||||
public DDSecurityCommand()
|
||||
{
|
||||
Keyword = "set-security";
|
||||
RequiredParameters = 1;
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
TGDreamDaemonSecurity sec;
|
||||
switch (parameters[0].ToLower())
|
||||
{
|
||||
case "safe":
|
||||
sec = TGDreamDaemonSecurity.Safe;
|
||||
break;
|
||||
case "ultra":
|
||||
case "ultrasafe":
|
||||
sec = TGDreamDaemonSecurity.Ultrasafe;
|
||||
break;
|
||||
case "trust":
|
||||
case "trusted":
|
||||
sec = TGDreamDaemonSecurity.Trusted;
|
||||
break;
|
||||
default:
|
||||
OutputProc("Invalid security word!");
|
||||
return ExitCode.BadCommand;
|
||||
}
|
||||
Server.GetComponent<ITGDreamDaemon>().SetSecurityLevel(sec);
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "<safe|ultrasafe|trusted>";
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Sets the visibility option for the DreamDaemon world";
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TGServiceInterface;
|
||||
|
||||
namespace TGCommandLine
|
||||
{
|
||||
class DDCommand : RootCommand
|
||||
{
|
||||
public DDCommand()
|
||||
{
|
||||
Keyword = "dd";
|
||||
Children = new Command[] { new DDStartCommand(), new DDStopCommand(), new DDRestartCommand(), new DDStatusCommand(), new DDAutostartCommand(), new DDPortCommand(), new DDSecurityCommand(), new DDWorldAnnounceCommand(), new DDWebclientCommand() };
|
||||
}
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Manage DreamDaemon";
|
||||
}
|
||||
}
|
||||
|
||||
class DDWorldAnnounceCommand : Command
|
||||
{
|
||||
public DDWorldAnnounceCommand()
|
||||
{
|
||||
Keyword = "announce";
|
||||
RequiredParameters = 1;
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Sends a message all players on the server";
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "<message>";
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var res = Server.GetComponent<ITGDreamDaemon>().WorldAnnounce(String.Join(" ", parameters));
|
||||
OutputProc(res ?? "Success!");
|
||||
return res == null ? ExitCode.Normal : ExitCode.ServerError;
|
||||
}
|
||||
}
|
||||
|
||||
class DDStartCommand : Command
|
||||
{
|
||||
public DDStartCommand()
|
||||
{
|
||||
Keyword = "start";
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Starts the server and watchdog";
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var res = Server.GetComponent<ITGDreamDaemon>().Start();
|
||||
OutputProc(res ?? "Success!");
|
||||
return res == null ? ExitCode.Normal : ExitCode.ServerError;
|
||||
}
|
||||
}
|
||||
|
||||
class DDStopCommand : Command
|
||||
{
|
||||
public DDStopCommand()
|
||||
{
|
||||
Keyword = "stop";
|
||||
}
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "[--graceful]";
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Stops the server and watchdog optionally waiting for the current round to end";
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var DD = Server.GetComponent<ITGDreamDaemon>();
|
||||
if (parameters.Count > 0 && parameters[0].ToLower() == "--graceful")
|
||||
{
|
||||
if (DD.DaemonStatus() != TGDreamDaemonStatus.Online)
|
||||
{
|
||||
OutputProc("Error: The game is not currently running!");
|
||||
return ExitCode.ServerError;
|
||||
}
|
||||
DD.RequestStop();
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
var res = DD.Stop();
|
||||
OutputProc(res ?? "Success!");
|
||||
return res == null ? ExitCode.Normal : ExitCode.ServerError;
|
||||
}
|
||||
}
|
||||
class DDRestartCommand : Command
|
||||
{
|
||||
public DDRestartCommand()
|
||||
{
|
||||
Keyword = "restart";
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "[--graceful]";
|
||||
}
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var DD = Server.GetComponent<ITGDreamDaemon>();
|
||||
if (parameters.Count > 0 && parameters[0].ToLower() == "--graceful")
|
||||
{
|
||||
if (DD.DaemonStatus() != TGDreamDaemonStatus.Online)
|
||||
{
|
||||
OutputProc("Error: The game is not currently running!");
|
||||
return ExitCode.ServerError;
|
||||
}
|
||||
DD.RequestRestart();
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
var res = DD.Restart();
|
||||
OutputProc(res ?? "Success!");
|
||||
return res == null ? ExitCode.Normal : ExitCode.ServerError;
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Restarts the server and watchdog optionally waiting for the current round to end";
|
||||
}
|
||||
}
|
||||
class DDStatusCommand : Command
|
||||
{
|
||||
public DDStatusCommand()
|
||||
{
|
||||
Keyword = "status";
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Gets the current status of the watchdog and server";
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var DD = Server.GetComponent<ITGDreamDaemon>();
|
||||
OutputProc(DD.StatusString(true));
|
||||
if (DD.ShutdownInProgress())
|
||||
OutputProc("The server will shutdown once the current round completes.");
|
||||
var pc = DD.PlayerCount();
|
||||
if (pc != -1)
|
||||
OutputProc(pc + " connected clients");
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
}
|
||||
|
||||
class DDAutostartCommand : Command
|
||||
{
|
||||
public DDAutostartCommand()
|
||||
{
|
||||
Keyword = "autostart";
|
||||
RequiredParameters = 1;
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var DD = Server.GetComponent<ITGDreamDaemon>();
|
||||
switch (parameters[0].ToLower())
|
||||
{
|
||||
case "on":
|
||||
DD.SetAutostart(true);
|
||||
break;
|
||||
case "off":
|
||||
DD.SetAutostart(false);
|
||||
break;
|
||||
case "check":
|
||||
OutputProc("Autostart is: " + (DD.Autostart() ? "On" : "Off"));
|
||||
break;
|
||||
default:
|
||||
OutputProc("Invalid parameter: " + parameters[0]);
|
||||
return ExitCode.BadCommand;
|
||||
}
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "<on|off|check>";
|
||||
}
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Change or check autostarting of the game server with the service";
|
||||
}
|
||||
}
|
||||
class DDWebclientCommand : Command
|
||||
{
|
||||
public DDWebclientCommand()
|
||||
{
|
||||
Keyword = "webclient";
|
||||
RequiredParameters = 1;
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
var DD = Server.GetComponent<ITGDreamDaemon>();
|
||||
switch (parameters[0].ToLower())
|
||||
{
|
||||
case "on":
|
||||
DD.SetWebclient(true);
|
||||
break;
|
||||
case "off":
|
||||
DD.SetWebclient(false);
|
||||
break;
|
||||
case "check":
|
||||
OutputProc("Webclient is: " + (DD.Webclient() ? "Enabled" : "Disabled"));
|
||||
break;
|
||||
default:
|
||||
OutputProc("Invalid parameter: " + parameters[0]);
|
||||
return ExitCode.BadCommand;
|
||||
}
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "<on|off|check>";
|
||||
}
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Change or check if the BYOND webclient is enabled for the game server";
|
||||
}
|
||||
}
|
||||
|
||||
class DDPortCommand : Command
|
||||
{
|
||||
public DDPortCommand()
|
||||
{
|
||||
Keyword = "set-port";
|
||||
RequiredParameters = 1;
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
ushort port;
|
||||
try
|
||||
{
|
||||
port = Convert.ToUInt16(parameters[0]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
OutputProc("Invalid port number!");
|
||||
return ExitCode.BadCommand;
|
||||
}
|
||||
|
||||
Server.GetComponent<ITGDreamDaemon>().SetPort(port);
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "<number>";
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Sets the port DreamDaemon will open the server on. Requires a server restart to apply and queues a graceful one up";
|
||||
}
|
||||
}
|
||||
|
||||
class DDSecurityCommand : Command
|
||||
{
|
||||
public DDSecurityCommand()
|
||||
{
|
||||
Keyword = "set-security";
|
||||
RequiredParameters = 1;
|
||||
}
|
||||
|
||||
protected override ExitCode Run(IList<string> parameters)
|
||||
{
|
||||
TGDreamDaemonSecurity sec;
|
||||
switch (parameters[0].ToLower())
|
||||
{
|
||||
case "safe":
|
||||
sec = TGDreamDaemonSecurity.Safe;
|
||||
break;
|
||||
case "ultra":
|
||||
case "ultrasafe":
|
||||
sec = TGDreamDaemonSecurity.Ultrasafe;
|
||||
break;
|
||||
case "trust":
|
||||
case "trusted":
|
||||
sec = TGDreamDaemonSecurity.Trusted;
|
||||
break;
|
||||
default:
|
||||
OutputProc("Invalid security word!");
|
||||
return ExitCode.BadCommand;
|
||||
}
|
||||
Server.GetComponent<ITGDreamDaemon>().SetSecurityLevel(sec);
|
||||
return ExitCode.Normal;
|
||||
}
|
||||
|
||||
public override string GetArgumentString()
|
||||
{
|
||||
return "<safe|ultrasafe|trusted>";
|
||||
}
|
||||
|
||||
public override string GetHelpText()
|
||||
{
|
||||
return "Sets the visibility option for the DreamDaemon world";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+220
-220
@@ -1,220 +1,220 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TGServiceInterface;
|
||||
|
||||
namespace TGCommandLine
|
||||
{
|
||||
|
||||
class Program
|
||||
{
|
||||
static ExitCode RunCommandLine(IList<string> argsAsList)
|
||||
{
|
||||
//first lookup the connection string
|
||||
bool badConnectionString = false;
|
||||
for (var I = 0; I < argsAsList.Count - 1; ++I) {
|
||||
var lowerarg = argsAsList[I].ToLower();
|
||||
if (lowerarg == "-c" || lowerarg == "--connect")
|
||||
{
|
||||
var connectionString = argsAsList[I + 1];
|
||||
var splits = connectionString.Split('@');
|
||||
var userpass = splits[0].Split(':');
|
||||
if (splits.Length != 2 || userpass.Length != 2)
|
||||
{
|
||||
badConnectionString = true;
|
||||
break;
|
||||
}
|
||||
var addrport = splits[1].Split(':');
|
||||
if (addrport.Length != 2)
|
||||
{
|
||||
badConnectionString = true;
|
||||
break;
|
||||
}
|
||||
var username = userpass[0];
|
||||
var password = userpass[1];
|
||||
var address = addrport[0];
|
||||
ushort port;
|
||||
try
|
||||
{
|
||||
port = Convert.ToUInt16(addrport[1]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
badConnectionString = true;
|
||||
break;
|
||||
}
|
||||
if(String.IsNullOrWhiteSpace(username) || String.IsNullOrWhiteSpace(password) || String.IsNullOrWhiteSpace(address))
|
||||
{
|
||||
badConnectionString = true;
|
||||
break;
|
||||
}
|
||||
argsAsList.RemoveAt(I);
|
||||
argsAsList.RemoveAt(I);
|
||||
Server.SetRemoteLoginInformation(address, port, username, password);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (badConnectionString)
|
||||
{
|
||||
Console.WriteLine("Remote connection usage: <-c/--connect> username:password@address:port");
|
||||
return ExitCode.BadCommand;
|
||||
}
|
||||
|
||||
var res = Server.VerifyConnection();
|
||||
if (res != null)
|
||||
{
|
||||
Console.WriteLine("Unable to connect to service: " + res);
|
||||
Console.WriteLine("Remote connection usage: <-c/--connect> username:password@address:port");
|
||||
return ExitCode.ConnectionError;
|
||||
}
|
||||
|
||||
if (!Server.Authenticate())
|
||||
{
|
||||
Console.WriteLine("Authentication error: Username/password/windows identity is not authorized!");
|
||||
return ExitCode.ConnectionError;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return new CLICommand().DoRun(argsAsList);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Error: " + e.ToString());
|
||||
return ExitCode.ConnectionError;
|
||||
};
|
||||
}
|
||||
public static string ReadLineSecure()
|
||||
{
|
||||
string result = "";
|
||||
while (true)
|
||||
{
|
||||
ConsoleKeyInfo i = Console.ReadKey(true);
|
||||
if (i.Key == ConsoleKey.Enter)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (i.Key == ConsoleKey.Backspace)
|
||||
{
|
||||
if (result.Length > 0)
|
||||
{
|
||||
result = result.Substring(0, result.Length - 1);
|
||||
Console.Write("\b \b");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result += i.KeyChar;
|
||||
Console.Write("*");
|
||||
}
|
||||
}
|
||||
Console.WriteLine();
|
||||
return result;
|
||||
}
|
||||
|
||||
static string AcceptedBadCert;
|
||||
static bool BadCertificateInteractive(string message)
|
||||
{
|
||||
if (AcceptedBadCert == message)
|
||||
return true;
|
||||
Console.WriteLine(message);
|
||||
Console.Write("Do you wish to continue? NOT RECCOMENDED! (y/N): ");
|
||||
var result = Console.ReadLine().Trim().ToLower();
|
||||
if (result == "y" || result == "yes")
|
||||
{
|
||||
AcceptedBadCert = message;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int Main(string[] args)
|
||||
{
|
||||
Command.OutputProcVar.Value = Console.WriteLine;
|
||||
if (args.Length != 0)
|
||||
{
|
||||
Server.SetBadCertificateHandler((message) => {
|
||||
foreach (var I in args)
|
||||
if (I.ToLower() == "--disable-ssl-verification") //im just not even going to document this because i hate it so much
|
||||
return true;
|
||||
return false;
|
||||
});
|
||||
//allow self signed certs in debug mode
|
||||
return (int)RunCommandLine(new List<string>(args));
|
||||
}
|
||||
|
||||
Server.SetBadCertificateHandler(BadCertificateInteractive);
|
||||
|
||||
Console.WriteLine("Type 'remote' to connect to a remote service");
|
||||
//interactive mode
|
||||
while (true)
|
||||
{
|
||||
Console.Write("Enter command: ");
|
||||
var NextCommand = Console.ReadLine();
|
||||
switch (NextCommand.ToLower())
|
||||
{
|
||||
case "remote":
|
||||
Console.Write("Enter server address: ");
|
||||
var address = Console.ReadLine();
|
||||
Console.Write("Enter server port: ");
|
||||
ushort port;
|
||||
try{
|
||||
port = Convert.ToUInt16(Console.ReadLine());
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("Error: Bad port!");
|
||||
break;
|
||||
}
|
||||
Console.Write("Enter username: ");
|
||||
var username = Console.ReadLine();
|
||||
Console.Write("Enter password: ");
|
||||
var password = ReadLineSecure();
|
||||
Server.SetRemoteLoginInformation(address, port, username, password);
|
||||
var res = Server.VerifyConnection();
|
||||
if (res != null)
|
||||
{
|
||||
Console.WriteLine("Unable to connect: " + res);
|
||||
Server.MakeLocalConnection();
|
||||
}
|
||||
else if (!Server.Authenticate())
|
||||
{
|
||||
Console.WriteLine("Authentication error: Username/password/windows identity is not authorized! Returning to local mode...");
|
||||
Server.MakeLocalConnection();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Connected remotely");
|
||||
Console.WriteLine("Type 'disconnect' to return to local mode");
|
||||
}
|
||||
break;
|
||||
case "disconnect":
|
||||
Server.MakeLocalConnection();
|
||||
Console.WriteLine("Switch to local mode");
|
||||
break;
|
||||
case "quit":
|
||||
case "exit":
|
||||
return (int)ExitCode.Normal;
|
||||
#if DEBUG
|
||||
case "debug-upgrade":
|
||||
Server.GetComponent<ITGSService>().PrepareForUpdate();
|
||||
return (int)ExitCode.Normal;
|
||||
#endif
|
||||
default:
|
||||
//linq voodoo to get quoted strings
|
||||
var formattedCommand = NextCommand.Split('"')
|
||||
.Select((element, index) => index % 2 == 0 // If even index
|
||||
? element.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) // Split the item
|
||||
: new string[] { element }) // Keep the entire item
|
||||
.SelectMany(element => element).ToList();
|
||||
|
||||
formattedCommand = formattedCommand.Select(x => x.Trim()).ToList();
|
||||
formattedCommand.Remove("");
|
||||
RunCommandLine(formattedCommand);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using TGServiceInterface;
|
||||
|
||||
namespace TGCommandLine
|
||||
{
|
||||
|
||||
class Program
|
||||
{
|
||||
static ExitCode RunCommandLine(IList<string> argsAsList)
|
||||
{
|
||||
//first lookup the connection string
|
||||
bool badConnectionString = false;
|
||||
for (var I = 0; I < argsAsList.Count - 1; ++I) {
|
||||
var lowerarg = argsAsList[I].ToLower();
|
||||
if (lowerarg == "-c" || lowerarg == "--connect")
|
||||
{
|
||||
var connectionString = argsAsList[I + 1];
|
||||
var splits = connectionString.Split('@');
|
||||
var userpass = splits[0].Split(':');
|
||||
if (splits.Length != 2 || userpass.Length != 2)
|
||||
{
|
||||
badConnectionString = true;
|
||||
break;
|
||||
}
|
||||
var addrport = splits[1].Split(':');
|
||||
if (addrport.Length != 2)
|
||||
{
|
||||
badConnectionString = true;
|
||||
break;
|
||||
}
|
||||
var username = userpass[0];
|
||||
var password = userpass[1];
|
||||
var address = addrport[0];
|
||||
ushort port;
|
||||
try
|
||||
{
|
||||
port = Convert.ToUInt16(addrport[1]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
badConnectionString = true;
|
||||
break;
|
||||
}
|
||||
if(String.IsNullOrWhiteSpace(username) || String.IsNullOrWhiteSpace(password) || String.IsNullOrWhiteSpace(address))
|
||||
{
|
||||
badConnectionString = true;
|
||||
break;
|
||||
}
|
||||
argsAsList.RemoveAt(I);
|
||||
argsAsList.RemoveAt(I);
|
||||
Server.SetRemoteLoginInformation(address, port, username, password);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (badConnectionString)
|
||||
{
|
||||
Console.WriteLine("Remote connection usage: <-c/--connect> username:password@address:port");
|
||||
return ExitCode.BadCommand;
|
||||
}
|
||||
|
||||
var res = Server.VerifyConnection();
|
||||
if (res != null)
|
||||
{
|
||||
Console.WriteLine("Unable to connect to service: " + res);
|
||||
Console.WriteLine("Remote connection usage: <-c/--connect> username:password@address:port");
|
||||
return ExitCode.ConnectionError;
|
||||
}
|
||||
|
||||
if (!Server.Authenticate())
|
||||
{
|
||||
Console.WriteLine("Authentication error: Username/password/windows identity is not authorized!");
|
||||
return ExitCode.ConnectionError;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return new CLICommand().DoRun(argsAsList);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Error: " + e.ToString());
|
||||
return ExitCode.ConnectionError;
|
||||
};
|
||||
}
|
||||
public static string ReadLineSecure()
|
||||
{
|
||||
string result = "";
|
||||
while (true)
|
||||
{
|
||||
ConsoleKeyInfo i = Console.ReadKey(true);
|
||||
if (i.Key == ConsoleKey.Enter)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (i.Key == ConsoleKey.Backspace)
|
||||
{
|
||||
if (result.Length > 0)
|
||||
{
|
||||
result = result.Substring(0, result.Length - 1);
|
||||
Console.Write("\b \b");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result += i.KeyChar;
|
||||
Console.Write("*");
|
||||
}
|
||||
}
|
||||
Console.WriteLine();
|
||||
return result;
|
||||
}
|
||||
|
||||
static string AcceptedBadCert;
|
||||
static bool BadCertificateInteractive(string message)
|
||||
{
|
||||
if (AcceptedBadCert == message)
|
||||
return true;
|
||||
Console.WriteLine(message);
|
||||
Console.Write("Do you wish to continue? NOT RECCOMENDED! (y/N): ");
|
||||
var result = Console.ReadLine().Trim().ToLower();
|
||||
if (result == "y" || result == "yes")
|
||||
{
|
||||
AcceptedBadCert = message;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int Main(string[] args)
|
||||
{
|
||||
Command.OutputProcVar.Value = Console.WriteLine;
|
||||
if (args.Length != 0)
|
||||
{
|
||||
Server.SetBadCertificateHandler((message) => {
|
||||
foreach (var I in args)
|
||||
if (I.ToLower() == "--disable-ssl-verification") //im just not even going to document this because i hate it so much
|
||||
return true;
|
||||
return false;
|
||||
});
|
||||
//allow self signed certs in debug mode
|
||||
return (int)RunCommandLine(new List<string>(args));
|
||||
}
|
||||
|
||||
Server.SetBadCertificateHandler(BadCertificateInteractive);
|
||||
|
||||
Console.WriteLine("Type 'remote' to connect to a remote service");
|
||||
//interactive mode
|
||||
while (true)
|
||||
{
|
||||
Console.Write("Enter command: ");
|
||||
var NextCommand = Console.ReadLine();
|
||||
switch (NextCommand.ToLower())
|
||||
{
|
||||
case "remote":
|
||||
Console.Write("Enter server address: ");
|
||||
var address = Console.ReadLine();
|
||||
Console.Write("Enter server port: ");
|
||||
ushort port;
|
||||
try{
|
||||
port = Convert.ToUInt16(Console.ReadLine());
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("Error: Bad port!");
|
||||
break;
|
||||
}
|
||||
Console.Write("Enter username: ");
|
||||
var username = Console.ReadLine();
|
||||
Console.Write("Enter password: ");
|
||||
var password = ReadLineSecure();
|
||||
Server.SetRemoteLoginInformation(address, port, username, password);
|
||||
var res = Server.VerifyConnection();
|
||||
if (res != null)
|
||||
{
|
||||
Console.WriteLine("Unable to connect: " + res);
|
||||
Server.MakeLocalConnection();
|
||||
}
|
||||
else if (!Server.Authenticate())
|
||||
{
|
||||
Console.WriteLine("Authentication error: Username/password/windows identity is not authorized! Returning to local mode...");
|
||||
Server.MakeLocalConnection();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Connected remotely");
|
||||
Console.WriteLine("Type 'disconnect' to return to local mode");
|
||||
}
|
||||
break;
|
||||
case "disconnect":
|
||||
Server.MakeLocalConnection();
|
||||
Console.WriteLine("Switch to local mode");
|
||||
break;
|
||||
case "quit":
|
||||
case "exit":
|
||||
return (int)ExitCode.Normal;
|
||||
#if DEBUG
|
||||
case "debug-upgrade":
|
||||
Server.GetComponent<ITGSService>().PrepareForUpdate();
|
||||
return (int)ExitCode.Normal;
|
||||
#endif
|
||||
default:
|
||||
//linq voodoo to get quoted strings
|
||||
var formattedCommand = NextCommand.Split('"')
|
||||
.Select((element, index) => index % 2 == 0 // If even index
|
||||
? element.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) // Split the item
|
||||
: new string[] { element }) // Keep the entire item
|
||||
.SelectMany(element => element).ToList();
|
||||
|
||||
formattedCommand = formattedCommand.Select(x => x.Trim()).ToList();
|
||||
formattedCommand.Remove("");
|
||||
RunCommandLine(formattedCommand);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user