yeah idk, basic form of reg, auth, upload, whitelist. lots of stuff.
This commit is contained in:
21
MareSynchronosServer/MareSynchronosServer/Hubs/Connection.cs
Normal file
21
MareSynchronosServer/MareSynchronosServer/Hubs/Connection.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace MareSynchronosServer.Hubs
|
||||
{
|
||||
public class Connection : Hub
|
||||
{
|
||||
public string Heartbeat()
|
||||
{
|
||||
var userId = Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
|
||||
if (userId != null)
|
||||
{
|
||||
var user = Clients.User(userId);
|
||||
}
|
||||
return userId ?? string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
75
MareSynchronosServer/MareSynchronosServer/Hubs/Files.cs
Normal file
75
MareSynchronosServer/MareSynchronosServer/Hubs/Files.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading.Tasks;
|
||||
using MareSynchronosServer.Authentication;
|
||||
using MareSynchronosServer.Data;
|
||||
using MareSynchronosServer.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace MareSynchronosServer.Hubs
|
||||
{
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AUTH_SCHEME)]
|
||||
public class Files : Hub
|
||||
{
|
||||
private readonly MareDbContext _dbContext;
|
||||
|
||||
public Files(MareDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AUTH_SCHEME)]
|
||||
public async Task SendFiles(List<string> fileList)
|
||||
{
|
||||
var existingFiles = _dbContext.Files.Where(f => fileList.Contains(f.Hash)).ToList();
|
||||
foreach (var file in fileList.Where(f => existingFiles.All(e => e.Hash != f)))
|
||||
{
|
||||
var userId = Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? "Unknown";
|
||||
await _dbContext.Files.AddAsync(new FileCache()
|
||||
{
|
||||
Hash = file,
|
||||
LastAccessTime = DateTime.Now,
|
||||
Uploaded = false,
|
||||
Uploader = _dbContext.Users.Single(u => u.UID == userId)
|
||||
});
|
||||
await _dbContext.SaveChangesAsync();
|
||||
await Clients.Caller!.SendAsync("FileRequest", file);
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AUTH_SCHEME)]
|
||||
public async Task<bool> UploadFile(string hash, byte[] file)
|
||||
{
|
||||
var relatedFile = _dbContext.Files.SingleOrDefault(f => f.Hash == hash);
|
||||
if (relatedFile == null) return false;
|
||||
var decodedFile = LZ4.LZ4Codec.Unwrap(file);
|
||||
using var sha1 = new SHA1CryptoServiceProvider();
|
||||
var computedHash = await sha1.ComputeHashAsync(new MemoryStream(decodedFile));
|
||||
var computedHashString = BitConverter.ToString(computedHash).Replace("-", "");
|
||||
if (hash != computedHashString)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
await File.WriteAllBytesAsync(@"G:\ServerTest\" + hash, file);
|
||||
relatedFile.Uploaded = true;
|
||||
relatedFile.LastAccessTime = DateTime.Now;
|
||||
await _dbContext.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override Task OnDisconnectedAsync(Exception exception)
|
||||
{
|
||||
var userId = Context.User!.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
|
||||
var notUploadedFiles = _dbContext.Files.Where(f => !f.Uploaded && f.Uploader.UID == userId).ToList();
|
||||
_dbContext.RemoveRange(notUploadedFiles);
|
||||
_dbContext.SaveChanges();
|
||||
return base.OnDisconnectedAsync(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
157
MareSynchronosServer/MareSynchronosServer/Hubs/User.cs
Normal file
157
MareSynchronosServer/MareSynchronosServer/Hubs/User.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MareSynchronos.API;
|
||||
using MareSynchronosServer.Authentication;
|
||||
using MareSynchronosServer.Data;
|
||||
using MareSynchronosServer.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MareSynchronosServer.Hubs
|
||||
{
|
||||
public class User : Hub
|
||||
{
|
||||
private readonly MareDbContext _dbContext;
|
||||
|
||||
public User(MareDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<string> Register()
|
||||
{
|
||||
using var sha256 = SHA256.Create();
|
||||
var computedHash = BitConverter.ToString(sha256.ComputeHash(Encoding.UTF8.GetBytes(GenerateRandomString(64)))).Replace("-", "");
|
||||
var user = new Models.User
|
||||
{
|
||||
SecretKey = BitConverter.ToString(sha256.ComputeHash(Encoding.UTF8.GetBytes(computedHash)))
|
||||
.Replace("-", ""),
|
||||
};
|
||||
|
||||
var hasValidUid = false;
|
||||
while (!hasValidUid)
|
||||
{
|
||||
var uid = GenerateRandomString(10);
|
||||
if (_dbContext.Users.Any(u => u.UID == uid)) continue;
|
||||
user.UID = uid;
|
||||
hasValidUid = true;
|
||||
}
|
||||
_dbContext.Users.Add(user);
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
return computedHash;
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AUTH_SCHEME)]
|
||||
public string GetUID()
|
||||
{
|
||||
return Context.User!.Claims.Single(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AUTH_SCHEME)]
|
||||
public async Task SendWhitelist(List<WhitelistDto> whiteListEntries)
|
||||
{
|
||||
var currentUserId = Context.User!.Claims.Single(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
||||
var user = _dbContext.Users.Single(u => u.UID == currentUserId);
|
||||
var userWhitelists = _dbContext.Whitelists
|
||||
.Include(w => w.User)
|
||||
.Include(w => w.OtherUser)
|
||||
.Where(w => w.User.UID == currentUserId)
|
||||
.ToList();
|
||||
foreach (var whitelist in whiteListEntries)
|
||||
{
|
||||
var otherEntry = _dbContext.Whitelists.SingleOrDefault(w =>
|
||||
w.User.UID == whitelist.OtherUID && w.OtherUser.UID == user.UID);
|
||||
|
||||
var prevEntry = userWhitelists.SingleOrDefault(w => w.OtherUser.UID == whitelist.OtherUID);
|
||||
if (prevEntry != null)
|
||||
{
|
||||
prevEntry.IsPaused = whitelist.IsPaused;
|
||||
}
|
||||
else
|
||||
{
|
||||
var otherUser = _dbContext.Users.SingleOrDefault(u => u.UID == whitelist.OtherUID);
|
||||
if (otherUser != null)
|
||||
{
|
||||
Whitelist wl = new Whitelist
|
||||
{
|
||||
User = user,
|
||||
OtherUser = otherUser,
|
||||
IsPaused = whitelist.IsPaused
|
||||
};
|
||||
otherEntry = wl;
|
||||
await _dbContext.Whitelists.AddAsync(wl);
|
||||
}
|
||||
}
|
||||
|
||||
if (otherEntry != null)
|
||||
{
|
||||
await Clients.User(whitelist.OtherUID).SendAsync("UpdateWhitelist", currentUserId, true,
|
||||
whitelist.IsPaused);
|
||||
}
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
foreach (var deletedEntry in userWhitelists.Where(u => whiteListEntries.All(e => e.OtherUID != u.OtherUser.UID)).ToList())
|
||||
{
|
||||
var otherEntry = _dbContext.Whitelists.SingleOrDefault(w =>
|
||||
w.User.UID == deletedEntry.OtherUser.UID && w.OtherUser.UID == user.UID);
|
||||
if (otherEntry != null)
|
||||
{
|
||||
await Clients.User(otherEntry.User.UID).SendAsync("UpdateWhitelist", currentUserId, false, false);
|
||||
}
|
||||
|
||||
_dbContext.Whitelists.Remove(deletedEntry);
|
||||
}
|
||||
_dbContext.Whitelists.RemoveRange();
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = SecretKeyAuthenticationHandler.AUTH_SCHEME)]
|
||||
public async Task<List<WhitelistDto>> GetWhitelist()
|
||||
{
|
||||
string userid = Context.User!.Claims.Single(c => c.Type == ClaimTypes.NameIdentifier).Value;
|
||||
return _dbContext.Whitelists.Include(u => u.OtherUser).Include(u => u.User).Where(w => w.User.UID == userid)
|
||||
.ToList()
|
||||
.Select(w =>
|
||||
{
|
||||
var otherEntry = _dbContext.Whitelists.SingleOrDefault(a => a.User.UID == w.OtherUser.UID && a.OtherUser.UID == userid);
|
||||
return new WhitelistDto
|
||||
{
|
||||
IsPaused = w.IsPaused,
|
||||
OtherUID = w.OtherUser.UID,
|
||||
IsSynced = otherEntry != null,
|
||||
IsPausedFromOthers = otherEntry?.IsPaused ?? false
|
||||
};
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public static string GenerateRandomString(int length, string allowableChars = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(allowableChars))
|
||||
allowableChars = @"ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
|
||||
|
||||
// Generate random data
|
||||
var rnd = new byte[length];
|
||||
using (var rng = new RNGCryptoServiceProvider())
|
||||
rng.GetBytes(rnd);
|
||||
|
||||
// Generate the output string
|
||||
var allowable = allowableChars.ToCharArray();
|
||||
var l = allowable.Length;
|
||||
var chars = new char[length];
|
||||
for (var i = 0; i < length; i++)
|
||||
chars[i] = allowable[rnd[i] % l];
|
||||
|
||||
return new string(chars);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user