UPnP Port Manager for mIRC

By FordLawnmower on Jul 24, 2010

Image
This dialog is just a demonstration script for a UPnP dll that I made.
This script will allow you to view, add and delete ports that are forwarded to your router with UPnP.
Almost all modern routers have UPnP, however, it is not always on by default. You may have to turn on UPnP in your routers settings.

How to use:

  • Check your routers settings and make sure UPnP is turned ON.
  • Copy the script to a new remote and save with a unique file name.
  • Right click Channel or Status and select UPnP Port Manager.
  • Click Initialize and wait several seconds.
  • Right click Channel or Status and select UPnP Port Manager.
  • Click Port Manager
    The script will download the dll on it's own.
    If you are interested in this dll for another project, scroll down in the script and there are detailed syntax etc. in the comments.
;UPnP Port Manager by Ford_Lawnmower irc.geekshed.net #Script-Help
alias UPnP return $scriptdirUPnP.dll
menu channel,status,menubar {
  .UPnP Port Manager
  ..$iif(!$exists($UPnP),$style(2)) Port Manager:dialog $iif($dialog(UPnP),-v,-m) UPnP UPnP
  ..$iif($exists($UPnP),$style(2)) Initialize:{
    GetUPnPDll
    WriteIPConfig
    echo -a Downloading UPnP.dll to $scriptdir
    echo -a This will take several seconds. Please be patient.
  }
}
On *:Dialog:UPnP:Init:*: {
  didtok -a $dname 1 126 $replace($listports,$chr(7),$chr(1))
  didtok -a $dname 9 44 TCP,UDP,BOTH
  did -a $dname 7 $GetLanIP
  did -c $dname 9 1
}
On *:Dialog:UPnP:Sclick:2,11: {
  if ($did == 2) {
    if ($did($dname,1)) {
      tokenize 1 $v1
      DelPortsGUI $3-
      didtok -ra $dname 1 126 $replace($listports,$chr(7),$chr(1))
    } 
  }
  else {
    tokenize 32 $did($dname,7) $did($dname,8) $did($dname,10)
    if ($regex($1-,((?:[\d]{1,3})\x2E(?:[\d]{1,3})\x2E(?:[\d]{1,3})\x2E(?:[\d]{1,3}))\s([\d]{1,})\s([^\s]*))) {
      if ($did($dname,9) == BOTH) {
        if ((!$FwdPorts($regml(1),$regml(2),TCP,$regml(3)))) {
          var %r $input(Unable to forward TCP $regml(2) to $regml(1),o,UPnP Port Manager,,)
          if (!$FwdPorts($regml(1),$regml(2),UDP,$regml(3))) {
            var %r $input(Unable to forward UDP $regml(2) to $regml(1),o,UPnP Port Manager,,)
          }
          else {
            var %r $input(Successfully forwarded UDP $regml(2) to $regml(1),o,UPnP Port Manager,,)
            didtok -ra $dname 1 126 $replace($listports,$chr(7),$chr(1))
          }
        }
        elseif (!$FwdPorts($regml(1),$regml(2),UDP,$regml(3))) {
          var %r $input(Unable to forward UDP $regml(2) to $regml(1),o,UPnP Port Manager,,)
          var %r $input(Successfully forwarded TCP $regml(2) to $regml(1),o,UPnP Port Manager,,)
          didtok -ra $dname 1 126 $replace($listports,$chr(7),$chr(1))
        }
        else {
          var %r $input(Successfully forwarded both TCP and UDP $regml(2) to $regml(1),o,UPnP Port Manager,,)
          didtok -ra $dname 1 126 $replace($listports,$chr(7),$chr(1))
        }
      }
    }
  }
}
dialog UPnP {
  title "UPnP Port Manager"
  size -1 -1 255 176
  option dbu
  combo 1, 5 6 244 99, vsbar
  button "Delete", 2, 211 109 37 12
  text "IPAddress:", 3, 28 130 27 8, center
  text "Port:", 4, 82 130 25 8, center
  text "Protocol:", 5, 131 130 25 8, right
  text "Description:", 6, 183 130 31 8, center
  edit "", 7, 17 142 50 10, limit 15
  edit "", 8, 76 142 39 10
  combo 9, 130 142 28 11, drop
  edit "", 10, 171 142 56 10, limit 20
  button "Forward", 11, 211 161 37 12
}
alias -l GetUPnPDll GetDll fordlawnmower.googlecode.com /files/UPnP.dll $nospace($scriptdir) UPnP.dll
alias -l GetDll {
  var %dir $nospace($3)
  if ($isfile($qt($+(%dir,$4)))) { .remove $qt($+(%dir,$4)) }
  var %sockname $+(GetDll,$ticks,$r(1,$ticks))
  if (!$isdir($qt(%dir))) { mkdir $qt(%dir) }
  sockopen %sockname $1 80
  sockmark %sockname $1-
}
On *:sockopen:GetDll*:{
  sockwrite -nt $sockname GET $gettok($sock($sockname).mark,2,32) HTTP/1.0
  sockwrite -n $sockname Host: $gettok($sock($sockname).mark,1,32) $+ $crlf $+ $crlf
}
On *:sockread:GetDll*:{
  if (!$gettok($sock($sockname).mark,5,32)) {
    var %GetDll | sockread %GetDll
    if (!%GetDll) { sockmark $sockname $sock($sockname).mark 1 }
  }
  else {
    sockread &GetDll
    bwrite -s $qt($+($nospace($gettok($sock($sockname).mark,3,32)),$gettok($sock($sockname).mark,4,32))) -1 -1 &GetDll
  }
}
alias -l nospace {
  if ($chr(7) isin $1-) return $replace($1-,$chr(7),$chr(32))
  else return $replace($1-,$chr(32),$chr(7))
}
On *:Connect: WriteIPConfig
On *:Load: WriteIPConfig
alias -l WriteIPConfig {
  var %tempdir $gettempdir
  if (!$exists($+(%tempdir,\,getipconfig.bat))) write -c $+(%tempdir,\,getipconfig.bat) ipconfig /all > $+(%tempdir,\,ipconfig.dat)
  run $+(%tempdir,\,getipconfig.bat)
}
alias -l GetTempDir {
  if (!$com(tempdir)) .comopen tempdir WScript.Shell
  if (!$comerr) var %Process $com(tempdir,ExpandEnvironmentStrings,1,bstr*,$+(%,temp,%)), %result $com(tempdir).result
  if ($com(tempdir)) .comclose tempdir
  return %result
}
alias -l GetLanIP {
  var %tempdir $gettempdir
  if (!$exists($+(%tempdir,\,ipconfig.dat))) WriteIPConfig
  var %result = $read($+($gettempdir,\,ipconfig.dat), w, *IPv4 Address*, 1)
  %result = $regex(%result,/IPv4 Address.*:\s((?:[\d]{1,3})\x2E(?:[\d]{1,3})\x2E(?:[\d]{1,3})\x2E(?:[\d]{1,3}))/)
  return $regml(1)
}
;Com Stuff for UPnP.dll below
;dispports Displays all active UPnP ports
;Syntax /dispports or $dispports Will return 1 or higher on error.
alias dispports {
  OpenCom $nospace($UPnP) UPnPdisp UPnP.Class1
  if (!$com(UPnPdisp)) return 1
  var %result $com(UPnPdisp,DispPorts,3), %error $com(UPnPdisp).error
  if ($com(UPnPdisp)) .comclose UPnPdisp
  return %error
}
;listports will return a list of active UPnP Ports.
;Syntax $listports (ie %var = $listports) Will return 0 on error.
alias listports {
  OpenCom $nospace($UPnP) UPnPlist UPnP.Class1
  if (!$com(UPnPlist)) return 1
  var %process $com(UPnPlist,ListPorts,3), %result $com(UPnPlist).result, %error $com(UPnPlist).error
  if ($com(UPnPlist)) .comclose UPnPlist
  return $iif(%error,0,%result)
}
;fwdports Will forward 1 port at a time.
;Syntax %var = $fwdports(ip,port,protocol,description)
;Check $var for success or fail. 1 for success and 0 for fail.
;*Note that fail may mean that the port is already in use by UPnP.
alias fwdports {
  OpenCom $nospace($UPnP) UPnPfwd UPnP.Class1
  if (!$com(UPnPfwd)) return 1  
  var %process $com(UPnPfwd,FwdPorts,3,bstr,$1,bstr,$2,bstr,$upper($3),bstr,$4), %result $com(UPnPfwd).result, %error $com(UPnPfwd).error
  if ($com(UPnPfwd)) .comclose UPnPfwd
  return $iif(%error,0,%result)
}
;fwdportsgui Will forward 1 port at a time with a gui response.
;Syntax /fwdportsgui ip port protocol description or $fwdportsgui(ip,port,protocol,description) Will return 1 or higher on error.
alias fwdportsgui {
  OpenCom $nospace($UPnP) UPnPfwdgui UPnP.Class1
  if (!$com(UPnPfwdgui)) return 1  
  var %result = $com(UPnPfwdgui,FwdPortsGUI,3,bstr,$1,bstr,$2,bstr,$upper($3),bstr,$4), %error $com(UPnPfwdgui).error
  if ($com(UPnPfwdgui)) .comclose UPnPfwdgui
  return %error
}
;delports Will delete active UPnP ports 1 at a time.
;Syntax %var = $delports(port,protocol) Check %var for success or fail. 1 for success 0 for fail.
;*Note that fail may mean that the port did not exist in UPnP.
alias delports {
  OpenCom $nospace($UPnP) UPnPdel UPnP.Class1
  if (!$com(UPnPdel)) return 1
  var %process $com(UPnPdel,DelPorts,3,bstr,$1,bstr,$upper($2)), %result $com(UPnPdel).result, %error $com(UPnPdel).error
  if ($com(UPnPdel)) .comclose UPnPdel
  return $iif(%error,0,%result)
}
;delportsgui Will delete active UPnP ports 1 at a time.
;Syntax /delportsgui port protocol or $delportsgui(port,protocol) Will return 1 or higher on error.
alias delportsgui {
  OpenCom $nospace($UPnP) UPnPdelgui UPnP.Class1
  if (!$com(UPnPdelgui)) return 1
  var %result = $com(UPnPdelgui,DelPortsGUI,3,bstr,$1,bstr,$upper($2)), %error $com(UPnPdelgui).error
  if ($com(UPnPdelgui)) .comclose UPnPdelgui
  return %error
}
alias -l OpenCom {
  if (!$com($2)) .comopen $2 $3
  if ($comerr) {
    .comreg $nospace($1)
    if (!$com($2)) .comopen $2 $3
  }
}

