Skip to content

Commit 096d23f

Browse files
committed
Handle false positive warning for interfaces cached data.
Interfaces now correctly showing live data for vlans when available
1 parent ac6cdc0 commit 096d23f

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

Protest/Front/deviceview.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -763,17 +763,16 @@ class DeviceView extends View {
763763
this.liveC.style.overflow = "auto";
764764

765765
if (!(".interfaces" in this.link)) return;
766-
let obj;
767766

767+
let obj;
768768
try {
769769
obj = JSON.parse(this.link[".interfaces"].v);
770+
if (obj === null) return;
770771
}
771772
catch {
772773
return;
773774
}
774775

775-
if (obj === null) return;
776-
777776
const container = document.createElement("div");
778777
container.style.display = "inline-block";
779778
container.style.verticalAlign = "top";
@@ -830,28 +829,28 @@ class DeviceView extends View {
830829
frontElement.appendChild(ledElement);
831830
}
832831

832+
const port = obj.i[i].i;
833+
const speed = this.switchInfo.success && i<(this.switchInfo.speed?.length ?? 0) ? this.switchInfo.speed[i] : obj.i[i].s;
834+
const untagged = this.switchInfo.success && i<(this.switchInfo.untagged?.length ?? 0) ? this.switchInfo.untagged[i] : obj.i[i].v;
835+
const tagged = this.switchInfo.success && i<(this.switchInfo.tagged?.length ?? 0) ? this.switchInfo.tagged[i] : obj.i[i].t;
836+
const link = this.switchInfo.success && i<(this.switchInfo.link?.length ?? 0) ? this.switchInfo.link[i] : obj.i[i].l;
837+
833838
list.push({
834839
frontElement : frontElement,
835840
iconElement : iconElement,
836841
numberElement: numElement,
837842
number : obj.i[i].n,
838843
port : obj.i[i].i,
839-
speed : obj.i[i].s,
840-
untagged : obj.i[i].v,
841-
tagged : obj.i[i].t,
842-
link : obj.i[i].l,
844+
speed : speed,
845+
untagged : untagged,
846+
tagged : tagged,
847+
link : link,
843848
comment : obj.i[i].c
844849
});
845850

846851
frontElement.onmouseenter = ()=> {
847852
this.floating.textContent = "";
848853

849-
const port = obj.i[i].i;
850-
const speed = this.switchInfo.success && i<(this.switchInfo.speed?.length ?? 0) ? this.switchInfo.speed[i] : obj.i[i].s;
851-
const untagged = this.switchInfo.success && i<(this.switchInfo.untagged?.length ?? 0) ? this.switchInfo.untagged[i] : obj.i[i].v;
852-
const tagged = this.switchInfo.success && i<(this.switchInfo.tagged?.length ?? 0) ? this.switchInfo.tagged[i] : obj.i[i].t;
853-
const link = this.switchInfo.success && i<(this.switchInfo.link?.length ?? 0) ? this.switchInfo.link[i] : obj.i[i].l;
854-
855854
const speedColorBox = document.createElement("div");
856855
speedColorBox.style.display = "inline-block";
857856
speedColorBox.style.width = "8px";
@@ -1658,7 +1657,7 @@ class DeviceView extends View {
16581657
}
16591658
}
16601659

1661-
if (".interfaces" in this.link && !this.switchInfo.success) {
1660+
if (".interfaces" in this.link && ("snmp profile" in this.link || ".snmp profile" in this.link) && !this.switchInfo.success) {
16621661
this.CreateWarning("SNMP fetch failed. Currently displaying local data", "SNMP");
16631662
}
16641663

Protest/Http/Listener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private static readonly Dictionary<string, Func<HttpListenerContext, Dictionary<
120120
["/snmp/get"] = (ctx, parameters, username) => Protocols.Snmp.Polling.GetHandler(ctx, parameters),
121121
["/snmp/set"] = (ctx, parameters, username) => Protocols.Snmp.Polling.SetHandler(ctx, parameters),
122122
["/snmp/walk"] = (ctx, parameters, username) => Protocols.Snmp.Polling.WalkHandler(ctx, parameters),
123-
["/snmp/switchinterface"] = (ctx, parameters, username) => Protocols.Snmp.Polling.SwitchInterface(ctx, parameters),
123+
["/snmp/switchinterface"] = (ctx, parameters, username) => Protocols.Snmp.Polling.SwitchInterface(parameters),
124124

125125
["/wmi/query"] = (ctx, parameters, username) => OperatingSystem.IsWindows() ? Protocols.Wmi.Query(ctx, parameters) : null,
126126
["/wmi/killprocess"] = (ctx, parameters, username) => OperatingSystem.IsWindows() ? Protocols.Wmi.WmiKillProcess(parameters) : null,

Protest/Protocols/Snmp.Polling.Switch.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Protest.Protocols.Snmp;
99

1010
internal static partial class Polling {
11-
public static byte[] SwitchInterface(HttpListenerContext ctx, Dictionary<string, string> parameters) {
11+
public static byte[] SwitchInterface(Dictionary<string, string> parameters) {
1212
if (parameters is null) { return Data.CODE_INVALID_ARGUMENT.Array; }
1313

1414
parameters.TryGetValue("file", out string file);
@@ -36,13 +36,16 @@ public static byte[] SwitchInterface(HttpListenerContext ctx, Dictionary<string,
3636
return Data.CODE_INVALID_ARGUMENT.Array;
3737
}
3838

39+
return SwitchInterface(_ipAddress, _snmpProfile);
40+
}
41+
42+
public static byte[] SwitchInterface(IPAddress target, SnmpProfiles.Profile snmpProfile) {
3943
try {
40-
IList<Variable> snmpResult = Polling.SnmpQuery(_ipAddress, _snmpProfile, Oid.SWITCH_OID, Polling.SnmpOperation.Walk);
44+
IList<Variable> snmpResult = Polling.SnmpQuery(target, snmpProfile, Oid.SWITCH_OID, Polling.SnmpOperation.Walk);
4145
Dictionary<string, string> interfaces = Polling.ParseResponse(snmpResult);
4246

4347
if (interfaces is null) return "{\"error\":\"Failed to fetch interfaces\"}"u8.ToArray();
4448

45-
4649
Dictionary<int, string> descriptor = new Dictionary<int, string>();
4750
Dictionary<int, string> alias = new Dictionary<int, string>();
4851
Dictionary<int, string> type = new Dictionary<int, string>();
@@ -69,15 +72,15 @@ public static byte[] SwitchInterface(HttpListenerContext ctx, Dictionary<string,
6972
for (int j = startIndex; j < maxIndex; j++) {
7073
byte b = raw[j];
7174
for (int k = 0; k < 8; k++) {
72-
if ((b & (1 << (7 - k))) != 0) {
73-
int portIndex = 8 * (j - startIndex) + (k + 1);
74-
if (!taggedMap.TryGetValue(vlanId, out var ports)) {
75-
ports = new List<int>();
76-
taggedMap[vlanId] = ports;
77-
}
78-
if (!ports.Contains(portIndex)) {
79-
ports.Add(portIndex);
75+
if ((b & (1 << (7 - k))) == 0) continue;
76+
77+
int portIndex = 8 * (j - startIndex) + (k + 1);
78+
if (!taggedMap.TryGetValue(vlanId, out var ports)) {
79+
ports = new List<int>();
80+
taggedMap[vlanId] = ports;
8081
}
82+
if (!ports.Contains(portIndex)) {
83+
ports.Add(portIndex);
8184
}
8285
}
8386
}
@@ -124,20 +127,20 @@ public static byte[] SwitchInterface(HttpListenerContext ctx, Dictionary<string,
124127
}
125128

126129
return JsonSerializer.SerializeToUtf8Bytes(
127-
type.Where(o=> o.Value == "6")
130+
type.Where(o => o.Value == "6")
128131
.Select(pair => new {
129-
number = descriptor.GetValueOrDefault(pair.Key, null),
130-
port = speed.GetValueOrDefault(pair.Key, "N/A") switch {
132+
number = descriptor.GetValueOrDefault(pair.Key, null),
133+
port = speed.GetValueOrDefault(pair.Key, "N/A") switch {
131134
"10000" => "SFP+",
132135
"25000" => "SFP+",
133136
"40000" => "QSFP",
134137
"100000" => "QSFP",
135138
"200000" => "QSFP",
136139
"400000" => "QSFP",
137140
"800000" => "QSFP",
138-
_ => "Ethernet"
141+
_ => "Ethernet"
139142
},
140-
speed = speed.GetValueOrDefault(pair.Key, "N/A") switch {
143+
speed = speed.GetValueOrDefault(pair.Key, "N/A") switch {
141144
"10" => "10 Mbps",
142145
"100" => "100 Mbps",
143146
"1000" => "1 Gbps",
@@ -150,12 +153,12 @@ public static byte[] SwitchInterface(HttpListenerContext ctx, Dictionary<string,
150153
"200000" => "200 Gbps",
151154
"400000" => "400 Gbps",
152155
"800000" => "800 Gbps",
153-
_ => "N/A"
156+
_ => "N/A"
154157
},
155158
untagged = untagged.GetValueOrDefault(pair.Key, ""),
156-
tagged = tagged.GetValueOrDefault(pair.Key, ""),
157-
comment = alias.GetValueOrDefault(pair.Key, String.Empty),
158-
link = DatabaseInstances.FindDeviceByMac(macTable.GetValueOrDefault(pair.Key, null)),
159+
tagged = tagged.GetValueOrDefault(pair.Key, ""),
160+
comment = alias.GetValueOrDefault(pair.Key, String.Empty),
161+
link = DatabaseInstances.FindDeviceByMac(macTable.GetValueOrDefault(pair.Key, null)),
159162
})
160163
);
161164
}

Protest/Tasks/Fetch.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,13 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
410410
data.TryAdd("snmp profile", new string[] { profile.guid.ToString(), "SNMP", string.Empty });
411411
}
412412

413+
if (!data.ContainsKey("type")) {
414+
IList<Variable> dot1dBaseBridgeAddress = Protocols.Snmp.Polling.SnmpQuery(ipAddress, profile, ["1.3.6.1.2.1.17.1.1.0"], Polling.SnmpOperation.Get);
415+
if (dot1dBaseBridgeAddress.Count > 0) {
416+
data.TryAdd("type", new string[] { "Switch", "SNMP", string.Empty });
417+
}
418+
}
419+
413420
if (data.TryGetValue("type", out string[] type) && profile is not null) {
414421
switch (type[0].ToLower().Trim()) {
415422
case "fax":
@@ -429,11 +436,7 @@ public static ConcurrentDictionary<string, string[]> SingleDevice(string target,
429436
case "firewall":
430437
case "router":
431438
case "switch":
432-
IList<Variable> switchResult = Protocols.Snmp.Polling.SnmpQuery(ipAddress, profile, Protocols.Snmp.Oid.SWITCH_OID, Polling.SnmpOperation.Get);
433-
Dictionary<string, string> switchFormatted = Protocols.Snmp.Polling.ParseResponse(switchResult);
434-
if (switchFormatted is not null && switchFormatted.TryGetValue(Protocols.Snmp.Oid.INTERFACE_TOTAL, out string snmpSwitchSerialNo)) {
435-
data.TryAdd("total interfaces", new string[] { snmpSwitchSerialNo, "SNMP", string.Empty });
436-
}
439+
byte[] s = Protocols.Snmp.Polling.SwitchInterface(ipList.First(), profile);
437440
break;
438441
}
439442
}

0 commit comments

Comments
 (0)