Initial commit
This commit is contained in:
35
ASS.Server/ASS.Server.csproj
Normal file
35
ASS.Server/ASS.Server.csproj
Normal file
@@ -0,0 +1,35 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Grpc" Version="1.22.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ASS.API\ASS.API.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="defaultConfig.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Attributes\" />
|
||||
<Folder Include="Extensions\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
28
ASS.Server/Helpers/GrpcLoggingWraper.cs
Normal file
28
ASS.Server/Helpers/GrpcLoggingWraper.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace ASS.Server.Helpers
|
||||
{
|
||||
class GrpcLoggingWraper : Grpc.Core.Logging.ILogger
|
||||
{
|
||||
private ILogger _logger;
|
||||
public GrpcLoggingWraper(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Grpc.Core.Logging.ILogger ForType<T>() => this;
|
||||
public void Debug(string message) => _logger.LogDebug(message);
|
||||
public void Debug(string format, params object[] formatArgs) => _logger.LogDebug(format, args: formatArgs);
|
||||
public void Error(string message) => _logger.LogError(message);
|
||||
public void Error(string format, params object[] formatArgs) => _logger.LogError(format, args: formatArgs);
|
||||
public void Error(Exception exception, string message) => _logger.LogError(exception, message);
|
||||
public void Info(string message) => _logger.LogInformation(message);
|
||||
public void Info(string format, params object[] formatArgs) => _logger.LogInformation(format, args: formatArgs);
|
||||
public void Warning(string message) => _logger.LogWarning(message);
|
||||
public void Warning(string format, params object[] formatArgs) => _logger.LogWarning(format, args: formatArgs);
|
||||
public void Warning(Exception exception, string message) => _logger.LogWarning(exception, message);
|
||||
}
|
||||
}
|
58
ASS.Server/Program.cs
Normal file
58
ASS.Server/Program.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using ASS.Server.Services;
|
||||
using ASS.Server.Helpers;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace ASS.Server
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
=> new Program().MainAsync(args).GetAwaiter().GetResult();
|
||||
|
||||
|
||||
public async Task MainAsync(string[] args)
|
||||
{
|
||||
|
||||
using (var services = ConfigureServices(args))
|
||||
{
|
||||
Grpc.Core.GrpcEnvironment.SetLogger(new GrpcLoggingWraper(services.GetRequiredService<ILogger<GrpcService>>()));
|
||||
var grpc = services.GetRequiredService<GrpcService>();
|
||||
grpc.Initialize();
|
||||
await Task.Delay(-1);
|
||||
}
|
||||
}
|
||||
|
||||
private ServiceProvider ConfigureServices(string[] args)
|
||||
{
|
||||
// Stage 1 services
|
||||
var services = new ServiceCollection()
|
||||
.AddSingleton<IConfiguration>(sp =>
|
||||
new ConfigurationBuilder()
|
||||
.AddJsonFile("defaultConfig.json", false, false)
|
||||
.AddJsonFile("config.json", true, false)
|
||||
.AddEnvironmentVariables()
|
||||
.AddCommandLine(args)
|
||||
.Build()
|
||||
);
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
// Stage 2 services
|
||||
services
|
||||
.AddLogging(c =>
|
||||
{
|
||||
c.ClearProviders();
|
||||
c.AddConsole();
|
||||
c.AddConfiguration(serviceProvider.GetRequiredService<IConfiguration>().GetSection("Logging"));
|
||||
})
|
||||
.AddSingleton(sp => new GrpcService(sp))
|
||||
.AddSingleton<InstanceService>()
|
||||
.AddSingleton<HttpClient>();
|
||||
;
|
||||
return services.BuildServiceProvider();
|
||||
}
|
||||
}
|
||||
}
|
53
ASS.Server/Services/ByondService.cs
Normal file
53
ASS.Server/Services/ByondService.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using ASS.API;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace ASS.Server.Services
|
||||
{
|
||||
class ByondService : Byond.ByondBase
|
||||
{
|
||||
const string BYOND_LATEST_URL = "https://secure.byond.com/download/build/LATEST/";
|
||||
const string BYOND_DOWNLOAD_URL = "https://secure.byond.com/download/build/";
|
||||
|
||||
HttpClient httpclient;
|
||||
IConfiguration config;
|
||||
|
||||
public ByondService(HttpClient _httpClient, IConfiguration configuration)
|
||||
{
|
||||
httpclient = _httpClient;
|
||||
config = configuration.GetSection("BYOND");
|
||||
}
|
||||
|
||||
public static string GetDownloadUrl(string version)
|
||||
{
|
||||
var split = version.Split(".");
|
||||
return GetDownloadUrl(split[0], split[1]);
|
||||
}
|
||||
public static string GetDownloadUrl(int major, int minor) => GetDownloadUrl(major.ToString(), minor.ToString());
|
||||
public static string GetDownloadUrl(ByondVersion version) => GetDownloadUrl(version.Major, version.Minor);
|
||||
public static string GetDownloadUrl(string major, string minor)
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
return $"{BYOND_DOWNLOAD_URL}/{major}/{major}.{minor}_byond_linux.zip";
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
return $"{BYOND_DOWNLOAD_URL}/{major}/{major}.{minor}_byond.zip";
|
||||
throw new Exception("Unsupported OS");
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ByondVersion>> GetVersions()
|
||||
{
|
||||
var response = await httpclient.SendAsync(new HttpRequestMessage(HttpMethod.Get, BYOND_LATEST_URL));
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
var regex = new Regex("\\\"([\\d]+)\\.([\\d]+)_byond.zip\\\"");
|
||||
var matches = regex.Matches(content);
|
||||
return matches.Select(m => new ByondVersion() { Major = int.Parse(m.Captures[0].Value), Minor = int.Parse(m.Captures[0].Value) });
|
||||
}
|
||||
}
|
||||
}
|
37
ASS.Server/Services/GRPCService.cs
Normal file
37
ASS.Server/Services/GRPCService.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Grpc.Core;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace ASS.Server.Services
|
||||
{
|
||||
class GrpcService : Grpc.Core.Server
|
||||
{
|
||||
IServiceProvider _sp;
|
||||
ILogger logger;
|
||||
IConfiguration config;
|
||||
|
||||
public GrpcService(IServiceProvider sp) : this(sp.GetRequiredService<IConfiguration>())
|
||||
{
|
||||
_sp = sp;
|
||||
logger = _sp.GetRequiredService<ILogger<GrpcService>>();
|
||||
}
|
||||
|
||||
public GrpcService(IConfiguration _config) : base()
|
||||
{
|
||||
config = _config;
|
||||
Ports.Add(config["GRPC:Host"], int.Parse(config["GRPC:Port"]), ServerCredentials.Insecure);
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
Services.Add(API.Instance.BindService(_sp.GetRequiredService<InstanceService>()));
|
||||
Start();
|
||||
logger.LogInformation($"gRPC server listening on {config["GRPC:Host"]}:{config["GRPC:Port"]}");
|
||||
}
|
||||
}
|
||||
}
|
20
ASS.Server/Services/InstanceService.cs
Normal file
20
ASS.Server/Services/InstanceService.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ASS.API;
|
||||
using Grpc.Core;
|
||||
|
||||
namespace ASS.Server.Services
|
||||
{
|
||||
class InstanceService : Instance.InstanceBase
|
||||
{
|
||||
public async override Task<InstanceStatus> GetStatus(EmptyRequest request, ServerCallContext context)
|
||||
{
|
||||
return new InstanceStatus
|
||||
{
|
||||
Message = $"YOU:{request.Auth.Token}:WE:{DateTime.Now.ToString()}"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
21
ASS.Server/defaultConfig.json
Normal file
21
ASS.Server/defaultConfig.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug"
|
||||
}
|
||||
},
|
||||
"GRPC": {
|
||||
"Port": 5001,
|
||||
"Host": "0.0.0.0"
|
||||
},
|
||||
"Repository": {
|
||||
"URL": "https://github.com/Aurorastation/Aurora.3.git"
|
||||
},
|
||||
"BYOND": {
|
||||
"Version": {
|
||||
"Major": 512,
|
||||
"Minor": 1467
|
||||
}
|
||||
},
|
||||
"WorkDir": "work"
|
||||
}
|
Reference in New Issue
Block a user