Jump to content

Check if port is open to WAN


Go to solution Solved by nullschritt,

Recommended Posts

Hello, I need to check if a port is open to wan or not, for a P2P messenger I am developing, I was able to write up a small snipplet of code that should work, but seems to always return 1 even if the port is not open over WAN(forwarded).

 

Anyone take a gander at what I'm doing wrong?

func _checkport($port = 71337)
$test = UDPBind("0.0.0.0", $port)
$ip = _GetIP()
$socket = UDPOpen($ip, $port)
If $socket[0] <> 0 Then
return 1
Else
Return 0
EndIf
UDPCloseSocket($test)
UDPCloseSocket($socket)
EndFunc
Edited by nullschritt
Link to comment
Share on other sites

That wont work as you are still testing from behind your internal router.

Try this.

Func _CheckPort($vPort = 80)
    $vUrl = InetRead("http://flibware.com/port.php?port=" & $vPort)
    $vCheckPort = StringRegExp(BinaryToString($vUrl), "<body>(.*?)</body></html>", 3)
        If $vCheckPort[0] = "Port Open" Then
            Return True
        Else
            Return False
        EndIf
EndFunc

This will test from a remote website

Edited by mrflibblehat

[font="'courier new', courier, monospace;"]Pastebin UDF | Prowl UDF[/font]

Link to comment
Share on other sites

mrflibblehat forgot to mention that's his personal website, so they reserve the rights.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • Solution

Here's my modified code:

Func _CheckPort($vPort = 7337)
TCPStartup()
$MainSocket = TCPListen('0.0.0.0', $vPort)
FileDelete(@ScriptDir & "\port.dat")
Local $hDownload, $go = 1
Do
    Local $ConnectedSocket = TCPAccept($MainSocket)
    If $ConnectedSocket >= 0 Then
       TCPSend($connectedsocket, "Cool Story Bro")
    EndIf
    Sleep(5)
if $go = 1 then 
$hDownload = InetGet("http://prodynamicsinc.com/link.php?CMD=page&ID=portcheck&buffer=0&port=" & $vPort&"&IP="&_GetIP(), @ScriptDir & "\port.dat", 1, 1)
$go = 0
EndIf
Until InetGetInfo($hDownload, 2)
local $portcheck = FileRead(@ScriptDir & "\port.dat")
FileDelete(@ScriptDir & "\port.dat")
InetClose($hDownload)
    TCPShutdown()
        If $portcheck = "Port Open" Then
            Return True
        Else
            Return False
        EndIf
EndFunc
and the sourcecode of the php page:

<?PHP
echo getserverstatus($_GET['IP'],$_GET['port']);
die();
function GetServerStatus($site, $port)
{
$status = array("Port Not Open", "Port Open");
$fp = @fsockopen($site, $port, $errno, $errstr, 2);
if (!$fp) {
    return $status[0];
} else
  { return $status[1];}
}
?>
Cheers.

 

Edit: Edited function to allow listening on incoming socket, and response, while awaiting a response on status of the port being checked.

Edit Edit: It still doesn't seem to work, it works for ports opened by other applications, but for some reason it's not letting me check if a port has been opened within the same script....

Edit Edit Edit: Nevermind on that last, seems to work perfect, the port I had defaulted was out of range.

Edited by nullschritt
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...