From 20645316c2d745b63a9b5dad362561a17df77821 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 10:58:03 -0400 Subject: [PATCH 01/26] Fix JobException's namespace --- .../Components/Repository/CredentialsProvider.cs | 1 + src/Tgstation.Server.Host/Components/Repository/Repository.cs | 1 + src/Tgstation.Server.Host/Core/JobException.cs | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Host/Components/Repository/CredentialsProvider.cs b/src/Tgstation.Server.Host/Components/Repository/CredentialsProvider.cs index 38dd416d11..23c2cdb7f2 100644 --- a/src/Tgstation.Server.Host/Components/Repository/CredentialsProvider.cs +++ b/src/Tgstation.Server.Host/Components/Repository/CredentialsProvider.cs @@ -2,6 +2,7 @@ using LibGit2Sharp.Handlers; using Microsoft.Extensions.Logging; using System; +using Tgstation.Server.Host.Core; namespace Tgstation.Server.Host.Components.Repository { diff --git a/src/Tgstation.Server.Host/Components/Repository/Repository.cs b/src/Tgstation.Server.Host/Components/Repository/Repository.cs index 1365a98810..bac6bdafa5 100644 --- a/src/Tgstation.Server.Host/Components/Repository/Repository.cs +++ b/src/Tgstation.Server.Host/Components/Repository/Repository.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using Tgstation.Server.Api.Models; +using Tgstation.Server.Host.Core; using Tgstation.Server.Host.IO; namespace Tgstation.Server.Host.Components.Repository diff --git a/src/Tgstation.Server.Host/Core/JobException.cs b/src/Tgstation.Server.Host/Core/JobException.cs index 88877c9548..fbf4449365 100644 --- a/src/Tgstation.Server.Host/Core/JobException.cs +++ b/src/Tgstation.Server.Host/Core/JobException.cs @@ -1,6 +1,6 @@ using System; -namespace Tgstation.Server.Host +namespace Tgstation.Server.Host.Core { /// /// Operation exceptions thrown from the context of a From 459ee499e6f80572a7d6d8421be9f64606c70fb1 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 10:59:34 -0400 Subject: [PATCH 02/26] Add missing ArgumentNullException --- src/Tgstation.Server.Host/ServerFactory.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Host/ServerFactory.cs b/src/Tgstation.Server.Host/ServerFactory.cs index b8b34e0553..cefdfd8897 100644 --- a/src/Tgstation.Server.Host/ServerFactory.cs +++ b/src/Tgstation.Server.Host/ServerFactory.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore; +using System; namespace Tgstation.Server.Host { @@ -6,6 +7,6 @@ namespace Tgstation.Server.Host public sealed class ServerFactory : IServerFactory { /// - public IServer CreateServer(string[] args, string updatePath) => new Server(WebHost.CreateDefaultBuilder(args), updatePath); + public IServer CreateServer(string[] args, string updatePath) => new Server(WebHost.CreateDefaultBuilder(args ?? throw new ArgumentNullException(nameof(args))), updatePath); } } From b1e49e32dec6c8fd22522ebdc755df70f1364888 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:04:37 -0400 Subject: [PATCH 03/26] Add TestServerFactory --- .../TestServerFactory.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/Tgstation.Server.Host.Tests/TestServerFactory.cs diff --git a/tests/Tgstation.Server.Host.Tests/TestServerFactory.cs b/tests/Tgstation.Server.Host.Tests/TestServerFactory.cs new file mode 100644 index 0000000000..1334ae87c9 --- /dev/null +++ b/tests/Tgstation.Server.Host.Tests/TestServerFactory.cs @@ -0,0 +1,32 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace Tgstation.Server.Host.Tests +{ + /// + /// Tests for + /// + [TestClass] + public sealed class TestServerFactory + { + [TestMethod] + public void TestWorksWithoutUpdatePath() + { + var factory = new ServerFactory(); + + Assert.ThrowsException(() => factory.CreateServer(null, null)); + factory.CreateServer(Array.Empty(), null); + } + + [TestMethod] + public void TestWorksWithUpdatePath() + { + var factory = new ServerFactory(); + const string Path = "/test"; + + Assert.ThrowsException(() => factory.CreateServer(null, null)); + Assert.ThrowsException(() => factory.CreateServer(null, Path)); + factory.CreateServer(Array.Empty(), Path); + } + } +} From f923b9dbc2304a37f6f6cdb9afe16e38b6cb3ba0 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:05:12 -0400 Subject: [PATCH 04/26] Remove unecessary return marshalling --- src/Tgstation.Server.Host/NativeMethods.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Tgstation.Server.Host/NativeMethods.cs b/src/Tgstation.Server.Host/NativeMethods.cs index afe778312a..18124400ee 100644 --- a/src/Tgstation.Server.Host/NativeMethods.cs +++ b/src/Tgstation.Server.Host/NativeMethods.cs @@ -36,7 +36,6 @@ namespace Tgstation.Server.Host /// See https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-enumchildwindows /// [DllImport("user32")] - [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam); /// From 924b5299ce20d0adc21f5b2c3ba7ac21baf51513 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:07:57 -0400 Subject: [PATCH 05/26] Removes DEBUG restriction on SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE --- src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs index 0fbacb6341..43b3fe7d31 100644 --- a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs +++ b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs @@ -15,10 +15,7 @@ namespace Tgstation.Server.Host.IO public Task CreateSymbolicLink(string targetPath, string linkPath, CancellationToken cancellationToken) => Task.Factory.StartNew(() => { //check if its not a file - var flags = File.Exists(targetPath) ? 0 : 1; //SYMBOLIC_LINK_FLAG_DIRECTORY -#if DEBUG - flags |= 2; //SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE on win10 1607+ developer mode -#endif + var flags = File.Exists(targetPath) ? 0 : 3; //SYMBOLIC_LINK_FLAG_DIRECTORY | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE on win10 1607+ developer mode cancellationToken.ThrowIfCancellationRequested(); if (!NativeMethods.CreateSymbolicLink(linkPath, targetPath, flags)) From e3bb2763b110eb7119b419574136ac3fc90f6454 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:19:32 -0400 Subject: [PATCH 06/26] Add ArgumentNullException to WindowsSymlinkFactory --- src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs index 43b3fe7d31..ded819552f 100644 --- a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs +++ b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using System; +using System.ComponentModel; using System.IO; using System.Runtime.InteropServices; using System.Threading; @@ -14,6 +15,11 @@ namespace Tgstation.Server.Host.IO /// public Task CreateSymbolicLink(string targetPath, string linkPath, CancellationToken cancellationToken) => Task.Factory.StartNew(() => { + if (targetPath == null) + throw new ArgumentNullException(nameof(targetPath)); + if (linkPath == null) + throw new ArgumentNullException(nameof(linkPath)); + //check if its not a file var flags = File.Exists(targetPath) ? 0 : 3; //SYMBOLIC_LINK_FLAG_DIRECTORY | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE on win10 1607+ developer mode From d69886bbba3e6368b75c49bac65aacee866ef317 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:28:51 -0400 Subject: [PATCH 07/26] Add TestWindowsSymlinkFactory --- .../IO/TestWindowsSymlinkFactory.cs | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs diff --git a/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs b/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs new file mode 100644 index 0000000000..c799ff19f3 --- /dev/null +++ b/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs @@ -0,0 +1,57 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.ComponentModel; +using System.IO; +using System.Runtime.InteropServices; +using System.Threading.Tasks; + +namespace Tgstation.Server.Host.IO.Tests +{ + [TestClass] + public sealed class TestWindowsSymlinkFactory + { + [TestMethod] + public async Task TestWorks() + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + Assert.Inconclusive("Windows only test"); + + var factory = new WindowsSymlinkFactory(); + + const string Text = "Hello world"; + string f2 = null; + var f1 = Path.GetTempFileName(); + try + { + f2 = f1 + ".linked"; + File.WriteAllText(f1, Text); + + await Assert.ThrowsExceptionAsync(() => factory.CreateSymbolicLink(null, null, default)).ConfigureAwait(false); + await Assert.ThrowsExceptionAsync(() => factory.CreateSymbolicLink(f1, null, default)).ConfigureAwait(false); + + await factory.CreateSymbolicLink(f1, f2, default).ConfigureAwait(false); + + var f2Contents = File.ReadAllText(f2); + Assert.AreEqual(Text, f2Contents); + } + finally + { + File.Delete(f2); + File.Delete(f1); + } + } + + [TestMethod] + public async Task TestFailsProperly() + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + Assert.Inconclusive("Windows only test"); + + var factory = new WindowsSymlinkFactory(); + + const string BadPath = "C:/?><:{ }"; + + await Assert.ThrowsExceptionAsync(() => factory.CreateSymbolicLink(BadPath, BadPath, default)).ConfigureAwait(false); + } + } +} From bd56de8ad2201d226f5078542dc5416be025d1de Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:29:39 -0400 Subject: [PATCH 08/26] Adds argument null exceptions to PosixSymlinkFactory --- src/Tgstation.Server.Host/IO/PosixSymlinkFactory.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Tgstation.Server.Host/IO/PosixSymlinkFactory.cs b/src/Tgstation.Server.Host/IO/PosixSymlinkFactory.cs index 90a00410be..4499d5ac13 100644 --- a/src/Tgstation.Server.Host/IO/PosixSymlinkFactory.cs +++ b/src/Tgstation.Server.Host/IO/PosixSymlinkFactory.cs @@ -1,4 +1,5 @@ using Mono.Unix; +using System; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -13,6 +14,11 @@ namespace Tgstation.Server.Host.IO /// public Task CreateSymbolicLink(string targetPath, string linkPath, CancellationToken cancellationToken) => Task.Factory.StartNew(() => { + if (targetPath == null) + throw new ArgumentNullException(nameof(targetPath)); + if (linkPath == null) + throw new ArgumentNullException(nameof(linkPath)); + UnixFileSystemInfo fsInfo; var isFile = File.Exists(targetPath); cancellationToken.ThrowIfCancellationRequested(); From 2bc0eaee7ba64c3416e3387b6d24fd85301540a7 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:37:24 -0400 Subject: [PATCH 09/26] Fix unprivileged file symlink creation --- src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs index ded819552f..d6b0da449c 100644 --- a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs +++ b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs @@ -21,7 +21,7 @@ namespace Tgstation.Server.Host.IO throw new ArgumentNullException(nameof(linkPath)); //check if its not a file - var flags = File.Exists(targetPath) ? 0 : 3; //SYMBOLIC_LINK_FLAG_DIRECTORY | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE on win10 1607+ developer mode + var flags = File.Exists(targetPath) ? 2 : 3; //SYMBOLIC_LINK_FLAG_DIRECTORY and SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE on win10 1607+ developer mode cancellationToken.ThrowIfCancellationRequested(); if (!NativeMethods.CreateSymbolicLink(linkPath, targetPath, flags)) From 7c289d3cb694e29dbca7629db081a740b2d3be40 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:38:33 -0400 Subject: [PATCH 10/26] Update documentation regarding admin mode on Windows --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e4cd2d0800..6c02c8f7ca 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ Note that the ratio of application installations to databases is 1:1. Do not att For the Windows service version start the `tgstation-server-4` service. If it fails to start, check the Windows event log under Windows/Application for entries from tgstation-server-4 for errors. -For the console version run `dotnet Tgstation.Server.Host.Console.dll` in the installation directory. The `tgs.bat` and `tgs.sh` shell scripts are shortcuts for this. If on Windows and you wish to install byond versions >= 512.1427 you must do this as admin to give the server permission to install the required DirectX dependency +For the console version run `dotnet Tgstation.Server.Host.Console.dll` in the installation directory. The `tgs.bat` and `tgs.sh` shell scripts are shortcuts for this. If on Windows, you must do this as admin to give the server permission to install the required DirectX dependency for BYOND as well as create symlinks. For the docker version run `docker start tgs` From 68e7932e68cbd252d138d40d0c851dac7dbe7ad8 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:41:25 -0400 Subject: [PATCH 11/26] Improve symlink factory tests --- .../IO/TestSymlinkFactory.cs | 103 ++++++++++++++++++ .../IO/TestWindowsSymlinkFactory.cs | 57 ---------- 2 files changed, 103 insertions(+), 57 deletions(-) create mode 100644 tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs delete mode 100644 tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs diff --git a/tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs b/tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs new file mode 100644 index 0000000000..d21955f066 --- /dev/null +++ b/tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs @@ -0,0 +1,103 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.ComponentModel; +using System.IO; +using System.Runtime.InteropServices; +using System.Threading.Tasks; + +namespace Tgstation.Server.Host.IO.Tests +{ + [TestClass] + public sealed class TestSymlinkFactory + { + ISymlinkFactory symlinkFactory; + + [TestInitialize] + public void SelectFactory() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + symlinkFactory = new WindowsSymlinkFactory(); + else + symlinkFactory = new PosixSymlinkFactory(); + } + + [TestMethod] + public async Task TestFileWorks() + { + const string Text = "Hello world"; + string f2 = null; + var f1 = Path.GetTempFileName(); + try + { + f2 = f1 + ".linked"; + File.WriteAllText(f1, Text); + + await Assert.ThrowsExceptionAsync(() => symlinkFactory.CreateSymbolicLink(null, null, default)).ConfigureAwait(false); + await Assert.ThrowsExceptionAsync(() => symlinkFactory.CreateSymbolicLink(f1, null, default)).ConfigureAwait(false); + + await symlinkFactory.CreateSymbolicLink(f1, f2, default).ConfigureAwait(false); + Assert.IsTrue(File.Exists(f2)); + + var f2Contents = File.ReadAllText(f2); + Assert.AreEqual(Text, f2Contents); + } + finally + { + File.Delete(f2); + File.Delete(f1); + } + } + + [TestMethod] + public async Task TestDirectoryWorks() + { + const string FileName = "TestFile.txt"; + const string Text = "Hello world"; + string f2 = null; + var f1 = Path.GetTempFileName(); + File.Delete(f1); + Directory.CreateDirectory(f1); + try + { + f2 = f1 + ".linked"; + var p1 = Path.Combine(f1, FileName); + File.WriteAllText(p1, Text); + + await Assert.ThrowsExceptionAsync(() => symlinkFactory.CreateSymbolicLink(null, null, default)).ConfigureAwait(false); + await Assert.ThrowsExceptionAsync(() => symlinkFactory.CreateSymbolicLink(f1, null, default)).ConfigureAwait(false); + + await symlinkFactory.CreateSymbolicLink(f1, f2, default).ConfigureAwait(false); + + var p2 = Path.Combine(f2, FileName); + Assert.IsTrue(File.Exists(p2)); + + var f2Contents = File.ReadAllText(p2); + Assert.AreEqual(Text, f2Contents); + + File.Delete(p2); + Assert.IsFalse(File.Exists(p1)); + } + finally + { + Directory.Delete(f2, true); + Directory.Delete(f1, true); + } + } + + [TestMethod] + public async Task TestFailsProperly() + { + const string BadPath = "/../../?>O(UF+}P{{??>/////"; + + try + { + await symlinkFactory.CreateSymbolicLink(BadPath, BadPath, default).ConfigureAwait(false); + } + catch + { + return; + } + Assert.Fail("No exception thrown!"); + } + } +} diff --git a/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs b/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs deleted file mode 100644 index c799ff19f3..0000000000 --- a/tests/Tgstation.Server.Host.Tests/IO/TestWindowsSymlinkFactory.cs +++ /dev/null @@ -1,57 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; -using System.ComponentModel; -using System.IO; -using System.Runtime.InteropServices; -using System.Threading.Tasks; - -namespace Tgstation.Server.Host.IO.Tests -{ - [TestClass] - public sealed class TestWindowsSymlinkFactory - { - [TestMethod] - public async Task TestWorks() - { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - Assert.Inconclusive("Windows only test"); - - var factory = new WindowsSymlinkFactory(); - - const string Text = "Hello world"; - string f2 = null; - var f1 = Path.GetTempFileName(); - try - { - f2 = f1 + ".linked"; - File.WriteAllText(f1, Text); - - await Assert.ThrowsExceptionAsync(() => factory.CreateSymbolicLink(null, null, default)).ConfigureAwait(false); - await Assert.ThrowsExceptionAsync(() => factory.CreateSymbolicLink(f1, null, default)).ConfigureAwait(false); - - await factory.CreateSymbolicLink(f1, f2, default).ConfigureAwait(false); - - var f2Contents = File.ReadAllText(f2); - Assert.AreEqual(Text, f2Contents); - } - finally - { - File.Delete(f2); - File.Delete(f1); - } - } - - [TestMethod] - public async Task TestFailsProperly() - { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - Assert.Inconclusive("Windows only test"); - - var factory = new WindowsSymlinkFactory(); - - const string BadPath = "C:/?><:{ }"; - - await Assert.ThrowsExceptionAsync(() => factory.CreateSymbolicLink(BadPath, BadPath, default)).ConfigureAwait(false); - } - } -} From 55752de18c731f0d9ff2ec1e8d7048835d23f629 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:42:05 -0400 Subject: [PATCH 12/26] Remove an unused using --- src/Tgstation.Server.Host/Core/RestartRegistration.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Tgstation.Server.Host/Core/RestartRegistration.cs b/src/Tgstation.Server.Host/Core/RestartRegistration.cs index 8df2cf3ad2..8e6fca271e 100644 --- a/src/Tgstation.Server.Host/Core/RestartRegistration.cs +++ b/src/Tgstation.Server.Host/Core/RestartRegistration.cs @@ -1,5 +1,4 @@ using System; -using Tgstation.Server.Host.Core; namespace Tgstation.Server.Host.Core { From fcf002b25a3481e0994f74d8ee43cfac1ba9c5cf Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:42:55 -0400 Subject: [PATCH 13/26] Fix ServerSideModifications file location --- .../{Core => Components/StaticFiles}/ServerSideModifications.cs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Tgstation.Server.Host/{Core => Components/StaticFiles}/ServerSideModifications.cs (100%) diff --git a/src/Tgstation.Server.Host/Core/ServerSideModifications.cs b/src/Tgstation.Server.Host/Components/StaticFiles/ServerSideModifications.cs similarity index 100% rename from src/Tgstation.Server.Host/Core/ServerSideModifications.cs rename to src/Tgstation.Server.Host/Components/StaticFiles/ServerSideModifications.cs From f2dec6ff43b8fcd32a1ec3389f8ae1a59a246cbe Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:44:49 -0400 Subject: [PATCH 14/26] Move PostWriteHandlers to the IO namespace --- src/Tgstation.Server.Host/{Core => IO}/IPostWriteHandler.cs | 2 +- src/Tgstation.Server.Host/{Core => IO}/PosixPostWriteHandler.cs | 2 +- .../{Core => IO}/WindowsPostWriteHandler.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/Tgstation.Server.Host/{Core => IO}/IPostWriteHandler.cs (86%) rename src/Tgstation.Server.Host/{Core => IO}/PosixPostWriteHandler.cs (94%) rename src/Tgstation.Server.Host/{Core => IO}/WindowsPostWriteHandler.cs (84%) diff --git a/src/Tgstation.Server.Host/Core/IPostWriteHandler.cs b/src/Tgstation.Server.Host/IO/IPostWriteHandler.cs similarity index 86% rename from src/Tgstation.Server.Host/Core/IPostWriteHandler.cs rename to src/Tgstation.Server.Host/IO/IPostWriteHandler.cs index 7209b44761..7686d742c8 100644 --- a/src/Tgstation.Server.Host/Core/IPostWriteHandler.cs +++ b/src/Tgstation.Server.Host/IO/IPostWriteHandler.cs @@ -1,4 +1,4 @@ -namespace Tgstation.Server.Host.Core +namespace Tgstation.Server.Host.IO { interface IPostWriteHandler { diff --git a/src/Tgstation.Server.Host/Core/PosixPostWriteHandler.cs b/src/Tgstation.Server.Host/IO/PosixPostWriteHandler.cs similarity index 94% rename from src/Tgstation.Server.Host/Core/PosixPostWriteHandler.cs rename to src/Tgstation.Server.Host/IO/PosixPostWriteHandler.cs index d121d2bca8..d656e0fa99 100644 --- a/src/Tgstation.Server.Host/Core/PosixPostWriteHandler.cs +++ b/src/Tgstation.Server.Host/IO/PosixPostWriteHandler.cs @@ -1,7 +1,7 @@ using Mono.Unix; using Mono.Unix.Native; -namespace Tgstation.Server.Host.Core +namespace Tgstation.Server.Host.IO { /// /// for POSIX systems diff --git a/src/Tgstation.Server.Host/Core/WindowsPostWriteHandler.cs b/src/Tgstation.Server.Host/IO/WindowsPostWriteHandler.cs similarity index 84% rename from src/Tgstation.Server.Host/Core/WindowsPostWriteHandler.cs rename to src/Tgstation.Server.Host/IO/WindowsPostWriteHandler.cs index 42ed2cdf1b..6d149644fa 100644 --- a/src/Tgstation.Server.Host/Core/WindowsPostWriteHandler.cs +++ b/src/Tgstation.Server.Host/IO/WindowsPostWriteHandler.cs @@ -1,4 +1,4 @@ -namespace Tgstation.Server.Host.Core +namespace Tgstation.Server.Host.IO { /// /// for Windows systems From 54b3caa327e0bae737771717ec06487b0013d861 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:46:47 -0400 Subject: [PATCH 15/26] Document UnrecognizedResponseException --- src/Tgstation.Server.Client/UnrecognizedResponseException.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Tgstation.Server.Client/UnrecognizedResponseException.cs b/src/Tgstation.Server.Client/UnrecognizedResponseException.cs index 24dfa9227d..56c5578f96 100644 --- a/src/Tgstation.Server.Client/UnrecognizedResponseException.cs +++ b/src/Tgstation.Server.Client/UnrecognizedResponseException.cs @@ -5,6 +5,9 @@ using Tgstation.Server.Api.Models; namespace Tgstation.Server.Client { + /// + /// Occurs when a response is received that did not deserialize to one of the expected + /// sealed class UnrecognizedResponseException : ClientException { /// From c1e7c55449b28d3279f939582d11ab693db77b4c Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:48:33 -0400 Subject: [PATCH 16/26] Version bump client to 4.0.1 --- src/Tgstation.Server.Client/Tgstation.Server.Client.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Client/Tgstation.Server.Client.csproj b/src/Tgstation.Server.Client/Tgstation.Server.Client.csproj index 77d2db004c..9ebd873506 100644 --- a/src/Tgstation.Server.Client/Tgstation.Server.Client.csproj +++ b/src/Tgstation.Server.Client/Tgstation.Server.Client.csproj @@ -3,7 +3,7 @@ netstandard2.0 Full - 4.0.0.0 + 4.0.1 true Cyberboss /tg/station 13 From d7f3556e43321d0102ccf7343ed9c420558bc3db Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 11:51:08 -0400 Subject: [PATCH 17/26] Cleanup ByondExecutableLock --- .../Components/Byond/ByondExecutableLock.cs | 19 ++++++++++++++++--- .../Components/Byond/ByondManager.cs | 7 +------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/Byond/ByondExecutableLock.cs b/src/Tgstation.Server.Host/Components/Byond/ByondExecutableLock.cs index 3431ede531..3e33e03e0c 100644 --- a/src/Tgstation.Server.Host/Components/Byond/ByondExecutableLock.cs +++ b/src/Tgstation.Server.Host/Components/Byond/ByondExecutableLock.cs @@ -6,13 +6,26 @@ namespace Tgstation.Server.Host.Components.Byond sealed class ByondExecutableLock : IByondExecutableLock { /// - public Version Version { get; set; } + public Version Version { get; } /// - public string DreamDaemonPath { get; set; } + public string DreamDaemonPath { get; } /// - public string DreamMakerPath { get; set; } + public string DreamMakerPath { get; } + + /// + /// Construct a + /// + /// The value of + /// The value of + /// The value of + public ByondExecutableLock(Version version, string dreamDaemonPath, string dreamMakerPath) + { + Version = version ?? throw new ArgumentNullException(nameof(version)); + dreamDaemonPath = dreamDaemonPath ?? throw new ArgumentNullException(nameof(dreamDaemonPath)); + dreamMakerPath = dreamMakerPath ?? throw new ArgumentNullException(nameof(dreamMakerPath)); + } //at one point in design, byond versions were to delete themselves if they werent the active version //That changed at some point so these functions are intentioanlly left blank diff --git a/src/Tgstation.Server.Host/Components/Byond/ByondManager.cs b/src/Tgstation.Server.Host/Components/Byond/ByondManager.cs index e152c2ddca..8c597e7d23 100644 --- a/src/Tgstation.Server.Host/Components/Byond/ByondManager.cs +++ b/src/Tgstation.Server.Host/Components/Byond/ByondManager.cs @@ -197,12 +197,7 @@ namespace Tgstation.Server.Host.Components.Byond var versionKey = VersionKey(versionToUse); - return new ByondExecutableLock - { - DreamDaemonPath = ioManager.ResolvePath(ioManager.ConcatPath(versionKey, BinPath, byondInstaller.DreamDaemonName)), - DreamMakerPath = ioManager.ResolvePath(ioManager.ConcatPath(versionKey, BinPath, byondInstaller.DreamMakerName)), - Version = versionToUse - }; + return new ByondExecutableLock(versionToUse, ioManager.ResolvePath(ioManager.ConcatPath(versionKey, BinPath, byondInstaller.DreamDaemonName)), ioManager.ResolvePath(ioManager.ConcatPath(versionKey, BinPath, byondInstaller.DreamMakerName))); } /// From 85f4b700f3c4939b55ba37560d57532ced0d5e54 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 12:04:20 -0400 Subject: [PATCH 18/26] Add missing throw from DatabaseContextFactory.UseContext() --- src/Tgstation.Server.Host/Core/DatabaseContextFactory.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Tgstation.Server.Host/Core/DatabaseContextFactory.cs b/src/Tgstation.Server.Host/Core/DatabaseContextFactory.cs index 69c5287aa9..fc23255d12 100644 --- a/src/Tgstation.Server.Host/Core/DatabaseContextFactory.cs +++ b/src/Tgstation.Server.Host/Core/DatabaseContextFactory.cs @@ -28,6 +28,9 @@ namespace Tgstation.Server.Host.Core /// public async Task UseContext(Func operation) { + if (operation == null) + throw new ArgumentNullException(nameof(operation)); + using (var scope = scopeFactory.CreateScope()) await operation(scope.ServiceProvider.GetRequiredService()).ConfigureAwait(false); } From b92f06b7fa74cba020461c75813c24aa8fa69869 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 12:04:42 -0400 Subject: [PATCH 19/26] Add TestDatabaseContextFactory --- .../Core/TestDatabaseContextFactory.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/Tgstation.Server.Host.Tests/Core/TestDatabaseContextFactory.cs diff --git a/tests/Tgstation.Server.Host.Tests/Core/TestDatabaseContextFactory.cs b/tests/Tgstation.Server.Host.Tests/Core/TestDatabaseContextFactory.cs new file mode 100644 index 0000000000..7585c8a300 --- /dev/null +++ b/tests/Tgstation.Server.Host.Tests/Core/TestDatabaseContextFactory.cs @@ -0,0 +1,56 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using System; +using System.Threading.Tasks; +using Tgstation.Server.Host.Models; + +namespace Tgstation.Server.Host.Core.Tests +{ + [TestClass] + public sealed class TestDatabaseContextFactory + { + [TestMethod] + public void TestConstructionThrows() + { + Assert.ThrowsException(() => new DatabaseContextFactory(null)); + var mockProvider = new Mock(); + mockProvider.Setup(x => x.GetService(typeof(IDatabaseContext))).Verifiable(); + var mockScope = new Mock(); + mockScope.SetupGet(x => x.ServiceProvider).Returns(mockProvider.Object).Verifiable(); + var mockScopeFactory = new Mock(); + mockScopeFactory.Setup(x => x.CreateScope()).Returns(mockScope.Object).Verifiable(); + Assert.ThrowsException(() => new DatabaseContextFactory(mockScopeFactory.Object)); + mockScopeFactory.VerifyAll(); + mockScope.VerifyAll(); + mockProvider.VerifyAll(); + } + + [TestMethod] + public async Task TestWorks() + { + var mockDatabase = new Mock(); + var mockProvider = new Mock(); + var mockDbo = mockDatabase.Object; + mockProvider.Setup(x => x.GetService(typeof(IDatabaseContext))).Returns(mockDbo).Verifiable(); + var mockScope = new Mock(); + mockScope.SetupGet(x => x.ServiceProvider).Returns(mockProvider.Object).Verifiable(); + var mockScopeFactory = new Mock(); + mockScopeFactory.Setup(x => x.CreateScope()).Returns(mockScope.Object).Verifiable(); + + var factory = new DatabaseContextFactory(mockScopeFactory.Object); + + await Assert.ThrowsExceptionAsync(() => factory.UseContext(null)).ConfigureAwait(false); + + await factory.UseContext(context => + { + Assert.AreSame(mockDbo, context); + return Task.CompletedTask; + }); + + mockScopeFactory.VerifyAll(); + mockScope.VerifyAll(); + mockProvider.VerifyAll(); + } + } +} From 6516bbc6c2ec5a913226cca6b654cf6df56f6fab Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 12:11:43 -0400 Subject: [PATCH 20/26] Adds TestGitHubClientFactory --- .../Core/TestGitHubClientFactory.cs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/Tgstation.Server.Host.Tests/Core/TestGitHubClientFactory.cs diff --git a/tests/Tgstation.Server.Host.Tests/Core/TestGitHubClientFactory.cs b/tests/Tgstation.Server.Host.Tests/Core/TestGitHubClientFactory.cs new file mode 100644 index 0000000000..dc8cf7b0b4 --- /dev/null +++ b/tests/Tgstation.Server.Host.Tests/Core/TestGitHubClientFactory.cs @@ -0,0 +1,55 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using Octokit; +using System; +using System.Threading.Tasks; + +namespace Tgstation.Server.Host.Core.Tests +{ + [TestClass] + public sealed class TestGitHubClientFactory + { + [TestMethod] + public void TestContruction() => Assert.ThrowsException(() => new GitHubClientFactory(null)); + + [TestMethod] + public async Task TestCreateBasicClient() + { + var mockApp = new Mock(); + mockApp.SetupGet(x => x.Version).Returns(new Version()).Verifiable(); + mockApp.SetupGet(x => x.VersionPrefix).Returns("TGSTests").Verifiable(); + + var factory = new GitHubClientFactory(mockApp.Object); + + var client = factory.CreateClient(); + Assert.IsNotNull(client); + var credentials = await client.Connection.CredentialStore.GetCredentials().ConfigureAwait(false); + + Assert.AreEqual(AuthenticationType.Anonymous, credentials.AuthenticationType); + + + mockApp.VerifyAll(); + } + + [TestMethod] + public async Task TestCreateTokenClient() + { + var mockApp = new Mock(); + mockApp.SetupGet(x => x.Version).Returns(new Version()).Verifiable(); + mockApp.SetupGet(x => x.VersionPrefix).Returns("TGSTests").Verifiable(); + + var factory = new GitHubClientFactory(mockApp.Object); + + Assert.ThrowsException(() => factory.CreateClient(null)); + + var client = factory.CreateClient("asdf"); + Assert.IsNotNull(client); + + var credentials = await client.Connection.CredentialStore.GetCredentials().ConfigureAwait(false); + + Assert.AreEqual(AuthenticationType.Oauth, credentials.AuthenticationType); + + mockApp.VerifyAll(); + } + } +} From 258a24dfcade05bcf37c1cb4274b3337dbc8813b Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 12:14:18 -0400 Subject: [PATCH 21/26] Adds TestJobException --- .../Core/TestJobException.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/Tgstation.Server.Host.Tests/Core/TestJobException.cs diff --git a/tests/Tgstation.Server.Host.Tests/Core/TestJobException.cs b/tests/Tgstation.Server.Host.Tests/Core/TestJobException.cs new file mode 100644 index 0000000000..9f5abc5235 --- /dev/null +++ b/tests/Tgstation.Server.Host.Tests/Core/TestJobException.cs @@ -0,0 +1,17 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace Tgstation.Server.Host.Core.Tests +{ + [TestClass] + public sealed class TestJobException + { + [TestMethod] + public void TestConstruction() + { + new JobException(); + new JobException("Message"); + new JobException("Message", new Exception()); + } + } +} From 7996425b8c66c3afa8d733512919c1341aacb00b Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 12:16:27 -0400 Subject: [PATCH 22/26] Version bump to 4.0.0.5 --- .../Tgstation.Server.Host.Console.csproj | 3 +-- src/Tgstation.Server.Host.Service/Properties/AssemblyInfo.cs | 4 ++-- .../Tgstation.Server.Host.Watchdog.csproj | 3 +-- src/Tgstation.Server.Host/Tgstation.Server.Host.csproj | 3 +-- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Tgstation.Server.Host.Console/Tgstation.Server.Host.Console.csproj b/src/Tgstation.Server.Host.Console/Tgstation.Server.Host.Console.csproj index 793140f189..39751be2d8 100644 --- a/src/Tgstation.Server.Host.Console/Tgstation.Server.Host.Console.csproj +++ b/src/Tgstation.Server.Host.Console/Tgstation.Server.Host.Console.csproj @@ -4,8 +4,7 @@ Exe netcoreapp2.1 Full - 4.0.0.4 - 4.0.0.4 + 4.0.0.5 diff --git a/src/Tgstation.Server.Host.Service/Properties/AssemblyInfo.cs b/src/Tgstation.Server.Host.Service/Properties/AssemblyInfo.cs index 32a4208542..d336b8f403 100644 --- a/src/Tgstation.Server.Host.Service/Properties/AssemblyInfo.cs +++ b/src/Tgstation.Server.Host.Service/Properties/AssemblyInfo.cs @@ -7,5 +7,5 @@ using System.Runtime.InteropServices; [assembly: Guid("29927416-3b78-49a7-a560-5ccaa638b6b4")] [assembly: InternalsVisibleTo("Tgstation.Server.Host.Service.Tests")] -[assembly: AssemblyVersion("4.0.0.4")] -[assembly: AssemblyFileVersion("4.0.0.4")] +[assembly: AssemblyVersion("4.0.0.5")] +[assembly: AssemblyFileVersion("4.0.0.5")] diff --git a/src/Tgstation.Server.Host.Watchdog/Tgstation.Server.Host.Watchdog.csproj b/src/Tgstation.Server.Host.Watchdog/Tgstation.Server.Host.Watchdog.csproj index 7c70efa5fd..bc54f79353 100644 --- a/src/Tgstation.Server.Host.Watchdog/Tgstation.Server.Host.Watchdog.csproj +++ b/src/Tgstation.Server.Host.Watchdog/Tgstation.Server.Host.Watchdog.csproj @@ -4,8 +4,7 @@ netstandard2.0 Full false - 4.0.0.4 - 4.0.0.4 + 4.0.0.5 diff --git a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj index f2234badd7..a4717a77e7 100644 --- a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj +++ b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj @@ -3,8 +3,7 @@ netcoreapp2.1 Full - 4.0.0.4 - 4.0.0.4 + 4.0.0.5 From c60fa38d0260ef85587e12c20884d42128fa28ee Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 12:30:51 -0400 Subject: [PATCH 23/26] Remove unused using --- tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs b/tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs index d21955f066..0b8b6fbfc5 100644 --- a/tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs +++ b/tests/Tgstation.Server.Host.Tests/IO/TestSymlinkFactory.cs @@ -1,6 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; -using System.ComponentModel; using System.IO; using System.Runtime.InteropServices; using System.Threading.Tasks; From 189c3798e9776f77ab658856fe75f81b7cefb402 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 13:42:29 -0400 Subject: [PATCH 24/26] Add missing ".dll" from DllImport --- src/Tgstation.Server.Host/NativeMethods.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tgstation.Server.Host/NativeMethods.cs b/src/Tgstation.Server.Host/NativeMethods.cs index 18124400ee..6b130f9aac 100644 --- a/src/Tgstation.Server.Host/NativeMethods.cs +++ b/src/Tgstation.Server.Host/NativeMethods.cs @@ -35,7 +35,7 @@ namespace Tgstation.Server.Host /// /// See https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-enumchildwindows /// - [DllImport("user32")] + [DllImport("user32.dll")] public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam); /// From e162ca57b05bf62b6e437d5ddd558eda40f907b6 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 13:46:32 -0400 Subject: [PATCH 25/26] Fix and improve windows symbolic link creation --- .../IO/WindowsSymlinkFactory.cs | 16 ++++++++++++++-- src/Tgstation.Server.Host/NativeMethods.cs | 13 ++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs index d6b0da449c..e1b54d7eca 100644 --- a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs +++ b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs @@ -21,11 +21,23 @@ namespace Tgstation.Server.Host.IO throw new ArgumentNullException(nameof(linkPath)); //check if its not a file - var flags = File.Exists(targetPath) ? 2 : 3; //SYMBOLIC_LINK_FLAG_DIRECTORY and SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE on win10 1607+ developer mode + var flags = File.Exists(targetPath) ? NativeMethods.CreateSymbolicLinkFlags.None : NativeMethods.CreateSymbolicLinkFlags.Directory; + + flags |= NativeMethods.CreateSymbolicLinkFlags.AllowUnprivilegedCreate; cancellationToken.ThrowIfCancellationRequested(); if (!NativeMethods.CreateSymbolicLink(linkPath, targetPath, flags)) - throw new Win32Exception(Marshal.GetLastWin32Error()); + { + var error = Marshal.GetLastWin32Error(); + if (error == 87) //INVALID_PARAMETER, SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE isn't supported + { + flags &= ~NativeMethods.CreateSymbolicLinkFlags.AllowUnprivilegedCreate; + if (NativeMethods.CreateSymbolicLink(linkPath, targetPath, flags)) + return; + error = Marshal.GetLastWin32Error(); + } + throw new Win32Exception(error); + } }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current); } } diff --git a/src/Tgstation.Server.Host/NativeMethods.cs b/src/Tgstation.Server.Host/NativeMethods.cs index 6b130f9aac..bcc1865686 100644 --- a/src/Tgstation.Server.Host/NativeMethods.cs +++ b/src/Tgstation.Server.Host/NativeMethods.cs @@ -9,6 +9,17 @@ namespace Tgstation.Server.Host /// static class NativeMethods { + /// + /// See https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-createsymboliclinka#parameters + /// + [Flags] + public enum CreateSymbolicLinkFlags : int + { + None = 0, + Directory = 1, + AllowUnprivilegedCreate = 2 + } + /// /// See https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowthreadprocessid /// @@ -54,6 +65,6 @@ namespace Tgstation.Server.Host /// See https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-createsymboliclinkw /// [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags); + public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, CreateSymbolicLinkFlags dwFlags); } } From b9e296a93ff8949da79b3fde0ce8eb0893d30849 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 14:00:38 -0400 Subject: [PATCH 26/26] Switch to using BetterWin32Exceptions --- .../Watchdog/WindowsNetworkPromptReaper.cs | 20 +++++++++---------- .../IO/WindowsSymlinkFactory.cs | 11 ++++------ .../Tgstation.Server.Host.csproj | 1 + 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/Watchdog/WindowsNetworkPromptReaper.cs b/src/Tgstation.Server.Host/Components/Watchdog/WindowsNetworkPromptReaper.cs index 2298971e6a..ef11168489 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/WindowsNetworkPromptReaper.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/WindowsNetworkPromptReaper.cs @@ -1,8 +1,8 @@ -using Microsoft.Extensions.Hosting; +using BetterWin32Errors; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -49,10 +49,10 @@ namespace Tgstation.Server.Host.Components.Watchdog { var gcChildhandlesList = GCHandle.FromIntPtr(lParam); - if (gcChildhandlesList == null || gcChildhandlesList.Target == null) + if (gcChildhandlesList.Target == null) return false; - var childHandles = (List )gcChildhandlesList.Target; + var childHandles = (List)gcChildhandlesList.Target; childHandles.Add(hWnd); return true; @@ -61,12 +61,10 @@ namespace Tgstation.Server.Host.Components.Watchdog static List GetAllChildHandles(IntPtr main) { var childHandles = new List(); - var gcChildhandlesList = GCHandle.Alloc(childHandles); - var pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList); - try { + var pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList); NativeMethods.EnumWindowProc childProc = new NativeMethods.EnumWindowProc(EnumWindow); NativeMethods.EnumChildWindows(main, childProc, pointerChildHandlesList); } @@ -74,7 +72,6 @@ namespace Tgstation.Server.Host.Components.Watchdog { gcChildhandlesList.Free(); } - return childHandles; } @@ -129,7 +126,7 @@ namespace Tgstation.Server.Host.Components.Watchdog if (NativeMethods.GetWindowText(I, stringBuilder, MaxLength) == 0) { - logger.LogWarning("Error calling GetWindowText! Exception: {0}", new Win32Exception(Marshal.GetLastWin32Error())); + logger.LogWarning("Error calling GetWindowText! Exception: {0}", new Win32Exception()); continue; } @@ -152,7 +149,10 @@ namespace Tgstation.Server.Host.Components.Watchdog logger.LogDebug("Unable to find \"Yes\" button for \"Network Accessibility\" window in owned process {0}!", processId); } } - catch (OperationCanceledException) { } + catch (OperationCanceledException) + { + logger.LogTrace("Cancelled!"); + } finally { logger.LogDebug("Exiting network prompt reaper..."); diff --git a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs index e1b54d7eca..13aae902c2 100644 --- a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs +++ b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs @@ -1,7 +1,6 @@ -using System; -using System.ComponentModel; +using BetterWin32Errors; +using System; using System.IO; -using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -28,15 +27,13 @@ namespace Tgstation.Server.Host.IO cancellationToken.ThrowIfCancellationRequested(); if (!NativeMethods.CreateSymbolicLink(linkPath, targetPath, flags)) { - var error = Marshal.GetLastWin32Error(); - if (error == 87) //INVALID_PARAMETER, SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE isn't supported + if (Win32Exception.GetLastWin32Error() == Win32Error.ERROR_INVALID_PARAMETER) //SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE isn't supported { flags &= ~NativeMethods.CreateSymbolicLinkFlags.AllowUnprivilegedCreate; if (NativeMethods.CreateSymbolicLink(linkPath, targetPath, flags)) return; - error = Marshal.GetLastWin32Error(); } - throw new Win32Exception(error); + throw new Win32Exception(); } }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current); } diff --git a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj index a4717a77e7..9d33cd974a 100644 --- a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj +++ b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj @@ -20,6 +20,7 @@ +