FinalSolution/FinalSolution/NetLimiterBridge.cs

143 lines
4.5 KiB
C#

using System.Collections.Generic;
using System.Linq;
using NetLimiter.Service;
namespace FinalSolution
{
public static class NetLimiterBridge
{
private const string Prefix = "tsf"; // thirty-seventy-four
private static NLClient _client;
private static NLClient GetClient()
{
if (_client != null) return _client;
_client = new NLClient();
_client.Connect();
return _client;
}
private static string GetFilterNameFor(string portName, bool upload = false)
{
return upload ? $"{Prefix}_{portName}_ul" : $"{Prefix}_{portName}";
}
private static FilterHandle CreateFilter(string portName, ushort portStart, ushort portEnd, bool upload = false)
{
var cli = GetClient();
var name = GetFilterNameFor(portName, upload);
var filter = cli.Filters.FirstOrDefault(f => f.Name == name) ??
cli.AddFilter(new Filter(name)
{
Functions =
{
new FFPathEqual(Config.Instance.Destiny2Path),
new FFRemotePortInRange(new PortRangeFilterValue(portStart, portEnd))
}
});
var hotkey = Config.Instance.Hotkeys[name];
return new FilterHandle(name, filter, cli, upload, 1, hotkey);
}
private static FilterHandle CreateFilterFullGame()
{
var cli = GetClient();
var name = GetFilterNameFor("fg");
var filter = cli.Filters.FirstOrDefault(f => f.Name == name)
??
cli.AddFilter(new Filter(name)
{
Functions = { new FFPathEqual(Config.Instance.Destiny2Path) }
});
var hotkey = Config.Instance.Hotkeys[name];
return new FilterHandle(name, filter, cli, false, 811, hotkey);
}
public static readonly FilterHandle F3074;
public static readonly FilterHandle F3074Ul;
public static readonly FilterHandle F7500;
public static readonly FilterHandle F27K;
public static readonly FilterHandle F30K;
public static readonly FilterHandle FFullGame;
static NetLimiterBridge()
{
F3074 = CreateFilter("3074", 3074, 3074);
F3074Ul = CreateFilter("3074", 3074, 3074, true);
F7500 = CreateFilter("7500", 7500, 7509);
F27K = CreateFilter("27k", 27015, 27200);
F30K = CreateFilter("30k", 30000, 30009);
FFullGame = CreateFilterFullGame();
}
}
public class FilterHandle
{
private readonly Filter _filter;
private readonly NLClient _client;
private readonly bool _upload;
private readonly uint _limitSize; // the limit rate, normally is 1, only FG is 800 or 811
public string Name { get; }
public readonly string Hotkey;
private Rule _rule;
public static readonly List<FilterHandle> AllFilters = new List<FilterHandle>();
public FilterHandle(string name, Filter filter, NLClient client, bool upload, uint limitSize = 1,
string hotkey = null)
{
Name = name;
_filter = filter;
_client = client;
_upload = upload;
_limitSize = limitSize;
Hotkey = hotkey;
InitLimitRule();
AllFilters.Add(this);
}
~FilterHandle()
{
AllFilters.Remove(this);
}
private void InitLimitRule()
{
if (_rule != null) return;
var dir = _upload ? RuleDir.Out : RuleDir.In;
_rule = _client.Rules.FirstOrDefault(r => r.FilterId == _filter.Id && r.Dir == dir) ??
_client.AddRule(_filter.Id, new LimitRule(dir, _limitSize) { IsEnabled = false });
}
public void Toggle()
{
_rule.IsEnabled = !_rule.IsEnabled;
_rule = _client.UpdateRule(_rule).Rule;
}
public void Set(bool value)
{
_rule.IsEnabled = value;
_rule = _client.UpdateRule(_rule).Rule;
}
public bool IsEnabled()
{
return _rule?.IsEnabled ?? false;
}
public string GetState()
{
return IsEnabled() ? "On" : "Off";
}
}
}