Comments

Sign in to comment.
osama1337   -  Oct 01, 2017

Doesn't work on mIRC v6.21.

  • Invalid format: $com (line 106, upnp_script.mrc)
 Respond  
Colten45   -  May 09, 2015

As usual, very fine work. I only have one problem and that is I have a windows network. I was just doing research to do my own via pure mirc sockets, I have a clear text capture of the xml for the microsoft upnp if it is needed.

 Respond  
FordLawnmower   -  Jul 26, 2010

Thank You jonesy44 :)

 Respond  
Jonesy44   -  Jul 26, 2010

This is f*cking genius. Good effort pal.

 Respond  
FordLawnmower   -  Jul 25, 2010

Oh Ok :)
Thanks :)

 Respond  
Aucun50   -  Jul 25, 2010

It was just hard to find my local ip address is all :) I don't feel like downloading a little program to find it or gussing for a long time.

 Respond  
FordLawnmower   -  Jul 25, 2010

Your welcome Aucun50 :)
Do you use this for forwarding ports for DCC, a program or for some other reason?
I'm not trying to be invasive, I'm just trying to gather some information for future projects involving UPnP. Imo DCC would be the most useful application, so I'm planning on making a script to automatically open/close ports for DCC transfers.
If you have any problems please let me know. I was only able to test this on 3 routers so I'm keeping my fingers crossed....

 Respond  
