Jump to content
Tuts 4 You

Looking for websocket Client code examples


LCF-AT

Recommended Posts

Posted

Hi guys,

I am looking for any code examples about websocket calling http/s and switching protocol 101.I would like to know how the code for this kind of connection / communication has to look like using Windows Socket functions or OpenSSL function as I use for normal website readings etc.

In some cases when sites using websockets I need to use Fiddler to check out what happend in that connection and what data it was getting but now I wanna write some code for this to do almost same and reading the data from.On internet I just find mostly any scripts for xy languages but no raw code I can do something with that you know.Just need any or some code examples of the function/s way I need to use.

Maybe anyone has some experiences and knowledge about this theme and knows how to code something what could help me to code something for myself in MASM.

Thank you

Posted

My usual advice - don't reinvent the wheel. Websockets protocol is ugly, has gazillion of versions and it will take you a long time to write even the basic support of websockets in ASM. So, try to find an existing library that you can use. :)

You could use WinHttp APIs which support websockets out of the box. Those APIs are Win8+. If you're stuck with WinXP/Win7, you will need to find another solution. 
The only standalone library that I could find is this: http://aspl.es/nopoll/index.html. From the first look, it might work for you, after you solve "missing libgcc_s_dw2-1.dll" error.

  

Note - I haven't used those libraries, haven't studied any documentation in details, and do not have any intentions to do any of that. You need it, you test and study it. 

 

  • Like 2
Teddy Rogers
Posted
4 hours ago, kao said:

You could use WinHttp APIs which support websockets out of the box.

+1, if you were to ask me that would be my recommendation too. Windows 7 support ceases in 2020 so you might as well start getting accustomed to the future...

Ted.

  • Like 2
Posted

Hi again,

I just use Win 7 and do not change the OS now. :)

I did download the npoll installer but have problem with the API function way I need to use.Starting with nopoll_ctx_new then nopoll_conn_new using localhost & port but then I stuck.Dont I need to call the http link first where I get the switch protocol 101 as response back?The problem is I dont have any code example where I can see all minmum API functions I need to use from first till last one.Something like this I need.

I also checked the WinHTTP code example called "WinHTTP WebSocket sample" where I can see the API calliing way starting with WinHttpOpen till WinHttpCloseHandle but this I cant use on my Win 7.Similar like that I need for that npoll too you know.

I can see some many examples into nopoll-regression-client.c but not sure which one I could use / should use for my plan to get that response I also get in Fiddler to see.

PS: I have that libgcc_s_dw2-1.dll.At the moment I didnt test it yet and trying first to find out the right function way I need to use before I code something.In this case I have to use the dlls itself into app folder before I try to make some static libs for MASM (if I can or fail again on this).Any more hints about that npoll would be nice of course.

greetz

Posted

WinHttpOpen and the other WinHTTP API were all accessible since Windows XP/2000. They work fine on Win7.

  • Like 1
Posted

Hi,

the websocket function of WinHttp only supports "Windows 8 [desktop apps only] Windows Server 2012 [desktop apps only]"

WinHttpWebSocketCompleteUpgrade
WinHttpWebSocketSend
WinHttpWebSocketReceive
WinHttpWebSocketClose
WinHttpWebSocketQueryCloseStatus

Did you checked them already?Or is the function Window version outdated?

Example file

https://code.msdn.microsoft.com/windowsdesktop/WinHTTP-WebSocket-sample-50a140b5

PS: I also dont have any WinHttp lib & inc files for MASM yet.

greetz

Posted

 

On 8/14/2018 at 12:24 AM, kao said:

WinHttp APIs which support websockets ... Those APIs are Win8+.

Sure, other WinHttp APIs are there since XP. But you were asking and I answered specifically about websockets, probably atom0s overlooked that part of the question..

 

21 hours ago, LCF-AT said:

I can see some many examples into nopoll-regression-client.c but not sure which one I could use / should use for my plan to get that response I also get in Fiddler to see.

Can't help you with that. I used my google-fu to find you a standalone lib that might work, that's all I can do.

  • Like 1
Posted

Hi again guys,

ok so maybe its a better idea to show it on a example site which used websocket/s on the main page and on each channel page for user chats.The example site in this case called https://vaughnlive.tv/ (just any random site).If I call this site and checking console in browser then I can see it calls the site https://sapi-ws-1x03.vaughnsoft.net/ like this..

