Example #1
0
        private PaloAltoUserIdUpdater()
        {
            var configKey = RegistryPath.Open(@"HKEY_LOCAL_MACHINE\SOFTWARE\AHA-NET\PaloAltoUserId");

            wifi_reauth_plus_padding         = Int32.Parse((string)configKey.GetValue("wifi_reauth_plus_padding"));
            dhcp_lease_duration_plus_padding = Int32.Parse((string)configKey.GetValue("dhcp_lease_duration_plus_padding"));
            dhcp_lease_timespan_plus_padding = TimeSpan.FromMinutes(dhcp_lease_duration_plus_padding);
            summertime_start = DateTime.Parse((string)configKey.GetValue("summertime_start"));
            summertime_end   = DateTime.Parse((string)configKey.GetValue("summertime_end"));
            public_abuse_session_length_timespan       = TimeSpan.FromDays(Double.Parse((string)configKey.GetValue("public_abuse_session_length_timespan")));
            public_abuse_session_count_threshold       = Int32.Parse((string)configKey.GetValue("public_abuse_session_count_threshold"));
            public_abuse_retain_session_count_timespan = TimeSpan.FromDays(Double.Parse((string)configKey.GetValue("public_abuse_retain_session_count_timespan")));

            rateLimitedMacs = new RateLimitedMacs(@"HKEY_LOCAL_MACHINE\SOFTWARE\AHA-NET\PaloAltoUserId\RateLimitedMacs", true);
            publicCache     = new DhcpRecordsByMac(rateLimitedMacs, dhcp_lease_duration_plus_padding);
            dhcpCache       = new DhcpRecordsByMac(rateLimitedMacs, dhcp_lease_duration_plus_padding);
            iasCache        = new IasRecordsByMac(wifi_reauth_plus_padding);
            uidCache        = new UidRecordsByIp();
            registeredMacs  = new RegSzRawDictionary(@"HKEY_LOCAL_MACHINE\SOFTWARE\AHA-NET\PaloAltoUserId\RegisteredMacs", true);
            ahanetMacs      = new RegSzDictionary(@"HKEY_LOCAL_MACHINE\SOFTWARE\AHA-NET\PaloAltoUserId\AhanetMacs", true);
            publicMacs      = new PublicRecordsByMac(@"HKEY_LOCAL_MACHINE\SOFTWARE\AHA-NET\PaloAltoUserId\PublicMacs", dhcp_lease_timespan_plus_padding, public_abuse_retain_session_count_timespan, true);
            debugTable      = new RegSzRawDictionary(@"HKEY_LOCAL_MACHINE\SOFTWARE\AHA-NET\PaloAltoUserId\Debug", true);
            mapper          = MapUserIp.Instance;
        }
        private void ThreadInitialize()
        {
            var configKey = RegistryPath.Open(@"HKEY_LOCAL_MACHINE\SOFTWARE\AHA-NET\PaloAltoUserId");

            var log_path = (string)configKey.GetValue("log_path");
            var log      = new LogTagged(new LogFileWeekly(log_path, true), 1);

            log.TagWithDateTime = true;
            Log.Replace(log);

            Log.Inform(">>>>> STARTING <<<<<");

            var firewall_address  = (string)configKey.GetValue("firewall_address");
            var firewall_username = (string)configKey.GetValue("firewall_username");
            var firewall_api_key  = (string)configKey.GetValue("firewall_api_key");

            MapUserIp.New(new PanXmlApi(firewall_address, firewall_username, firewall_api_key));

            var dhcpFileFilter        = new FileFilterRecent(new FileFilterChanged());
            var dhcpWatcher           = new FolderWatchers(dhcpFileFilter);
            var dhcp_logfile_wildcard = (string)configKey.GetValue("dhcp_logfile_wildcard");

            foreach (var dhcp_log_paths in ((string)configKey.GetValue("dhcp_log_paths")).Split(';'))
            {
                dhcpWatcher.AddWatcher(dhcp_log_paths, dhcp_logfile_wildcard);
            }
            var dhcpHandler     = AssembleDhcpRecordHandler(new DsvLineConfig());
            var dhcpFileManager = new FileFollowerManager(dhcpHandler, token);
            var dhcpLogs        = new FolderWatcherManager(dhcpWatcher, dhcpFileManager, token);

            var iasFileFilter        = new FileFilterRecent(new FileFilterChanged());
            var iasWatcher           = new FolderWatchers(iasFileFilter);
            var ias_logfile_wildcard = (string)configKey.GetValue("ias_logfile_wildcard");

            foreach (var ias_log_paths in ((string)configKey.GetValue("ias_log_paths")).Split(';'))
            {
                iasWatcher.AddWatcher(ias_log_paths, ias_logfile_wildcard);
            }
            var iasHandler     = AssembleIasRecordHandler(new DsvLineConfig());
            var iasFileManager = new FileFollowerManager(iasHandler, token);
            var iasLogs        = new FolderWatcherManager(iasWatcher, iasFileManager, token);

            var dhcpTask = dhcpLogs.ProcessAsync();
            var IasTask  = iasLogs.ProcessAsync();

            Func <bool> checkIfAllOldLogsRead = () => {
                return(!(dhcpFileManager.AreAllFollowing && iasFileManager.AreAllFollowing));
            };
            Func <bool> wait15msecs = () => {
                const int _15_msecs = 15;
                Task      taskDelay = null;
                try     { taskDelay = Task.Delay(_15_msecs, token); taskDelay.Wait(); return(true); }
                catch   { return(false); }
                finally { if (taskDelay != null)
                          {
                              taskDelay.Dispose();
                          }
                }
            };
            var blockUntilAllOldLogsRead = new RepeatedTask(checkIfAllOldLogsRead, wait15msecs);

            blockUntilAllOldLogsRead.Process();

            if (token.IsCancellationRequested)
            {
                return;
            }

            Log.Inform(">>>>> FOLLOWING <<<<<");
            var updater = PaloAltoUserIdUpdater.Instance;

            updater.LoginCached();

            if (token.IsCancellationRequested)
            {
                return;
            }

            Func <bool> maintainDatabases = () => {
                Log.Inform(">>>>> MAINTAIN DATABASES <<<<<");
                updater.RemoveStaleEntries();
                return(true);
            };
            Func <bool> wait1day = () => {
                TimeSpan _1_day    = TimeSpan.FromDays(1);
                Task     taskDelay = null;
                try     { taskDelay = Task.Delay(_1_day, token); taskDelay.Wait(); return(true); }
                catch   { return(false); }
                finally { if (taskDelay != null)
                          {
                              taskDelay.Dispose();
                          }
                }
            };
            var maintainDatabasesEveryDay = new RepeatedTask(maintainDatabases, wait1day);

            maintainDatabasesEveryDay.Process();
        }