Aucun50   -  Jul 25, 2010

Thank you that's great.

 Respond  
FordLawnmower   -  Jul 25, 2010

@Aucun50 I added this code to get the local ip address.

On *:Connect: WriteIPConfig
On *:Load: WriteIPConfig
alias -l WriteIPConfig {
  var %tempdir $gettempdir
  if (!$exists($+(%tempdir,\,getipconfig.bat))) write -c $+(%tempdir,\,getipconfig.bat) ipconfig /all > $+(%tempdir,\,ipconfig.dat)
  run $+(%tempdir,\,getipconfig.bat)
}
alias -l GetTempDir {
  if (!$com(tempdir)) .comopen tempdir WScript.Shell
  if (!$comerr) var %Process $com(tempdir,ExpandEnvironmentStrings,1,bstr*,$+(%,temp,%)), %result $com(tempdir).result
  if ($com(tempdir)) .comclose tempdir
  return %result
}
alias -l GetLanIP {
  var %tempdir $gettempdir
  if (!$exists($+(%tempdir,\,ipconfig.dat))) WriteIPConfig
  var %result = $read($+($gettempdir,\,ipconfig.dat), w, *IPv4 Address*, 1)
  %result = $regex(%result,/IPv4 Address.*:\s((?:[\d]{1,3})\x2E(?:[\d]{1,3})\x2E(?:[\d]{1,3})\x2E(?:[\d]{1,3}))/)
  return $regml(1)
}
___________________________________________________________________________________________________________________

This code requires that you have "run" enabled in mirc settings.
If you want to use this code independent of this script, just remove the -l from the alias GetLanIP and the syntax will be $GetLanIP (ie echo -a $GetLanIP). Note that the first time it won't return a result if you don't load the script with the "load" command.
After the first use the command will work fine and the file will update on each connect.

I've included this code in the script, so if you just want the ipaddress to show in the ip box on the dialog, just load the code at the top of the page. Note Same as the alias, the ip box will be blank on first run if you just paste in the script as an update.
If you are loading this for the first time, the ip info will update when the dll initializes.
Thanks for the suggestion Aucun50 :)

 Respond  
Aucun50   -  Jul 25, 2010

Yea I find it hard to guess me local ip most the time 192.168.0.1 maybe .2 .3 who knows

 Respond  
FordLawnmower   -  Jul 25, 2010

It doesn't give your local ip Aucun50 but it shouldn't be very hard to make a mIRC script to find it. I'm planning to make a script for UPnP.dll that will need to automatically gather your lan ip so I will make a script to get "ipconfig info" and post it here first.

 Respond  
Aucun50   -  Jul 25, 2010

Does this give you your local ip or do you have to find that yourself?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.