Forum > Networking and Web Programming

[SOLVED] How to open UPnP port with synapse

(1/24) > >>

Key-Real:
Hi

I cant open a port with UPnP :(


my Server to listen:

the Server works if I do PortForwarding

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC}{$H+}uses    blcksock, sockets, Synautil, SysUtils, HTTPSend, synaip, Process, synamisc, crt;var    soket : TUDPBlockSocket;                              begin        soket:= TUDPBlockSocket.Create;    soket.bind('0.0.0.0', '4444');      readln;    soket.Free;end. 
my Code to open the port:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC}{$H+}uses httpsend,sysutils,classes,synautil;var        f : text;        post : string;        s : string;        http : THTTPSend;        Response : TStringList;begin         assignfile(f, 'add.xml');        reset(f);        post:= '';        repeat                readln(f, s);                post:= post + s;        until eof(f);        close(f);          //writeln(post);         http:=THTTPSend.Create;         http.Document.Clear;     WriteStrToStream(HTTP.Document, post);          http.MimeType:='text/xml; charset="UTF-8"';     http.Headers.Add('SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"');         if not http.HTTPMethod('POST', 'http://192.168.0.1/upnp/control/WANIPConnection0') then begin        end;         writeln(http.ResultCode);        writeln(http.ResultString);            Response:= TStringList.Create;        Response.LoadFromStream(HTTP.Document);        s:= Response.Text;        writeln(s);         http.Free;        Response.Free;end. 
the add.xml:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---<?xml version="1.0"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body>    <m:AddPortMapping xmlns:m="urn:schemas-upnp-org:service:WANIPConnection:1">        <NewPortMappingDescription>test</NewPortMappingDescription>        <NewLeaseDuration>0</NewLeaseDuration>        <NewInternalClient>192.168.0.4</NewInternalClient>        <NewEnabled>True</NewEnabled>        <NewExternalPort>3333</NewExternalPort>        <NewRemoteHost></NewRemoteHost>        <NewProtocol>UDP</NewProtocol>        <NewInternalPort>4444</NewInternalPort>    </m:AddPortMapping></SOAP-ENV:Body></SOAP-ENV:Envelope> 

the controll.xml sniped from my router:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---<serviceList><service><serviceType>urn:schemas-upnp-org:service:WANIPConnection:1</serviceType><serviceId>urn:upnp-org:serviceId:WANIPConn1</serviceId><SCPDURL>/WANIPConnectionServiceSCPD.xml</SCPDURL><controlURL>/upnp/control/WANIPConnection0</controlURL><eventSubURL>/upnp/event/WANIPConnection0</eventSubURL></service></serviceList> 

I get this if I send the M-SEARCH to my router:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---HTTP/1.1 200 OKCACHE-CONTROL: max-age=1800DATE: Tue, 13 Jun 2023 09:35:31 GMTEXT:LOCATION: http://192.168.0.1:49153/IGDdevicedesc_brlan0.xmlOPT: "http://schemas.upnp.org/upnp/1/0/"; ns=0101-NLS: b8c5fa84-09cc-11ee-a2e9-9217cd6c62d8SERVER: Linux/4.9.135-Prod_19.3, UPnP/1.0, Portable SDK for UPnP devices/1.6.22X-User-Agent: redsonicST: upnp:rootdeviceUSN: uuid:ebf5a0a0-1dd1-11b2-a90f-021018951e6c::upnp:rootdevice 

I check the openPort with nmap


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---sudo nmap -sU -v -p 3333 31.18.77.240

what do I did wrong?

rvk:

--- Quote from: Key-Real on June 13, 2023, 11:39:49 am ---what do I did wrong?

--- End quote ---
Well, you don't mention to us what the result of the POST to WANIPConnection0 is.

Are you sure UPnP is enabled on your router?

And you don't need to check with nmap. Just check in the router itself if the UPnP is done correctly (there should be a list for this).
And otherwise you can do a POST with #GetGenericPortMappingEntry to get the ports.

(You can also check with the utility miniupnp to see if the redirect is done correctly.)

PS. The xml is correct (assuming the internal IP is correct). With the same code (changing the internal IP) the xml works for me.

Key-Real:
then i do:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#! /usr/bin/env pythonimport miniupnpc # create the objectu = miniupnpc.UPnP()u.discoverdelay = 200;  print('Discovering... delay=%ums' % u.discoverdelay)ndevices = u.discover()print(ndevices, 'device(s) detected') # select an igdu.selectigd() print('local ip address :', u.lanaddr)externalipaddress = u.externalipaddress()print('external ip address :', externalipaddress)  try:                u.addportmapping(4444, 'UDP', u.lanaddr, 4444, 'test', '') except:         print('Fail') 
and after that I start my server it works.




