Archived
1
0
Fork 0
forked from Moux23333/FreeSR

remove database(no need mongodb anymore)

This commit is contained in:
moux23333 2024-01-28 11:28:52 +08:00
parent 0641d6b596
commit caf71ca841
30 changed files with 10 additions and 710 deletions

View file

@ -1,51 +0,0 @@
namespace FreeSR.Admin
{
using FreeSR.Admin.Command.Handlers;
using FreeSR.Admin.Service;
using FreeSR.Shared.Command;
using FreeSR.Shared.Configuration;
using FreeSR.Shared.Exceptions;
using NLog;
internal static class AdminServer
{
private static readonly Logger s_log = LogManager.GetCurrentClassLogger();
private static void Main(string[] args)
{
Directory.SetCurrentDirectory(AppContext.BaseDirectory);
AppDomain.CurrentDomain.UnhandledException += OnFatalException;
Console.WriteLine("FreeSR is a free and open-source software, if you paid for this, you have been scammed!");
Console.WriteLine("FreeSR是一个免费且开源的软件如果你是花钱买来的则说明你被骗了");
Console.WriteLine("https://git.xeondev.com/Moux23333/FreeSR");
s_log.Info("Initializing...");
CommandManager.Instance.Initialize(typeof(AccountCommandCategory));
ConfigurationManager<AdminServerConfiguration>.Instance.Initialize("AdminServer.json");
var serverConfiguration = ConfigurationManager<AdminServerConfiguration>.Instance.Model;
HttpAdminService.Initialize(serverConfiguration.Network);
s_log.Info("Server is ready!");
Thread.Sleep(-1);
}
private static void OnFatalException(object sender, UnhandledExceptionEventArgs args)
{
if (args.ExceptionObject is ServerInitializationException initException)
{
Console.WriteLine("Server initialization failed, unhandled exception!");
Console.WriteLine(initException);
}
else
{
Console.WriteLine("Unhandled exception in runtime!");
Console.WriteLine(args.ExceptionObject);
}
Console.WriteLine("Press enter to close this window...");
Console.ReadLine();
}
}
}

View file

@ -1,7 +0,0 @@
{
"Network": {
"Host": "0.0.0.0",
"Port": 1337
},
"DispatchUrl": "http://localhost:8888"
}

View file

@ -1,10 +0,0 @@
namespace FreeSR.Admin
{
using FreeSR.Shared.Configuration;
internal class AdminServerConfiguration
{
public NetworkConfiguration Network { get; set; }
public string DispatchUrl { get; set; }
}
}

View file

@ -1,19 +0,0 @@
namespace FreeSR.Admin.Command
{
using FreeSR.Shared.Command.Context;
internal class AdminCommandContext : ICommandContext
{
public string Message { get; private set; }
public void SendError(string message)
{
Message = "Error: " + message;
}
public void SendMessage(string message)
{
Message = message;
}
}
}

View file

@ -1,17 +0,0 @@
namespace FreeSR.Admin.Command.Handlers
{
using FreeSR.Shared.Command;
using FreeSR.Shared.Command.Context;
using FreeSR.Shared.Configuration;
[Command("account")]
internal class AccountCommandCategory : CommandCategory
{
[Command("create")]
public void AccountCreateCommandHandler(ICommandContext context, string username, string password)
{
var config = ConfigurationManager<AdminServerConfiguration>.Instance.Model;
context.SendMessage($"dohttpreq={config.DispatchUrl}/sdk/createaccount?user={username}&pass={password}");
}
}
}

View file

@ -1,27 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ceen.Httpd" Version="0.9.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FreeSR.Shared\FreeSR.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="AdminServer.example.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="assets\console.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View file

@ -1,25 +0,0 @@
namespace FreeSR.Admin.Handlers
{
using Ceen;
using FreeSR.Admin.Service;
internal class ConsolePageRequestHandler : IHttpModule
{
public async Task<bool> HandleAsync(IHttpContext context)
{
context.Response.StatusCode = HttpStatusCode.OK;
await context.Response.WriteAllAsync(CreateHTMLDocument(), "text/html");
return true;
}
private static string CreateHTMLDocument()
{
string baseString = HttpAdminService.ConsoleHTML;
return baseString.Replace("%SERVER_VERSION%", "v0.1.0 dev - experimental open source")
.Replace("%GAME_VERSION%", "1.2.0");
}
}
}

View file

