5using System.Threading.Tasks;
24 private readonly DateTime created = DateTime.UtcNow;
25 private readonly DateTime expires;
27 private readonly
string[] privileges;
28 private readonly
string userVariable;
29 private readonly
string resource;
30 private readonly
string snifferId;
31 private readonly
bool feedbackCheck;
32 private string[] tabIds =
null;
33 private DateTime tabIdTimestamp = DateTime.MinValue;
48 string UserVariable, params
string[] Privileges)
49 : base(
"Web Sniffer: " + PageResource)
51 this.expires = DateTime.UtcNow.Add(MaxLife);
52 this.comLayer = ComLayer;
54 this.resource = PageResource;
57 this.userVariable = UserVariable;
58 this.privileges = Privileges;
61 if (this.feedbackCheck)
62 this.outgoing =
new Cache<string, bool>(
int.MaxValue, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1),
true);
73 return base.DisposeAsync();
86 private Task Process(DateTime Timestamp,
string Message,
string Function)
88 if (Timestamp >= this.expires)
91 return this.Push(Timestamp, Message, Function,
true);
94 private async Task Push(DateTime Timestamp,
string Message,
string Function,
bool CloseIfNoTabs)
98 DateTime Now = DateTime.UtcNow;
100 if ((Now - this.tabIdTimestamp).TotalSeconds > 2 || this.tabIds is
null || this.tabIds.Length == 0)
103 this.tabIdTimestamp = Now;
106 if (this.feedbackCheck && Message.StartsWith(
'{') && Message.EndsWith(
'}'))
111 if (Parsed is IDictionary<string, object> Obj &&
112 Obj.TryGetValue(
"data", out
object Temp) &&
113 Temp is IDictionary<string, object> Obj2 &&
114 Obj2.TryGetValue(
"timestamp", out
object Timestamp2) &&
115 Obj2.TryGetValue(
"message", out
object Message2) &&
116 (
this.outgoing?.ContainsKey(
this.ToJson(Timestamp2, Message2)) ??
true))
127 string Data = this.ToJson(
XML.
Encode(Timestamp), Message);
129 this.outgoing?.
Add(Data,
true);
131 int Tabs = await ClientEvents.PushEvent(this.tabIds, Function, Data,
true, this.userVariable, this.privileges);
133 if (CloseIfNoTabs && Tabs <= 0 && (Now - this.created).TotalSeconds >= 5)
142 private string ToJson(
object Timestamp,
object Message)
144 return JSON.
Encode(
new KeyValuePair<string, object>[]
146 new KeyValuePair<string, object>(
"timestamp", Timestamp),
147 new KeyValuePair<string, object>(
"message", Message)
151 private async Task Close()
153 await this.Push(DateTime.UtcNow,
"Sniffer closed.",
"Information",
false);
154 this.comLayer.Remove(
this);
158 private Task Process(DateTime Timestamp,
byte[] Data,
int Offset,
int Count,
string Function)
160 if (Timestamp >= this.expires)
163 return this.Push(Timestamp, this.HexOutput(Data, Offset, Count), Function,
true);
166 private string HexOutput(
byte[] Data,
int Offset,
int Count)
169 return "<" + Count.ToString() +
" bytes>";
174 StringBuilder sb =
new StringBuilder();
190 sb.Append(b.ToString(
"X2"));
195 return sb.ToString();
198 return Convert.ToBase64String(Data);
202 return "<" + Count.ToString() +
" bytes>";
Helps with common JSON-related tasks.
static object Parse(string Json)
Parses a JSON string.
static string Encode(string s)
Encodes a string for inclusion in JSON.
Helps with common XML-related tasks.
static string Encode(string s)
Encodes a string for use in XML.
Class representing an event.
DateTime Timestamp
Timestamp of event.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
The ClientEvents class allows applications to push information asynchronously to web clients connecte...
static string[] GetTabIDsForLocation(string Location)
Gets the Tab IDs of all tabs that display a particular resource.
Sending sniffer events to the corresponding web page(s).
override Task Process(SnifferRxBinary Event, CancellationToken Cancel)
Processes a binary reception event.
override Task Process(SnifferInformation Event, CancellationToken Cancel)
Processes an information event.
string SnifferId
Sniffer ID
override Task Process(SnifferError Event, CancellationToken Cancel)
Processes an error event.
override Task DisposeAsync()
IDisposableAsync.DisposeAsync
WebSniffer(string SnifferId, string PageResource, TimeSpan MaxLife, BinaryPresentationMethod BinaryPresentationMethod, ICommunicationLayer ComLayer, string UserVariable, params string[] Privileges)
Sending sniffer events to the corresponding web page(s).
override Task Process(SnifferRxText Event, CancellationToken Cancel)
Processes a text reception event.
override Task Process(SnifferWarning Event, CancellationToken Cancel)
Processes a warning event.
override BinaryPresentationMethod BinaryPresentationMethod
How the sniffer handles binary data.
override Task Process(SnifferTxText Event, CancellationToken Cancel)
Processes a text transmission event.
override Task Process(SnifferTxBinary Event, CancellationToken Cancel)
Processes a binary transmission event.
override Task Process(SnifferException Event, CancellationToken Cancel)
Processes an exception event.
Implements an HTTP server.
Represents a sniffer error event.
Represents a sniffer exception event.
Represents a sniffer binary reception event.
Represents a sniffer text reception event.
Represents a sniffer binary transmission event.
Represents a sniffer text transmission event.
Represents a sniffer warning event.
Abstract base class for sniffers. Implements default method overloads.
Implements an in-memory cache.
void Dispose()
IDisposable.Dispose
void Add(KeyType Key, ValueType Value)
Adds an item to the cache.
Interface for observable classes implementing communication protocols.
BinaryPresentationMethod
How binary data is to be presented.