cleaned up configuration files to use appsettings

This commit is contained in:
RaidMax 2018-03-14 00:36:25 -05:00
parent 81d965a718
commit c1a1d65261
22 changed files with 199 additions and 170 deletions

View File

@ -247,7 +247,7 @@ namespace StatsPlugin.Helpers
//statsSvc.KillStatsSvc.Insert(kill);
//await statsSvc.KillStatsSvc.SaveChangesAsync();
if (attacker.CurrentServer.Config.EnableAntiCheat)
if(Manager.GetApplicationSettings().EnableAntiCheat)
{
async Task executePenalty(Cheat.DetectionPenaltyResult penalty)
{

View File

@ -92,6 +92,7 @@ namespace StatsPlugin
int deaths = clientStats.Sum(c => c.Deaths);
double kdr = Math.Round(kills / (double)deaths, 2);
double skill = Math.Round(clientStats.Sum(c => c.Skill) / clientStats.Count, 2);
double spm = Math.Round(clientStats.Sum(c => c.SPM), 1);
double chestRatio = 0;
double abdomenRatio = 0;
@ -135,6 +136,11 @@ namespace StatsPlugin
Value = skill
},
new ProfileMeta()
{
Key = "Score Per Minute",
Value = spm
},
new ProfileMeta()
{
Key = "Chest Ratio",
Value = chestRatio,

View File

@ -150,7 +150,11 @@
<Name>SharedLibrary</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions">
<Version>1.1.2</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>copy /Y "$(TargetDir)$(TargetName).dll" "$(SolutionDir)BUILD\plugins\"</PostBuildEvent>

View File

@ -424,9 +424,9 @@ namespace SharedLibrary.Commands
if (newPerm == Player.Permission.Owner && E.Origin.Level != Player.Permission.Console)
newPerm = Player.Permission.Banned;
if (newPerm == Player.Permission.Owner && !E.Owner.Config.AllowMultipleOwners)
if (newPerm == Player.Permission.Owner && !E.Owner.Manager.GetApplicationSettings().EnableMultipleOwners)
{
await E.Origin.Tell("There can only be 1 owner. Modify your server configuration if multiple owners are required");
await E.Origin.Tell("There can only be 1 owner. Modify your appsettings if multiple owners are required");
return;
}
@ -1033,13 +1033,15 @@ namespace SharedLibrary.Commands
#endif
process.StartInfo.FileName = $"{process.StartInfo.WorkingDirectory}\\iw4x.exe";
process.StartInfo.Arguments = commandLine.Substring(6);
process.StartInfo.UserName = E.Owner.Config.RestartUsername;
/*process.StartInfo.UserName = E.Owner.ServerConfig.RestartUsername;
var pw = new System.Security.SecureString();
foreach (char c in E.Owner.Config.RestartPassword)
foreach (char c in E.Owner.ServerConfig.RestartPassword)
pw.AppendChar(c);
process.StartInfo.Password = pw;
*/
process.Start();
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SharedLibrary.Configuration
{
public class ApplicationConfiguration
{
public bool EnableMultipleOwners { get; set; }
public bool EnableTrustedRank { get; set; }
public bool EnableClientVPNs { get; set; }
public bool EnableAntiCheat { get; set; }
public bool EnableDiscordLink { get; set; }
public string DiscordInviteCode { get; set; }
public string IPHubAPIKey { get; set; }
public List<ServerConfiguration> Servers { get; set; }
}
}

View File

@ -0,0 +1,9 @@
namespace SharedLibrary.Configuration
{
public class ServerConfiguration
{
public string IPAddress { get; set; }
public short Port { get; set; }
public string Password { get; set; }
}
}

View File

@ -3,6 +3,8 @@ using SharedLibrary.Objects;
using SharedLibrary.Database.Models;
using SharedLibrary.Services;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using SharedLibrary.Configuration;
namespace SharedLibrary.Interfaces
{
@ -16,6 +18,7 @@ namespace SharedLibrary.Interfaces
IList<Command> GetCommands();
IList<Helpers.MessageToken> GetMessageTokens();
IList<Player> GetActiveClients();
ApplicationConfiguration GetApplicationSettings();
ClientService GetClientService();
AliasService GetAliasService();
PenaltyService GetPenaltyService();

View File

@ -11,6 +11,7 @@ using System.Threading.Tasks;
using SharedLibrary.Helpers;
using SharedLibrary.Objects;
using SharedLibrary.Dtos;
using SharedLibrary.Configuration;
namespace SharedLibrary
{
@ -30,11 +31,11 @@ namespace SharedLibrary
public Server(Interfaces.IManager mgr, ServerConfiguration config)
{
Password = config.Password;
IP = config.IP;
IP = config.IPAddress;
Port = config.Port;
Manager = mgr;
Logger = Manager.GetLogger();
Config = config;
ServerConfig = config;
Players = new List<Player>(new Player[18]);
Reports = new List<Report>();
@ -360,7 +361,7 @@ namespace SharedLibrary
// Objects
public Interfaces.IManager Manager { get; protected set; }
public Interfaces.ILogger Logger { get; private set; }
public ServerConfiguration Config { get; private set; }
public ServerConfiguration ServerConfig { get; private set; }
public List<Map> Maps { get; protected set; }
public List<string> Rules { get; protected set; }
public List<Report> Reports { get; set; }

View File

@ -1,23 +0,0 @@
using SharedLibrary.Interfaces;
namespace SharedLibrary
{
public class ServerConfiguration : Serialize<ServerConfiguration>
{
public string IP;
public int Port;
public string Password;
public string FtpPrefix;
public bool AllowMultipleOwners;
public bool AllowTrustedRank;
public string RestartUsername;
public string RestartPassword;
public bool EnableAntiCheat;
public bool AllowClientVpn;
public override string Filename()
{
return $"{Utilities.OperatingDirectory}config/servers/{IP}_{Port}.cfg";
}
}
}

View File

@ -146,6 +146,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Configuration\ServerConfiguration.cs" />
<Compile Include="Database\Importer.cs" />
<Compile Include="Database\DatabaseContext.cs" />
<Compile Include="Database\Models\EFAlias.cs" />
@ -198,7 +199,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RCON.cs" />
<Compile Include="Server.cs" />
<Compile Include="ServerConfiguration.cs" />
<Compile Include="Configuration\ApplicationConfiguration.cs" />
<Compile Include="Services\AliasService.cs" />
<Compile Include="Services\ClientService.cs" />
<Compile Include="Services\GenericRepository.cs" />
@ -226,6 +227,9 @@
<PackageReference Include="EntityFramework.SqlServerCompact">
<Version>6.2.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions">
<Version>1.1.2</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>11.0.1</Version>
</PackageReference>

View File

@ -0,0 +1,96 @@
using SharedLibrary;
using SharedLibrary.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace IW4MAdmin
{
class ConfigurationGenerator
{
public static List<ServerConfiguration> GenerateServerConfig(List<ServerConfiguration> configList)
{
var newConfig = new ServerConfiguration();
while (string.IsNullOrEmpty(newConfig.IPAddress))
{
try
{
Console.Write("Enter server IP Address: ");
string input = Console.ReadLine();
IPAddress.Parse(input);
newConfig.IPAddress = input;
}
catch (Exception)
{
continue;
}
}
while (newConfig.Port == 0)
{
try
{
Console.Write("Enter server port: ");
newConfig.Port = Int16.Parse(Console.ReadLine());
}
catch (Exception)
{
continue;
}
}
Console.Write("Enter server RCON password: ");
newConfig.Password = Console.ReadLine();
configList.Add(newConfig);
Console.Write("Configuration saved, add another? [y/n]:");
if (Console.ReadLine().ToLower().First() == 'y')
GenerateServerConfig(configList);
return configList;
}
public static ApplicationConfiguration GenerateApplicationConfig()
{
var config = new ApplicationConfiguration();
Console.Write("Enable multiple owners? [y/n]: ");
config.EnableMultipleOwners = (Console.ReadLine().ToLower().FirstOrDefault() as char?) == 'y';
Console.Write("Enable trusted rank? [y/n]: ");
config.EnableTrustedRank = (Console.ReadLine().ToLower().FirstOrDefault() as char?) == 'y';
Console.Write("Enable server-side anti-cheat [y/n]: ");
config.EnableAntiCheat = (Console.ReadLine().ToLower().FirstOrDefault() as char?) == 'y';
Console.Write("Enable client VPNS [y/n]: ");
config.EnableClientVPNs = (Console.ReadLine().ToLower().FirstOrDefault() as char?) == 'y';
if (!config.EnableClientVPNs)
{
Console.Write("Enter iphub.info api key: ");
config.IPHubAPIKey = Console.ReadLine();
}
Console.Write("Display Discord link on webfront [y/n]: ");
config.EnableDiscordLink = (Console.ReadLine().ToLower().FirstOrDefault() as char?) == 'y';
if (config.EnableDiscordLink)
{
Console.Write("Enter Discord invite link: ");
config.DiscordInviteCode = Console.ReadLine();
}
return config;
}
}
}

View File

@ -13,6 +13,11 @@ using SharedLibrary.Exceptions;
using SharedLibrary.Objects;
using SharedLibrary.Services;
using WebfrontCore.Application.API;
using Microsoft.Extensions.Configuration;
using WebfrontCore;
using SharedLibrary.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace IW4MAdmin
{
@ -32,6 +37,7 @@ namespace IW4MAdmin
ClientService ClientSvc;
AliasService AliasSvc;
PenaltyService PenaltySvc;
IConfigurationRoot AppSettings;
#if FTP_LOG
const int UPDATE_FREQUENCY = 700;
#else
@ -40,7 +46,7 @@ namespace IW4MAdmin
private ApplicationManager()
{
Logger = new Logger($@"{SharedLibrary.Utilities.OperatingDirectory}Logs{Path.DirectorySeparatorChar}IW4MAdmin.log");
Logger = new Logger($@"{Utilities.OperatingDirectory}Logs{Path.DirectorySeparatorChar}IW4MAdmin.log");
_servers = new List<Server>();
Commands = new List<Command>();
TaskStatuses = new List<AsyncStatus>();
@ -52,6 +58,13 @@ namespace IW4MAdmin
ServerEventOccurred += EventAPI.OnServerEventOccurred;
}
private void BuildConfiguration()
{
AppSettings = new ConfigurationBuilder()
.AddJsonFile($"{AppDomain.CurrentDomain.BaseDirectory}IW4MAdminSettings.json")
.Build();
}
public IList<Server> GetServers()
{
return Servers;
@ -95,15 +108,21 @@ namespace IW4MAdmin
#endregion
#region CONFIG
var Configs = Directory.EnumerateFiles($"{Program.OperatingDirectory}config/servers").Where(x => x.Contains(".cfg"));
BuildConfiguration();
var settings = AppSettings.Get<ApplicationConfiguration>();
if (Configs.Count() == 0)
ServerConfigurationGenerator.Generate();
foreach (var file in Configs)
if (settings == null)
{
var Conf = ServerConfiguration.Read(file);
settings = ConfigurationGenerator.GenerateApplicationConfig();
settings.Servers = ConfigurationGenerator.GenerateServerConfig(new List<ServerConfiguration>());
var appConfigJSON = JsonConvert.SerializeObject(settings, Formatting.Indented);
File.WriteAllText($"{AppDomain.CurrentDomain.BaseDirectory}IW4MAdminSettings.json", appConfigJSON);
BuildConfiguration();
}
foreach (var Conf in settings.Servers)
{
try
{
var ServerInstance = new IW4MServer(this, Conf);
@ -126,7 +145,7 @@ namespace IW4MAdmin
catch (ServerException e)
{
Logger.WriteError($"Not monitoring server {Conf.IP}:{Conf.Port} due to uncorrectable errors");
Logger.WriteError($"Not monitoring server {Conf.IPAddress}:{Conf.Port} due to uncorrectable errors");
if (e.GetType() == typeof(DvarException))
Logger.WriteDebug($"Could not get the dvar value for {(e as DvarException).Data["dvar_name"]} (ensure the server has a map loaded)");
else if (e.GetType() == typeof(NetworkException))
@ -260,5 +279,7 @@ namespace IW4MAdmin
public ClientService GetClientService() => ClientSvc;
public AliasService GetAliasService() => AliasSvc;
public PenaltyService GetPenaltyService() => PenaltySvc;
public ApplicationConfiguration GetApplicationSettings() => AppSettings.Get<ApplicationConfiguration>();
}
}

View File

@ -9,7 +9,7 @@ namespace WebfrontCore.Application.Misc
{
public class VPNCheck
{
public static async Task<bool> UsingVPN(string ip)
public static async Task<bool> UsingVPN(string ip, string apiKey)
{
#if DEBUG
return false;
@ -18,7 +18,7 @@ namespace WebfrontCore.Application.Misc
{
using (var RequestClient = new System.Net.Http.HttpClient())
{
RequestClient.DefaultRequestHeaders.Add("X-Key", Startup.Configuration["VPN:APIKey"]);
RequestClient.DefaultRequestHeaders.Add("X-Key", apiKey);
string response = await RequestClient.GetStringAsync($"http://v2.api.iphub.info/ip/{ip}");
var responseJson = JsonConvert.DeserializeObject<JObject>(response);
int blockType = Convert.ToInt32(responseJson["block"]);

View File

@ -14,6 +14,7 @@ using SharedLibrary.Services;
using SharedLibrary.Database.Models;
using SharedLibrary.Dtos;
using WebfrontCore.Application.Misc;
using SharedLibrary.Configuration;
namespace IW4MAdmin
{
@ -143,7 +144,8 @@ namespace IW4MAdmin
await ExecuteEvent(new Event(Event.GType.Connect, "", player, null, this));
if (Config.AllowClientVpn && await VPNCheck.UsingVPN(player.IPAddressString))
if (!Manager.GetApplicationSettings().EnableClientVPNs &&
await VPNCheck.UsingVPN(player.IPAddressString, Manager.GetApplicationSettings().IPHubAPIKey))
{
await player.Kick("VPNs are not allowed on this server", new Player() { ClientId = 1 });
}
@ -621,7 +623,7 @@ namespace IW4MAdmin
string logPath = (game.Value == "" || onelog?.Value == 1) ?
$"{basepath.Value.Replace('\\', Path.DirectorySeparatorChar)}{Path.DirectorySeparatorChar}{mainPath}{Path.DirectorySeparatorChar}{logfile.Value}" :
$"{basepath.Value.Replace('\\', Path.DirectorySeparatorChar)}{Path.DirectorySeparatorChar}{game.Value.Replace('\\', Path.DirectorySeparatorChar)}{Path.DirectorySeparatorChar}{logfile.Value}";
$"{basepath.Value.Replace('\\', Path.DirectorySeparatorChar)}{Path.DirectorySeparatorChar}{game.Value.Replace('/', Path.DirectorySeparatorChar)}{Path.DirectorySeparatorChar}{logfile.Value}";
if (!File.Exists(logPath))
{
@ -661,7 +663,7 @@ namespace IW4MAdmin
await E.Origin.Tell($"There are ^5{Reports.Count} ^7recent reports");
// give trusted rank
if (Config.AllowTrustedRank &&
if (Manager.GetApplicationSettings().EnableTrustedRank &&
E.Origin.TotalConnectionTime / 60.0 >= 2880 &&
E.Origin.Level < Player.Permission.Trusted &&
E.Origin.Level != Player.Permission.Flagged)

View File

@ -1,90 +0,0 @@
using SharedLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace IW4MAdmin
{
class ServerConfigurationGenerator
{
public static ServerConfiguration Generate()
{
string IP = String.Empty;
int Port = 0;
string Password;
bool AllowMultipleOwners;
bool AllowTrustedRank;
bool AntiCheat;
bool AllowVpns;
while (IP == String.Empty)
{
try
{
Console.Write("Enter server IP: ");
string input = Console.ReadLine();
IPAddress.Parse(input);
IP = input;
}
catch (Exception)
{
continue;
}
}
while (Port == 0)
{
try
{
Console.Write("Enter server port: ");
Port = Int32.Parse(Console.ReadLine());
}
catch (Exception)
{
continue;
}
}
Console.Write("Enter server RCON password: ");
Password = Console.ReadLine();
Console.Write("Allow multiple owners? [y/n]: ");
AllowMultipleOwners = (Console.ReadLine().ToLower().FirstOrDefault() as char?) == 'y';
Console.Write("Allow trusted rank? [y/n]: ");
AllowTrustedRank = (Console.ReadLine().ToLower().FirstOrDefault() as char?) == 'y';
Console.Write("Allow server-side anti-cheat [y/n]: ");
AntiCheat = (Console.ReadLine().ToLower().FirstOrDefault() as char?) == 'y';
Console.Write("Allow client VPNS [y/n]: ");
AllowVpns = (Console.ReadLine().ToLower().FirstOrDefault() as char?) == 'y';
var config = new ServerConfiguration()
{
IP = IP,
Password = Password,
Port = Port,
AllowMultipleOwners = AllowMultipleOwners,
AllowTrustedRank = AllowTrustedRank,
RestartPassword = "",
RestartUsername = "",
EnableAntiCheat = AntiCheat,
AllowClientVpn = AllowVpns
};
config.Write();
Console.Write("Configuration saved, add another? [y/n]:");
if (Console.ReadLine().ToLower().First() == 'y')
Generate();
return config;
}
}
}

View File

@ -22,7 +22,7 @@ namespace WebfrontCore.Controllers
Manager.AdministratorIPs.Contains(context.HttpContext.Connection.RemoteIpAddress.ToString().ConvertToIP());
ViewBag.Authorized = Authorized;
ViewBag.Url = Startup.Configuration["Web:Address"];
ViewBag.DiscordLink = Startup.Configuration["Discord:InviteLink"];
ViewBag.DiscordLink = Manager.GetApplicationSettings().DiscordInviteCode;
base.OnActionExecuting(context);
}
}

View File

View File

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SharedLibrary.Configuration;
namespace WebfrontCore
{
@ -16,8 +17,7 @@ namespace WebfrontCore
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddEnvironmentVariables();
Configuration = builder.Build();

View File

@ -34,9 +34,9 @@
<li class="nav-item text-center text-md-left">@Html.ActionLink("Penalties", "List", "Penalty", new { area = "" }, new { @class = "nav-link" })</li>
<li class="nav-item text-center text-md-left">@Html.ActionLink("Admins", "PrivilegedAsync", "Client", new { area = "" }, new { @class = "nav-link" })</li>
<li class="nav-item text-center text-md-left">@Html.ActionLink("Console", "Index", "Console", new { area = "" }, new { @class = "nav-link" })</li>
@if (ViewBag.DiscordLink != string.Empty)
@if (!string.IsNullOrEmpty(ViewBag.DiscordLink))
{
<li class="nav-item text-center text-md-left"><a href="@ViewBag.DiscordLink" target="_blank"></a></li>
<li class="nav-item text-center text-md-left"><a href="@ViewBag.DiscordLink" class="nav-link" target="_blank">Discord</a></li>
}
</ul>
<form class="form-inline text-primary pt-3 pb-3" method="get" action="/Client/FindAsync">

View File

@ -63,6 +63,12 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Update="IW4MAdminSettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy /Y &quot;$(SolutionDir)BUILD\Plugins&quot; &quot;$(TargetDir)Plugins\&quot;&#xD;&#xA;xcopy /Y /I /E &quot;$(SolutionDir)BUILD\Lib&quot; &quot;$(TargetDir)&quot; &#xD;&#xA;&#xD;&#xA;xcopy /Y /I /E &quot;$(ProjectDir)Application\Config&quot; &quot;$(TargetDir)Config&quot;" />
</Target>

View File

@ -1,21 +0,0 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "None"
}
},
"Web": {
"Address": "127.0.0.1:5000"
},
"VPN": {
"APIKey": ""
},
"IW4MAdmin": {
},
"Discord": {
"InviteLink" : ""
}
}

View File

@ -9,13 +9,5 @@
},
"Web": {
"Address": "http://127.0.0.1:5000"
},
"VPN": {
"APIKey": ""
},
"IW4MAdmin": {
},
"Discord": {
"InviteLink" : ""
}
}