Jump to content

TCPConnect issue with a HTTP/S Proxy where i cant connect to certain IP's


Recommended Posts

Hi everyone,

so im working on a ipv4 and ipv6 server - client communication UDF and with the focus of making it really performant and somewhat secure. Besides that i also addin a couple of more features for convenience. For example a relay and a proxy.

I tested both quite alot and think that it is somewhat fast alread. My provider can deliver me a maximum of 200 mbps and the proxy can fully utilize it.

Spoiler

i2t2V68.png

But while testing the relay and proxy i encountered an issue with connecting to some IP's. I wont post the code of either of these as the issue seems to be easy to describe on an example.

Global $__net_hWs2_32 = -1

Local $hSocket = __netcode_TCPConnect("108.174.11.65", 443)
MsgBox(0, "", $hSocket)

Func __netcode_TCPConnect($sIP, $sPort, $nAdressFamily = 2)
    if $__net_hWs2_32 = -1 Then $__net_hWs2_32 = DllOpen('Ws2_32.dll')

    ; create socket
    Local $arGen = DllCall($__net_hWs2_32, "uint", "socket", "int", $nAdressFamily, "int", 1, "int", 6)
    Local $hSocket = $arGen[0]

    ; create ip and port struct
    ; ~ todo IPv6 support here
    Local $tDataBuffer = DllStructCreate("short; ushort; uint; char[8]")
    DllStructSetData($tDataBuffer, 1, 2)

    $arGen = DllCall($__net_hWs2_32, "ushort", "htons", "ushort", $sPort)
    DllStructSetData($tDataBuffer, 2, $arGen[0])

    $arGen = DllCall($__net_hWs2_32, "uint", "inet_addr", "str", $sIP)
    DllStructSetData($tDataBuffer, 3, $arGen[0])

    ; connect
    $arGen = DllCall($__net_hWs2_32, "int", "connect", "uint", $hSocket, "ptr", DllStructGetPtr($tDataBuffer), "int", DllStructGetSize($tDataBuffer))
    if $arGen[0] <> 0 Then Return SetError(__netcode_WSAGetLastError(), 0, -1)

    Return $hSocket
EndFunc

Func __netcode_WSAGetLastError()
    If $__net_hWs2_32 = -1 Then $__net_hWs2_32 = DllOpen('Ws2_32.dll')
    Local $iRet = DllCall($__net_hWs2_32, "int", "WSAGetLastError")
    If @error Then
        SetExtended(1)
        Return 0
    EndIf
    Return $iRet[0]
EndFunc

The IP belongs to LinkedIn. I can clearly connect to it in the browser. But not in Autoit.

I researched the problem but dont find an explanation of the issue. I recoded all mayor TCP functions already, so i think i need todo some changes there, but i just dont seem to find which.

Maybe someone has some ideas?

Edited by Rurorita
Link to comment
Share on other sites

Are you saying that the example, as provided above, works with other IP addresses?  If so, I seriously doubt that.  😉  Your example does not initialize Winsock before trying to make the connection.  If you would've added error checking after the line that executes your __netcode_TCPConnect() function, you would've seen that you received a 10093 Winsock error.  That error basically means that Winsock was not initialized.

If you run the modified example below, as is, you will see the error.  If you uncomment the TCPStartup() and TCPShutdown functions and run it again, it returns a socket handle -- at least it does for me.  TCPStartup() is equivalent to calling the wsastartup Winsock API.

Global $__net_hWs2_32 = -1

;~ TCPStartup() ;added by TheXman

Global $hSocket = __netcode_TCPConnect("108.174.11.65", 443)
If @error Then Exit MsgBox(0, "ERROR", "__netcode_TCPConnect failed with @error = " & @error) ;added by TheXman

MsgBox(0, "", $hSocket)

;~ TCPShutdown() ;added by TheXman

Func __netcode_TCPConnect($sIP, $sPort, $nAdressFamily = 2)
    if $__net_hWs2_32 = -1 Then $__net_hWs2_32 = DllOpen('Ws2_32.dll')

    ; create socket
    Local $arGen = DllCall($__net_hWs2_32, "uint", "socket", "int", $nAdressFamily, "int", 1, "int", 6)
    Local $hSocket = $arGen[0]

    ; create ip and port struct
    ; ~ todo IPv6 support here
    Local $tDataBuffer = DllStructCreate("short; ushort; uint; char[8]")
    DllStructSetData($tDataBuffer, 1, 2)

    $arGen = DllCall($__net_hWs2_32, "ushort", "htons", "ushort", $sPort)
    DllStructSetData($tDataBuffer, 2, $arGen[0])

    $arGen = DllCall($__net_hWs2_32, "uint", "inet_addr", "str", $sIP)
    DllStructSetData($tDataBuffer, 3, $arGen[0])

    ; connect
    $arGen = DllCall($__net_hWs2_32, "int", "connect", "uint", $hSocket, "ptr", DllStructGetPtr($tDataBuffer), "int", DllStructGetSize($tDataBuffer))
    if $arGen[0] <> 0 Then Return SetError(__netcode_WSAGetLastError(), 0, -1)

    Return $hSocket
EndFunc

Func __netcode_WSAGetLastError()
    If $__net_hWs2_32 = -1 Then $__net_hWs2_32 = DllOpen('Ws2_32.dll')
    Local $iRet = DllCall($__net_hWs2_32, "int", "WSAGetLastError")
    If @error Then
        SetExtended(1)
        Return 0
    EndIf
    Return $iRet[0]
EndFunc

 

Edited by TheXman
Corrected typos in the text of the reply.
Link to comment
Share on other sites

I looked into the proxy and i actually never called TCPStartup once. And id never thought id need to because it was working on most sites even if it doesnt in this example. But after initing tcp now, yet sites load up that never loaded before. It is weird to me. But thanks this solution was much easier then i thought it would be.

Now i have to make all sockets async so that autoit stops hanging on connection attempts that take longer and to make the proxy less noticable.

Link to comment
Share on other sites

39 minutes ago, Rurorita said:

And id never thought id need to because it was working on most sites even if it doesnt in this example.

Do you have an example of a public IP that works without TCPStartup() ?

Code hard, but don’t hard code...

Link to comment
Share on other sites

2 hours ago, Rurorita said:

No i couldnt reproduce it in this example

So to be clear you are saying that, now when you try to connect to any of the previously working IPs with the original code you posted (that did not include TCPStartup()), it gives an error?

Code hard, but don’t hard code...

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