@ -1,23 +0,0 @@
namespace FreeSR.Admin.Handlers
{
using Ceen;
using FreeSR.Admin.Command;
using FreeSR.Shared.Command;
internal class ExecuteCommandRequestHandler : IHttpModule
{
public async Task<bool> HandleAsync(IHttpContext context)
{
var query = context.Request.QueryString;
string command = query["command"];
var ctx = new AdminCommandContext();
CommandManager.Instance.Invoke(ctx, command);
context.Response.StatusCode = HttpStatusCode.OK;
await context.Response.WriteAllAsync(ctx.Message, "text/plain");
return true;
}
}
}

View file

@ -1,43 +0,0 @@
namespace FreeSR.Admin.Service
{
using Ceen.Httpd;
using Ceen.Httpd.Logging;
using FreeSR.Admin.Handlers;
using FreeSR.Shared.Configuration;
using System.Net;
internal static class HttpAdminService
{
public static string ConsoleHTML { get; private set; }
private static ServerConfig s_httpdConfiguration;
public static void Initialize(NetworkConfiguration config)
{
LoadHtDocs();
s_httpdConfiguration = CreateConfiguration();
_ = BootHttpAsync(config);
}
private static void LoadHtDocs()
{
ConsoleHTML = File.ReadAllText("assets/console.html");
}
private static ServerConfig CreateConfiguration()
{
return new ServerConfig().AddLogger(new CLFStdOut())
.AddRoute("/console", new ConsolePageRequestHandler())
.AddRoute("/console/exec", new ExecuteCommandRequestHandler());
}
private static async Task BootHttpAsync(NetworkConfiguration config)
{
await HttpServer.ListenAsync(new IPEndPoint(
IPAddress.Parse(config.Host),
config.Port),
false, s_httpdConfiguration);
}
}
}

View file

@ -1,101 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>FreeSR</title>
<style>
body {
font-family: Arial, sans-serif;
}
#status {
font-weight: bold;
}
#console {
width: 100%;
height: 300px;
font-family: monospace;
background-color: #f0f0f0;
overflow-y: scroll;
}
#commandInput {
width: 100%;
padding: 5px;
}
#submitBtn {
padding: 5px 10px;
margin-top: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>FreeSR Control Panel</h1>
<div>
<h2>Status:</h2>
<p id="status">Loading...</p>
</div>
<div>
<h2>Server Statistics:</h2>
<p>Server build: %SERVER_VERSION%</p>
<p>Supported game version: %GAME_VERSION%</p>
</div>
<div>
<h2>Admin Console:</h2>
<textarea id="console" readonly></textarea>
<input type="text" id="commandInput" placeholder="Enter admin command...">
<button id="submitBtn">Execute</button>
</div>
<script>
const consoleOutput = document.getElementById('console');
const commandInput = document.getElementById('commandInput');
const submitBtn = document.getElementById('submitBtn');
// Function to update the status section (replace with actual server status)
function updateStatus(statusText) {
const statusElement = document.getElementById('status');
statusElement.innerText = statusText;
}
// Function to add a new line to the console
function addToConsole(text) {
consoleOutput.value += text + '\n';
consoleOutput.scrollTop = consoleOutput.scrollHeight;
}
function sendCommand() {
const command = commandInput.value;
if (command.length == 0)
return;
addToConsole(`> ${command}`);
fetch(`/console/exec?command=${encodeURIComponent(command)}`)
.then(response => response.text())
.then(data => {
if (data.startsWith("dohttpreq")) {
fetch(data.replace("dohttpreq=", ""))
.then(response => response.text())
.then(data => {
addToConsole(data);
});
}
else {
addToConsole(data);
}
})
.catch(error => {
addToConsole(`Error: ${error.message}`);
});
commandInput.value = '';
}
submitBtn.addEventListener('click', sendCommand);
</script>
</body>
</html>

View file

@ -1,59 +0,0 @@
namespace FreeSR.Database.Account
{
using FreeSR.Database.Account.Model;
using FreeSR.Database.Account.Util;
using FreeSR.Database.Mongo;
using MongoDB.Driver;
public class AccountDatabase : SRMongoDatabase<AccountModel>
{
private int _maxUid;
public AccountDatabase(IMongoDatabase database, string collectionName) : base(database, collectionName)
{
// AccountDatabase.
}
public async Task<AccountModel> Create(string name, string password)
{
if (_maxUid == 0)
_maxUid = await FetchMaxUid();
if (await GetByName(name) != null)
return null;
var model = new AccountModel
{
Uid = Interlocked.Increment(ref _maxUid),
Name = name,
Password = password,
CreationDateUtc = DateTime.UtcNow,
Token = AccountTokenUtil.Generate()
};
await Insert(model);
return model;
}
public async Task<AccountModel> GetByUid(int uid)
{
return await FindOne(account => account.Uid == uid);
}
public async Task<AccountModel> GetByName(string name)
{
return await FindOne(account => account.Name == name);
}
public async Task Update(AccountModel account)
{
await Update(model => model.Uid == account.Uid, account);
}
private async Task<int> FetchMaxUid()
{
var maxUidAccount = await FindMax(account => account.Uid);
return maxUidAccount?.Uid ?? 0;
}
}
}

