Jump to content

Port forwarding check


Couby
 Share

Recommended Posts

I'd like to build a fonction which would check specific port forwarding.

This is to automate a check before launching a specific game, to ensure port forwarding which is necessary is correct.

At the moment I have no idea on how to code that. If someone has any idea...

Link to comment
Share on other sites

  • Developers

I'd like to build a fonction which would check specific port forwarding.

This is to automate a check before launching a specific game, to ensure port forwarding which is necessary is correct.

At the moment I have no idea on how to code that. If someone has any idea...

Port forwarding ?

On A PC or a Router ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

You need to get the external ip of the computer...

You could have your script download whatismyip.com and check the source for your ip...

(there may be a built in function for the same purpose).

After you get your external ip ping and try to open a tpc/udp connection to that port.

Link to comment
Share on other sites

You need to get the external ip of the computer...

You could have your script download whatismyip.com and check the source for your ip...

(there may be a built in function for the same purpose).

That's done, I got it with autoit udf :whistle:.

After you get your external ip ping and try to open a tpc/udp connection to that port.

How do you do that ?

Link to comment
Share on other sites

Port forwarding ?

On A PC or a Router ?

Made by a router or a PC, the interest of the check is just to know if it is effectiv on the computer checked, just to warn the user if there is a problem. Then if it is not wroking it is the user responsability to do the necessary on his PC or router to get it working.

I just want to check the port :whistle:.

Link to comment
Share on other sites

Sure there is! Lmao, my pet monkey can code this :whistle:

#include <INet.au3>

If $CmdLine[0] = 2 Then
    TCPStartup()
    TCPConnect($CmdLine[1],$CmdLine[2])
    Exit
EndIf

MsgBox(0, "Manadar #1", "1 indicates open and 0 indicates closed: " & CheckPortForwarding(3725))

Func CheckPortForwarding($sPort)
    $WAN = _GetIP()
    
    TCPStartup()

    $MainSocket = TCPListen(@IPAddress1,$sPort)

    Run(@AutoItExe & " " & @ScriptName & " " & $WAN & " " & $sPort)
    $Timeout = TimerInit()
    While TimerDiff($Timeout) < 3000
        $Socket = TCPAccept($MainSocket)
        If $Socket <> -1 Then Return 1
    WEnd
    Return 0
EndFunc

Multithreading is necessary.

Edited by Manadar
Link to comment
Share on other sites

I made a little application with this, it's good stuff.

CODE
#NoTrayIcon

#include <INet.au3>
#include <GUIConstants.au3>

If $CmdLine[0] = 2 Then
    TCPStartup()
    TCPConnect($CmdLine[1],$CmdLine[2])
    Exit
EndIf

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("WAN Port Forwarding Check", 232, 142, 193, 115)
$Input1 = GUICtrlCreateInput("", 5, 5, 223, 21, BitOR($ES_RIGHT,$ES_AUTOHSCROLL,$ES_NUMBER))
$Label1 = GUICtrlCreateLabel("Enter a port to check for..", 5, 30, 223, 107, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetFont(-1,10,400,0,"Verdana")
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            GUICtrlSetState($Input1,$GUI_DISABLE)
            
            $Port = GUICtrlRead($Input1)
            If $Port < 0 OR $Port > 65536 Then
                GUICtrlSetData($Label1,"Enter a port in the range 0-65536.")
                GUICtrlSetState($Input1,$GUI_ENABLE)
            Else
                $IP = _GetIP()
                If @error Then
                    GUICtrlSetData($Label1,"Error: Could not get your ip.." & @CRLF & @CRLF & "Make sure you are connected to the internet!")
                Else
                    GUICtrlSetData($Label1,"Checking port: " & $Port & "...")
                    If CheckPortForwarding($Port,$IP) Then
                        GUICtrlSetData($Label1,GUICtrlRead($Label1) & @CRLF & @CRLF & "NAT OK.")
                    Else
                        GUICtrlSetData($Label1,GUICtrlRead($Label1) & @CRLF & @CRLF & "Could not connect!" & @CRLF & "You have a NAT problem.")
                    EndIf
                    GUICtrlSetState($Input1,$GUI_ENABLE)
                EndIf
            EndIf
    EndSwitch
WEnd

Func CheckPortForwarding($sPort,$WAN)
    
    TCPStartup()

    $MainSocket = TCPListen(@IPAddress1,$sPort)

    $sPID = Run(@AutoItExe & " """ & @ScriptName & """ " & $WAN & " " & $sPort)
    $Timeout = TimerInit()
    While TimerDiff($Timeout) < 3000
        $Socket = TCPAccept($MainSocket)
        If $Socket <> -1 Then Return 1
    WEnd
    ProcessClose($sPID)
    Return 0
EndFunc
Edited by Manadar
Link to comment
Share on other sites

I made a little application with this, it's good stuff.

CODE
#NoTrayIcon

#include <INet.au3>
#include <GUIConstants.au3>

If $CmdLine[0] = 2 Then
    TCPStartup()
    TCPConnect($CmdLine[1],$CmdLine[2])
    Exit
EndIf

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("WAN Port Forwarding Check", 232, 142, 193, 115)
$Input1 = GUICtrlCreateInput("", 5, 5, 223, 21, BitOR($ES_RIGHT,$ES_AUTOHSCROLL,$ES_NUMBER))
$Label1 = GUICtrlCreateLabel("Enter a port to check for..", 5, 30, 223, 107, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetFont(-1,10,400,0,"Verdana")
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            GUICtrlSetState($Input1,$GUI_DISABLE)
            
            $Port = GUICtrlRead($Input1)
            If $Port < 0 OR $Port > 65536 Then
                GUICtrlSetData($Label1,"Enter a port in the range 0-65536.")
                GUICtrlSetState($Input1,$GUI_ENABLE)
            Else
                $IP = _GetIP()
                If @error Then
                    GUICtrlSetData($Label1,"Error: Could not get your ip.." & @CRLF & @CRLF & "Make sure you are connected to the internet!")
                Else
                    GUICtrlSetData($Label1,"Checking port: " & $Port & "...")
                    If CheckPortForwarding($Port,$IP) Then
                        GUICtrlSetData($Label1,GUICtrlRead($Label1) & @CRLF & @CRLF & "NAT OK.")
                    Else
                        GUICtrlSetData($Label1,GUICtrlRead($Label1) & @CRLF & @CRLF & "Could not connect!" & @CRLF & "You have a NAT problem.")
                    EndIf
                    GUICtrlSetState($Input1,$GUI_ENABLE)
                EndIf
            EndIf
    EndSwitch
WEnd

Func CheckPortForwarding($sPort,$WAN)
    
    TCPStartup()

    $MainSocket = TCPListen(@IPAddress1,$sPort)

    $sPID = Run(@AutoItExe & " """ & @ScriptName & """ " & $WAN & " " & $sPort)
    $Timeout = TimerInit()
    While TimerDiff($Timeout) < 3000
        $Socket = TCPAccept($MainSocket)
        If $Socket <> -1 Then Return 1
    WEnd
    ProcessClose($sPID)
    Return 0
EndFunc
I cannot wait to try this.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

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...