/
ProfanityInspector.cs
155 lines (127 loc) · 5.24 KB
/
ProfanityInspector.cs
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
namespace ChatShared
{
class ProfanityInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
if (channel.LocalAddress.ToString().Contains("duplex"))
{
var replacements = new Dictionary<string, string>();
replacements.Add("fuck", "floob");
replacements.Add("god", "glob");
replacements.Add("ass", "butt");
replacements.Add("fart", "poot");
replacements.Add("bitch", "dingus");
replacements.Add("shit", "poo");
request = ChangeString(request, replacements);
Console.WriteLine("Inspector Called...");
} else
{
Console.WriteLine("Not a chat message, skip inspection...");
}
//no need for correlationState, so return null
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
public Message ChangeString(Message oldMessage, Dictionary<string,string> replacements)
{
Message newMessage = null;
//load the old message into XML
MessageBuffer msgbuf = oldMessage.CreateBufferedCopy(int.MaxValue);
Message tmpMessage = msgbuf.CreateMessage();
XmlDictionaryReader xdr = tmpMessage.GetReaderAtBodyContents();
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xdr);
xdr.Close();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("a", "http://failedturing.com/chatdemo");
nsmgr.AddNamespace("i", "http://www.w3.org/2001/XMLSchema-instance");
XmlNode node = xdoc.SelectSingleNode("//a:Message", nsmgr);
if (node != null) {
//node.InnerText = "[Modified in SimpleMessageInspector]" + node.InnerText;
string body = node.InnerText;
foreach (string from in replacements.Keys)
{
string to = replacements[from];
body = Regex.Replace(body, from, to, RegexOptions.IgnoreCase);
}
node.InnerText = body;
}
MemoryStream ms = new MemoryStream();
XmlWriter xw = XmlWriter.Create(ms);
xdoc.Save(xw);
xw.Flush();
xw.Close();
ms.Position = 0;
XmlReader xr = XmlReader.Create(ms);
newMessage = Message.CreateMessage(oldMessage.Version, null, xr);
newMessage.Headers.CopyHeadersFrom(oldMessage);
newMessage.Properties.CopyProperties(oldMessage.Properties);
return newMessage;
}
}
public class ProfanityInterceptorBehavior : IServiceBehavior, IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
throw new NotImplementedException();
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new ProfanityInspector());
}
//https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-inspect-and-modify-messages-on-the-service
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
{
epDisp.DispatchRuntime.MessageInspectors.Add(new ProfanityInspector());
}
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
public class ProfanityInterceptorBehaviorExtensionElement : BehaviorExtensionElement
{
public override Type BehaviorType
{
get
{
return typeof(ProfanityInterceptorBehavior);
}
}
protected override object CreateBehavior()
{
return new ProfanityInterceptorBehavior();
}
}
}