Jump to content

Function _Get_IP - AutoIt Get public IP address of a network/computer!


Trong
 Share

Recommended Posts

May be useful for someone!

; START CODE TEST 
Local $iTime = TimerInit()
Local $Sip = _Get_IP()
Local $iError = @error, $iExtended = @extended
Local $eTime = TimerDiff($iTime)
$iTime = "! IP: " & $Sip & @CRLF & "! ON: " & Round($eTime / 1000, 3) & "s" & @CRLF & "! Error: " & $iError & @CRLF & "! Extended: " & $iExtended & @CRLF
ConsoleWrite($iTime)
MsgBox(64 + 262144, "_Get_IP()", $iTime)
; END CODE TEST


Func _Get_IP()
;~  HttpSetUserAgent('Mozilla/5.0 (Windows NT ' & StringRegExpReplace(FileGetVersion('kernel32.dll'), '^(\d+\.\d+)(.*)$', '$1', 1) & '; ' & (((StringInStr(@OSArch, "64") > 0) And (Not (@AutoItX64 > 0))) ? "WOW64" : "Win64; x64") & ")")
    HttpSetUserAgent('Mozilla/5.0 (Windows NT Win64; x64)')
    Local $aUrlGetIP = ["https://api.ipify.org", "https://ip4.seeip.org", "http://checkip.dyndns.org", "http://ip.eprci.net/text", "http://www.networksecuritytoolkit.org/nst/tools/ip.php"]
    Local $aStunServers[8][2] = [["stun.l.google.com", 19302], ["stun.a-mm.tv", 3478], ["stun.cloopen.com", 3478], ["stun.fbsbx.com", 3478], ["stun.hoiio.com", 3478], ["stun.miwifi.com", 3478], ["stun.pjsip.org", 3478], ["stun.yy.com", 3478]]
    ;_GetIP_ by Dao Van Trong - TRONG.LIVE
    Local $bPacketRd12 = BinaryMid(BinaryMid(Binary(Random(1.1, 2 ^ 31 - 1)), 1, 6) & Binary(Random(1.1, 2 ^ 31 - 1)), 1, 12)     ;Generate request: some random unique ID in size of 12 bytes
    ; Binding request has class=0x00 and  method=0x000000000001 (Binding) and is encoded into the first two bytes as 0x0001. Check http://tools.ietf.org/html/rfc5389#section-15
    Local $bBinary = Binary("0x0001000000000000") & $bPacketRd12, $sReturn = "", $iError = 0, $sIpServ, $iSocket, $bRcvData, $StunID = -1, $iReTry = 2, $stunOK = 1, $MAPPED_ADDRESS = 0x0001, $IPv4 = 0x01, $IPv6 = 0x02, $SumStunSrv = UBound($aStunServers) - 1
    TCPStartup()
    While 1
        $bRcvData = ""
        $StunID += 1
        If $StunID > $SumStunSrv Then
            $iReTry -= 1
            If $iReTry < 1 Then
                $stunOK = 0
                $iError = -1
                ExitLoop
            EndIf
            $StunID = -1
            ContinueLoop
        EndIf
        If $aStunServers[$StunID][0] = "" Then ContinueLoop
        $sIpServ = TCPNameToIP($aStunServers[$StunID][0])
        If @error Then ContinueLoop     ; couldn't resolve server's IP
        $iSocket = UDPOpen($sIpServ, $aStunServers[$StunID][1])
        If @error Then ContinueLoop
        UDPSend($iSocket, $bBinary)
        If @error Then
            UDPCloseSocket($iSocket)
            ContinueLoop
        EndIf
        Local $xTimer = 0, $iTimer = TimerInit()
        Do
            Sleep(1)
            $xTimer = TimerDiff($iTimer)
            $bRcvData = UDPRecv($iSocket, 1280)
        Until (@error Or ($bRcvData <> "") Or ($xTimer > 2000))
        UDPCloseSocket($iSocket)
        If $bRcvData = "" Then
            ContinueLoop
        EndIf
