Skip to content

Commit 02419cf

Browse files
author
Lucas Ontivero
committed
Merge pull request lontivero#27 from lontivero/tracing
Tracing
2 parents 79642af + 3d07f15 commit 02419cf

13 files changed

+131
-39
lines changed

Open.Nat.ConsoleTest/ColorConsoleTraceListener.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,31 @@ public override void TraceEvent(TraceEventCache eventCache, string source, Trace
4242
{
4343
lock (_sync)
4444
{
45-
ConsoleColor color;
46-
switch (eventType)
47-
{
48-
case TraceEventType.Error:
49-
color = ConsoleColor.Red;
50-
break;
51-
case TraceEventType.Warning:
52-
color = ConsoleColor.Yellow;
53-
break;
54-
case TraceEventType.Information:
55-
color = ConsoleColor.Green;
56-
break;
57-
case TraceEventType.Verbose:
58-
color = ConsoleColor.DarkCyan;
59-
break;
60-
default:
61-
color = ConsoleColor.Gray;
62-
break;
63-
}
64-
65-
var eventTypeString = Enum.GetName(typeof (TraceEventType), eventType);
66-
var message = source + " - " + eventTypeString + " > " + string.Format(format, args);
45+
if(Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, format, args, null, null)) return;
46+
ConsoleColor color;
47+
switch (eventType)
48+
{
49+
case TraceEventType.Error:
50+
color = ConsoleColor.Red;
51+
break;
52+
case TraceEventType.Warning:
53+
color = ConsoleColor.Yellow;
54+
break;
55+
case TraceEventType.Information:
56+
color = ConsoleColor.Green;
57+
break;
58+
case TraceEventType.Verbose:
59+
color = ConsoleColor.DarkCyan;
60+
break;
61+
default:
62+
color = ConsoleColor.Gray;
63+
break;
64+
}
6765

68-
WriteColor(message + Environment.NewLine, color);
66+
var eventTypeString = Enum.GetName(typeof (TraceEventType), eventType);
67+
var message = source + " - " + eventTypeString + " > " + (args.Length > 0 ? string.Format(format, args): format);
6968

69+
WriteColor(message + Environment.NewLine, color);
7070
}
7171
}
7272

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Diagnostics;
2+
3+
namespace Open.Nat.ConsoleTest
4+
{
5+
public class HttpTrafficOnlyFilter : TraceFilter
6+
{
7+
public override bool ShouldTrace(TraceEventCache cache, string source, TraceEventType eventType, int id, string formatOrMessage, object[] args, object data1, object[] data)
8+
{
9+
if(source == "System.Net" && eventType == TraceEventType.Verbose )
10+
{
11+
if (formatOrMessage.Contains("<<") || formatOrMessage.Contains("//"))
12+
{
13+
return true;
14+
}
15+
return false;
16+
}
17+
if (source == "System.Net" && eventType == TraceEventType.Information)
18+
{
19+
if (formatOrMessage.Contains("Request:") || formatOrMessage.Contains("Sending headers") ||
20+
formatOrMessage.Contains("Received status line:") || formatOrMessage.Contains("Received headers"))
21+
{
22+
return true;
23+
}
24+
return false;
25+
26+
}
27+
return true;
28+
}
29+
}
30+
}

