forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteEndpoint.cs
More file actions
258 lines (225 loc) · 5.31 KB
/
RemoteEndpoint.cs
File metadata and controls
258 lines (225 loc) · 5.31 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Waher.Persistence.Attributes;
namespace Waher.Security.LoginMonitor
{
/// <summary>
/// Login state information relating to a remote endpoint
/// </summary>
[CollectionName("RemoteEndpoints")]
[TypeName(TypeNameSerialization.None)]
[ArchivingTime("ArchiveDays")]
[Index("Endpoint", "Created")]
public class RemoteEndpoint
{
private string objectId = null;
private string endpoint = null;
private string lastProtocol = string.Empty;
private string reason = string.Empty;
private string whois = string.Empty;
private string city = string.Empty;
private string region = string.Empty;
private string country = string.Empty;
private string code = string.Empty;
private string flag = string.Empty;
private bool blocked = false;
private int[] state = null;
private DateTime[] timestamps = null;
private DateTime created = DateTime.MinValue;
/// <summary>
/// Login state information relating to a remote endpoint
/// </summary>
public RemoteEndpoint()
{
}
/* NOTE: No default values are reported in attributes. This is to assure serialized object size do not vary when changed.
* These objects might change a lot. By maintaining a constant object size over its lifetime, minimizes the risk of blocks
* being split or joined in the object database during operation. */
/// <summary>
/// Object ID
/// </summary>
[ObjectId]
public string ObjectId
{
get => this.objectId;
set => this.objectId = value;
}
/// <summary>
/// String-representation of remote endpoint.
/// </summary>
public string Endpoint
{
get => this.endpoint;
set => this.endpoint = value;
}
/// <summary>
/// When record was created.
/// </summary>
public DateTime Created
{
get => this.created;
set => this.created = value;
}
/// <summary>
/// Last protocol used.
/// </summary>
public string LastProtocol
{
get => this.lastProtocol;
set => this.lastProtocol = value;
}
/// <summary>
/// If endpoint is blocked or not.
/// </summary>
public bool Blocked
{
get => this.blocked;
set => this.blocked = value;
}
/// <summary>
/// Current login state. Null represents no login attempts have been made, or last one successfull.
/// </summary>
public int[] State
{
get => this.state;
set => this.state = value;
}
/// <summary>
/// Timestamps of first attempt in each interval. Null represents no login attempts have been made, or last one successfull.
/// </summary>
public DateTime[] Timestamps
{
get => this.timestamps;
set => this.timestamps = value;
}
/// <summary>
/// Reason for blocking the endpoint.
/// </summary>
[DefaultValueStringEmpty]
public string Reason
{
get => this.reason;
set => this.reason = value;
}
/// <summary>
/// WHOIS information about the remote endpoint.
/// </summary>
[DefaultValueStringEmpty]
public string WhoIs
{
get => this.whois;
set => this.whois = value;
}
/// <summary>
/// City related to the endpoint.
/// </summary>
[DefaultValueStringEmpty]
public string City
{
get => this.city;
set => this.city = value;
}
/// <summary>
/// Region related to the endpoint.
/// </summary>
[DefaultValueStringEmpty]
public string Region
{
get => this.region;
set => this.region = value;
}
/// <summary>
/// Country related to the endpoint.
/// </summary>
[DefaultValueStringEmpty]
public string Country
{
get => this.country;
set => this.country = value;
}
/// <summary>
/// Country Code related to the endpoint.
/// </summary>
[DefaultValueStringEmpty]
public string Code
{
get => this.code;
set => this.code = value;
}
/// <summary>
/// Flag related to the endpoint.
/// </summary>
[DefaultValueStringEmpty]
public string Flag
{
get => this.flag;
set => this.flag = value;
}
/// <summary>
/// Checks if last login attempt was a failed login attempt.
/// </summary>
public bool LastFailed
{
get
{
int i, c = this.state?.Length ?? 0;
for (i = 0; i < c; i++)
{
if (this.state[i] > 0)
return true;
}
return false;
}
}
internal void Reset(bool Unblock)
{
int i, c;
for (i = 0, c = this.state?.Length ?? 0; i < c; i++)
this.state[i] = 0;
for (i = 0, c = this.timestamps?.Length ?? 0; i < c; i++)
this.timestamps[i] = DateTime.MinValue;
if (Unblock)
{
this.blocked = false;
this.reason = string.Empty;
}
}
internal async Task<KeyValuePair<string, object>[]> Annotate(params KeyValuePair<string, object>[] Tags)
{
Tags = await LoginAuditor.Annotate(this.endpoint, Tags);
foreach (KeyValuePair<string, object> Tag in Tags)
{
switch (Tag.Key)
{
case "City":
this.city = Tag.Value.ToString();
break;
case "Region":
this.region = Tag.Value.ToString();
break;
case "Country":
this.country = Tag.Value.ToString();
break;
case "Code":
this.code = Tag.Value.ToString();
break;
case "Flag":
this.flag = Tag.Value.ToString();
break;
}
}
return Tags;
}
/// <summary>
/// Number of days to archive field.
/// </summary>
public int ArchiveDays
{
get
{
return this.blocked ? int.MaxValue : 0;
}
}
}
}