Skip to content

Instantly share code, notes, and snippets.

@Cyberboss
Created October 20, 2018 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cyberboss/adfe6b4c2ac263096ea88ea5e2839d28 to your computer and use it in GitHub Desktop.
Save Cyberboss/adfe6b4c2ac263096ea88ea5e2839d28 to your computer and use it in GitHub Desktop.
TGS4 diagnostic quickie
using System;
using System.IO;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Tgstation.Server.Api;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Client;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
try
{
Console.WriteLine("This is a hack so just restart if you fuck up");
Console.Write("Enter your server address:port: ");
var address = new Uri(Console.ReadLine());
Console.Write("Enter your username: ");
var username = Console.ReadLine();
Console.Write("Enter your password (too lazy to censor this sorry, leave blank for default admin): ");
var password = Console.ReadLine();
if (password.Length == 0)
password = User.DefaultAdminPassword;
Console.WriteLine("Using API version: " + typeof(ApiHeaders).Assembly.GetName().Version);
Console.WriteLine("Connecting and logging in (timeout 10s)...");
var clientFac = new ServerClientFactory(new ProductHeaderValue("DiagnosticClient", new Version(1, 0).ToString()));
var client = await clientFac.CreateServerClient(address, username, password, TimeSpan.FromSeconds(10), default).ConfigureAwait(false);
Console.WriteLine("Recieved token: " + client.Token.Bearer);
Console.WriteLine("Expires at: " + client.Token.ExpiresAt);
Console.WriteLine("Querying server info...");
var info = await client.Version(default).ConfigureAwait(false);
Console.WriteLine("Server version: " + info.Version);
Console.WriteLine("Server API version: " + info.ApiVersion);
Console.WriteLine("Reading current user info...");
var user = await client.Users.Read(default).ConfigureAwait(false);
Console.WriteLine("User ID: " + user.Id);
Console.WriteLine("User name: " + user.Name);
Console.WriteLine("User SID: " + user.SystemIdentifier);
Console.WriteLine("Created by ID: " + user.CreatedBy?.Id);
Console.WriteLine("Created time: " + user.CreatedAt);
Console.WriteLine("Admin rights: " + user.AdministrationRights);
Console.WriteLine("Instance manager rights: " + user.InstanceManagerRights);
Console.WriteLine("Listing instances...");
var instances = await client.Instances.List(default).ConfigureAwait(false);
Console.WriteLine("Recieved " + instances.Count + " instances");
foreach(var I in instances)
{
Console.WriteLine("Diagnosing instance ID: " + I.Id);
Console.WriteLine("Name: " + I.Name);
Console.WriteLine("Path: " + I.Path);
Console.WriteLine("Online: " + I.Online);
if (!I.Online.Value)
{
Console.WriteLine("Not digging further due to offline (as no client should)");
continue;
}
var iClient = client.Instances.CreateClient(I);
Console.WriteLine("Querying dream daemon controller...");
var dd = await iClient.DreamDaemon.Read(default).ConfigureAwait(false);
Console.WriteLine("Server online: " + dd.Running);
Console.WriteLine("Public port: " + dd.PrimaryPort);
Console.WriteLine("Okay i really expected this to error but if it isn't let me know I need more info");
}
}
catch (Exception e)
{
Console.WriteLine("Encountered exception!");
Console.WriteLine(e.ToString());
if (e is ServerErrorException se)
{
var tmpFile = Path.GetTempFileName();
Console.WriteLine("Dumping error page to: " + tmpFile);
await File.WriteAllTextAsync(tmpFile, se.Html).ConfigureAwait(false);
}
}
finally
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment