Jump to content
Sign in to follow this  
trancexx

MyIP()

Recommended Posts

trancexx

It's about public IP addres that you have.

There are many ways to get it and at least like 10000 ways posted here on forum. So, here is another one :)

What is special here?

- it uses not HTTP but SMTP protocol to get IP address

- I wrote this

- very likely nothing else

What is not so special?

- that is stupid question

Seriously now... This method tends to be more secure one than connecting to some site in order to get info about IP. This servers are made to respond to your connection with data containing your IP and you won't be treated as abuser or whatever when doing that. We'll be practicing good SMTP protocol manners. IP that you retrieve is your real addres i.e. you cannot hide behind proxy (no false readings like other methods have).

Script is heavily commented. Maximum five servers will be used, but only one for real, of course.

If for some reason function fails return value is -1 (not rising error).

Script with example:

MsgBox(0,"My IP", MyIP())



Func MyIP()

    Local $sIP = -1

    If TCPStartup() Then

        Local $sServer
        For $t = 1 To 5

            Switch $t
                Case 1
                    $sServer = "65.54.245.40" ;        "mx1.hotmail.com"
                Case 2
                    $sServer = "206.188.24.42" ;       "mail.bluebottle.com"
                Case 3
                    $sServer = "165.212.11.125" ;      "smtp.postoffice.net"
                Case 4
                    $sServer = "194.67.23.111" ;       "smtp.mail.ru"
                Case 5
                    $sServer = "194.153.21.68" ;       "smtp.namestoday.ws"
            EndSwitch

            Local $iSocket = TCPConnect($sServer, 25) ; connecting, port 25

            If Not ($iSocket = -1) Then
                Local $sResponse
                Local $iTik

                Do
                    $sResponse = TCPRecv($iSocket, 512) ; receive response
                    $iTik += 1
                    Sleep(100)
                Until $sResponse Or $iTik = 20 ; waiting for response, but not forever (2 sec max)

                If StringLeft($sResponse, 3) = 220 Then  ; Server responds "Service ready"

                    If TCPSend($iSocket, "HELO GimmeIP" & @CRLF) = 14 Then ; SMTP protocol requirement

                        Local $sData
                        $iTik = 0 ; back to zero, gonna use this again

                        Do
                            $sData = TCPRecv($iSocket, 512) ; receive data
                            $iTik += 1
                            Sleep(100)
                        Until $sData Or $iTik = 20 ; waiting for response or 2 sec, whatever comes first.

                        If $sData Then

                            TCPSend($iSocket, "QUIT" & @CRLF) ; being polite (SMTP protocol)
                            TCPCloseSocket($iSocket)

                            Local $aIpAddress = StringRegExp($sData, '((?:\d{1,3}\.){3}\d{1,3})', 3) ;good enough for this case
                            If IsArray($aIpAddress) Then
                                $sIP = $aIpAddress[UBound($aIpAddress) - 1]
                                If $sIP Then
                                    ;ConsoleWrite("Compliments of: " & $sServer & @CRLF) ; this is our IP provider
                                    ExitLoop
                                EndIf
                            EndIf
                        Else
                            TCPCloseSocket($iSocket)
                        EndIf
                    Else
                        TCPCloseSocket($iSocket)
                    EndIf
                Else
                    TCPCloseSocket($iSocket)
                EndIf
            EndIf

        Next

        TCPShutdown()

    EndIf

    Return SetError(0, 0, $sIP)

EndFunc

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Michel Claveau

Hi!

It is an idea of interesting alternate solution. A good idea.

However, you should not forget that SMTP is slower than HTTP (or, better, direct TCP/IP).

@-salutations

--

Michel Claveau

Share this post


Link to post
Share on other sites
trancexx

Hi!

It is an idea of interesting alternate solution. A good idea.

However, you should not forget that SMTP is slower than HTTP (or, better, direct TCP/IP).

@-salutations

--

Michel Claveau

Thanks

Yes, it's slower but other than that it tends to be more beneficiary.

btw, what is 'direct TCP/IP'? You lost me there.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Michel Claveau

Hi!

> direct TCP

If you have a server ; and if you can script-it, you can do a script who answer your IP.

Then, send your demand, like (example):

TCPStartUp()
$socket = TCPConnect( $ip, $port )
TCPSend($socket, "WantMyIP")
$myIP=TCPRecv($socket, 512)
TCPCloseSocket($socket)
Edited by Michel Claveau

Share this post


Link to post
Share on other sites
torels

very good work :)


Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Share this post


Link to post
Share on other sites
wraithdu

I don't know why, but this hangs on my Vista system on TCPConnect() to the server. I tried turning off the Windows firewall, no luck. Maybe a problem with my router?

Share this post


Link to post
Share on other sites
trancexx

I don't know why, but this hangs on my Vista system on TCPConnect() to the server. I tried turning off the Windows firewall, no luck. Maybe a problem with my router?

That is strange. Did you try connecting to some other servers on different ports (maybe this or something like it)

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
GtaSpider

Hi

Here you have your TCP/IP Version, quick'n'dirty :):lmao:

#include <inet.au3>
TCPStartup()

$hTI = TimerInit()
_GetIP()
ConsoleWrite("Normal GETip: "&@TAB&TimerDiff($hTI) & @CRLF)
$hTI = TimerInit()
_GetIP2()
ConsoleWrite("New GETip: "&@TAB&TimerDiff($hTI) & @CRLF)



Func _GetIP2()

    $IP = TCPNameToIP("checkip.dyndns.org")
    $hCon = TCPConnect($IP, 80)
    TCPSend($hCon, "GET / HTTP/1.1" & @CRLF & "Host: checkip.dyndns.org" & @CRLF & @CRLF)
    Do
        $sRecv = TCPRecv($hCon, 1024)
    Until $sRecv <> ''
    $aRegEx = StringRegExp($sRecv, "<body>Current IP Address: (.*?)</body>", 3)
    Return $aRegEx[0]
EndFunc   ;==>_GetIP2

Well its a joke, it is not realy TCP/IP, its http.

Spider


www.AutoIt.de - Moderator of the German AutoIt Froum

Share this post


Link to post
Share on other sites
trancexx

Hi

Here you have your TCP/IP Version, quick'n'dirty :):lmao:

#include <inet.au3>
TCPStartup()

$hTI = TimerInit()
_GetIP()
ConsoleWrite("Normal GETip: "&@TAB&TimerDiff($hTI) & @CRLF)
$hTI = TimerInit()
_GetIP2()
ConsoleWrite("New GETip: "&@TAB&TimerDiff($hTI) & @CRLF)



Func _GetIP2()

    $IP = TCPNameToIP("checkip.dyndns.org")
    $hCon = TCPConnect($IP, 80)
    TCPSend($hCon, "GET / HTTP/1.1" & @CRLF & "Host: checkip.dyndns.org" & @CRLF & @CRLF)
    Do
        $sRecv = TCPRecv($hCon, 1024)
    Until $sRecv <> ''
    $aRegEx = StringRegExp($sRecv, "<body>Current IP Address: (.*?)</body>", 3)
    Return $aRegEx[0]
EndFunc   ;==>_GetIP2

Well its a joke, it is not realy TCP/IP, its http.

Spider

Posted Image

I would have eat you for that if you weren't joking.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
wraithdu

@trancexx

Your other script works, albeit not for the URL you used as an example (I got a 404 error). Google worked though. Anyway, so yeah I can connect and use the TCP functions. It's something about the port number. I wonder if it's blocked by my ISP or something?

Edited by wraithdu

Share this post


Link to post
Share on other sites
trancexx

@trancexx

Your other script works, albeit not for the URL you used as an example (I got a 404 error). Google worked though. Anyway, so yeah I can connect and use the TCP functions. It's something about the port number. I wonder if it's blocked by my ISP or something?

Yes, it appears that your ISP is blocking port 25. Damn asses

Can you modify script from the first post here to return when that case occurs. If it just hangs then return after second or something like that.

Can you test your remote screen capture on port 25 (same machine).


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
wraithdu

My remote screen cap works ok on my local machine with port 25.

Unfortunately I can't modify your script to do that, because it seems to hang indefinitely on the TCPConnect() function the first time it runs. I don't know how long the timeout is, I've never waited longer that 30 seconds or so, too impatient.

Share this post


Link to post
Share on other sites
trancexx

My remote screen cap works ok on my local machine with port 25.

Unfortunately I can't modify your script to do that, because it seems to hang indefinitely on the TCPConnect() function the first time it runs. I don't know how long the timeout is, I've never waited longer that 30 seconds or so, too impatient.

Under assumption that AdlibEnable() would not work if TCPConnect() is not returning (can you confirm that) could you test this code:

Global $iTimeOut
MsgBox(0, "My IP", MyIP())



