Skip to content

Commit f605ab6

Browse files
committed
Reliability and maintainability stuff
1 parent 06f9b96 commit f605ab6

21 files changed

+94
-112
lines changed

Protest-Agent/Button.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial class Button : UserControl {
1616
LineAlignment = StringAlignment.Center
1717
};
1818

19-
private static Font font;
19+
private readonly Font font;
2020
private GraphicsPath path;
2121

2222
public event EventHandler OnPressed;

Protest-Agent/Configuration.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ internal class Configuration {
1515

1616
public string password;
1717
public static string presharedKey;
18-
public static Configuration stamp = new Configuration();
19-
public static Configuration smb = new Configuration();
20-
public static Configuration compmgmt = new Configuration();
21-
public static Configuration rdp = new Configuration();
22-
public static Configuration pse = new Configuration();
23-
public static Configuration uvnc = new Configuration();
24-
public static Configuration anydesk = new Configuration();
25-
public static Configuration winbox = new Configuration();
18+
public static readonly Configuration stamp = new Configuration();
19+
public static readonly Configuration smb = new Configuration();
20+
public static readonly Configuration compmgmt = new Configuration();
21+
public static readonly Configuration rdp = new Configuration();
22+
public static readonly Configuration pse = new Configuration();
23+
public static readonly Configuration uvnc = new Configuration();
24+
public static readonly Configuration anydesk = new Configuration();
25+
public static readonly Configuration winbox = new Configuration();
2626

2727
public static byte[] KeyToBytes(string key, byte length) {
2828
using (SHA512 sha = SHA512.Create()) {

Protest-Agent/TabsControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public partial class TabsControl : UserControl {
2828
LineAlignment = StringAlignment.Center
2929
};
3030

31-
private static Font font;
31+
private readonly Font font;
3232
private GraphicsPath path;
3333

3434
public event EventHandler OnPressed;

Protest/Database/Database.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ private Entry SaveOverwrite(string file, ConcurrentDictionary<string, Attribute>
265265

266266
//keep old origin and date, if data didn't change
267267
foreach (KeyValuePair<string, Attribute> pair in modifications) {
268-
if (!oldEntry.attributes.ContainsKey(pair.Key)) continue;
269-
if (oldEntry.attributes[pair.Key].value != pair.Value.value) continue;
270-
pair.Value.origin = oldEntry.attributes[pair.Key].origin;
271-
pair.Value.date = oldEntry.attributes[pair.Key].date;
268+
if (!oldEntry.attributes.TryGetValue(pair.Key, out Attribute existingAttribute)) continue;
269+
if (existingAttribute.value != pair.Value.value) continue;
270+
pair.Value.origin = existingAttribute.origin;
271+
pair.Value.date = existingAttribute.date;
272272
}
273273

274274
oldEntry.attributes = modifications;

Protest/Database/DatabaseJsonConverter.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,20 @@ public override Database Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
3737
}
3838
}
3939
//else if (propertyName == "length") {}
40-
else if (propertyName == "data") {
41-
if (reader.TokenType == JsonTokenType.StartObject) {
42-
while (reader.Read()) {
43-
if (reader.TokenType == JsonTokenType.EndObject) {
44-
break;
45-
}
46-
47-
string entryKey = reader.GetString();
48-
reader.Read();
49-
50-
Database.Entry entry = new Database.Entry {
51-
filename = entryKey,
52-
attributes = this.converter.Read(ref reader, typeof(Database.Attribute), options),
53-
mutex = new Lock()
54-
};
55-
56-
database.dictionary.TryAdd(entryKey, entry);
57-
}
40+
else if (propertyName == "data" && reader.TokenType == JsonTokenType.StartObject) {
41+
while (reader.Read()) {
42+
if (reader.TokenType == JsonTokenType.EndObject) break;
43+
44+
string entryKey = reader.GetString();
45+
reader.Read();
46+
47+
Database.Entry entry = new Database.Entry {
48+
filename = entryKey,
49+
attributes = this.converter.Read(ref reader, typeof(Database.Attribute), options),
50+
mutex = new Lock()
51+
};
52+
53+
database.dictionary.TryAdd(entryKey, entry);
5854
}
5955
}
6056
}

Protest/Front/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
<line id="analog_clock_m" x1="48" y1="48" x2="48" y2="16" stroke="#000" stroke-width="3" />
2626
</mask>
2727
</defs>
28-
<circle cx="48" cy="48" r="42" fill="light-dark(#202020, #F0F0F0)" mask="url(#analog_clock_mask)" />
29-
<circle cx="48" cy="48" r="46" fill="none" stroke="light-dark(#202020, #F0F0F0)" stroke-width="3" />
28+
<circle cx="48" cy="48" r="42" fill="light-dark(#202020, #111)" mask="url(#analog_clock_mask)" />
29+
<circle cx="48" cy="48" r="46" fill="none" stroke="light-dark(#202020, #111)" stroke-width="3" />
3030
</svg>
3131

3232
<svg id="date_calendar" width="96" height="96" aria-hidden="true">
@@ -41,7 +41,7 @@
4141
<text fill="#000" id="date_day" x="50%" y="80px" font-size="14" text-anchor="middle"></text>
4242
</mask>
4343
</defs>
44-
<rect width="100%" height="100%" rx="4" fill="light-dark(#202020, #F0F0F0)" mask="url(#date_month_mask)" />
44+
<rect width="100%" height="100%" rx="4" fill="light-dark(#202020, #111)" mask="url(#date_month_mask)" />
4545
</svg>
4646

4747
<div id="menubox">

Protest/Http/Auth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal static class Auth {
1919
private const int MAX_REQUESTS_PER_WIN_PERIOD = 8;
2020
private static readonly ConcurrentDictionary<IPAddress, List<long>> rateLimLog = new ConcurrentDictionary<IPAddress, List<long>>();
2121

22-
private static Random rng = new Random();
22+
private static readonly Random rng = new Random();
2323
private static readonly JsonSerializerOptions serializerOptions;
2424
private static readonly ConcurrentDictionary<string, OtpToken> otpTokens = new();
2525

Protest/Http/Cache.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,18 @@ private void HandleFile(string name, byte[] bytes, bool isGzipped) {
246246
#if SVG_TO_LIGHT
247247
byte[] pattern = "\"#202020\""u8.ToArray();
248248
byte[] target = "\"#c0c0c0\""u8.ToArray();
249-
if (name.StartsWith("/mono/") && name.EndsWith(".svg")) {
250-
if (Data.ContainsBytesSequence(entry.bytes, pattern)) {
251-
Data.ReplaceAllBytesSequence(entry.bytes, Encoding.UTF8.GetBytes("\"#202020\""), target);
252-
byte[] lightBytes = entry.bytes.ToArray();
253-
string lightName = $"{name}?light";
254-
Entry lightEntry = ConstructEntry(lightName, lightBytes, false, "svg");
255-
cache.AddOrUpdate(lightName, key => lightEntry, (key, existingValue) => lightEntry);
249+
if (name.StartsWith("/mono/") && name.EndsWith(".svg") && Data.ContainsBytesSequence(entry.bytes, pattern)) {
250+
Data.ReplaceAllBytesSequence(entry.bytes, Encoding.UTF8.GetBytes("\"#202020\""), target);
251+
byte[] lightBytes = entry.bytes.ToArray();
252+
string lightName = $"{name}?light";
253+
Entry lightEntry = ConstructEntry(lightName, lightBytes, false, "svg");
254+
cache.AddOrUpdate(lightName, key => lightEntry, (key, existingValue) => lightEntry);
256255

257256
#if SVG_TO_SVGZ //svgz
258-
if (!files.ContainsKey($"{name}z?light")) {
259-
toSvg.Add($"{name}z?light", lightEntry.gzip);
260-
}
261-
#endif
257+
if (!files.ContainsKey($"{name}z?light")) {
258+
toSvg.Add($"{name}z?light", lightEntry.gzip);
262259
}
260+
#endif
263261
}
264262
#endif
265263
}

Protest/Protocols/Snmp.Oid.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
internal static class Oid {
44

5-
public static string[] GENERIC_OID = new string[] {
5+
public static readonly string[] GENERIC_OID = new string[] {
66
SYSTEM_DESCRIPTOR,
77
SYSTEM_NAME,
88
SYSTEM_LOCATION,
99
SYSTEM_CONTACT,
1010
};
1111

12-
public static string[] PRINTERS_OID = new string[] {
12+
public static readonly string[] PRINTERS_OID = new string[] {
1313
PRINTER_MODEL,
1414
PRINTER_SERIAL_NO,
1515
};
1616

17-
public static string[] SWITCH_OID = new string[] {
17+
public static readonly string[] SWITCH_OID = new string[] {
1818
INT_TYPE,
1919
INT_DESCRIPTOR,
2020
INT_ALIAS,
@@ -24,7 +24,7 @@ internal static class Oid {
2424
INT_1D_TP_FDB,
2525
};
2626

27-
public static string[] LIVEVIEW_SWITCH_OID = new string[] {
27+
public static readonly string[] LIVEVIEW_SWITCH_OID = new string[] {
2828
INT_TYPE,
2929
INT_SPEED,
3030
INT_1Q_VLAN,
@@ -37,41 +37,41 @@ internal static class Oid {
3737
INT_1D_TP_FDB,
3838
};
3939

40-
public static string[] LIVESTATS_OID = new string[] {
40+
public static readonly string[] LIVESTATS_OID = new string[] {
4141
SYSTEM_UPTIME,
4242
SYSTEM_TEMPERATURE,
4343
};
4444

45-
public static string[] LIVESTATS_PRINTER_OID = new string[] {
45+
public static readonly string[] LIVESTATS_PRINTER_OID = new string[] {
4646
PRINTER_STATUS,
4747
PRINTER_DISPLAY_MESSAGE,
4848
PRINTER_JOBS,
4949
PRINTER_MARKER_COUNTER_LIFE,
5050
};
5151

52-
public static string[] LIFELINE_PRINTER_OID = new string[] {
52+
public static readonly string[] LIFELINE_PRINTER_OID = new string[] {
5353
PRINTER_MARKER_COUNTER_LIFE,
5454
PRINTER_MARKER_COUNTER_PRINTS,
5555
PRINTER_MARKER_COUNTER_MARKERS,
5656
PRINTER_MARKER_COUNTER_MONO,
5757
PRINTER_MARKER_COUNTER_COLOR,
5858
};
5959

60-
public static string[] LIFELINE_SWITCH_OID = new string[] {
60+
public static readonly string[] LIFELINE_SWITCH_OID = new string[] {
6161
INT_TYPE,
6262
INT_TRAFFIC_BYTES_IN,
6363
INT_TRAFFIC_BYTES_OUT,
6464
INT_ERROR_IN,
6565
INT_ERROR_OUT,
6666
};
6767

68-
public static string[] TOPOLOGY_DOT1Q = new string[] {
68+
public static readonly string[] TOPOLOGY_DOT1Q = new string[] {
6969
INT_1Q_STATIC_NAME,
7070
INT_1Q_VLAN_ENGRESS,
7171
INT_1Q_VLAN_UNTAGGED,
7272
};
7373

74-
public static string[] TOPOLOGY_TRAFFIC = new string[] {
74+
public static readonly string[] TOPOLOGY_TRAFFIC = new string[] {
7575
INT_TRAFFIC_BYTES_IN,
7676
INT_TRAFFIC_PKTS_IN_UCAST,
7777
INT_TRAFFIC_PKTS_IN_MCAST,
@@ -82,7 +82,7 @@ internal static class Oid {
8282
INT_TRAFFIC_PKTS_OUT_BCAST,
8383
};
8484

85-
public static string[] TOPOLOGY_ERROR = new string[] {
85+
public static readonly string[] TOPOLOGY_ERROR = new string[] {
8686
INT_ERROR_IN,
8787
INT_ERROR_OUT,
8888
};

Protest/Proxy/ReverseProxy.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ public enum ProxyProtocol {
2222
}
2323

2424
public struct ReverseProxyObject {
25-
public Guid guid;
26-
public string name;
25+
public Guid guid;
26+
public string name;
2727
public ProxyProtocol protocol;
28-
public string certificate;
29-
public string password;
30-
public string proxyaddr;
31-
public int proxyport;
32-
public string destaddr;
33-
public int destport;
34-
public bool autostart;
28+
public string certificate;
29+
public string password;
30+
public string proxyaddr;
31+
public int proxyport;
32+
public string destaddr;
33+
public int destport;
34+
public bool autostart;
3535
}
3636

37-
public static ConcurrentDictionary<string, ReverseProxyAbstract> running = new ConcurrentDictionary<string, ReverseProxyAbstract>();
37+
public static readonly ConcurrentDictionary<string, ReverseProxyAbstract> running = new ConcurrentDictionary<string, ReverseProxyAbstract>();
3838

3939
private static readonly JsonSerializerOptions serializerOptions;
4040
private static readonly JsonSerializerOptions serializerOptionsWithPassword;
@@ -112,10 +112,8 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
112112
await WsWriteText(ws, GetTotalTraffic());
113113

114114
clientsToggle = !clientsToggle;
115-
if (select != Guid.Empty) {
116-
if (interval > 500 || interval <= 500 && clientsToggle) {
117-
await WsWriteText(ws, GetProxyTraffic(select));
118-
}
115+
if (select != Guid.Empty && interval > 500 || interval <= 500 && clientsToggle) {
116+
await WsWriteText(ws, GetProxyTraffic(select));
119117
}
120118

121119
await Task.Delay(interval);

0 commit comments

Comments
 (0)