-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGeoblockModuleService.cs
More file actions
105 lines (94 loc) · 4.26 KB
/
GeoblockModuleService.cs
File metadata and controls
105 lines (94 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#nullable disable
/* GeoblockModuleService.cs
*
* Copyright (C) 2009 Triple IT. All Rights Reserved.
* Author: Frank Lippes, Modified for IIS 10 (.Net 4.6) by RvdH
*
* 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.
*/
using System;
using System.Collections.Generic;
using Microsoft.Web.Management.Server;
using System.Collections;
namespace IISGeoIP2blockModule
{
/// <summary>
/// Module service to enable the client to get the settings
/// </summary>
public class GeoblockModuleService : ModuleService
{
/// <summary>
/// Gets the configuration of the geoblock module
/// </summary>
/// <returns>The geoblock configuration in a propertybag</returns>
[ModuleServiceMethod(PassThrough = true)]
public PropertyBag GetGeoblockConfiguration()
{
GeoblockConfigurationSection config = (GeoblockConfigurationSection)ManagementUnit.Configuration.GetSection(GeoblockConfigurationSection.SectionName, typeof(GeoblockConfigurationSection));
PropertyBag result = new PropertyBag();
result.Add(0, config.Enabled);
result.Add(1, config.DenyAction);
result.Add(2, config.GeoIpFilepath);
result.Add(3, config.VerifyAll);
result.Add(4, config.AllowedMode);
ArrayList countries = new ArrayList();
foreach (CountryConfigurationElement country in config.SelectedCountryCodes)
{
PropertyBag item = new PropertyBag();
item.Add(0, country.Code);
countries.Add(item);
}
result.Add(5, countries);
ArrayList exceptionRules = new ArrayList();
foreach (ExceptionRuleConfigurationElement exceptionRule in config.ExceptionRules)
{
PropertyBag item = new PropertyBag();
item.Add(0, exceptionRule.AllowedMode);
item.Add(1, exceptionRule.IpAddress);
item.Add(2, exceptionRule.Mask);
exceptionRules.Add(item);
}
result.Add(6, exceptionRules);
return result;
}
/// <summary>
/// Writes the configuration of the geoblock module
/// </summary>
/// <param name="updatedGeoblockConfiguration">The new geoblock configuration in a propertybag</param>
[ModuleServiceMethod(PassThrough = true)]
public void UpdateGeoblockConfiguration(PropertyBag updatedGeoblockConfiguration)
{
if (updatedGeoblockConfiguration == null)
{
throw new ArgumentNullException("updatedGeoblockConfiguration");
}
GeoblockConfigurationSection config = (GeoblockConfigurationSection)ManagementUnit.Configuration.GetSection(GeoblockConfigurationSection.SectionName, typeof(GeoblockConfigurationSection));
config.Enabled = (bool)updatedGeoblockConfiguration[0];
config.DenyAction = (string)updatedGeoblockConfiguration[1];
config.GeoIpFilepath = (string)updatedGeoblockConfiguration[2];
config.VerifyAll = (bool)updatedGeoblockConfiguration[3];
config.AllowedMode = (bool)updatedGeoblockConfiguration[4];
config.SelectedCountryCodes.Clear();
ArrayList countries = (ArrayList)updatedGeoblockConfiguration[5];
foreach (PropertyBag item in countries)
{
config.SelectedCountryCodes.Add((string)item[0]);
}
config.ExceptionRules.Clear();
ArrayList exceptionRules = (ArrayList)updatedGeoblockConfiguration[6];
foreach (PropertyBag item in exceptionRules)
{
config.ExceptionRules.Add((bool)item[0], (string)item[1], (string)item[2]);
}
ManagementUnit.Update();
}
}
}