示例#1
0
        private void FlushIPsInternal()
        {
            using var adapter = System.GetTableAdapter(4);
            var sync = new DefaultNetfilterSync <IpTablesRule>();

            (System.GetChain(adapter, IpTable, IpChain) as IpTablesChain).Sync(adapter, Enumerable.Empty <IpTablesRule>(), sync);
        }
示例#2
0
        private async Task RefreshIPWhiteListAsync()
        {
            await AccessSemaphore.WaitAsync();

            Logger.LogInformation("Refreshing iptables whitelist...");
            FlushIPsInternal();

            try
            {
                using var scope = Provider.CreateScope();
                var dbContext = scope.ServiceProvider.GetRequiredService <MinerContext>();
                using var adapter = System.GetTableAdapter(4);
                var miners = await dbContext.Miners.ToListAsync();

                var rules = new List <string>();

                foreach (var miner in miners.Where(x => x.LastAddress != null &&                                      //Has an IP
                                                   x.NextIncrement >= DateTimeOffset.UtcNow - TimeSpan.FromHours(1))) //Has updated
                {
                    rules.Add(GetAcceptRule(miner.LastAddress));
                }

                var ruleSet = new IpTablesRuleSet(4, rules, System);

                var sync = new DefaultNetfilterSync <IpTablesRule>();
                (System.GetChain(adapter, IpTable, IpChain) as IpTablesChain).Sync(adapter, ruleSet.Rules, sync);
                Logger.LogInformation("Finished refreshing iptables whitelist");
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "There was an error while refreshing iptables whitelist");
            }
            finally
            {
                AccessSemaphore.Release();
            }
        }