public bool IsDateTimeAllowed(ZonedDateTime date, TimeRestrictionModel model) { if (!model.RestrictionsEnabled) { return(true); } LocalDateTime d = date.LocalDateTime; LocalDateTime startDateLocal, endDateLocal; int hour, minute; getHoursMinutes(model.EnabledThrough[0], out hour, out minute); if (hour == 24) { startDateLocal = new LocalDateTime(d.Year, d.Month, d.Day, 23, 59, 59, 999); } else { startDateLocal = new LocalDateTime(d.Year, d.Month, d.Day, hour, minute); } getHoursMinutes(model.EnabledThrough[1], out hour, out minute); if (hour == 24) { endDateLocal = new LocalDateTime(d.Year, d.Month, d.Day, 23, 59, 59, 999); } else { endDateLocal = new LocalDateTime(d.Year, d.Month, d.Day, hour, minute); } return(d.CompareTo(startDateLocal) >= 0 && d.CompareTo(endDateLocal) <= 0); }
public void TestIsDateTimeAllowed_RestrictionsEnabled2() { var model = new TimeRestrictionModel() { EnabledThrough = new decimal[] { 8.25m, 17.5m }, RestrictionsEnabled = true }; var tzProvider = new TestTzProvider(); TestClock[] clocks = getTestClocks(tzProvider); ZonedDateTime[] dates = clocks.Select(c => new ZonedDateTime(c.CurrentInstant, tzProvider.GetSystemDefault())).ToArray(); bool[] testResults = new bool[34] { false, false, true, false, false, false, true, true, false, false, false, true, true, false, false, false, true, true, false, false, false, true, true, false, false, false, true, true, false, false, false, true, true, false, }; for (int i = 0; i < clocks.Length; i++) { TimeDetection detection = new TimeDetection(clocks[i], tzProvider); Assert.AreEqual(testResults[i], detection.IsDateTimeAllowed(dates[i], model), "Unexpected result from IsDateTimeAllowed"); } }
/// <summary> /// Called when each request is intercepted and has not gone to the server yet. /// </summary> internal ProxyNextAction OnBeforeRequest(GoproxyWrapper.Session args) { ProxyNextAction nextAction = ProxyNextAction.AllowAndIgnoreContent; int trackId = 0; lock (trackIdLock) { trackId = nextTrackId++; } // Don't allow filtering if our user has been denied access and they // have not logged back in. if (m_ipcServer != null && m_ipcServer.WaitingForAuth) { return(ProxyNextAction.AllowAndIgnoreContentAndResponse); } try { ZonedDateTime date = m_timeDetection.GetRealTime(); TimeRestrictionModel todayRestriction = null; if (m_policyConfiguration != null && m_policyConfiguration.TimeRestrictions != null && date != null) { todayRestriction = m_policyConfiguration.TimeRestrictions[(int)date.ToDateTimeOffset().DayOfWeek]; } string urlString = args.Request.Url; Uri url = new Uri(urlString); Uri serviceProviderPath = new Uri(CompileSecrets.ServiceProviderApiPath); if (url.Host == serviceProviderPath.Host) { return(ProxyNextAction.AllowAndIgnoreContentAndResponse); } else if (todayRestriction != null && todayRestriction.RestrictionsEnabled && !m_timeDetection.IsDateTimeAllowed(date, todayRestriction)) { sendBlockResponse(args, urlString, null, BlockType.TimeRestriction); return(ProxyNextAction.DropConnection); } } catch (Exception e) { LoggerUtil.RecursivelyLogException(m_logger, e); } return(nextAction); }
public void UpdateRestrictions(Dictionary<string, TimeRestrictionModel> restrictions) { if (restrictions == null) return; var restrictionsArray = new TimeRestrictionModel[7]; TimeRestrictionModel model; for(int i = 0; i < 7; i++) { model = null; restrictions.TryGetValue(((DayOfWeek)i).ToString().ToLowerInvariant(), out model); if(model != null && !model.RestrictionsEnabled) { model.EnabledThrough[0] = 0; model.EnabledThrough[1] = 24; } restrictionsArray[i] = model; } this.TimeRestrictions = restrictionsArray; }
/// <summary> /// Queries the service provider for updated filtering rules. /// </summary> /// <param name="cfgStream">Added this parameter so that we could test the default policy configuration.</param> public bool LoadConfiguration(Stream cfgStream) { try { // Load our configuration file and load configured lists, etc. if (cfgStream != null) { string cfgJson = string.Empty; using (TextReader textReader = new StreamReader(cfgStream)) { cfgJson = textReader.ReadToEnd(); } if (!StringExtensions.Valid(cfgJson)) { m_logger.Error("Could not find valid JSON config for filter."); return(false); } try { LoadConfigFromJson(cfgJson, s_configSerializerSettings); m_logger.Info("Configuration loaded from JSON."); } catch (Exception deserializationError) { m_logger.Error("Failed to deserialize JSON config."); LoggerUtil.RecursivelyLogException(m_logger, deserializationError); return(false); } if (Configuration.UpdateFrequency.Minutes <= 0 || Configuration.UpdateFrequency == Timeout.InfiniteTimeSpan) { // Just to ensure that we enforce a minimum value here. Configuration.UpdateFrequency = TimeSpan.FromMinutes(5); } loadAppList(BlacklistedApplications, Configuration.BlacklistedApplications, BlacklistedApplicationGlobs); loadAppList(WhitelistedApplications, Configuration.WhitelistedApplications, WhitelistedApplicationGlobs); TimeRestrictions = new TimeRestrictionModel[7]; for (int i = 0; i < 7; i++) { DayOfWeek day = (DayOfWeek)i; string configDay = day.ToString().ToLowerInvariant(); TimeRestrictionModel restriction = null; Configuration.TimeRestrictions?.TryGetValue(configDay, out restriction); TimeRestrictions[i] = restriction; } AreAnyTimeRestrictionsEnabled = TimeRestrictions.Any(r => r?.RestrictionsEnabled == true); if (Configuration.CannotTerminate) { // Turn on process protection if requested. PlatformTypes.New <IAntitampering>().EnableProcessProtection(); } else { PlatformTypes.New <IAntitampering>().DisableProcessProtection(); } // Don't do list loading in the same function as configuration loading, because those are now indeed two separate functions. } } catch (Exception e) { LoggerUtil.RecursivelyLogException(m_logger, e); return(false); } return(true); }