Func MyIP()

    Local $sIP = -1

    If TCPStartup() Then

        Local $sServer
        For $t = 1 To 5

            $iTimeOut = 1

            Switch $t
                Case 1
                    $sServer = "65.54.245.40";      "mx1.hotmail.com"
                Case 2
                    $sServer = "206.188.24.42";    "mail.bluebottle.com"
                Case 3
                    $sServer = "165.212.11.125";      "smtp.postoffice.net"
                Case 4
                    $sServer = "194.67.23.111";    "smtp.mail.ru"
                Case 5
                    $sServer = "194.153.21.68";    "smtp.namestoday.ws"
            EndSwitch

            Local $hCallback = DllCallbackRegister("_CallBackFunction", "none", "lparam;int")

            Local $a_hCall = DllCall("kernel32.dll", "int", "CreateTimerQueueTimer", _
                    "hwnd*", 0, _
                    "hwnd", 0, _
                    "ptr", DllCallbackGetPtr($hCallback), _
                    "int", 0, _
                    "dword", 1000, _
                    "dword", 0, _
                    "dword", 0)

            Local $iSocket = TCPConnect($sServer, 25)
            
            If $iTimeOut = 2 Then
                Return SetError(0, 0, $sIP)
            EndIf

            $iTimeOut = 0

            If Not ($iSocket = -1) Then
                Local $sResponse
                Local $iTik

                Do
                    $sResponse = TCPRecv($iSocket, 512)
                    $iTik += 1
                    Sleep(100)
                Until $sResponse Or $iTik = 20

                If StringLeft($sResponse, 3) = 220 Then

                    If TCPSend($iSocket, "HELO GimmeIP" & @CRLF) = 14 Then

                        Local $sData
                        $iTik = 0

                        Do
                            $sData = TCPRecv($iSocket, 512)
                            $iTik += 1
                            Sleep(100)
                        Until $sData Or $iTik = 20

                        If $sData Then

                            TCPSend($iSocket, "QUIT" & @CRLF)
                            TCPCloseSocket($iSocket)

                            Local $aIpAddress = StringRegExp($sData, '((?:\d{1,3}\.){3}\d{1,3})', 3)
                            If IsArray($aIpAddress) Then
                                $sIP = $aIpAddress[UBound($aIpAddress) - 1]
                                If $sIP Then
                        ;ConsoleWrite("Compliments of: " & $sServer & @CRLF); this is our IP provider
                                    ExitLoop
                                EndIf
                            EndIf
                        Else
                            TCPCloseSocket($iSocket)
                        EndIf
                    Else
                        TCPCloseSocket($iSocket)
                    EndIf
                Else
                    TCPCloseSocket($iSocket)
                EndIf
            EndIf

        Next

        TCPShutdown()

    EndIf

    Return SetError(0, 0, $sIP)

EndFunc;==>MyIP




Func _CallBackFunction($lParam, $TimerOrWaitFired)

    If $iTimeOut Then
        ConsoleWrite("Problem with TCPConnect" & @CRLF)
        TCPShutdown()
        $iTimeOut = 2
    Else
        ConsoleWrite("TCPConnect OK" & @CRLF)
    EndIf

EndFunc;==>_CallBack

Code in callback function should be executed regardless of any delays in script (I think that even Kip can verify that - him being dedicated multithreading breaker, lol). Purpose of that part of the code is to check that TCPConnect() function is executed within 1 sec. and to stop TCP service in hope that that would be enough for TCPConnect() to return.

This is kind of important to me, so please... please... try.

Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
wraithdu

The timer is called with the error condition. I waited it out this time, and TCPConnect() takes ~22 seconds to timeout and return. TCPShutdown() in the timer does not cause TCPConnect() to return immediately.

If I leave TCPShutdown() in the timer, I get WSA error 10038 from TCPConnect():

WSAENOTSOCK

10038

Socket operation on nonsocket.

An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.

If I remove it, I get WSA error 10060:

WSAETIMEDOUT

10060

Connection timed out.

A connection attempt failed because the connected party did not properly respond after a period of time, or the established connection failed because the connected host has failed to respond.

Not very helpful :) Don't worry, I don't blame your function. There's some other reason I can't connect on port 25, and I'm thinking it might actually be my ISP, although I can't confirm that.

Edited by wraithdu

Share this post


Link to post
Share on other sites
wraithdu

N/M, I confirmed it, it's my ISP. I have a VPN service I use when I'm at work or on a public hotspot (Comodo's TrustConnect). If I fire it up, your function succeeds immediately and returns the VPN's public IP correctly.

FYI I use AT&T DSL in Chicago, IL (formerly Yahoo / SBC DSL).

Edited by wraithdu

Share this post


Link to post
Share on other sites
trancexx

Thanks for being pedantic.

I still need to deal with case when TCPConnect() is not returning :lmao:

So if you have any idea please say. Or anyone else of course.

Btw, that timer code is kind of edgy. :)

It would exit if I replace TCPShutdown() with Exit?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
wraithdu

Not sure, I can test it out when I get home from work.

The TCP functions are supposed to timeout after 100ms (default). My guess is that the ~22 seconds is a Windows Winsock timeout value. I'm not sure if that's changeable.

Share this post


Link to post
Share on other sites
trancexx

Not sure, I can test it out when I get home from work.

The TCP functions are supposed to timeout after 100ms (default). My guess is that the ~22 seconds is a Windows Winsock timeout value. I'm not sure if that's changeable.

Please do.

♡♡♡

.

eMyvnE

Share this post


Link to post
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
Sign in to follow this  

×