View file

@ -1,13 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\FreeSR.Database\FreeSR.Database.csproj" />
</ItemGroup>
</Project>

View file

@ -1,14 +0,0 @@
namespace FreeSR.Database.Account.Model
{
using MongoDB.Bson.Serialization.Attributes;
[BsonIgnoreExtraElements]
public class AccountModel
{
public int Uid { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public string Token { get; set; }
public DateTime CreationDateUtc { get; set; }
}
}

View file

@ -1,37 +0,0 @@
namespace FreeSR.Database.Account.Util
{
using FreeSR.Database.Account.Model;
public static class AccountTokenUtil
{
private const int AccountTokenLength = 128;
private const string TokenCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
private static Random s_random;
static AccountTokenUtil()
{
s_random = new Random();
}
public static string Generate()
{
var token = "";
for (int i = 0; i < AccountTokenLength; i++)
{
token += TokenCharacters[s_random.Next(TokenCharacters.Length)];
}
return token;
}
public static bool Verify(AccountModel accountModel, string clientToken)
{
if (accountModel == null)
return false;
return string.Equals(accountModel.Token, clientToken);
}
}
}

View file

@ -1,9 +0,0 @@
namespace FreeSR.Database.Configuration
{
public class DatabaseConfiguration
{
public string ConnectionString { get; set; }
public string Name { get; set; }
public DatabaseEntry[] Entries { get; set; }
}
}

View file

@ -1,8 +0,0 @@
namespace FreeSR.Database.Configuration
{
public class DatabaseEntry
{
public string CollectionName { get; set; }
public DatabaseType Type { get; set; }
}
}

View file

@ -1,12 +0,0 @@
namespace FreeSR.Database.Configuration
{
using FreeSR.Shared.Exceptions;
internal class DatabaseMisconfiguredException : ServerInitializationException
{
public DatabaseMisconfiguredException(string message) : base(message)
{
// DatabaseMisconfiguredException.
}
}
}

View file

@ -1,7 +0,0 @@
namespace FreeSR.Database.Configuration
{
public enum DatabaseType
{
Account
}
}

View file

@ -1,55 +0,0 @@
namespace FreeSR.Database
{
using FreeSR.Database.Configuration;
using FreeSR.Shared;
using MongoDB.Driver;
using NLog;
public sealed class DatabaseManager : Singleton<DatabaseManager>
{
private static readonly Logger s_log = LogManager.GetCurrentClassLogger();
private DatabaseConfiguration _configuration;
private Dictionary<Type, object> _databases;
public IMongoDatabase MongoDatabase { get; private set; }
private DatabaseManager()
{
_databases = new Dictionary<Type, object>();
}
public void Initialize(DatabaseConfiguration configuration)
{
_configuration = configuration;
var mongoClient = new MongoClient(configuration.ConnectionString);
MongoDatabase = mongoClient.GetDatabase(configuration.Name);
}
public string GetCollectionName(DatabaseType databaseType)
{
foreach (var entry in _configuration.Entries)
{
if (entry.Type == databaseType)
return entry.CollectionName;
}
throw new DatabaseMisconfiguredException($"Can not find database of type {databaseType} in provided configuration.");
}
public DatabaseManager Add<T>(ISRDatabase<T> database) where T : class
{
_databases.Add(database.GetType(), database);
return this;
}
public T Get<T>() where T : class
{
if (_databases.TryGetValue(typeof(T), out var database))
return database as T;
return null;
}
}
}

View file

@ -1,17 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.20.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FreeSR.Shared\FreeSR.Shared.csproj" />
</ItemGroup>
</Project>

View file

@ -1,18 +0,0 @@
namespace FreeSR.Database
{
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
public interface ISRDatabase<T> where T : class
{
Task Insert(T document);
Task InsertMany(IEnumerable<T> documents);
Task<List<T>> Find(Expression<Func<T, bool>> filter);
Task<T> FindOne(Expression<Func<T, bool>> filter);
Task Update(Expression<Func<T, bool>> filter, T updatedDocument);
Task Delete(Expression<Func<T, bool>> filter);
Task<long> Count();
Task<T> FindMax(Expression<Func<T, object>> fieldSelector);
}
}

View file

@ -1,58 +0,0 @@
namespace FreeSR.Database.Mongo
{
using MongoDB.Driver;
using System.Linq.Expressions;
public class SRMongoDatabase<T> : ISRDatabase<T> where T : class
{
protected readonly IMongoCollection<T> _collection;
public SRMongoDatabase(IMongoDatabase database, string collectionName)
{
_collection = database.GetCollection<T>(collectionName);
}
public async Task Insert(T document)
{
await _collection.InsertOneAsync(document);
}
public async Task InsertMany(IEnumerable<T> documents)
{
await _collection.InsertManyAsync(documents);
}
public async Task<List<T>> Find(Expression<Func<T, bool>> filter)
{
var result = await _collection.FindAsync(filter);
return await result.ToListAsync();
}
public async Task<T> FindOne(Expression<Func<T, bool>> filter)
{
var result = await _collection.FindAsync(filter);
return await result.FirstOrDefaultAsync();
}
public async Task Update(Expression<Func<T, bool>> filter, T updatedDocument)
{
await _collection.ReplaceOneAsync(filter, updatedDocument);
}
public async Task Delete(Expression<Func<T, bool>> filter)
{
await _collection.DeleteOneAsync(filter);
}
public async Task<long> Count()
{
return await _collection.CountDocumentsAsync(Builders<T>.Filter.Empty);
}
public async Task<T> FindMax(Expression<Func<T, object>> fieldSelector)
{
var sortDefinition = Builders<T>.Sort.Descending(fieldSelector);
return await _collection.Find(Builders<T>.Filter.Empty).Sort(sortDefinition).FirstOrDefaultAsync();
}
}
}

View file

@ -1,8 +1,5 @@
namespace FreeSR.Dispatch
{
using FreeSR.Database;
using FreeSR.Database.Account;
using FreeSR.Database.Configuration;
using FreeSR.Dispatch.Service;
using FreeSR.Dispatch.Service.Manager;
using FreeSR.Shared.Configuration;
@ -30,11 +27,6 @@
ConfigurationManager<DispatchServerConfiguration>.Instance.Initialize("DispatchServer.json");
var serverConfiguration = ConfigurationManager<DispatchServerConfiguration>.Instance.Model;
DatabaseManager.Instance.Initialize(serverConfiguration.Database);
var mongoDatabase = DatabaseManager.Instance.MongoDatabase;
DatabaseManager.Instance.Add(new AccountDatabase(mongoDatabase, DatabaseManager.Instance.GetCollectionName(DatabaseType.Account)));
RegionManager.Initialize(serverConfiguration.Region);
HttpDispatchService.Initialize(serverConfiguration.Network);

View file

@ -3,16 +3,6 @@
"Host": "0.0.0.0",
"Port": 8888
},
"Database": {
"ConnectionString": "mongodb://127.0.0.1:27017/",
"Name": "FreeSR",
"Entries": [
{
"CollectionName": "accounts",
"Type": "Account"
}
]
},
"Region": {
"Name": "FreeSR",
"EnvType": "2",

View file

@ -1,13 +1,11 @@
namespace FreeSR.Dispatch
{
using FreeSR.Database.Configuration;
using FreeSR.Dispatch.Configuration;
using FreeSR.Shared.Configuration;
internal class DispatchServerConfiguration
{
public NetworkConfiguration Network { get; set; }
public DatabaseConfiguration Database { get; set; }
public RegionConfiguration Region { get; set; }
}
}

View file

@ -13,7 +13,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FreeSR.Database.Account\FreeSR.Database.Account.csproj" />
<ProjectReference Include="..\FreeSR.Proto\FreeSR.Proto.csproj" />
<ProjectReference Include="..\FreeSR.Shared\FreeSR.Shared.csproj" />
</ItemGroup>

View file

@ -1,9 +1,6 @@
namespace FreeSR.Dispatch.Handlers
{
using Ceen;
using FreeSR.Database;
using FreeSR.Database.Account;
using FreeSR.Database.Account.Model;
using FreeSR.Dispatch.Util;
using FreeSR.Proto;
using Newtonsoft.Json.Linq;
@ -19,30 +16,17 @@
string data = await context.Request.Body.ReadAllAsStringAsync();
JObject loginJson = JObject.Parse(data);
AccountDatabase accountDatabase = DatabaseManager.Instance.Get<AccountDatabase>();
string accountName = (string)loginJson["account"];
string password = (string)loginJson["password"];
AccountModel account = await accountDatabase.GetByName(accountName);
if (account == null)
{
await context.Response.WriteAllJsonAsync(DispatchResponseBuilder.Create()
.Retcode((int)RetcodeStatus.RetFail)
.Message("Account not found.")
.Object("data", null)
.Build());
return true;
}
// no password check, because client patch is closed-source for now.
var accountData = DispatchHelper.ToLoginResponseData();
await context.Response.WriteAllJsonAsync(DispatchResponseBuilder.Create()
.Retcode((int)RetcodeStatus.RetSucc)
.Message("OK")
.Object("data", new JObject
{
{"account", account.ToLoginResponseData()},
{"account", accountData},
{"device_grant_required", false},
{"safe_moblie_required", false},
{"realperson_required", false},

View file

@ -1,9 +1,6 @@
namespace FreeSR.Dispatch.Handlers
{
using Ceen;
using FreeSR.Database;
using FreeSR.Database.Account;
using FreeSR.Database.Account.Model;
using FreeSR.Dispatch.Util;
using FreeSR.Proto;
using Newtonsoft.Json.Linq;
@ -13,41 +10,14 @@
{
public async Task<bool> HandleAsync(IHttpContext context)
{
var data = await context.Request.Body.ReadAllAsStringAsync();
var json = JObject.Parse(data);
var uid = int.Parse((string)json["uid"]);
var token = (string)json["token"];
AccountDatabase accountDatabase = DatabaseManager.Instance.Get<AccountDatabase>();
AccountModel account = await accountDatabase.GetByUid(uid);
if (account == null)
{
await context.Response.WriteAllJsonAsync(DispatchResponseBuilder.Create()
.Retcode((int)RetcodeStatus.RetFail)
.Message("Account not found.")
.Object("data", null)
.Build());
return true;
}
else if (account.Token != token)
{
await context.Response.WriteAllJsonAsync(DispatchResponseBuilder.Create()
.Retcode((int)RetcodeStatus.RetFail)
.Message("Invalid user token.")
.Object("data", null)
.Build());
return true;
}
var accountData = DispatchHelper.ToLoginResponseData();
await context.Response.WriteAllJsonAsync(DispatchResponseBuilder.Create()
.Retcode((int)RetcodeStatus.RetSucc)
.Message("OK")
.Object("data", new JObject
{
{"account", account.ToLoginResponseData()},
{"account", accountData},
{"device_grant_required", false},
{"safe_moblie_required", false},
{"realperson_required", false},

View file

@ -3,7 +3,6 @@
using Ceen.Httpd;
using Ceen.Httpd.Logging;
using FreeSR.Dispatch.Handlers;
using FreeSR.Dispatch.Handlers.Sdk;
using FreeSR.Shared.Configuration;
using System.Net;
@ -30,8 +29,7 @@
.AddRoute("/account/risky/api/check", new RiskyApiCheckHandler())
.AddRoute("/hkrpg_global/mdk/agreement/api/getAgreementInfos", new GetAgreementInfosHandler())
.AddRoute("/data_abtest_api/config/experiment/list", new GetExperimentListHandler())
.AddRoute("/hkrpg_global/combo/granter/api/getConfig", new ComboGranterApiGetConfigHandler())
.AddRoute("/sdk/createaccount", new CreateAccountHandler());
.AddRoute("/hkrpg_global/combo/granter/api/getConfig", new ComboGranterApiGetConfigHandler());
}
private static async Task BootHttpAsync(NetworkConfiguration config)

View file

@ -1,17 +1,16 @@
namespace FreeSR.Dispatch.Util
{
using FreeSR.Database.Account.Model;
using Newtonsoft.Json.Linq;
internal static class DispatchHelper
{
public static JObject ToLoginResponseData(this AccountModel model)
public static JObject ToLoginResponseData()
{
return new JObject
{
{"uid", model.Uid},
{"name", model.Name},
{"email", "reversedrooms"},
{"uid", 1337},
{"name", "reversedrooms"},
{"email", "reversedrooms@mihomo.com"},
{"mobile", ""},
{"is_email_verify", "0"},
{"realname", ""},
@ -31,7 +30,7 @@
{"steam_name", ""},
{"unmasked_email", ""},
{"unmasked_email_type", 0},
{"token", model.Token}
{"token", "FreesrToken"}
};
}
}