admin管理员组文章数量:1289402
I am using the ws
module and I'd like to limit the amount of data being sent by the client over websocket to 1Mb. This will prevent a malicious user from sending huge amounts of data (in terms of GB) causing the server to run out of memory, which would cause denial of service errors for every normal user.
For example, example Express allows to specify the max size of a post request body like so:
bodyParser.json({limit:'1Mb'})
How I do something similar with the ws
module?
I tried
var ws = require('ws').Server
var wsserver = new ws({port:8080, limit:'1Mb'})
But this of course doesn't work.
I want the transmission of data to be interrupted (after 1Mb is exceeded) and the websocket connection to be closed. How can I do that?
There must be a way to limit the frames of data ing from the client...
I am using the ws
module and I'd like to limit the amount of data being sent by the client over websocket to 1Mb. This will prevent a malicious user from sending huge amounts of data (in terms of GB) causing the server to run out of memory, which would cause denial of service errors for every normal user.
For example, example Express allows to specify the max size of a post request body like so:
bodyParser.json({limit:'1Mb'})
How I do something similar with the ws
module?
I tried
var ws = require('ws').Server
var wsserver = new ws({port:8080, limit:'1Mb'})
But this of course doesn't work.
I want the transmission of data to be interrupted (after 1Mb is exceeded) and the websocket connection to be closed. How can I do that?
There must be a way to limit the frames of data ing from the client...
- guess no one knows yet ... github./websockets/ws/issues/513 ... github./websockets/ws/issues?utf8=%E2%9C%93&q=limit – rafaelcastrocouto Commented Jun 14, 2015 at 23:47
-
Which
ws
library are you using? If there isn't an answer in the documentation for the webSocket library you are using, then an answer can only be determined by studying the code for the library to see if it has any sort of limit capability or understanding where the code could be changed to add such a limit. It does sound like a reasonable thing to want. – jfriend00 Commented Jun 15, 2015 at 4:55 - @jfriend00 npmjs./package/ws here it is. Apparently in the documentation there is no property of the option argument which would do so. Maybe there is a method of the wsServer instance which can set the limit. I will check it out. – Core_dumped Commented Jun 15, 2015 at 7:33
-
yay: github./faye/faye-websocket-node#initialization-options has
maxLength
– dandavis Commented Jun 23, 2015 at 21:16
1 Answer
Reset to default 12 +25That ability does not (currently) exist in that library.
Poking around their source code, it appears that the place to start would be processPacket()
method in https://github./websockets/ws/blob/master/lib/Receiver.js .
Once you have the packet header available, you can see the size of the message being sent. If it's above a certain threshold, there should be a way to close the connection before all of the bytes are even hitting your network.
Of course, the nice thing to do would be to fork their repository, issue a feature request, add in a configuration option that defaults to not taking any action if it's not set (don't break backwards patibility), and submit a pull request.
If they like it, they'll merge. If not, you'll still be able to merge their future versions into your own repo and stay up to date without having to re-do your work each time they submit a new release.
本文标签: javascripthow to limit the amount of data being sent by the client through websocketStack Overflow
版权声明:本文标题:javascript - how to limit the amount of data being sent by the client through websocket? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741461628a2380070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论