I have a code to return a M-SEARCH message from Router:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC}uses    sockets, SysUtils; procedure SocketError(s:string);begin    writeln('Scket Error: ', s);    halt(1);end; const        MS = 'M-SEARCH * HTTP/1.1' + #$0D + #$0A +    'HOST:239.255.255.250:1900' + #$0D + #$0A +    'ST:upnp:rootdevice' + #$0D + #$0A +    'MX:2' + #$0D + #$0A +    'MAN:"ssdp:discover"' + #$0D + #$0A +    #$0D + #$0A; var        socket : longint;        addr : sockaddr_in;        slen : dword;        buf : array [0..8192] of char;begin        socket:= fpsocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);    if socket = -1 then SocketError('Init Socket');         fillchar(addr, 0, sizeof(addr));    addr.sin_family:= AF_INET;        addr.sin_addr:= StrToNetAddr('239.255.255.250');        addr.sin_port:= htons(1900);         slen:= sizeof(addr);         if fpsendto(socket, pchar(MS), sizeof(MS), 0, @addr, sizeof(addr)) = -1 then SocketError('send MS message');         if fprecvfrom(socket, @buf, sizeof(buf), 0, @addr, @slen) = -1 then SocketError('Receive message from Router');         writeln('router send me this ip and address as response: ',NetAddrToStr(addr.sin_addr), ':', addr.sin_port);         writeln(buf);     closeSocket(socket);end. 


it returns:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---router send me this ip and address as response: 192.168.0.1:63633HTTP/1.1 200 OKCACHE-CONTROL: max-age=1800DATE: Tue, 13 Jun 2023 11:06:18 GMTEXT:LOCATION: http://192.168.0.1:49153/IGDdevicedesc_brlan0.xmlOPT: "http://schemas.upnp.org/upnp/1/0/"; ns=0101-NLS: b8c5fa84-09cc-11ee-a2e9-9217cd6c62d8SERVER: Linux/4.9.135-Prod_19.3, UPnP/1.0, Portable SDK for UPnP devices/1.6.22X-User-Agent: redsonicST: upnp:rootdeviceUSN: uuid:ebf5a0a0-1dd1-11b2-a90f-021018951e6c::upnp:rootdevice 

so I do:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- {$MODE OBJFPC}{$H+}uses httpsend,sysutils,classes,synautil; var  S, Found: String;  HTTP: THTTPSend;  Location,ServiceType : String;  Response : TStringList;  f:text;begin           // SHOULD I ADD THE PORT NUMBER QUTED IN THE root.xml ?          // the port 63633 is returned router address on the resoponse to the M-SEARCH          // nothing happens        // The port: 49153 is retrunden in the M-SEARCH MESSAGE itself        // here i get 400 Bad Request        // without the port I get a long response, I post it   Location := 'http://192.168.0.1/upnp/control/WANIPConnection0';       ServiceType := 'urn:schemas-upnp-org:service:WANIPConnection:1';   S := '';  S := S + '<?xml version="1.0" encoding="utf-8"?>';  S := S + '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">';  S := S + ' <s:Body>';  S := S + '  <u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">';  S := S + '   <NewPortMappingIndex>0</NewPortMappingIndex>';  S := S + '  </u:GetGenericPortMappingEntry>';  S := S + ' </s:Body>';  S := S + '</s:Envelope>';   Response := TStringList.Create;  HTTP := THTTPSend.Create;  try    WriteStrToStream(HTTP.Document, S);    HTTP.MimeType := 'text/xml; charset="utf-8"';    HTTP.Headers.Add('SOAPAction: ' + ServiceType + '#GetGenericPortMappingEntry');    if HTTP.HTTPMethod('POST', Location) then    begin      Response.LoadFromStream(HTTP.Document);      Found := Response.Text;      writeln(found);    end;  finally    Response.Free;    HTTP.Free;  end; assign(f,'res.html');rewrite(f);writeln(f,found);close(f);end. 
the response I put in attachment


PS: Unfortunly my Router doesn't list the UPnP ports


PSS:
I posted a sniped of my conrtoll.xml (or root.xml).
It is retruned by
http://192.168.0.1:49153/IGDdevicedesc_brlan0.xml

rvk:
Yeah, that result is just html.
You are quering port 80.

You should include the found port in your location.
:49153

So

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Location := 'http://192.168.0.1:49153/upnp/control/WANIPConnection0';

Key-Real:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  Location := 'http://192.168.0.1:49153/upnp/control/WANIPConnection0'; 

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---<html><body><h1>400 Bad Request</h1></body></html> 

Navigation

[0] Message Index

[#] Next page

Go to full version