Skip to content

Commit

Permalink
Don't try to parse UPnP response as XML
Browse files Browse the repository at this point in the history
Also don't bother logging Target in error logs, because it's useless 99% of the time as it's just the same method as first line of stacktrace
  • Loading branch information
UnknownShadow200 committed Jul 3, 2021
1 parent 42150fd commit cb45fc8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
31 changes: 15 additions & 16 deletions GUI/UPnP.cs
Expand Up @@ -19,12 +19,11 @@
using System.Text;
using System.Threading;
using System.Xml;

using MCGalaxy.Network;
//This upnp class comes from http://www.codeproject.com/Articles/27992/NAT-Traversal-with-UPnP-in-C, Modified for use with MCForge

namespace MCGalaxy.Core {

public sealed class UPnP {
namespace MCGalaxy {
public static class UPnP {

public static TimeSpan Timeout = TimeSpan.FromSeconds(3);

Expand Down Expand Up @@ -85,7 +84,7 @@ public sealed class UPnP {
if (String.IsNullOrEmpty(_serviceUrl) )
throw new InvalidOperationException("No UPnP service available or Discover() has not been called");

XmlDocument xdoc = SOAPRequest(_serviceUrl,
string xdoc = SOAPRequest(_serviceUrl,
"<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
"<NewRemoteHost></NewRemoteHost>" +
"<NewExternalPort>" + port + "</NewExternalPort>" +
Expand All @@ -102,7 +101,7 @@ public sealed class UPnP {
if (String.IsNullOrEmpty(_serviceUrl) )
throw new InvalidOperationException("No UPnP service available or Discover() has not been called");

XmlDocument xdoc = SOAPRequest(_serviceUrl,
string xdoc = SOAPRequest(_serviceUrl,
"<u:DeletePortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
"<NewRemoteHost></NewRemoteHost>" +
"<NewExternalPort>" + port + "</NewExternalPort>" +
Expand Down Expand Up @@ -156,25 +155,25 @@ public sealed class UPnP {
return "?";
}

static XmlDocument SOAPRequest(string url, string soap, string function) {
string req = "<?xml version=\"1.0\"?>" +
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
"<s:Body>" + soap + "</s:Body>" +
"</s:Envelope>";
/// <summary> Performs a XML SOAP request </summary>
/// <returns> XML response from the service </returns>
static string SOAPRequest(string url, string soap, string function) {
string req =
"<?xml version=\"1.0\"?>" +
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
"<s:Body>" + soap + "</s:Body>" +
"</s:Envelope>";

WebRequest r = HttpWebRequest.Create(url);
r.Method = "POST";
r.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:WANIPConnection:1#" + function + "\"");
r.ContentType = "text/xml; charset=\"utf-8\"";

byte[] data = Encoding.UTF8.GetBytes(req);
r.ContentLength = data.Length;
r.GetRequestStream().Write(data, 0, data.Length);
HttpUtil.SetRequestData(r, data);

XmlDocument doc = new XmlDocument();
WebResponse res = r.GetResponse();
doc.Load(res.GetResponseStream());
return doc;
return HttpUtil.GetResponseText(res);
}
}
}
3 changes: 1 addition & 2 deletions MCGalaxy/Server/Logger.cs
Expand Up @@ -133,11 +133,10 @@ public static class Logger {
}

static void DescribeError(Exception ex, StringBuilder sb) {
// Attempt to gather this info. Skip anything that you can't read for whatever reason
// Attempt to gather this info. Skip anything that you can't read for whatever reason
try { sb.AppendLine("Type: " + ex.GetType().Name); } catch { }
try { sb.AppendLine("Source: " + ex.Source); } catch { }
try { sb.AppendLine("Message: " + ex.Message); } catch { }
try { sb.AppendLine("Target: " + ex.TargetSite.Name); } catch { }
try { sb.AppendLine("Trace: " + ex.StackTrace); } catch { }

// Exception-specific extra details
Expand Down

0 comments on commit cb45fc8

Please sign in to comment.