GET / HTTP/1.1
Host: sapi-ws-1x03.vaughnsoft.net
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:61.0) Gecko/20100101 Firefox/61.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate, br
Sec-WebSocket-Version: 13
Origin: https://vaughnlive.tv
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: a9CybjKhW/d43rM58Nn4gg==
DNT: 1
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket

...and as response I get this...

HTTP/1.1 101 Switching Protocols
Server: nginx
Date: Thu, 16 Aug 2018 19:30:47 GMT
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Accept: QyVLSqUAfXFDOR4rhOenJZGwZjo=
WebSocket-Server: VaughnSoft Chat

Switching Protocols 101 = success.Ok.So now it means I need to change the protocol to wss using same address which is in this case wss://sapi-ws-1x03.vaughnsoft.net/

Ok so far,and now my question is how to make a connection to this wss address now?

I tried to check again that npoll functions and the example test source code..

https://github.com/ASPLes/nopoll/blob/master/test/nopoll-regression-client.c

...but I am still not sure which of them I should use for this.Only thing I can see with wss protocol is at "nopoll_bool test_22" example.Has anyone a better understanding as me checking that functions...

http://www.aspl.es/nopoll/html/modules.html

....and find out a way which I have to call to make a simple example how to do it?Just need to know the function way starting from "nopoll_ctx_new" till "nopoll_ctx_unref" etc you know.

greetz

 

Posted

WinHttp.lib and functions should still be usable by win7 etc - most of the msdn doc functions indicate xp upwards.

Can get the lib file from an sdk - from installing visual studio, express or one of the sdk kits directly (if still available)

I found WinHttp.lib in C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86

