rendered paste bodyusing System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Net;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections;
using System.Xml;
using System.Xml.Schema;
namespace WindowsApplication1
{
class upnpDevice
{
private String url;
private String type;
private String urlbase;
/**
* This object receives a query reply,
* From here this object should get all the info from the upnp device
* :)
*/
public upnpDevice(String reply){
Console.WriteLine(getUrl(reply));
getXML();
//Console.WriteLine("?");
}
private string getUrl(string reply)
{
/* QueryResult is somthing like this:
*
HTTP/1.1 200 OK
Cache-Control:max-age=60
Location:http://10.10.10.1:80/upnp/service/des_ppp.xml
Server:NT/5.0 UPnP/1.0
ST:upnp:rootdevice
EXT:
USN:uuid:upnp-InternetGatewayDevice-1_0-00095bd945a2::upnp:rootdevice
*/
string location = "";
string[] parts = reply.Split(new string[] {
System.Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries);
//Go through the xml and get the location
foreach (string part in parts)
{
if (part.ToLower().StartsWith("location"))
{
location = part.Substring(part.IndexOf(':') + 1);
location = location.Substring(1);
break;
}
}
//What kind of upnp device does not fucking contain this!
if (location.Length == 0)
return "";
//save that bitch!
this.url = location;
//then using the location url, we get more information:
return location;
}
private void getXML() {
XmlDocument xDoc = new XmlDocument();
System.Net.WebClient webClient = new WebClient();
try
{
string ret = webClient.DownloadString(this.url);
xDoc.LoadXml(ret);
XmlNodeList typedesc = xDoc.GetElementsByTagName("URLBase");
this.type = typedesc[0].InnerText;
urlbase = this.type;
//Console.WriteLine(this.type);
//return ret;//return services
//Console.Write(ret);
openPortFromService("urn:upnp-org:serviceId:WANIPConn1",
"urn:schemas-upnp-org:service:WANIPConnection:1", "192.168.1.123", "192.168.1.1", 81, 81);
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
webClient.Dispose();
}
//return "";
}
//This is a bit of a hack, I have to figure out the xml to get the controlURL, but thats not hard.
private void openPortFromService(string controlURL, String serviceType,
string machineIP, string firewallIP, int gatewayPort, int
portToForward)
{
String controlUrl = "";
controlUrl = "/uuid:0016-b6cf-48690200cbb4/WANIPConnection:1";
string soapBody = "<s:Envelope " +
"xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/ \" " +
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/ \">" +
"<s:Body>" +
"<u:AddPortMapping xmlns:u=\"" + serviceType + "\">" +
"<NewRemoteHost></NewRemoteHost>" +
"<NewExternalPort>" + portToForward.ToString() +
"</NewExternalPort>" +
"<NewProtocol>TCP</NewProtocol>" +
"<NewInternalPort>" + portToForward.ToString() +
"</NewInternalPort>" +
"<NewInternalClient>" + machineIP +
"</NewInternalClient>" +
"<NewEnabled>1</NewEnabled>" +
"<NewPortMappingDescription>Altum</NewPortMappingDescription>" +
"<NewLeaseDuration>0</NewLeaseDuration>" +
"</u:AddPortMapping>" +
"</s:Body>" +
"</s:Envelope>";
byte[] body =
System.Text.UTF8Encoding.ASCII.GetBytes(soapBody);
string url = urlbase + controlUrl.Substring(1);
Console.WriteLine("------" + url);
System.Net.WebRequest wr =
System.Net.WebRequest.Create(url);//+ controlUrl);
wr.Method = "POST";
wr.Headers.Add("SOAPAction", "\"" + serviceType +
"#AddPortMapping\"");
wr.ContentType = "text/xml;charset=\"utf-8\"";
wr.ContentLength = body.Length;
System.IO.Stream stream = wr.GetRequestStream();
stream.Write(body, 0, body.Length);
stream.Flush();
stream.Close();
WebResponse wres = wr.GetResponse();
System.IO.StreamReader sr = new
System.IO.StreamReader(wres.GetResponseStream());
string ret = sr.ReadToEnd();
Console.WriteLine(":"+ret);
sr.Close();
Debug.WriteLine("Setting port forwarding:" +
portToForward.ToString() + "\r\r" + ret);
}
/*
public static void OpenFirewallPort(string machineIP, string
firewallIP, int openPort)
{
string svc = getServicesFromDevice(firewallIP);
openPortFromService(svc, "urn:schemas-upnp-org:service:WANIPConnection:1", machineIP,
firewallIP, 90, openPort);
openPortFromService(svc,
"urn:schemas-upnp-org:service:WANPPPConnection:1", machineIP,
firewallIP, 90, openPort);
}*/
}
}