Refactoring of BNetServer

This commit is contained in:
hondacrx
2020-07-12 00:06:43 -04:00
parent 4164384b72
commit 581d077acd
318 changed files with 2046 additions and 4694 deletions
+10 -25
View File
@@ -1,28 +1,13 @@
/*
* Copyright (C) 2012-2020 CypherCore <http://github.com/CypherCore>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Copyright (c) CypherCore <http://github.com/CypherCore> All rights reserved.
// Licensed under the GNU GENERAL PUBLIC LICENSE. See LICENSE file in the project root for full license information.
using BNetServer.Services;
using System;
using System.Collections.Generic;
using System.Text;
using BNetServer;
namespace BNetServer
public static class Global
{
public static class Global
{
public static RealmManager RealmMgr { get { return RealmManager.Instance; } }
public static SessionManager SessionMgr { get { return SessionManager.Instance; } }
public static ServiceDispatcher ServiceDispatcher { get { return ServiceDispatcher.Instance; } }
}
}
public static RealmManager RealmMgr { get { return RealmManager.Instance; } }
public static LoginServiceManager LoginServiceMgr { get { return LoginServiceManager.Instance; } }
}
@@ -0,0 +1,195 @@
// Copyright (c) CypherCore <http://github.com/CypherCore> All rights reserved.
// Licensed under the GNU GENERAL PUBLIC LICENSE. See LICENSE file in the project root for full license information.
using BNetServer.Networking;
using Framework.Configuration;
using Framework.Constants;
using Framework.Web;
using Google.Protobuf;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Net;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
namespace BNetServer
{
public class LoginServiceManager : Singleton<LoginServiceManager>
{
ConcurrentDictionary<(uint ServiceHash, uint MethodId), BnetServiceHandler> serviceHandlers;
FormInputs formInputs;
IPEndPoint externalAddress;
IPEndPoint localAddress;
X509Certificate2 certificate;
LoginServiceManager()
{
serviceHandlers = new ConcurrentDictionary<(uint ServiceHash, uint MethodId), BnetServiceHandler>();
formInputs = new FormInputs();
}
public void Initialize()
{
int port = ConfigMgr.GetDefaultValue("LoginREST.Port", 8081);
if (port < 0 || port > 0xFFFF)
{
Log.outError(LogFilter.Network, $"Specified login service port ({port}) out of allowed range (1-65535), defaulting to 8081");
port = 8081;
}
string configuredAddress = ConfigMgr.GetDefaultValue("LoginREST.ExternalAddress", "127.0.0.1");
IPAddress address;
if (!IPAddress.TryParse(configuredAddress, out address))
{
Log.outError(LogFilter.Network, $"Could not resolve LoginREST.ExternalAddress {configuredAddress}");
return;
}
externalAddress = new IPEndPoint(address, port);
configuredAddress = ConfigMgr.GetDefaultValue("LoginREST.LocalAddress", "127.0.0.1");
if (!IPAddress.TryParse(configuredAddress, out address))
{
Log.outError(LogFilter.Network, $"Could not resolve LoginREST.ExternalAddress {configuredAddress}");
return;
}
localAddress = new IPEndPoint(address, port);
// set up form inputs
formInputs.Type = "LOGIN_FORM";
var input = new FormInput();
input.Id = "account_name";
input.Type = "text";
input.Label = "E-mail";
input.MaxLength = 320;
formInputs.Inputs.Add(input);
input = new FormInput();
input.Id = "password";
input.Type = "password";
input.Label = "Password";
input.MaxLength = 16;
formInputs.Inputs.Add(input);
input = new FormInput();
input.Id = "log_in_submit";
input.Type = "submit";
input.Label = "Log In";
formInputs.Inputs.Add(input);
certificate = new X509Certificate2("BNetServer.pfx");
Assembly currentAsm = Assembly.GetExecutingAssembly();
foreach (var type in currentAsm.GetTypes())
{
foreach (var methodInfo in type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic))
{
foreach (var serviceAttr in methodInfo.GetCustomAttributes<ServiceAttribute>())
{
if (serviceAttr == null)
continue;
var key = (serviceAttr.ServiceHash, serviceAttr.MethodId);
if (serviceHandlers.ContainsKey(key))
{
Log.outError(LogFilter.Network, $"Tried to override ServiceHandler: {serviceHandlers[key]} with {methodInfo.Name} (ServiceHash: {serviceAttr.ServiceHash} MethodId: {serviceAttr.MethodId})");
continue;
}
var parameters = methodInfo.GetParameters();
if (parameters.Length == 0)
{
Log.outError(LogFilter.Network, $"Method: {methodInfo.Name} needs atleast one paramter");
continue;
}
serviceHandlers[key] = new BnetServiceHandler(methodInfo, parameters);
}
}
}
}
public BnetServiceHandler GetHandler(uint serviceHash, uint methodId)
{
return serviceHandlers.LookupByKey((serviceHash, methodId));
}
public IPEndPoint GetAddressForClient(IPAddress address)
{
if (IPAddress.IsLoopback(address))
return localAddress;
return externalAddress;
}
public FormInputs GetFormInput()
{
return formInputs;
}
public X509Certificate2 GetCertificate()
{
return certificate;
}
}
public class BnetServiceHandler
{
Delegate methodCaller;
Type requestType;
Type responseType;
public BnetServiceHandler(MethodInfo info, ParameterInfo[] parameters)
{
requestType = parameters[0].ParameterType;
if (parameters.Length > 1)
responseType = parameters[1].ParameterType;
if (responseType != null)
methodCaller = info.CreateDelegate(Expression.GetDelegateType(new[] { typeof(Session), requestType, responseType, info.ReturnType }));
else
methodCaller = info.CreateDelegate(Expression.GetDelegateType(new[] { typeof(Session), requestType, info.ReturnType }));
}
public void Invoke(Session session, uint token, CodedInputStream stream)
{
var request = (IMessage)Activator.CreateInstance(requestType);
request.MergeFrom(stream);
BattlenetRpcErrorCode status;
if (responseType != null)
{
var response = (IMessage)Activator.CreateInstance(responseType);
status = (BattlenetRpcErrorCode)methodCaller.DynamicInvoke(session, request, response);
Log.outDebug(LogFilter.ServiceProtobuf, "{0} Client called server Method: {1}) Returned: {2} Status: {3}.", session.GetClientInfo(), request, response, status);
if (status == 0)
session.SendResponse(token, response);
else
session.SendResponse(token, status);
}
else
{
status = (BattlenetRpcErrorCode)methodCaller.DynamicInvoke(session, request);
Log.outDebug(LogFilter.ServiceProtobuf, "{0} Client called server Method: {1}) Status: {2}.", session.GetClientInfo(), request, status);
if (status != 0)
session.SendResponse(token, status);
}
}
}
[AttributeUsage(AttributeTargets.Method)]
public sealed class ServiceAttribute : System.Attribute
{
public uint ServiceHash { get; set; }
public uint MethodId { get; set; }
public ServiceAttribute(OriginalHash serviceHash, uint methodId)
{
ServiceHash = (uint)serviceHash;
MethodId = methodId;
}
}
}
@@ -1,116 +0,0 @@
/*
* Copyright (C) 2012-2020 CypherCore <http://github.com/CypherCore>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Framework.Configuration;
using Framework.Rest;
using System.Net;
using System.Security.Cryptography.X509Certificates;
namespace BNetServer
{
public class SessionManager : Singleton<SessionManager>
{
SessionManager()
{
_formInputs = new FormInputs();
}
public bool Initialize()
{
int _port = ConfigMgr.GetDefaultValue("LoginREST.Port", 8081);
if (_port < 0 || _port > 0xFFFF)
{
Log.outError(LogFilter.Network, "Specified login service port ({0}) out of allowed range (1-65535), defaulting to 8081", _port);
_port = 8081;
}
string configuredAddress = ConfigMgr.GetDefaultValue("LoginREST.ExternalAddress", "127.0.0.1");
IPAddress address;
if (!IPAddress.TryParse(configuredAddress, out address))
{
Log.outError(LogFilter.Network, "Could not resolve LoginREST.ExternalAddress {0}", configuredAddress);
return false;
}
_externalAddress = new IPEndPoint(address, _port);
configuredAddress = ConfigMgr.GetDefaultValue("LoginREST.LocalAddress", "127.0.0.1");
if (!IPAddress.TryParse(configuredAddress, out address))
{
Log.outError(LogFilter.Network, "Could not resolve LoginREST.ExternalAddress {0}", configuredAddress);
return false;
}
_localAddress = new IPEndPoint(address, _port);
// set up form inputs
_formInputs.Type = "LOGIN_FORM";
var input = new FormInput();
input.Id = "account_name";
input.Type = "text";
input.Label = "E-mail";
input.MaxLength = 320;
_formInputs.Inputs.Add(input);
input = new FormInput();
input.Id = "password";
input.Type = "password";
input.Label = "Password";
input.MaxLength = 16;
_formInputs.Inputs.Add(input);
input = new FormInput();
input.Id = "log_in_submit";
input.Type = "submit";
input.Label = "Log In";
_formInputs.Inputs.Add(input);
_certificate = new X509Certificate2("BNetServer.pfx");
return true;
}
public IPEndPoint GetAddressForClient(IPAddress address)
{
if (IPAddress.IsLoopback(address))
return _localAddress;
return _externalAddress;
}
public FormInputs GetFormInput()
{
return _formInputs;
}
public X509Certificate2 GetCertificate()
{
return _certificate;
}
FormInputs _formInputs;
IPEndPoint _externalAddress;
IPEndPoint _localAddress;
X509Certificate2 _certificate;
}
public enum BanMode
{
Ip = 0,
Account = 1
}
}