Initial Commit
This commit is contained in:
15
Akari.Prototype.Server/Akari.Prototype.Server.csproj
Normal file
15
Akari.Prototype.Server/Akari.Prototype.Server.csproj
Normal file
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Grpc.AspNetCore" Version="2.34.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
22
Akari.Prototype.Server/Options/TcpProviderOptions.cs
Normal file
22
Akari.Prototype.Server/Options/TcpProviderOptions.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Akari.Prototype.Server.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server.Options
|
||||
{
|
||||
public class TcpProviderOptions
|
||||
{
|
||||
public const string FilePath = "provider.json";
|
||||
public const string SectionPath = "Provider";
|
||||
|
||||
[IPAddress]
|
||||
public string ListeningAddress { get; set; } = "0.0.0.0";
|
||||
|
||||
[Range(ushort.MinValue, ushort.MaxValue)]
|
||||
public int Port { get; set; } = 1892;
|
||||
}
|
||||
}
|
||||
34
Akari.Prototype.Server/Program.cs
Normal file
34
Akari.Prototype.Server/Program.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Akari.Prototype.Server.Options;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
// Additional configuration is required to successfully run gRPC on macOS.
|
||||
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureHostConfiguration(configuration =>
|
||||
{
|
||||
configuration.AddJsonFile(TcpProviderOptions.FilePath, true);
|
||||
})
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
||||
13
Akari.Prototype.Server/Properties/launchSettings.json
Normal file
13
Akari.Prototype.Server/Properties/launchSettings.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Akari.Prototype.Server": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": "true",
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5000;https://localhost:5001",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Akari.Prototype.Server/Protos/greet.proto
Normal file
21
Akari.Prototype.Server/Protos/greet.proto
Normal file
@@ -0,0 +1,21 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option csharp_namespace = "Akari.Prototype.Server";
|
||||
|
||||
package greet;
|
||||
|
||||
// The greeting service definition.
|
||||
service Greeter {
|
||||
// Sends a greeting
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
// The request message containing the user's name.
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
// The response message containing the greetings.
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
26
Akari.Prototype.Server/Services/GreeterService.cs
Normal file
26
Akari.Prototype.Server/Services/GreeterService.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Grpc.Core;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server
|
||||
{
|
||||
public class GreeterService : Greeter.GreeterBase
|
||||
{
|
||||
private readonly ILogger<GreeterService> _logger;
|
||||
public GreeterService(ILogger<GreeterService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
|
||||
{
|
||||
return Task.FromResult(new HelloReply
|
||||
{
|
||||
Message = "Hello " + request.Name
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
124
Akari.Prototype.Server/Services/TcpProviderService.cs
Normal file
124
Akari.Prototype.Server/Services/TcpProviderService.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using Akari.Prototype.Server.Options;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Buffers.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server.Services
|
||||
{
|
||||
public class TcpProviderService : BackgroundService
|
||||
{
|
||||
public const int BufferLength = 4096;
|
||||
public const int MaxTokenLength = 192;
|
||||
public const int ReceiveTimeout = 500_000;
|
||||
|
||||
private readonly ILogger<TcpProviderService> _logger;
|
||||
private readonly TcpProviderOptions _options;
|
||||
private readonly IHostApplicationLifetime _hostApplicationLifetime;
|
||||
private Task _backgroundTask;
|
||||
private TcpListener _listener;
|
||||
|
||||
public TcpProviderService(ILogger<TcpProviderService> logger, IOptions<TcpProviderOptions> options, IHostApplicationLifetime hostApplicationLifetime)
|
||||
{
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
_hostApplicationLifetime = hostApplicationLifetime;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await TcpLoop(stoppingToken);
|
||||
}
|
||||
|
||||
private async Task TcpLoop(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_listener = new TcpListener(IPAddress.Parse(_options.ListeningAddress), _options.Port);
|
||||
_listener.Start();
|
||||
|
||||
_logger.LogInformation($"Now listening on: {_listener.LocalEndpoint}");
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
_logger.LogDebug("Waiting for client...");
|
||||
var task = await Task.WhenAny(_listener.AcceptTcpClientAsync(), Task.Delay(Timeout.Infinite, cancellationToken));
|
||||
|
||||
if (task is not Task<TcpClient> clientTask)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using var client = clientTask.Result;
|
||||
|
||||
_logger.LogDebug($"Client connected from: {client.Client.RemoteEndPoint}");
|
||||
|
||||
try
|
||||
{
|
||||
await ReceiveAuth(client, cancellationToken);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogDebug($"Couldn't proccess client request: {e.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e.ToString());
|
||||
_logger.LogError("An error occured in TcpProvider, exiting...");
|
||||
|
||||
_hostApplicationLifetime.StopApplication();
|
||||
|
||||
Environment.Exit(1);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_listener?.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReceiveAuth(TcpClient client, CancellationToken cancellationToken)
|
||||
{
|
||||
using var stream = client.GetStream();
|
||||
var data = new Memory<byte>(new byte[MaxTokenLength]);
|
||||
var buffer = new Memory<byte>(new byte[BufferLength]);
|
||||
|
||||
// Receive token
|
||||
int position = 0;
|
||||
int read = 0;
|
||||
|
||||
var timeoutToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, new CancellationTokenSource(ReceiveTimeout).Token).Token;
|
||||
|
||||
_logger.LogDebug("Waiting for token...");
|
||||
while (position < MaxTokenLength && (read = await stream.ReadAsync(buffer, timeoutToken)) != 0)
|
||||
{
|
||||
if (position + read > data.Length)
|
||||
{
|
||||
// Invalid token
|
||||
return;
|
||||
}
|
||||
|
||||
buffer[..read].CopyTo(data[position..]);
|
||||
|
||||
position += read;
|
||||
}
|
||||
|
||||
// TODO: Send token to AuthManager that will compare it to stored hash using Argon2
|
||||
_logger.LogDebug($"Received token: {Convert.ToBase64String(data[..position].Span)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
59
Akari.Prototype.Server/Startup.cs
Normal file
59
Akari.Prototype.Server/Startup.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Akari.Prototype.Server.Options;
|
||||
using Akari.Prototype.Server.Services;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddGrpc();
|
||||
|
||||
services.AddOptions<TcpProviderOptions>()
|
||||
.Bind(Configuration.GetSection(TcpProviderOptions.SectionPath))
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
services.AddHostedService<TcpProviderService>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapGrpcService<GreeterService>();
|
||||
|
||||
endpoints.MapGet("/", async context =>
|
||||
{
|
||||
await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Akari.Prototype.Server/Utils/IPAddressAttribute.cs
Normal file
27
Akari.Prototype.Server/Utils/IPAddressAttribute.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server.Utils
|
||||
{
|
||||
public class IPAddressAttribute : ValidationAttribute
|
||||
{
|
||||
public IPAddressAttribute() : base("Must be a valid IP address")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override bool IsValid(object value)
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return value is string ip && IPAddress.TryParse(ip, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Akari.Prototype.Server/appsettings.Development.json
Normal file
10
Akari.Prototype.Server/appsettings.Development.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Grpc": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Akari.Prototype.Server/appsettings.json
Normal file
15
Akari.Prototype.Server/appsettings.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Kestrel": {
|
||||
"EndpointDefaults": {
|
||||
"Protocols": "Http2"
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Akari.Prototype.Server/provider.json
Normal file
7
Akari.Prototype.Server/provider.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"Provider":
|
||||
{
|
||||
"ListeningAddress": "0.0.0.0",
|
||||
"Port": 1892
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user