Just have to use lib2inc (http://www.vortex.masmcode.com/) to get the prototypes:

lib2inc winhttp.lib -s

and you will have your winhttp.inc file created for you. Place that in your masm32\includes folder, and the lib in the masm32\lib folder.

  • Like 1
Posted

Hi fearless,

so you mean it could work with Winhttp also if they write its for Win 8?

I dont have any Windows Kit also not that WinHttp lib / inc files anywhere.Could you maybe build that lib / inc and attach it here so that I can use them in MASM?Then I could try this too.

Thank you

Posted

you really should get and install visual studio and the kits.. it would make things so much easier for you... 

  • Like 1
Posted

Could just install an SDK package so you have all libs for any future requirements: https://developer.microsoft.com/en-us/windows/downloads/sdk-archive

Or at a pinch you could download and install PellesC v8.0 32bit and/or 64bit and get the libs from that: http://www.pellesc.de/index.php?page=download&lang=en&version=8.00

Less libs in Pelle's (about 150ish vs 450 in sdk) but it does have the winhttp.lib one. And PellesC is smaller download package.

  • Like 1
Teddy Rogers
Posted
11 hours ago, fearless said:

PellesC

👍👌

Ted.

  • Like 1
Posted

Hi guys,

I did download the Pelles setup and see the winhttp lib now I can use.Also made a quick include file for winhttp as fearless said.Now I was writing the websocket code to MASM and at the function WinHttpWebSocketCompleteUpgrade I had to stop because this function is not present in any of my winhttp.dlls on Windows 7 = I cant use it as kao already said before.Thats really BS now!

What now?I can only try that nopoll dll in this case but there I have some problems to build any example using right & filled functions.So could anyone check that maybe a little out to help with that nopoll to get anything working?

greetz

Posted

Hi again,

one  more thing.I testet the nopoll-regression-client.exe file from nopoll set and see it does fail.See error at the end..

** NoPoll: Websocket toolkit (regression test).
** Copyright (C) 2015 Advanced Software Production Line, S.L.
**
** NoPoll regression tests: version=0.4.4.b349
**
** To gather information about time performance you can use:
**
**     >> time ./nopoll-regression-client [--debug,--show-critical-only]
**
** To gather information about memory consumed (and leaks) use:
**
**     >> libtool --mode=execute valgrind --leak-check=yes --error-limit=no ./nopoll-regression-client
**
**
** Report bugs to:
**
**     <nopoll@lists.aspl.es> noPoll mailing list
**
**     <info@aspl.es> ASPL's contact
**
INFO: starting tests with pid: 1716
Test 01-strings: Library strings support [   OK   ]
Test 01-base64: Library base64 support [   OK   ]
Test-01 masking: using masking value 41
Test 01 masking: found mask in the buffer 41 == 41
Test 01-masking: Library websocket content masking support [   OK   ]
Test 01: reference counting for the connection: 2
ERROR (4.1 jkd412): expected to find proper connection handshake finished, but found connection is broken: session=-1, errno=0 : No error..
Test 01: Simple connect and disconnect [ FAILED ]

...at nopoll_conn_is_ready it returns 0 and right after nopoll_conn_is_ok fails too before the error gets printed. :( Why this now?Also at nopoll_conn_new function it does take a long time till it returns from that function and right after I can see a error called WSAENOTCONN in register which it got inside this function anywhere.

I really stuck again this time and come no step forward.

greetz

Posted

Hi again,

ok I have test some more and see now that I need to run both commandline files nopoll-regression-listener.exe and then nopoll-regression-client.exe.Now I get this results into cmd window to see...

2018-08-19_205143.png.3e0d6ecee970f08c76d82b8bb22e4959.png

...so my question now is what do I need for my goal now?Both?Client code + listener code?I dont check that yet.As I said,I wanna make a connection to http/s wss:// address...

sapi-ws-1x03.vaughnsoft.net

...and reading the content etc.

Does it mean I have to rewrite the listener code to the address of sapi-ws-1x03.vaughnsoft.net:443 to get something etc?Maybe anyone could explain that way a little how it should be just to understand it better.Anything like that would be helpfully if possible.Just asking again before I stuck go on. :)

Thank you

Posted

Without looking at the actual code - I think that listener is just an example server for testing purposes. You don't need that part of code if you want to connect to some other, existing server.

  • Like 1
Posted

Hi,

ok.I tried something like this now....

    invoke nopoll_ctx_new
    mov ctx, eax

    invoke nopoll_conn_new,ctx,chr$("sapi-ws-1x03.vaughnsoft.net"),chr$("443"),0,0,0,0
    mov conn, eax
    
    invoke nopoll_conn_wait_until_connection_ready,conn,5
    .if eax != nopoll_true ; 1
        ; failed
    .endif
    
    invoke nopoll_conn_close,conn
    
    invoke nopoll_ctx_unref,ctx

	Ret

...but I get failed at nopoll_conn_wait_until_connection_ready function or nopoll_conn_is_ready too.

Problem is there is no tiny example I could check and see how to do it correctly.What about localhost?Have I not also to use it anywhere to get later some datas etc and how to read them then?Uhhmm!

greetz

Posted

Read. The. Fabulous. Manual.

Spoiler

You're trying to create a secure websockets connection. So, you should be using nopoll_conn_tls_new , not nopoll_conn_new

 

This is my last response in the thread. I'm sick and tired of you whining all the time instead of taking 10 minutes to Google and read the code and documentation. 

 

  • Like 1
Posted

read a person documentation, they learn a bit

teach them to read, they should then be able to use google and such and educate themselves

optionally they could also download visual studio, and install it and the kits, and have all the libs, and then (usually quite easily) compile the c (more common than asm) examples and debug through them by easily inserting DebugBreak() in the code portion they are interested in, and from that, if the person is a masochist they can work on porting the structs and code to asm, and then have to realise they need to do it 2x because of x64 ... 

 

Posted

Hi guys,

whats wrong with you guys?Isn't it no more allowed to ask question here?Anyway whether they are smart or not (like in my case maybe for some people)?I am really sorry that I am not a super brain like you kao and have problems to understand some docs you dont have problems with and other too etc.So what do you think I am trying the whole time to get that in my head and testing that stuff to get it working.I am also sick & tired too to get such answers which are not very helpfully for me.Men men men you!No idea why are you so offten so stubborn and monotonous.

greetz

  • Like 2
Posted

it isnt about being a super brain, or male for that matter (that part was pretty pathetic and not required to be honest)...

its about reading the information you've been given (documentation) and also googling when you hit a brick wall, downloading c example code or whatever you can find, compiling it, debugging it, trying to solve the problem

and doing that BEFORE posting a 'help me' question...

before i had to train myself in debugging many things, reversing and so on, i actually didnt have an inet connection, so i'd hit the cafe, download as much as i could, and process it when i got home, and debugged, and learned, and crashed, and learned, and hit brick walls, and learned... sometimes having an inet connection sitting there can cause more harm than good... 

so when you hit a brick wall, take a breather, and think about the problem, check your code is right, check the documentation (its there for a reason)... but above all else dont throw a wobbly when people who are usually incredibly helpful get pissed off because you dont read simple documentation that's critical to your 'research'

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
×
×
  • Create New...