Open.Nat.ConsoleTest/Main.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ namespace Open.Nat.ConsoleTest
3838
class NatTest
3939
{
4040
public static void Main(string[] args)
41-
{
42-
NatDiscoverer.TraceSource.Switch.Level = SourceLevels.Verbose;
43-
NatDiscoverer.TraceSource.Listeners.Add(new ColorConsoleTraceListener());
41+
{
4442
Test().Wait();
4543

4644
Console.WriteLine("");
@@ -89,7 +87,6 @@ private async static Task Test()
8987
await device.DeletePortMapAsync(new Mapping(Protocol.Tcp, 1600, 1700));
9088
sb.AppendFormat("\n[Done]");
9189

92-
9390
Console.WriteLine(sb.ToString());
9491
/*
9592
var mappings = await device.GetAllMappingsAsync();

Open.Nat.ConsoleTest/Open.Nat.ConsoleTest.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<ItemGroup>
4646
<Compile Include="AssemblyInfo.cs" />
4747
<Compile Include="ColorConsoleTraceListener.cs" />
48+
<Compile Include="HttpTrafficOnlyFilter.cs" />
4849
<Compile Include="Main.cs" />
4950
</ItemGroup>
5051
<ItemGroup>
@@ -54,7 +55,9 @@
5455
</ProjectReference>
5556
</ItemGroup>
5657
<ItemGroup>
57-
<None Include="app.config" />
58+
<None Include="app.config">
59+
<SubType>Designer</SubType>
60+
</None>
5861
</ItemGroup>
5962
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets" />
6063
<ProjectExtensions>

Open.Nat.ConsoleTest/app.config

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
11
<?xml version="1.0"?>
22
<configuration>
3-
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
3+
<system.diagnostics>
4+
<sources>
5+
<source name="System.Net" tracemode="protocolonly" maxdatasize="1024">
6+
<listeners>
7+
<add name="System.Net"/>
8+
</listeners>
9+
</source>
10+
<source name="Open.NAT">
11+
<listeners>
12+
<add name="System.Net"/>
13+
<add name="ColoredConsole"/>
14+
</listeners>
15+
</source>
16+
<source name="System.Net.Sockets">
17+
<listeners>
18+
<add name="System.Net"/>
19+
</listeners>
20+
</source>
21+
</sources>
22+
<switches>
23+
<add name="System.Net" value="Verbose"/>
24+
<add name="System.Net.Sockets" value="Warning"/>
25+
<add name="Open.NAT" value="Information"/>
26+
</switches>
27+
<sharedListeners>
28+
<add name="System.Net"
29+
type="System.Diagnostics.TextWriterTraceListener"
30+
initializeData="network.log">
31+
<filter type="Open.Nat.ConsoleTest.HttpTrafficOnlyFilter, Open.Nat.ConsoleTest, Version=0.6.0.0, Culture=neutral, PublicKeyToken=null" />
32+
</add>
33+
<add name="ColoredConsole" type="Open.Nat.ConsoleTest.ColorConsoleTraceListener, Open.Nat.ConsoleTest, Version=0.6.0.0, Culture=neutral, PublicKeyToken=null">
34+
</add>
35+
</sharedListeners>
36+
<trace autoflush="true"/>
37+
</system.diagnostics>
38+
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
39+
</configuration>

Open.Nat/Discovery/Searcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal abstract class Searcher
4545

4646
public async Task<IEnumerable<NatDevice>> Search(CancellationToken cancelationToken)
4747
{
48-
await Task.Factory.StartNew(async _ =>
48+
await Task.Factory.StartNew(_ =>
4949
{
5050
NatDiscoverer.TraceSource.LogInfo("Searching for: {0}", GetType().Name);
5151
while (!cancelationToken.IsCancellationRequested)

Open.Nat/NatDiscoverer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public async Task<NatDevice> DiscoverDeviceAsync(PortMapper portMapper, Cancella
6767
var device = devices.FirstOrDefault();
6868
if(device==null)
6969
{
70+
TraceSource.LogInfo("Device not found. Common reasons:");
71+
TraceSource.LogInfo("\t* No device is present or,");
72+
TraceSource.LogInfo("\t* Upnp is disabled in the router or");
73+
TraceSource.LogInfo("\t* Antivirus software is filtering SSDP (discovery protocol).");
7074
throw new NatDeviceNotFoundException();
7175
}
7276
return device;

Open.Nat/Open.Nat.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@
9797
<Compile Include="Utils\WellKnownConstants.cs" />
9898
</ItemGroup>
9999
<ItemGroup>
100-
<None Include="Open.Nat.nuspec" />
100+
<None Include="Open.Nat.nuspec">
101+
<SubType>Designer</SubType>
102+
</None>
101103
<None Include="Open.Nat.snk" />
102104
</ItemGroup>
103105
<ItemGroup />

Open.Nat/Open.Nat.nuspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
<copyright>Please see LICENSE for more details.</copyright>
1818
<tags>NAT TRAVERSAL UPNP PMP PORT FORWARD .NET MONO</tags>
1919
<releaseNotes>
20-
**Version 2.0.11
20+
**Version 2.0.11**
2121
Allows the creation of mappings with arbitrary Private IP address.
22-
Fixes [defect #22](https://github.com/lontivero/Open.NAT/issues/22).
22+
Fixes defect #22.
2323
- Routers failed with 404 when service control url had a question mark (?)
2424
- DD-WRT Linux base router (and others probably) fails with 402-InvalidArgument when index is out of range.
2525
- Some routers retuns invalid mapping entries with empty internal client.
2626

27-
Fixes [defect #24](https://github.com/lontivero/Open.NAT/issues/24).
27+
Fixes defect #24.
2828
GetSpecificMappingEntry fails with 402-InvalidArgument in DD-WRT Linux base router when mapping is not found.
2929

3030
**Version 2.0.10**

Open.Nat/Upnp/SoapClient.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public async Task<XmlDocument> InvokeAsync(string operationName, IDictionary<str
7474
: reader.ReadToEnd();
7575

7676
var responseXml = GetXmlDocument(responseBody);
77-
NatDiscoverer.TraceSource.TraceEvent(TraceEventType.Verbose, 0, "Response: \n{0}", responseXml.ToPrintableXml());
7877

7978
response.Close();
8079
return responseXml;
@@ -130,7 +129,6 @@ private byte[] BuildMessageBody(string operationName, IEnumerable<KeyValuePair<s
130129
sb.Append("</s:Envelope>\r\n\r\n");
131130
string requestBody = sb.ToString();
132131

133-
NatDiscoverer.TraceSource.TraceEvent(TraceEventType.Verbose, 0, requestBody);
134132
byte[] messageBody = Encoding.UTF8.GetBytes(requestBody);
135133
return messageBody;
136134
}

Open.Nat/Upnp/UpnpNatDevice.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ internal UpnpNatDevice (UpnpNatDeviceInfo deviceInfo)
4949

5050
public override async Task<IPAddress> GetExternalIPAsync()
5151
{
52+
NatDiscoverer.TraceSource.LogInfo("GetExternalIPAsync - Getting external IP address");
5253
var message = new GetExternalIPAddressRequestMessage();
5354
var responseData = await _soapClient
5455
.InvokeAsync("GetExternalIPAddress", message.ToXml())
@@ -61,8 +62,9 @@ public override async Task<IPAddress> GetExternalIPAsync()
6162
public override async Task CreatePortMapAsync(Mapping mapping)
6263
{
6364
Guard.IsNotNull(mapping, "mapping");
65+
if(mapping.PrivateIP.Equals(IPAddress.None)) mapping.PrivateIP = DeviceInfo.LocalAddress;
6466

65-
mapping.PrivateIP = DeviceInfo.LocalAddress;
67+
NatDiscoverer.TraceSource.LogInfo("CreatePortMapAsync - Creating port mapping {0}", mapping);
6668
try
6769
{
6870
var message = new CreatePortMappingRequestMessage(mapping);
@@ -109,6 +111,10 @@ public override async Task DeletePortMapAsync(Mapping mapping)
109111
{
110112
Guard.IsNotNull(mapping, "mapping");
111113

114+
if (mapping.PrivateIP.Equals(IPAddress.None)) mapping.PrivateIP = DeviceInfo.LocalAddress;
115+
116+
NatDiscoverer.TraceSource.LogInfo("DeletePortMapAsync - Deleteing port mapping {0}", mapping);
117+
112118
try
113119
{
114120
var message = new DeletePortMappingRequestMessage(mapping);
@@ -128,6 +134,7 @@ public override async Task<IEnumerable<Mapping>> GetAllMappingsAsync()
128134
var index = 0;
129135
var mappings = new List<Mapping>();
130136

137+
NatDiscoverer.TraceSource.LogInfo("GetAllMappingsAsync - Getting all mappings");
131138
while (true)
132139
{
133140
try
@@ -178,6 +185,8 @@ public override async Task<Mapping> GetSpecificMappingAsync (Protocol protocol,
178185
Guard.IsTrue(protocol == Protocol.Tcp || protocol == Protocol.Udp, "protocol");
179186
Guard.IsInRange(port, 0, ushort.MaxValue, "port");
180187

188+
NatDiscoverer.TraceSource.LogInfo("GetSpecificMappingAsync - Getting mapping for protocol: {0} port: {1}", Enum.GetName(typeof(Protocol), protocol), port);
189+
181190
try
182191
{
183192
var message = new GetSpecificPortMappingEntryRequestMessage(protocol, port);

Open.Nat/Upnp/UpnpSearcher.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ public override NatDevice AnalyseReceivedResponse(IPAddress localAddress, byte[]
121121
var message = new DiscoveryResponseMessage(dataString);
122122
var serviceType = message["ST"];
123123

124-
NatDiscoverer.TraceSource.TraceEvent(TraceEventType.Verbose, 0, "UPnP Response: {0}", dataString);
124+
if (!IsValidControllerService(serviceType))
125+
{
126+
NatDiscoverer.TraceSource.LogWarn("Invalid controller service. Ignoring.");
125127

126-
if (!IsValidControllerService(serviceType)) return null;
128+
return null;
129+
}
127130
NatDiscoverer.TraceSource.LogInfo("UPnP Response: Router advertised a '{0}' service!!!", serviceType);
128131

129132
var location = message["Location"] ?? message["AL"];

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ Build Status
6565

6666
[![NuGet version](https://badge.fury.io/nu/open.nat.png)](http://badge.fury.io/nu/open.nat)
6767

68+
[![Issue Stats](http://www.issuestats.com/github/lontivero/Open.NAT/badge/issue)](http://www.issuestats.com/github/lontivero/Open.NAT)
69+
70+
[![Issue Stats](http://www.issuestats.com/github/lontivero/Open.NAT/badge/pr?style=flat)](http://www.issuestats.com/github/lontivero/Open.NAT)
71+
6872
### Version 2.0.11
6973
* Allows the creation of mappings with arbitrary Private IP address.
7074
* Fixes [defect #22](https://github.com/lontivero/Open.NAT/issues/22).
@@ -119,3 +123,9 @@ Fixes [defect #20](https://github.com/lontivero/Open.NAT/issues/20). Absolute se
119123
* Tracing improved
120124
* Cleaner code
121125

126+
127+
##Help me to maitain Open.NAT
128+
129+
![Bitcoin address](https://github.com/lontivero/Open.Nat/raw/gh-pages/images/bitcoinQR.png)
130+
131+
15fdF4xeZBZMqj8ybrrW7L392gZbx4sCXH

0 commit comments

Comments
 (0)