;~      $xTimer = TimerDiff($iTimer)
;~      ConsoleWrite('+ Connected and Get IP from  ' & $aStunServers[$StunID][0] & ':' & $aStunServers[$StunID][1] & '  ON: ' & Round($xTimer / 100, 3) & 's ' & @CRLF)
        ExitLoop
    WEnd
    If $stunOK Then
        #cs
            ; Struct can be written now in place of binary data, but it's all big-endian (weird for reading in AutoIt):
            Local $tSTUN = DllStructCreate("byte Header_[8]; byte Header_ID[12];" & _
            "byte Type[2];" & _
            "byte Length[2];" & _
            "byte Attrib;" & _
            "byte Family;" & _
            "byte Port[2];" & _
            "byte IP[4];")
        #ce
        ; ...so I will just parse binary directly instead.
        Local $iSizeData = BinaryLen($bRcvData)
        If $iSizeData Then ; sanity check
            Local $bReadID = BinaryMid($bRcvData, 9, 12) ; server returns my unique "ID"
            Local $iType, $iLength = 0
            Local $iPos = 21 ; further parsing starts after the header, see the struct and STUN doc
            If $bReadID = $bPacketRd12 Then ; check validity of the response by checking returned ID (handle)
                While $iPos < $iSizeData
                    $iType = Dec(Hex(BinaryMid($bRcvData, $iPos, 2))) ; Big endian to number
                    $iPos += 2 ; skip the size of "Type" field
                    $iLength = Dec(Hex(BinaryMid($bRcvData, $iPos, 2))) ; Big endian to number
                    $iPos += 2 ; skip the size of "Length" field
                    If $iType = $MAPPED_ADDRESS Then ExitLoop
                    $iPos += $iLength ; skip the size of all of the data in this chunk
                WEnd
            EndIf
            $iPos += 1 ; skip the size of "Attrib" field
            Local $iFamily = Dec(Hex(BinaryMid($bRcvData, $iPos, 1))) ; read "Family" info. - ; Big endian to number
            $iPos += 1 ; skip the size of "Family" field
            $iPos += 2 ; skip the size of "Port" field
            If $iFamily = $IPv4 Then
                ; Read IP info. Four bytes are IP in network byte order (big endian)
                $sReturn = Int(BinaryMid($bRcvData, $iPos, 1)) & "." & Int(BinaryMid($bRcvData, $iPos + 1, 1)) & "." & Int(BinaryMid($bRcvData, $iPos + 2, 1)) & "." & Int(BinaryMid($bRcvData, $iPos + 3, 1))
                If ($sReturn <> "") And ($sReturn <> "0.0.0.0") Then
                    UDPShutdown()
                    Return $sReturn
                EndIf
                $iError = -2
            ElseIf $iFamily = $IPv6 Then
                ; IPv6
                $iError = 1
            EndIf
            ; No such data available
            $iError = 2
        EndIf
        ; You are blocked or something
        $iError = 3
    EndIf
    Local $aReturn = 0, $iDomain, $aHost, $sPattern = "^(?i)(?:(?:[a-z]+):\/\/)?(?:(?:(?:[^@:]+))(?::(?:[^@]+))?@)?([^\/:]+)(?::(?:\d+))?(?:\/(?:[^?]+)?)?(?:\?\N+)?"
    For $i = 0 To UBound($aUrlGetIP) - 1
        $aHost = StringRegExp($aUrlGetIP[$i], $sPattern, 1)
        If Not @error And IsArray($aHost) Then $iDomain = $aHost[0]
        TCPNameToIP($iDomain)
        If @error Then ContinueLoop
        $sReturn = InetRead($aUrlGetIP[$i], 1 + 2 + 8 + 16)
        If @error Or $sReturn == "" Then ContinueLoop
        $aReturn = StringRegExp(BinaryToString($sReturn), "((?:\d{1,3}\.){3}\d{1,3})", 3) ; [\d\.]{7,15}
        If Not @error Then
            $sReturn = $aReturn[0]
            ExitLoop
        EndIf
        $sReturn = ""
    Next
    TCPShutdown()
    If ($sReturn <> "") And ($sReturn <> "0.0.0.0") Then Return SetError(0, $iError, $sReturn)
    Return SetError(4, $iError, "")
    ;_GetIP_ by Dao Van Trong - TRONG.LIVE
EndFunc   ;==>_Get_IP

 

Edited by VIP
ms

Regards,
 

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