init
This commit is contained in:
commit
f25ba8adcd
64 changed files with 5959 additions and 0 deletions
9
src/GsDChat.Core/GsDChat.Core.csproj
Normal file
9
src/GsDChat.Core/GsDChat.Core.csproj
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
13
src/GsDChat.Core/Models/Channel.cs
Normal file
13
src/GsDChat.Core/Models/Channel.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
|
||||
namespace GsDChat.Core.Models;
|
||||
|
||||
public class Channel
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString("N");
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Topic { get; set; } = string.Empty;
|
||||
public bool IsPrivate { get; set; } = false;
|
||||
public string AllowedRoles { get; set; } = string.Empty; // Comma-separated or empty for public
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
17
src/GsDChat.Core/Models/ChatMessage.cs
Normal file
17
src/GsDChat.Core/Models/ChatMessage.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
|
||||
namespace GsDChat.Core.Models;
|
||||
|
||||
public class ChatMessage
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString("N");
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string SenderId { get; set; } = string.Empty;
|
||||
public string SenderUsername { get; set; } = string.Empty;
|
||||
public string SenderRole { get; set; } = UserRoles.Member;
|
||||
public string Content { get; set; } = string.Empty;
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
public string? AttachmentId { get; set; }
|
||||
public string? AttachmentName { get; set; }
|
||||
public long? AttachmentSizeBytes { get; set; }
|
||||
}
|
||||
17
src/GsDChat.Core/Models/FileRecord.cs
Normal file
17
src/GsDChat.Core/Models/FileRecord.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
|
||||
namespace GsDChat.Core.Models;
|
||||
|
||||
public class FileRecord
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString("N");
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string UploaderId { get; set; } = string.Empty;
|
||||
public string UploaderUsername { get; set; } = string.Empty;
|
||||
public string OriginalFileName { get; set; } = string.Empty;
|
||||
public string ContentType { get; set; } = "application/octet-stream";
|
||||
public long FileSizeBytes { get; set; }
|
||||
public string StoragePath { get; set; } = string.Empty;
|
||||
public string Sha256Checksum { get; set; } = string.Empty;
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
13
src/GsDChat.Core/Models/Session.cs
Normal file
13
src/GsDChat.Core/Models/Session.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
|
||||
namespace GsDChat.Core.Models;
|
||||
|
||||
public class Session
|
||||
{
|
||||
public string Token { get; set; } = string.Empty;
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
public DateTime ExpiresAt { get; set; } = DateTime.UtcNow.AddDays(30);
|
||||
|
||||
public bool IsExpired => DateTime.UtcNow > ExpiresAt;
|
||||
}
|
||||
21
src/GsDChat.Core/Models/User.cs
Normal file
21
src/GsDChat.Core/Models/User.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
|
||||
namespace GsDChat.Core.Models;
|
||||
|
||||
public static class UserRoles
|
||||
{
|
||||
public const string Owner = "Owner";
|
||||
public const string Admin = "Admin";
|
||||
public const string Member = "Member";
|
||||
}
|
||||
|
||||
public class User
|
||||
{
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString("N");
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string PasswordHash { get; set; } = string.Empty;
|
||||
public string PasswordSalt { get; set; } = string.Empty;
|
||||
public string Role { get; set; } = UserRoles.Member;
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
public bool IsBanned { get; set; } = false;
|
||||
}
|
||||
48
src/GsDChat.Core/Protocol/HttpModels.cs
Normal file
48
src/GsDChat.Core/Protocol/HttpModels.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
using GsDChat.Core.Models;
|
||||
|
||||
namespace GsDChat.Core.Protocol;
|
||||
|
||||
public class RegisterRequest
|
||||
{
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
public string? ServerPassword { get; set; }
|
||||
public string? InviteToken { get; set; }
|
||||
}
|
||||
|
||||
public class LoginRequest
|
||||
{
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
public string? ServerPassword { get; set; }
|
||||
}
|
||||
|
||||
public class AuthResponse
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string? Token { get; set; }
|
||||
public string? UserId { get; set; }
|
||||
public string? Username { get; set; }
|
||||
public string? Role { get; set; }
|
||||
public string? ErrorMessage { get; set; }
|
||||
}
|
||||
|
||||
public class ServerInfoResponse
|
||||
{
|
||||
public string ServerName { get; set; } = "GsDChat Server";
|
||||
public string Version { get; set; } = "1.0.0";
|
||||
public string RegistrationMode { get; set; } = "Open"; // Open or InviteOnly
|
||||
public bool RequiresServerPassword { get; set; }
|
||||
public string Motd { get; set; } = "Welcome to GsDChat!";
|
||||
public long MaxFileSizeMb { get; set; } = 50;
|
||||
}
|
||||
|
||||
public class UploadFileResponse
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string? FileId { get; set; }
|
||||
public string? FileName { get; set; }
|
||||
public long FileSizeBytes { get; set; }
|
||||
public string? ChannelId { get; set; }
|
||||
public string? ErrorMessage { get; set; }
|
||||
}
|
||||
157
src/GsDChat.Core/Protocol/NetworkMessages.cs
Normal file
157
src/GsDChat.Core/Protocol/NetworkMessages.cs
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using GsDChat.Core.Models;
|
||||
|
||||
namespace GsDChat.Core.Protocol;
|
||||
|
||||
public static class MessageTypes
|
||||
{
|
||||
public const string Auth = "AUTH";
|
||||
public const string AuthResult = "AUTH_RESULT";
|
||||
public const string UiSync = "UI_SYNC";
|
||||
public const string SendMessage = "SEND_MESSAGE";
|
||||
public const string MessageBroadcast = "MESSAGE_BROADCAST";
|
||||
public const string HistorySync = "HISTORY_SYNC";
|
||||
public const string JoinChannel = "JOIN_CHANNEL";
|
||||
public const string ChannelListSync = "CHANNEL_LIST_SYNC";
|
||||
public const string CreateChannel = "CREATE_CHANNEL";
|
||||
public const string DeleteChannel = "DELETE_CHANNEL";
|
||||
public const string UserListSync = "USER_LIST_SYNC";
|
||||
public const string FileListSync = "FILE_LIST_SYNC";
|
||||
public const string SystemNotice = "SYSTEM_NOTICE";
|
||||
public const string Error = "ERROR";
|
||||
public const string ExecuteCommand = "EXECUTE_COMMAND";
|
||||
public const string CommandResult = "COMMAND_RESULT";
|
||||
public const string Ping = "PING";
|
||||
public const string Pong = "PONG";
|
||||
}
|
||||
|
||||
public class NetworkEnvelope
|
||||
{
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public string Payload { get; set; } = "{}";
|
||||
|
||||
public static NetworkEnvelope Create<T>(string type, T payload)
|
||||
{
|
||||
return new NetworkEnvelope
|
||||
{
|
||||
Type = type,
|
||||
Payload = JsonSerializer.Serialize(payload)
|
||||
};
|
||||
}
|
||||
|
||||
public T? Unpack<T>()
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(Payload);
|
||||
}
|
||||
}
|
||||
|
||||
public class AuthPacket
|
||||
{
|
||||
public string Token { get; set; } = string.Empty;
|
||||
public string? ServerPassword { get; set; }
|
||||
}
|
||||
|
||||
public class AuthResultPacket
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string Role { get; set; } = UserRoles.Member;
|
||||
public string? ErrorMessage { get; set; }
|
||||
public ServerUiSync? UiSync { get; set; }
|
||||
public string? ActiveChannelId { get; set; }
|
||||
}
|
||||
|
||||
public class SendMessagePacket
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string Content { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class MessageBroadcastPacket
|
||||
{
|
||||
public ChatMessage Message { get; set; } = new();
|
||||
}
|
||||
|
||||
public class HistorySyncPacket
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public List<ChatMessage> Messages { get; set; } = new();
|
||||
}
|
||||
|
||||
public class JoinChannelPacket
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class ChannelListSyncPacket
|
||||
{
|
||||
public List<Channel> Channels { get; set; } = new();
|
||||
public string ActiveChannelId { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class CreateChannelPacket
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Topic { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class DeleteChannelPacket
|
||||
{
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class UserSummary
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string Role { get; set; } = UserRoles.Member;
|
||||
public bool IsOnline { get; set; }
|
||||
}
|
||||
|
||||
public class UserListSyncPacket
|
||||
{
|
||||
public List<UserSummary> Users { get; set; } = new();
|
||||
}
|
||||
|
||||
public class FileSummary
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
public string OriginalFileName { get; set; } = string.Empty;
|
||||
public string ContentType { get; set; } = string.Empty;
|
||||
public long FileSizeBytes { get; set; }
|
||||
public string UploaderUsername { get; set; } = string.Empty;
|
||||
public System.DateTime CreatedAt { get; set; }
|
||||
}
|
||||
|
||||
public class FileListSyncPacket
|
||||
{
|
||||
public List<FileSummary> Files { get; set; } = new();
|
||||
}
|
||||
|
||||
public class SystemNoticePacket
|
||||
{
|
||||
public string Text { get; set; } = string.Empty;
|
||||
public string Level { get; set; } = "Info"; // Info, Warning, Success
|
||||
}
|
||||
|
||||
public class ErrorPacket
|
||||
{
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public string? Code { get; set; }
|
||||
}
|
||||
|
||||
public class ExecuteCommandPacket
|
||||
{
|
||||
public string Command { get; set; } = string.Empty;
|
||||
public string Args { get; set; } = string.Empty;
|
||||
public string ChannelId { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class CommandResultPacket
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string Output { get; set; } = string.Empty;
|
||||
}
|
||||
108
src/GsDChat.Core/Protocol/UiProtocol.cs
Normal file
108
src/GsDChat.Core/Protocol/UiProtocol.cs
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace GsDChat.Core.Protocol;
|
||||
|
||||
public class PanelDefinition
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Position { get; set; } = "Center"; // Top, Center, Bottom, Left, Right
|
||||
public int? Height { get; set; }
|
||||
public int? Width { get; set; }
|
||||
public bool ShowBorder { get; set; } = true;
|
||||
public string? BorderTitle { get; set; }
|
||||
public string? ContentTemplate { get; set; }
|
||||
}
|
||||
|
||||
public class ScreenDefinition
|
||||
{
|
||||
public string ScreenId { get; set; } = string.Empty;
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string ShortcutKey { get; set; } = string.Empty; // e.g. F1, F2, F3, F4, F5
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public List<PanelDefinition> Panels { get; set; } = new();
|
||||
}
|
||||
|
||||
public class ThemeDefinition
|
||||
{
|
||||
public string PrimaryColor { get; set; } = "Cyan";
|
||||
public string SecondaryColor { get; set; } = "DarkCyan";
|
||||
public string BorderColor { get; set; } = "DarkGray";
|
||||
public string HighlightColor { get; set; } = "Yellow";
|
||||
public string ErrorColor { get; set; } = "Red";
|
||||
public string SuccessColor { get; set; } = "Green";
|
||||
public string TextMutedColor { get; set; } = "Gray";
|
||||
public string SystemNoticeColor { get; set; } = "Magenta";
|
||||
}
|
||||
|
||||
public class ServerUiSync
|
||||
{
|
||||
public string ServerName { get; set; } = "GsDChat Server";
|
||||
public string Motd { get; set; } = "Welcome to GsDChat!";
|
||||
public ThemeDefinition Theme { get; set; } = new();
|
||||
public List<ScreenDefinition> Screens { get; set; } = new();
|
||||
|
||||
public static ServerUiSync CreateDefault(string serverName = "GsDChat Server", string motd = "Welcome to GsDChat!")
|
||||
{
|
||||
return new ServerUiSync
|
||||
{
|
||||
ServerName = serverName,
|
||||
Motd = motd,
|
||||
Theme = new ThemeDefinition(),
|
||||
Screens = new List<ScreenDefinition>
|
||||
{
|
||||
new()
|
||||
{
|
||||
ScreenId = "F1_Chat",
|
||||
Title = "Chat",
|
||||
ShortcutKey = "F1",
|
||||
Description = "Active Channel Chat View",
|
||||
Panels = new List<PanelDefinition>
|
||||
{
|
||||
new() { Name = "Header", Position = "Top", Height = 3, ShowBorder = true, BorderTitle = "Channel Info" },
|
||||
new() { Name = "Messages", Position = "Center", ShowBorder = true, BorderTitle = "Messages" },
|
||||
new() { Name = "Input", Position = "Bottom", Height = 3, ShowBorder = true, BorderTitle = "Message Input" }
|
||||
}
|
||||
},
|
||||
new()
|
||||
{
|
||||
ScreenId = "F2_Channels",
|
||||
Title = "Channels",
|
||||
ShortcutKey = "F2",
|
||||
Description = "Browse and switch chat channels",
|
||||
Panels = new List<PanelDefinition>
|
||||
{
|
||||
new() { Name = "Header", Position = "Top", Height = 3, ShowBorder = true, BorderTitle = "Channels Browser" },
|
||||
new() { Name = "ChannelList", Position = "Center", ShowBorder = true, BorderTitle = "Available Channels" },
|
||||
new() { Name = "Help", Position = "Bottom", Height = 3, ShowBorder = true, BorderTitle = "Navigation" }
|
||||
}
|
||||
},
|
||||
new()
|
||||
{
|
||||
ScreenId = "F3_Users",
|
||||
Title = "Users",
|
||||
ShortcutKey = "F3",
|
||||
Description = "User directory and online presence",
|
||||
Panels = new List<PanelDefinition>
|
||||
{
|
||||
new() { Name = "Header", Position = "Top", Height = 3, ShowBorder = true, BorderTitle = "Community Members" },
|
||||
new() { Name = "UserList", Position = "Center", ShowBorder = true, BorderTitle = "Members Directory" },
|
||||
new() { Name = "Help", Position = "Bottom", Height = 3, ShowBorder = true, BorderTitle = "Commands" }
|
||||
}
|
||||
},
|
||||
new()
|
||||
{
|
||||
ScreenId = "F4_Files",
|
||||
Title = "Files",
|
||||
ShortcutKey = "F4",
|
||||
Description = "Shared files and download browser",
|
||||
Panels = new List<PanelDefinition>
|
||||
{
|
||||
new() { Name = "Header", Position = "Top", Height = 3, ShowBorder = true, BorderTitle = "Shared Files" },
|
||||
new() { Name = "FileList", Position = "Center", ShowBorder = true, BorderTitle = "Available Files" },
|
||||
new() { Name = "Actions", Position = "Bottom", Height = 3, ShowBorder = true, BorderTitle = "Transfer Commands" }
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
57
src/GsDChat.Core/Security/PasswordHasher.cs
Normal file
57
src/GsDChat.Core/Security/PasswordHasher.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace GsDChat.Core.Security;
|
||||
|
||||
public static class PasswordHasher
|
||||
{
|
||||
private const int SaltByteLength = 32;
|
||||
private const int HashByteLength = 64; // SHA512 produces 64 bytes
|
||||
private const int Iterations = 100_000;
|
||||
|
||||
public static (string Hash, string Salt) HashPassword(string password)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(password);
|
||||
|
||||
byte[] salt = RandomNumberGenerator.GetBytes(SaltByteLength);
|
||||
byte[] hash = Rfc2898DeriveBytes.Pbkdf2(
|
||||
password,
|
||||
salt,
|
||||
Iterations,
|
||||
HashAlgorithmName.SHA512,
|
||||
HashByteLength
|
||||
);
|
||||
|
||||
return (Convert.ToHexString(hash), Convert.ToHexString(salt));
|
||||
}
|
||||
|
||||
public static bool VerifyPassword(string password, string storedHashHex, string storedSaltHex)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(password) ||
|
||||
string.IsNullOrWhiteSpace(storedHashHex) ||
|
||||
string.IsNullOrWhiteSpace(storedSaltHex))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
byte[] salt = Convert.FromHexString(storedSaltHex);
|
||||
byte[] expectedHash = Convert.FromHexString(storedHashHex);
|
||||
|
||||
byte[] actualHash = Rfc2898DeriveBytes.Pbkdf2(
|
||||
password,
|
||||
salt,
|
||||
Iterations,
|
||||
HashAlgorithmName.SHA512,
|
||||
expectedHash.Length
|
||||
);
|
||||
|
||||
return CryptographicOperations.FixedTimeEquals(actualHash, expectedHash);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/GsDChat.Core/Security/TokenGenerator.cs
Normal file
13
src/GsDChat.Core/Security/TokenGenerator.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace GsDChat.Core.Security;
|
||||
|
||||
public static class TokenGenerator
|
||||
{
|
||||
public static string GenerateSecureToken(int byteLength = 32)
|
||||
{
|
||||
byte[] bytes = RandomNumberGenerator.GetBytes(byteLength);
|
||||
return Convert.ToHexString(bytes).ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue