refactored the welcome plugin to use a web api instead of a hard coded file

removed deprecated file class
don't wait for response when setting dvar/sending command in T6
potential fix for duplicate kick message in JS plugin
This commit is contained in:
RaidMax 2018-10-08 21:15:59 -05:00
parent b289917319
commit 7caee55a7e
13 changed files with 57 additions and 296 deletions

View File

@ -6,7 +6,7 @@
<RuntimeFrameworkVersion>2.1.5</RuntimeFrameworkVersion> <RuntimeFrameworkVersion>2.1.5</RuntimeFrameworkVersion>
<MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish> <MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
<PackageId>RaidMax.IW4MAdmin.Application</PackageId> <PackageId>RaidMax.IW4MAdmin.Application</PackageId>
<Version>2.1.9.5</Version> <Version>2.1.9.6</Version>
<Authors>RaidMax</Authors> <Authors>RaidMax</Authors>
<Company>Forever None</Company> <Company>Forever None</Company>
<Product>IW4MAdmin</Product> <Product>IW4MAdmin</Product>
@ -31,8 +31,8 @@
<PropertyGroup> <PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection> <ServerGarbageCollection>true</ServerGarbageCollection>
<TieredCompilation>true</TieredCompilation> <TieredCompilation>true</TieredCompilation>
<AssemblyVersion>2.1.9.5</AssemblyVersion> <AssemblyVersion>2.1.9.6</AssemblyVersion>
<FileVersion>2.1.9.5</FileVersion> <FileVersion>2.1.9.6</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -9,8 +9,6 @@ using SharedLibraryCore.Objects;
using SharedLibraryCore.RCon; using SharedLibraryCore.RCon;
using SharedLibraryCore.Exceptions; using SharedLibraryCore.Exceptions;
using System.Text; using System.Text;
using System.Linq;
using System.Net.Http;
namespace IW4MAdmin.Application.RconParsers namespace IW4MAdmin.Application.RconParsers
{ {

View File

@ -966,7 +966,6 @@ namespace IW4MAdmin
Offender = Target, Offender = Target,
Offense = Reason, Offense = Reason,
Punisher = Origin, Punisher = Origin,
Active = true,
When = DateTime.UtcNow, When = DateTime.UtcNow,
Link = Target.AliasLink Link = Target.AliasLink
}; };

View File

@ -1,142 +0,0 @@
/* CountryLookup.cs
*
* Copyright (C) 2008 MaxMind, Inc. All Rights Reserved.
*
* This library 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 2 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using SharedLibraryCore;
using System;
using System.IO;
using System.Net;
namespace CountryLookupProj
{
/// <summary>
/// Summary description for CountryLookup.
/// </summary>
public class CountryLookup
{
private FileStream fileInput;
private static long COUNTRY_BEGIN = 16776960;
private static string[] countryCode =
{ "--","AP","EU","AD","AE","AF","AG","AI","AL","AM","AN","AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB","BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO","BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD","CF","CG","CH","CI","CK","CL","CM","CN","CO","CR","CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO","DZ",
"EC","EE","EG","EH","ER","ES","ET","FI","FJ","FK","FM","FO","FR","FX","GA","GB","GD","GE","GF","GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT","GU","GW","GY","HK","HM","HN","HR","HT","HU","ID","IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO","JP","KE","KG","KH","KI","KM","KN","KP","KR","KW","KY","KZ",
"LA","LB","LC","LI","LK","LR","LS","LT","LU","LV","LY","MA","MC","MD","MG","MH","MK","ML","MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV","MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI","NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF","PG","PH","PK","PL","PM","PN","PR","PS","PT","PW","PY","QA",
"RE","RO","RU","RW","SA","SB","SC","SD","SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO","SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH","TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW","TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE","VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA","ZM","ME","ZW","A1","A2",
"O1","AX","GG","IM","JE","BL","MF"
};
private static string[] countryName =
{"a third world country","Asia/Pacific Region","Europe","Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia","Netherlands Antilles","Angola","Antarctica","Argentina","American Samoa","Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina","Barbados","Bangladesh","Belgium",
"Burkina Faso","Bulgaria","Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia","Brazil","Bahamas","Bhutan","Bouvet Island","Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands","Congo, The Democratic Republic of the","Central African Republic","Congo","Switzerland","Cote D'Ivoire",
"Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica","Cuba","Cape Verde","Christmas Island","Cyprus","Czech Republic","Germany","Djibouti","Denmark","Dominica","Dominican Republic","Algeria","Ecuador","Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland","Fiji","Falkland Islands (Malvinas)",
"Micronesia, Federated States of","Faroe Islands","France","France, Metropolitan","Gabon","United Kingdom","Grenada","Georgia","French Guiana","Ghana","Gibraltar","Greenland","Gambia","Guinea","Guadeloupe","Equatorial Guinea","Greece","South Georgia and the South Sandwich Islands","Guatemala","Guam","Guinea-Bissau","Guyana",
"Hong Kong","Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary","Indonesia","Ireland","Israel","India","British Indian Ocean Territory","Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica","Jordan","Japan","Kenya","Kyrgyzstan","Cambodia","Kiribati","Comoros","Saint Kitts and Nevis",
"Korea, Democratic People's Republic of","Korea, Republic of","Kuwait","Cayman Islands","Kazakstan","Lao People's Democratic Republic","Lebanon","Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho","Lithuania","Luxembourg","Latvia","Libyan Arab Jamahiriya","Morocco","Monaco","Moldova, Republic of","Madagascar",
"Marshall Islands","Macedonia","Mali","Myanmar","Mongolia","Macau","Northern Mariana Islands","Martinique","Mauritania","Montserrat","Malta","Mauritius","Maldives","Malawi","Mexico","Malaysia","Mozambique","Namibia","New Caledonia","Niger","Norfolk Island","Nigeria","Nicaragua","Netherlands",
"Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama","Peru","French Polynesia","Papua New Guinea","Philippines","Pakistan","Poland","Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico","Palestinian Territory","Portugal","Palau","Paraguay","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia",
"Solomon Islands","Seychelles","Sudan","Sweden","Singapore","Saint Helena","Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino","Senegal","Somalia","Suriname","Sao Tome and Principe","El Salvador","Syrian Arab Republic","Swaziland","Turks and Caicos Islands","Chad","French Southern Territories","Togo",
"Thailand","Tajikistan","Tokelau","Turkmenistan","Tunisia","Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu","Taiwan","Tanzania, United Republic of","Ukraine","Uganda","United States Minor Outlying Islands","United States","Uruguay","Uzbekistan","Holy See (Vatican City State)","Saint Vincent and the Grenadines",
"Venezuela","Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa","Zambia","Montenegro","Zimbabwe","Anonymous Proxy","Satellite Provider",
"Other","Aland Islands","Guernsey","Isle of Man","Jersey","Saint Barthelemy","Saint Martin"};
public CountryLookup(string fileName)
{
fileInput = new FileStream(fileName, FileMode.Open, FileAccess.Read);
}
private long AddrToNum(IPAddress addr)
{
long ipnum = 0;
byte[] b = BitConverter.GetBytes((UInt32)addr.ToString().ConvertToIP());
for (int i = 0; i < 4; ++i)
{
long y = b[i];
if (y < 0)
{
y += 256;
}
ipnum += y << ((3 - i) * 8);
}
return ipnum;
}
public string LookupCountryCode(IPAddress addr)
{
return (countryCode[(int)SeekCountry(0, AddrToNum(addr), 31)]);
}
public string LookupCountryName(string str)
{
IPAddress addr;
try
{
addr = IPAddress.Parse(str);
}
catch (FormatException)
{
return "a third world country";
}
return LookupCountryName(addr);
}
public string LookupCountryName(IPAddress addr)
{
return (countryName[(int)SeekCountry(0, AddrToNum(addr), 31)]);
}
private long SeekCountry(long offset, long ipnum, int depth)
{
byte[] buf = new byte[6];
long[] x = new long[2];
fileInput.Seek(6 * offset, 0);
fileInput.Read(buf, 0, 6);
for (int i = 0; i < 2; i++)
{
x[i] = 0;
for (int j = 0; j < 3; j++)
{
int y = buf[i * 3 + j];
if (y < 0)
{
y += 256;
}
x[i] += (y << (j * 8));
}
}
if ((ipnum & (1 << depth)) > 0)
{
if (x[1] >= COUNTRY_BEGIN)
{
return x[1] - COUNTRY_BEGIN;
}
return SeekCountry(x[1], ipnum, depth - 1);
}
else
{
if (x[0] >= COUNTRY_BEGIN)
{
return x[0] - COUNTRY_BEGIN;
}
return SeekCountry(x[0], ipnum, depth - 1);
}
}
}
}

Binary file not shown.

View File

@ -10,6 +10,9 @@ using SharedLibraryCore.Database.Models;
using System.Linq; using System.Linq;
using SharedLibraryCore.Database; using SharedLibraryCore.Database;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Net;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
namespace IW4MAdmin.Plugins.Welcome namespace IW4MAdmin.Plugins.Welcome
{ {
@ -84,9 +87,9 @@ namespace IW4MAdmin.Plugins.Welcome
{ {
Player newPlayer = E.Origin; Player newPlayer = E.Origin;
if (newPlayer.Level >= Player.Permission.Trusted && !E.Origin.Masked) if (newPlayer.Level >= Player.Permission.Trusted && !E.Origin.Masked)
E.Owner.Broadcast(ProcessAnnouncement(Config.Configuration().PrivilegedAnnouncementMessage, newPlayer)); E.Owner.Broadcast(await ProcessAnnouncement(Config.Configuration().PrivilegedAnnouncementMessage, newPlayer));
newPlayer.Tell(ProcessAnnouncement(Config.Configuration().UserWelcomeMessage, newPlayer)); newPlayer.Tell(await ProcessAnnouncement(Config.Configuration().UserWelcomeMessage, newPlayer));
if (newPlayer.Level == Player.Permission.Flagged) if (newPlayer.Level == Player.Permission.Flagged)
{ {
@ -104,27 +107,46 @@ namespace IW4MAdmin.Plugins.Welcome
E.Owner.ToAdmins($"^1NOTICE: ^7Flagged player ^5{newPlayer.Name} ^7({penaltyReason}) has joined!"); E.Owner.ToAdmins($"^1NOTICE: ^7Flagged player ^5{newPlayer.Name} ^7({penaltyReason}) has joined!");
} }
else else
E.Owner.Broadcast(ProcessAnnouncement(Config.Configuration().UserAnnouncementMessage, newPlayer)); E.Owner.Broadcast(await ProcessAnnouncement(Config.Configuration().UserAnnouncementMessage, newPlayer));
} }
} }
private string ProcessAnnouncement(string msg, Player joining) private async Task<string> ProcessAnnouncement(string msg, Player joining)
{ {
msg = msg.Replace("{{ClientName}}", joining.Name); msg = msg.Replace("{{ClientName}}", joining.Name);
msg = msg.Replace("{{ClientLevel}}", Utilities.ConvertLevelToColor(joining.Level, joining.ClientPermission.Name)); msg = msg.Replace("{{ClientLevel}}", Utilities.ConvertLevelToColor(joining.Level, joining.ClientPermission.Name));
try // this prevents it from trying to evaluate it every message
if (msg.Contains("{{ClientLocation}}"))
{ {
CountryLookupProj.CountryLookup CLT = new CountryLookupProj.CountryLookup($"{Utilities.OperatingDirectory}Plugins{System.IO.Path.DirectorySeparatorChar}GeoIP.dat"); msg = msg.Replace("{{ClientLocation}}", await GetCountryName(joining.IPAddressString));
msg = msg.Replace("{{ClientLocation}}", CLT.LookupCountryName(joining.IPAddressString));
}
catch (Exception)
{
joining.CurrentServer.Logger.WriteError("Could not open file Plugins\\GeoIP.dat for Welcome Plugin");
} }
msg = msg.Replace("{{TimesConnected}}", TimesConnected(joining)); msg = msg.Replace("{{TimesConnected}}", TimesConnected(joining));
return msg; return msg;
} }
/// <summary>
/// makes a webrequest to determine IP origin
/// </summary>
/// <param name="ip">IP address to get location of</param>
/// <returns></returns>
private async Task<string> GetCountryName(string ip)
{
using (var wc = new WebClient())
{
try
{
string response = await wc.DownloadStringTaskAsync(new Uri($"http://extreme-ip-lookup.com/json/{ip}"));
var responseObj = JObject.Parse(response);
return responseObj["country"].ToString();
}
catch
{
return "a third world country";
}
}
}
} }
} }

View File

@ -19,18 +19,12 @@
<ProjectReference Include="..\..\SharedLibraryCore\SharedLibraryCore.csproj" /> <ProjectReference Include="..\..\SharedLibraryCore\SharedLibraryCore.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="MaxMind\GeoIP.dat">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Update="Microsoft.NETCore.App" Version="2.1.5" /> <PackageReference Update="Microsoft.NETCore.App" Version="2.1.5" />
</ItemGroup> </ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent"> <Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy &quot;$(TargetPath)&quot; &quot;$(SolutionDir)BUILD\Plugins&quot;&#xD;&#xA;copy &quot;$(ProjectDir)MaxMind\GeoIP.dat&quot; &quot;$(SolutionDir)BUILD\Plugins\GeoIP.dat&quot;" /> <Exec Command="copy &quot;$(TargetPath)&quot; &quot;$(SolutionDir)BUILD\Plugins&quot;" />
</Target> </Target>
</Project> </Project>

View File

@ -220,7 +220,8 @@ namespace SharedLibraryCore
{ {
return (queuedEvent.Target != null && queuedEvent.Target.ClientNumber != -1) && return (queuedEvent.Target != null && queuedEvent.Target.ClientNumber != -1) &&
(queuedEvent.Target.State != Player.ClientState.Connected && (queuedEvent.Target.State != Player.ClientState.Connected &&
queuedEvent.Target.NetworkId != 0); queuedEvent.Target.NetworkId != 0 &&
queuedEvent.Origin?.ClientId != 1);
} }
} }
} }

View File

@ -1,107 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Linq;
namespace SharedLibraryCore
{
public class RemoteFile : IFile
{
readonly string Location;
string[] FileCache = new string[0];
public RemoteFile(string location) : base(string.Empty)
{
Location = location;
}
private void Retrieve()
{
using (var cl = new HttpClient())
FileCache = cl.GetStringAsync(Location).Result.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
}
public override long Length()
{
Retrieve();
return FileCache.Sum(l => l.Length);
}
public override Task<string[]> Tail(int lineCount)
{
return Task.FromResult(FileCache);
}
}
public class IFile
{
public IFile(String fileName)
{
if (fileName != string.Empty)
{
Name = fileName;
Handle = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true), Utilities.EncodingType);
sze = Handle.BaseStream.Length;
}
}
public virtual long Length()
{
sze = Handle.BaseStream.Length;
return sze;
}
public void Close()
{
Handle?.Close();
}
public String[] ReadAllLines()
{
return Handle?.ReadToEnd().Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
}
public String GetText()
{
return Handle?.ReadToEnd();
}
public virtual async Task<String[]> Tail(int lineCount)
{
var buffer = new List<string>(lineCount);
string line;
for (int i = 0; i < lineCount; i++)
{
line = await Handle.ReadLineAsync();
if (line == null) return buffer.ToArray();
buffer.Add(line);
}
int lastLine = lineCount - 1; //The index of the last line read from the buffer. Everything > this index was read earlier than everything <= this indes
while (null != (line = await Handle.ReadLineAsync()))
{
lastLine++;
if (lastLine == lineCount) lastLine = 0;
buffer[lastLine] = line;
}
if (lastLine == lineCount - 1) return buffer.ToArray();
var retVal = new string[lineCount];
buffer.CopyTo(lastLine + 1, retVal, 0, lineCount - lastLine - 1);
buffer.CopyTo(0, retVal, lineCount - lastLine - 1, lastLine + 1);
return retVal;
}
//END
private long sze;
private readonly String Name;
private StreamReader Handle;
}
}

View File

@ -95,6 +95,7 @@ namespace SharedLibraryCore.RCon
Ttl = 42, Ttl = 42,
ExclusiveAddressUse = true, ExclusiveAddressUse = true,
}; };
connectionState.OnSentData.Reset(); connectionState.OnSentData.Reset();
connectionState.OnReceivedData.Reset(); connectionState.OnReceivedData.Reset();
connectionState.ConnectionAttempts++; connectionState.ConnectionAttempts++;
@ -103,7 +104,7 @@ namespace SharedLibraryCore.RCon
#endif #endif
try try
{ {
response = await SendPayloadAsync(payload); response = await SendPayloadAsync(payload, waitForResponse);
connectionState.OnComplete.Release(1); connectionState.OnComplete.Release(1);
connectionState.ConnectionAttempts = 0; connectionState.ConnectionAttempts = 0;
} }
@ -139,7 +140,7 @@ namespace SharedLibraryCore.RCon
return splitResponse; return splitResponse;
} }
private async Task<byte[]> SendPayloadAsync(byte[] payload) private async Task<byte[]> SendPayloadAsync(byte[] payload, bool waitForResponse)
{ {
var connectionState = ActiveQueries[this.Endpoint]; var connectionState = ActiveQueries[this.Endpoint];
var rconSocket = (Socket)connectionState.SendEventArgs.UserToken; var rconSocket = (Socket)connectionState.SendEventArgs.UserToken;
@ -171,6 +172,11 @@ namespace SharedLibraryCore.RCon
} }
} }
if (!waitForResponse)
{
return new byte[0];
}
connectionState.ReceiveEventArgs.SetBuffer(connectionState.ReceiveBuffer); connectionState.ReceiveEventArgs.SetBuffer(connectionState.ReceiveBuffer);
// get our response back // get our response back

View File

@ -321,7 +321,6 @@ namespace SharedLibraryCore
protected int ConnectionErrors; protected int ConnectionErrors;
protected List<string> BroadcastMessages; protected List<string> BroadcastMessages;
protected TimeSpan LastMessage; protected TimeSpan LastMessage;
protected IFile LogFile;
protected DateTime LastPoll; protected DateTime LastPoll;
protected ManualResetEventSlim OnRemoteCommandResponse; protected ManualResetEventSlim OnRemoteCommandResponse;

View File

@ -24,7 +24,6 @@ namespace SharedLibraryCore.Services
PunisherId = newEntity.Punisher.ClientId, PunisherId = newEntity.Punisher.ClientId,
LinkId = newEntity.Link.AliasLinkId, LinkId = newEntity.Link.AliasLinkId,
Type = newEntity.Type, Type = newEntity.Type,
Active = true,
Expires = newEntity.Expires, Expires = newEntity.Expires,
Offense = newEntity.Offense, Offense = newEntity.Offense,
When = newEntity.When, When = newEntity.When,
@ -69,19 +68,7 @@ namespace SharedLibraryCore.Services
public async Task<IList<EFPenalty>> Find(Func<EFPenalty, bool> expression) public async Task<IList<EFPenalty>> Find(Func<EFPenalty, bool> expression)
{ {
throw await Task.FromResult(new Exception()); throw await Task.FromResult(new Exception());
/*
return await Task.FromResult(new List<EFPenalty>());
// fixme: this is so slow!
return await Task.Run(() =>
{
using (var context = new DatabaseContext())
return context.Penalties
.Include(p => p.Offender)
.Include(p => p.Punisher)
.Where(expression)
.Where(p => p.Active)
.ToList();
});*/
} }
public Task<EFPenalty> Get(int entityID) public Task<EFPenalty> Get(int entityID)
@ -225,8 +212,6 @@ namespace SharedLibraryCore.Services
return list; return list;
} }
} }
} }

View File

@ -27,7 +27,13 @@ namespace SharedLibraryCore
#endif #endif
public static Encoding EncodingType; public static Encoding EncodingType;
public static Localization.Layout CurrentLocalization = new Localization.Layout(new Dictionary<string, string>()); public static Localization.Layout CurrentLocalization = new Localization.Layout(new Dictionary<string, string>());
public static Player IW4MAdminClient(Server server = null) => new Player() { ClientId = 1, Level = Player.Permission.Console, CurrentServer = server }; public static Player IW4MAdminClient(Server server = null) => new Player()
{
ClientId = 1,
State = Player.ClientState.Connected,
Level = Player.Permission.Console,
CurrentServer = server
};
public static string HttpRequest(string location, string header, string headerValue) public static string HttpRequest(string location, string header, string headerValue)
{ {